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 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef COMMONCPP_TCP_H_
44 #define COMMONCPP_TCP_H_
45 
46 #include <cstdio>
47 
48 #ifndef COMMONCPP_CONFIG_H_
49 #include <commoncpp/config.h>
50 #endif
51 
52 #ifndef COMMONCPP_STRING_H_
53 #include <commoncpp/string.h>
54 #endif
55 
56 #ifndef COMMONCPP_ADDRESS_H_
57 #include <commoncpp/address.h>
58 #endif
59 
60 #ifndef COMMONCPP_SOCKET_H_
61 #include <commoncpp/socket.h>
62 #endif
63 
64 NAMESPACE_COMMONCPP
65 
90 class __EXPORT TCPSocket : protected Socket
91 {
92 protected:
93  int segsize;
94  void setSegmentSize(unsigned mss);
95 
96 public:
108  virtual bool onAccept(const IPV4Host &ia, tpport_t port);
109 
113  inline SOCKET getSocket(void)
114  {return so;};
115 
119  inline int getSegmentSize(void)
120  {return segsize;};
121 
134  TCPSocket(const IPV4Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536);
135 
146  TCPSocket(const char *name, unsigned backlog = 5, unsigned mss = 536);
147 
156  inline IPV4Host getRequest(tpport_t *port = NULL) const
157  {return Socket::getIPV4Sender(port);}
158 
162  void reject(void);
163 
167  inline IPV4Host getLocal(tpport_t *port = NULL) const
168  {return Socket::getIPV4Local(port);}
169 
175  inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */
176  {return Socket::isPending(Socket::pendingInput, timeout);}
177 
181  virtual ~TCPSocket();
182 };
183 
184 #ifdef CCXX_IPV6
185 
209 class __EXPORT TCPV6Socket : protected Socket
210 {
211 private:
212  int segsize;
213  void setSegmentSize(unsigned mss);
214 
215 public:
227  virtual bool onAccept(const IPV6Host &ia, tpport_t port);
228 
232  inline SOCKET getSocket(void)
233  {return so;};
234 
235  inline int getSegmentSize(void)
236  {return segsize;};
237 
250  TCPV6Socket(const IPV6Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536);
251 
262  TCPV6Socket(const char *name, unsigned backlog = 5, unsigned mss = 536);
263 
272  inline IPV6Host getRequest(tpport_t *port = NULL) const
273  {return Socket::getIPV6Sender(port);}
274 
278  void reject(void);
279 
283  inline IPV6Host getLocal(tpport_t *port = NULL) const
284  {return Socket::getIPV6Local(port);}
285 
291  inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */
292  {return Socket::isPending(Socket::pendingInput, timeout);}
293 
297  virtual ~TCPV6Socket();
298 };
299 #endif
300 
314 class __EXPORT TCPStream : protected std::streambuf, public Socket, public std::iostream
315 {
316 private:
317  int doallocate();
318 
319  void segmentBuffering(unsigned mss);
320 
321  friend TCPStream& crlf(TCPStream&);
322  friend TCPStream& lfcr(TCPStream&);
323 
324  // no copy constructor...
325  TCPStream(const TCPStream &source);
326 
327 
328 protected:
329  timeout_t timeout;
330  size_t bufsize;
331  Family family;
332  char *gbuf, *pbuf;
333 
334 public:
339  TCPStream(Family family = IPV4, bool throwflag = true, timeout_t to = 0);
340 
344  void disconnect(void);
345 
349  int getSegmentSize(void);
350 
351 protected:
358  void allocate(size_t size);
359 
364  void endStream(void);
365 
372  int underflow();
373 
382  int uflow();
383 
391  int overflow(int ch);
392 
401  void connect(const IPV4Host &host, tpport_t port, unsigned mss = 536);
402 #ifdef CCXX_IPV6
403  void connect(const IPV6Host &host, tpport_t port, unsigned mss = 536);
404 #endif
405 
413  void connect(const char *name, unsigned mss = 536);
414 
422  std::iostream *tcp(void)
423  {return ((std::iostream *)this);};
424 
425 public:
435  TCPStream(TCPSocket &server, bool throwflag = true, timeout_t timeout = 0);
436 #ifdef CCXX_IPV6
437  TCPStream(TCPV6Socket &server, bool throwflag = true, timeout_t timeout = 0);
438 #endif
439 
445  void connect(TCPSocket &server);
446 #ifdef CCXX_IPV6
447  void connect(TCPV6Socket &server);
448 #endif
449 
460  TCPStream(const IPV4Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);
461 #ifdef CCXX_IPV6
462  TCPStream(const IPV6Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);
463 #endif
464 
474  TCPStream(const char *name, Family family = IPV4, unsigned mss = 536, bool throwflag = false, timeout_t timer = 0);
475 
481  inline void setTimeout(timeout_t timer)
482  {timeout = timer;};
483 
484 
489  virtual ~TCPStream();
490 
497  int sync(void);
498 
505  size_t printf(const char *format, ...);
506 
514  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
515 
523  inline ssize_t peek(void *buf, size_t len)
524  {return ::recv(so, (char *)buf, len, MSG_PEEK);};
525 
531  inline size_t getBufferSize(void) const
532  {return bufsize;};
533 };
534 
545 class __EXPORT TCPSession : public Thread, public TCPStream
546 {
547 private:
548  TCPSession(const TCPSession &rhs); // not defined
549 protected:
562  int waitConnection(timeout_t timeout = TIMEOUT_INF);
563 
570  void initial(void);
571 
572 public:
583  TCPSession(const IPV4Host &host,
584  tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);
585 #ifdef CCXX_IPV6
586  TCPSession(const IPV6Host &host,
587  tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);
588 #endif
589 
599  TCPSession(TCPSocket &server, int pri = 0, size_t stack = 0);
600 #ifdef CCXX_IPV6
601  TCPSession(TCPV6Socket &server, int pri = 0, size_t stack = 0);
602 #endif
603 
607  virtual ~TCPSession();
608 };
609 
610 END_NAMESPACE
611 
612 #endif
int getSegmentSize(void)
Get the buffer size for servers.
Definition: tcp.h:119
IPV6Host getRequest(tpport_t *port=((void *) 0)) const
Return address and port of next connection request.
Definition: tcp.h:272
std::iostream * tcp(void)
Used in derived classes to refer to the current object via it's iostream.
Definition: tcp.h:422
size_t getBufferSize(void) const
Return the size of the current stream buffering used.
Definition: tcp.h:531
void setTimeout(timeout_t timer)
Set the I/O operation timeout for socket I/O operations.
Definition: tcp.h:481
IPV6Host getLocal(tpport_t *port=((void *) 0)) const
Used to get local bound address.
Definition: tcp.h:283
SOCKET getSocket(void)
Fetch out the socket.
Definition: tcp.h:232
SOCKET getSocket(void)
Fetch out the socket.
Definition: tcp.h:113
IPV4Host getRequest(tpport_t *port=((void *) 0)) const
Return address and port of next connection request.
Definition: tcp.h:156
The TCP session is used to primarily to represent a client connection that can be managed on a sepera...
Definition: tcp.h:545
TCPV6 sockets are used for stream based connected sessions between two ipv6 sockets.
Definition: tcp.h:209
bool isPendingConnection(timeout_t timeout=ucommon::Timer::inf)
Used to wait for pending connection requests.
Definition: tcp.h:291
Common C++ generic string class.
IPV4Host getLocal(tpport_t *port=((void *) 0)) const
Used to get local bound address.
Definition: tcp.h:167
The network name and address objects are all derived from a common IPV4Address base class...
Definition: address.h:331
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: tcp.h:523
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:542
TCP streams are used to represent TCP client connections to a server by TCP protocol servers for acce...
Definition: tcp.h:314
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
socket operations.
Network addresses and sockets related classes.
bool isPendingConnection(timeout_t timeout=ucommon::Timer::inf)
Used to wait for pending connection requests.
Definition: tcp.h:175
TCP sockets are used for stream based connected sessions between two sockets.
Definition: tcp.h:90
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:929