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 
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <errno.h>
31 
32 namespace fawkes {
33 
34 
35 /** @class StreamSocket netcomm/socket/stream.h
36  * TCP stream socket over IP.
37  *
38  * @ingroup NetComm
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor.
43  * This assumes that the socket will later be created using connect().
44  * @param timeout timeout, if 0 all operationsare blocking, otherwise it
45  * is tried for timeout seconds.
46  */
48  : Socket(Socket::TCP, timeout)
49 {
50 }
51 
52 /** Constructor.
53  * @param addr_type Specify IPv4 or IPv6
54  * @param timeout timeout, if 0 all operationsare blocking, otherwise it
55  * is tried for timeout seconds.
56  */
58  : Socket(addr_type, Socket::TCP, timeout)
59 {
60 }
61 
62 
63 /** Copy constructor.
64  * @param stream_socket socket to copy.
65  */
67  : Socket(stream_socket)
68 {
69 }
70 
71 
72 /** Clone socket.
73  * @return a copied instance of StreamSocket.
74  */
75 Socket *
77 {
78  return new StreamSocket(*this);
79 }
80 
81 
82 /** Check if Nalge algorithm is disabled.
83  * This checks the TCP_NODELAY option on the socket. If it is set then the
84  * Nagle algorithm is disabled and all data is send out immediately.
85  * @return true, if nodelay is enabled and thus the Nagle algorithm disabled,
86  * false otherwise
87  */
88 bool
90 {
91  if (sock_fd == -1) {
92  throw Exception("Socket not initialized, call bind() or connect()");
93  }
94 
95  int val = 0;
96  socklen_t val_len = sizeof(val);
97  if ( getsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &val, &val_len) == -1 ) {
98  throw SocketException("StreamSocket::nodelay: getsockopt failed", errno);
99  }
100  return (val == 1);
101 }
102 
103 
104 /** Enable or disable Nagle algorithm.
105  * @param nodelay true to disable Nagle algorithm, false to enable it
106  * @see nodelay()
107  */
108 void
110 {
111  if (sock_fd == -1) {
112  throw Exception("Socket not initialized, call bind() or connect()");
113  }
114 
115  int val = (nodelay ? 1 : 0);
116  socklen_t val_len = sizeof(val);
117  if ( setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &val, val_len) == -1 ) {
118  throw SocketException("StreamSocket::set_nodelay: setsockopt failed", errno);
119  }
120 }
121 
122 } // end namespace fawkes
StreamSocket(float timeout=0.f)
Constructor.
Definition: stream.cpp:47
TCP stream socket.
Definition: socket.h:86
Fawkes library namespace.
AddrType
Address type specification.
Definition: socket.h:78
Socket base class.
Definition: socket.h:65
TCP stream socket over IP.
Definition: stream.h:31
virtual Socket * clone()
Clone socket.
Definition: stream.cpp:76
Base class for exceptions in Fawkes.
Definition: exception.h:36
bool nodelay()
Check if Nalge algorithm is disabled.
Definition: stream.cpp:89
AddrType addr_type
Address type/family of socket.
Definition: socket.h:139
int sock_fd
Socket file descriptor.
Definition: socket.h:140
float timeout
Timeout in seconds for various operations.
Definition: socket.h:141
void set_nodelay(bool no_delay)
Enable or disable Nagle algorithm.
Definition: stream.cpp:109
Socket exception.
Definition: socket.h:58