Fawkes API
Fawkes Development Version
stream.cpp
1
2
/***************************************************************************
3
* stream.cpp - Fawkes stream socket (TCP)
4
*
5
* Created: Fri Nov 10 10:02:54 2006 (on train to Google, Hamburg)
6
* Copyright 2006 Tim Niemueller [www.niemueller.de]
7
*
8
****************************************************************************/
9
10
/* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version. A runtime exception applies to
14
* this software (see LICENSE.GPL_WRE file mentioned below for details).
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Library General Public License for more details.
20
*
21
* Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22
*/
23
24
#include <netcomm/socket/stream.h>
25
#include <netinet/in.h>
26
#include <netinet/tcp.h>
27
#include <sys/socket.h>
28
#include <sys/types.h>
29
30
#include <errno.h>
31
32
namespace
fawkes
{
33
34
/** @class StreamSocket netcomm/socket/stream.h
35
* TCP stream socket over IP.
36
*
37
* @ingroup NetComm
38
* @author Tim Niemueller
39
*/
40
41
/** Constructor.
42
* This assumes that the socket will later be created using connect().
43
* @param timeout timeout, if 0 all operationsare blocking, otherwise it
44
* is tried for timeout seconds.
45
*/
46
StreamSocket::StreamSocket
(
float
timeout) : Socket(Socket::TCP, timeout)
47
{
48
}
49
50
/** Constructor.
51
* @param addr_type Specify IPv4 or IPv6
52
* @param timeout timeout, if 0 all operationsare blocking, otherwise it
53
* is tried for timeout seconds.
54
*/
55
StreamSocket::StreamSocket
(
AddrType
addr_type,
float
timeout)
56
:
Socket
(addr_type,
Socket
::TCP, timeout)
57
{
58
}
59
60
/** Copy constructor.
61
* @param stream_socket socket to copy.
62
*/
63
StreamSocket::StreamSocket
(
StreamSocket
&stream_socket) :
Socket
(stream_socket)
64
{
65
}
66
67
/** Assingment operator.
68
* @param s socket to copy from
69
* @return reference to this
70
*/
71
StreamSocket
&
72
StreamSocket::operator=
(
StreamSocket
&s)
73
{
74
Socket::operator=
(s);
75
return
*
this
;
76
}
77
78
/** Clone socket.
79
* @return a copied instance of StreamSocket.
80
*/
81
Socket
*
82
StreamSocket::clone
()
83
{
84
return
new
StreamSocket
(*
this
);
85
}
86
87
/** Check if Nalge algorithm is disabled.
88
* This checks the TCP_NODELAY option on the socket. If it is set then the
89
* Nagle algorithm is disabled and all data is send out immediately.
90
* @return true, if nodelay is enabled and thus the Nagle algorithm disabled,
91
* false otherwise
92
*/
93
bool
94
StreamSocket::nodelay
()
95
{
96
if
(
sock_fd
== -1) {
97
throw
Exception
(
"Socket not initialized, call bind() or connect()"
);
98
}
99
100
int
val = 0;
101
socklen_t val_len =
sizeof
(val);
102
if
(getsockopt(
sock_fd
, IPPROTO_TCP, TCP_NODELAY, &val, &val_len) == -1) {
103
throw
SocketException
(
"StreamSocket::nodelay: getsockopt failed"
, errno);
104
}
105
return
(val == 1);
106
}
107
108
/** Enable or disable Nagle algorithm.
109
* @param nodelay true to disable Nagle algorithm, false to enable it
110
* @see nodelay()
111
*/
112
void
113
StreamSocket::set_nodelay
(
bool
nodelay)
114
{
115
if
(
sock_fd
== -1) {
116
throw
Exception(
"Socket not initialized, call bind() or connect()"
);
117
}
118
119
int
val = (
nodelay
? 1 : 0);
120
socklen_t val_len =
sizeof
(val);
121
if
(setsockopt(
sock_fd
, IPPROTO_TCP, TCP_NODELAY, &val, val_len) == -1) {
122
throw
SocketException
(
"StreamSocket::set_nodelay: setsockopt failed"
, errno);
123
}
124
}
125
126
}
// end namespace fawkes
fawkes::SocketException
Definition:
socket.h:62
fawkes::Socket::sock_fd
int sock_fd
Definition:
socket.h:143
fawkes::StreamSocket::nodelay
bool nodelay()
Check if Nalge algorithm is disabled.
Definition:
stream.cpp:100
fawkes::StreamSocket::operator=
StreamSocket & operator=(StreamSocket &s)
Assingment operator.
Definition:
stream.cpp:78
fawkes::Socket::operator=
Socket & operator=(Socket &socket)
Copy constructor.
Definition:
socket.cpp:250
fawkes
fawkes::StreamSocket::StreamSocket
StreamSocket(float timeout=0.f)
Constructor.
Definition:
stream.cpp:52
fawkes::StreamSocket::set_nodelay
void set_nodelay(bool no_delay)
Enable or disable Nagle algorithm.
Definition:
stream.cpp:119
fawkes::StreamSocket::clone
virtual Socket * clone()
Clone socket.
Definition:
stream.cpp:88
fawkes::StreamSocket
Definition:
stream.h:37
fawkes::Socket::AddrType
AddrType
Address type specification.
Definition:
socket.h:81
fawkes::Socket
Definition:
socket.h:69
fawkes::Exception
Definition:
exception.h:41
src
libs
netcomm
socket
stream.cpp
Generated by
1.8.17