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 
65 namespace ost {
66 
91 class __EXPORT TCPSocket : protected Socket
92 {
93 protected:
94  int segsize;
95  void setSegmentSize(unsigned mss);
96 
97  __DELETE_COPY(TCPSocket);
98 
99 public:
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
193 
217 class __EXPORT TCPV6Socket : protected Socket
218 {
219 private:
220  int segsize;
221  void setSegmentSize(unsigned mss);
222 
223  __DELETE_COPY(TCPV6Socket);
224 
225 public:
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 
329 class __EXPORT TCPStream : protected std::streambuf, public Socket, public std::iostream
330 {
331 private:
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 
343 protected:
344  timeout_t timeout;
345  size_t bufsize;
346  Family family;
347  char *gbuf, *pbuf;
348 
349 public:
354  TCPStream(Family family = IPV4, bool throwflag = true, timeout_t to = 0);
355 
359  void disconnect(void);
360 
364  int getSegmentSize(void);
365 
366 protected:
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 
441 public:
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 
564 class __EXPORT TCPSession : public Thread, public TCPStream
565 {
566 private:
567  TCPSession(const TCPSession &rhs); // not defined
568 
569 protected:
582  int waitConnection(timeout_t timeout = TIMEOUT_INF);
583 
590  void initial(void) __OVERRIDE;
591 
592 public:
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
bool isPendingConnection(timeout_t timeout=ucommon::Timer::inf)
Used to wait for pending connection requests.
Definition: tcp.h:182
TCP streams are used to represent TCP client connections to a server by TCP protocol servers for acce...
Definition: tcp.h:329
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:578
TCPV6 sockets are used for stream based connected sessions between two ipv6 sockets.
Definition: tcp.h:217
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
This object is used to hold the actual and valid internet address of a specific host machine that wil...
Definition: address.h:981
Network addresses and sockets related classes.
void setTimeout(timeout_t timer)
Set the I/O operation timeout for socket I/O operations.
Definition: tcp.h:497
std::iostream * tcp(void)
Used in derived classes to refer to the current object via it's iostream.
Definition: tcp.h:437
size_t getBufferSize(void) const
Return the size of the current stream buffering used.
Definition: tcp.h:549
ssize_t peek(void *buf, size_t len)
Examine contents of next waiting packet.
Definition: tcp.h:540
in_port_t tpport_t
Transport Protocol Ports.
Definition: address.h:80
IPV6Host getRequest(tpport_t *port=NULL) const
Return address and port of next connection request.
Definition: tcp.h:284
IPV6Host getLocal(tpport_t *port=NULL) const
Used to get local bound address.
Definition: tcp.h:296
SOCKET getSocket(void)
Fetch out the socket.
Definition: tcp.h:242
Common C++ generic string class.
bool isPendingConnection(timeout_t timeout=ucommon::Timer::inf)
Used to wait for pending connection requests.
Definition: tcp.h:305
The TCP session is used to primarily to represent a client connection that can be managed on a sepera...
Definition: tcp.h:564
TCP sockets are used for stream based connected sessions between two sockets.
Definition: tcp.h:91
socket operations.
SOCKET getSocket(void) const
Fetch out the socket.
Definition: tcp.h:116
IPV4Host getRequest(tpport_t *port=NULL) const
Return address and port of next connection request.
Definition: tcp.h:161
The network name and address objects are all derived from a common IPV4Address base class.
Definition: address.h:362