UCommon
tcp.h
Go to the documentation of this file.
1// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3// Copyright (C) 2015 Cherokees of Idaho.
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
44#ifndef COMMONCPP_TCP_H_
45#define COMMONCPP_TCP_H_
46
47#include <cstdio>
48
49#ifndef COMMONCPP_CONFIG_H_
50#include <commoncpp/config.h>
51#endif
52
53#ifndef COMMONCPP_STRING_H_
54#include <commoncpp/string.h>
55#endif
56
57#ifndef COMMONCPP_ADDRESS_H_
58#include <commoncpp/address.h>
59#endif
60
61#ifndef COMMONCPP_SOCKET_H_
62#include <commoncpp/socket.h>
63#endif
64
65namespace ost {
66
91class __EXPORT TCPSocket : protected Socket
92{
93protected:
94 int segsize;
95 void setSegmentSize(unsigned mss);
96
97 __DELETE_COPY(TCPSocket);
98
99public:
111 virtual bool onAccept(const IPV4Host &ia, tpport_t port);
112
116 inline SOCKET getSocket(void) const {
117 return so;
118 }
119
123 inline int getSegmentSize(void) const {
124 return segsize;
125 }
126
139 TCPSocket(const IPV4Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536);
140
151 TCPSocket(const char *name, unsigned backlog = 5, unsigned mss = 536);
152
161 inline IPV4Host getRequest(tpport_t *port = NULL) const {
162 return Socket::getIPV4Sender(port);
163 }
164
168 void reject(void);
169
173 inline IPV4Host getLocal(tpport_t *port = NULL) const {
174 return Socket::getIPV4Local(port);
175 }
176
182 inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) {
183 return Socket::isPending(Socket::pendingInput, timeout);
184 }
185
189 virtual ~TCPSocket();
190};
191
192#ifdef CCXX_IPV6
217class __EXPORT TCPV6Socket : protected Socket
218{
219private:
220 int segsize;
221 void setSegmentSize(unsigned mss);
222
223 __DELETE_COPY(TCPV6Socket);
224
225public:
237 virtual bool onAccept(const IPV6Host &ia, tpport_t port);
238
242 inline SOCKET getSocket(void) {
243 return so;
244 }
245
246 inline int getSegmentSize(void) {
247 return segsize;
248 }
249
262 TCPV6Socket(const IPV6Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536);
263
274 TCPV6Socket(const char *name, unsigned backlog = 5, unsigned mss = 536);
275
284 inline IPV6Host getRequest(tpport_t *port = NULL) const {
285 return Socket::getIPV6Sender(port);
286 }
287
291 void reject(void);
292
296 inline IPV6Host getLocal(tpport_t *port = NULL) const {
297 return Socket::getIPV6Local(port);
298 }
299
305 inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) {
306 return Socket::isPending(Socket::pendingInput, timeout);
307 }
308
312 virtual ~TCPV6Socket();
313};
314#endif
315
329class __EXPORT TCPStream : protected std::streambuf, public Socket, public std::iostream
330{
331private:
332 int doallocate();
333
334 void segmentBuffering(unsigned mss);
335
336 friend TCPStream& crlf(TCPStream&);
337 friend TCPStream& lfcr(TCPStream&);
338
339 // no copy constructor...
340 TCPStream(const TCPStream &source);
341
342
343protected:
344 timeout_t timeout;
345 size_t bufsize;
346 Family family;
347 char *gbuf, *pbuf;
348
349public:
354 TCPStream(Family family = IPV4, bool throwflag = true, timeout_t to = 0);
355
359 void disconnect(void);
360
364 int getSegmentSize(void);
365
366protected:
373 void allocate(size_t size);
374
379 void endStream(void);
380
387 int underflow() __OVERRIDE;
388
397 int uflow() __OVERRIDE;
398
406 int overflow(int ch) __OVERRIDE;
407
416 void connect(const IPV4Host &host, tpport_t port, unsigned mss = 536);
417#ifdef CCXX_IPV6
418 void connect(const IPV6Host &host, tpport_t port, unsigned mss = 536);
419#endif
420
428 void connect(const char *name, unsigned mss = 536);
429
437 std::iostream *tcp(void) {
438 return ((std::iostream *)this);
439 }
440
441public:
451 TCPStream(TCPSocket &server, bool throwflag = true, timeout_t timeout = 0);
452#ifdef CCXX_IPV6
453 TCPStream(TCPV6Socket &server, bool throwflag = true, timeout_t timeout = 0);
454#endif
455
461 void connect(TCPSocket &server);
462#ifdef CCXX_IPV6
463 void connect(TCPV6Socket &server);
464#endif
465
476 TCPStream(const IPV4Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);
477#ifdef CCXX_IPV6
478 TCPStream(const IPV6Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);
479#endif
480
490 TCPStream(const char *name, Family family = IPV4, unsigned mss = 536, bool throwflag = false, timeout_t timer = 0);
491
497 inline void setTimeout(timeout_t timer) {
498 timeout = timer;
499 }
500
501
506 virtual ~TCPStream();
507
514 int sync(void);
515
522 size_t printf(const char *format, ...);
523
531 bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF) __OVERRIDE;
532
540 inline ssize_t peek(void *buf, size_t len) {
541 return ::recv(so, (char *)buf, (socksize_t)len, MSG_PEEK);
542 }
543
549 inline size_t getBufferSize(void) const {
550 return bufsize;
551 }
552};
553
564class __EXPORT TCPSession : public Thread, public TCPStream
565{
566private:
567 TCPSession(const TCPSession &rhs); // not defined
568
569protected:
582 int waitConnection(timeout_t timeout = TIMEOUT_INF);
583
590 void initial(void) __OVERRIDE;
591
592public:
603 TCPSession(const IPV4Host &host,
604 tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);
605#ifdef CCXX_IPV6
606 TCPSession(const IPV6Host &host,
607 tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);
608#endif
609
619 TCPSession(TCPSocket &server, int pri = 0, size_t stack = 0);
620#ifdef CCXX_IPV6
621 TCPSession(TCPV6Socket &server, int pri = 0, size_t stack = 0);
622#endif
623
627 virtual ~TCPSession();
628};
629
630} // namespace ost
631
632#endif
Network addresses and sockets related classes.
in_port_t tpport_t
Transport Protocol Ports.
Definition address.h:80
The network name and address objects are all derived from a common IPV4Address base class.
Definition address.h:363
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition address.h:579
The network name and address objects are all derived from a common IPV6Address base class.
Definition address.h:778
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition address.h:982
TCP sockets are used for stream based connected sessions between two sockets.
Definition tcp.h:92
void reject(void)
Used to reject the next incoming connection request.
int getSegmentSize(void) const
Get the buffer size for servers.
Definition tcp.h:123
IPV4Host getLocal(tpport_t *port=NULL) const
Used to get local bound address.
Definition tcp.h:173
bool isPendingConnection(timeout_t timeout=ucommon::Timer::inf)
Used to wait for pending connection requests.
Definition tcp.h:182
virtual ~TCPSocket()
Use base socket handler for ending this socket.
virtual bool onAccept(const IPV4Host &ia, tpport_t port)
A method to call in a derived TCPSocket class that is acting as a server when a connection request is...
TCPSocket(const IPV4Address &bind, tpport_t port, unsigned backlog=5, unsigned mss=536)
A TCP "server" is created as a TCP socket that is bound to a hardware address and port number on the ...
TCPSocket(const char *name, unsigned backlog=5, unsigned mss=536)
Create a named tcp socket by service and/or interface id.
IPV4Host getRequest(tpport_t *port=NULL) const
Return address and port of next connection request.
Definition tcp.h:161
SOCKET getSocket(void) const
Fetch out the socket.
Definition tcp.h:116
TCPV6 sockets are used for stream based connected sessions between two ipv6 sockets.
Definition tcp.h:218
bool isPendingConnection(timeout_t timeout=ucommon::Timer::inf)
Used to wait for pending connection requests.
Definition tcp.h:305
virtual ~TCPV6Socket()
Use base socket handler for ending this socket.
virtual bool onAccept(const IPV6Host &ia, tpport_t port)
A method to call in a derived TCPSocket class that is acting as a server when a connection request is...
IPV6Host getLocal(tpport_t *port=NULL) const
Used to get local bound address.
Definition tcp.h:296
void reject(void)
Used to reject the next incoming connection request.
TCPV6Socket(const IPV6Address &bind, tpport_t port, unsigned backlog=5, unsigned mss=536)
A TCP "server" is created as a TCP socket that is bound to a hardware address and port number on the ...
TCPV6Socket(const char *name, unsigned backlog=5, unsigned mss=536)
Create a TCP server for a named host interface and service port.
SOCKET getSocket(void)
Fetch out the socket.
Definition tcp.h:242
IPV6Host getRequest(tpport_t *port=NULL) const
Return address and port of next connection request.
Definition tcp.h:284
TCP streams are used to represent TCP client connections to a server by TCP protocol servers for acce...
Definition tcp.h:330
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition tcp.h:540
void connect(const char *name, unsigned mss=536)
Connect a TCP stream to a named destination host and port number, using getaddrinfo interface if avai...
void setTimeout(timeout_t timer)
Set the I/O operation timeout for socket I/O operations.
Definition tcp.h:497
TCPStream(const IPV4Host &host, tpport_t port, unsigned mss=536, bool throwflag=true, timeout_t timeout=0)
Create a TCP stream by connecting to a TCP socket (on a remote machine).
void disconnect(void)
Disconnect the current session and prepare for a new one.
int getSegmentSize(void)
Get protocol segment size.
TCPStream(const char *name, Family family=IPV4, unsigned mss=536, bool throwflag=false, timeout_t timer=0)
Construct a named TCP Socket connected to a remote machine.
int sync(void)
Flushes the stream input and output buffers, writes pending output.
void connect(TCPSocket &server)
Accept a connection from a TCP Server.
int underflow()
This streambuf method is used to load the input buffer through the established tcp socket connection.
TCPStream(TCPSocket &server, bool throwflag=true, timeout_t timeout=0)
Create a TCP stream by accepting a connection from a bound TCP socket acting as a server.
TCPStream(Family family=IPV4, bool throwflag=true, timeout_t to=0)
The constructor required for building other classes or to start an unconnected TCPStream for connect.
size_t printf(const char *format,...)
Print content into a socket.
void allocate(size_t size)
Used to allocate the buffer space needed for iostream operations.
size_t getBufferSize(void) const
Return the size of the current stream buffering used.
Definition tcp.h:549
std::iostream * tcp(void)
Used in derived classes to refer to the current object via it's iostream.
Definition tcp.h:437
void endStream(void)
Used to terminate the buffer space and cleanup the socket connection.
virtual ~TCPStream()
Flush and empty all buffers, and then remove the allocated buffers.
bool isPending(Pending pend, timeout_t timeout=ucommon::Timer::inf)
Get the status of pending stream data.
The TCP session is used to primarily to represent a client connection that can be managed on a sepera...
Definition tcp.h:565
TCPSession(TCPSocket &server, int pri=0, size_t stack=0)
Create a TCP socket from a bound TCP server by accepting a pending connection from that server and ex...
virtual ~TCPSession()
Make sure destruction happens through a virtual...
TCPSession(const IPV4Host &host, tpport_t port, size_t size=536, int pri=0, size_t stack=0)
Create a TCP socket that will be connected to a remote TCP server and that will execute under it's ow...
void initial(void)
The initial method is used to esablish a connection when delayed completion is used.
int waitConnection(timeout_t timeout=ucommon::Timer::inf)
Normally called during the thread Initial() method by default, this will wait for the socket connecti...
socket operations.
Common C++ generic string class.