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
32namespace 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 */
46StreamSocket::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 */
55StreamSocket::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 */
63StreamSocket::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 */
73{
75 return *this;
76}
77
78/** Clone socket.
79 * @return a copied instance of StreamSocket.
80 */
81Socket *
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 */
93bool
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 */
112void
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
Base class for exceptions in Fawkes.
Definition: exception.h:36
Socket exception.
Definition: socket.h:57
Socket base class.
Definition: socket.h:64
int sock_fd
Socket file descriptor.
Definition: socket.h:137
AddrType
Address type specification.
Definition: socket.h:75
Socket & operator=(Socket &socket)
Copy constructor.
Definition: socket.cpp:250
TCP stream socket over IP.
Definition: stream.h:32
StreamSocket(float timeout=0.f)
Constructor.
Definition: stream.cpp:46
void set_nodelay(bool no_delay)
Enable or disable Nagle algorithm.
Definition: stream.cpp:113
virtual Socket * clone()
Clone socket.
Definition: stream.cpp:82
bool nodelay()
Check if Nalge algorithm is disabled.
Definition: stream.cpp:94
StreamSocket & operator=(StreamSocket &s)
Assingment operator.
Definition: stream.cpp:72
Fawkes library namespace.