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  * @param timeout timeout, if 0 all operationsare blocking, otherwise it
44  * is tried for timeout seconds.
45  */
47  : Socket(PF_INET, SOCK_STREAM, 0, timeout)
48 {
49 }
50 
51 
52 /** Copy constructor.
53  * @param stream_socket socket to copy.
54  */
56  : Socket(stream_socket)
57 {
58 }
59 
60 
61 /** Clone socket.
62  * @return a copied instance of StreamSocket.
63  */
64 Socket *
66 {
67  return new StreamSocket(*this);
68 }
69 
70 
71 /** Check if Nalge algorithm is disabled.
72  * This checks the TCP_NODELAY option on the socket. If it is set then the
73  * Nagle algorithm is disabled and all data is send out immediately.
74  * @return true, if nodelay is enabled and thus the Nagle algorithm disabled,
75  * false otherwise
76  */
77 bool
79 {
80  int val = 0;
81  socklen_t val_len = sizeof(val);
82  if ( getsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &val, &val_len) == -1 ) {
83  throw SocketException("StreamSocket::nodelay: getsockopt failed", errno);
84  }
85  return (val == 1);
86 }
87 
88 
89 /** Enable or disable Nagle algorithm.
90  * @param nodelay true to disable Nagle algorithm, false to enable it
91  * @see nodelay()
92  */
93 void
95 {
96  int val = (nodelay ? 1 : 0);
97  socklen_t val_len = sizeof(val);
98  if ( setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &val, val_len) == -1 ) {
99  throw SocketException("StreamSocket::set_nodelay: setsockopt failed", errno);
100  }
101 }
102 
103 } // end namespace fawkes
StreamSocket(float timeout=0.f)
Constructor.
Definition: stream.cpp:46
Fawkes library namespace.
Socket base class.
Definition: socket.h:65
TCP stream socket over IP.
Definition: stream.h:31
virtual Socket * clone()
Clone socket.
Definition: stream.cpp:65
bool nodelay()
Check if Nalge algorithm is disabled.
Definition: stream.cpp:78
int sock_fd
Socket file descriptor.
Definition: socket.h:125
void set_nodelay(bool no_delay)
Enable or disable Nagle algorithm.
Definition: stream.cpp:94
Socket exception.
Definition: socket.h:58