ucommon
tcp.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 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 General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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_COMMONCPP
66 
91 class __EXPORT TCPSocket : protected Socket
92 {
93 protected:
94  int segsize;
95  void setSegmentSize(unsigned mss);
96 
97 public:
109  virtual bool onAccept(const IPV4Host &ia, tpport_t port);
110 
114  inline SOCKET getSocket(void)
115  {return so;};
116 
120  inline int getSegmentSize(void)
121  {return segsize;};
122 
135  TCPSocket(const IPV4Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536);
136 
147  TCPSocket(const char *name, unsigned backlog = 5, unsigned mss = 536);
148 
157  inline IPV4Host getRequest(tpport_t *port = NULL) const
158  {return Socket::getIPV4Sender(port);}
159 
163  void reject(void);
164 
168  inline IPV4Host getLocal(tpport_t *port = NULL) const
169  {return Socket::getIPV4Local(port);}
170 
176  inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */
177  {return Socket::isPending(Socket::pendingInput, timeout);}
178 
182  virtual ~TCPSocket();
183 };
184 
185 #ifdef CCXX_IPV6
186 
210 class __EXPORT TCPV6Socket : protected Socket
211 {
212 private:
213  int segsize;
214  void setSegmentSize(unsigned mss);
215 
216 public:
228  virtual bool onAccept(const IPV6Host &ia, tpport_t port);
229 
233  inline SOCKET getSocket(void)
234  {return so;};
235 
236  inline int getSegmentSize(void)
237  {return segsize;};
238 
251  TCPV6Socket(const IPV6Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536);
252 
263  TCPV6Socket(const char *name, unsigned backlog = 5, unsigned mss = 536);
264 
273  inline IPV6Host getRequest(tpport_t *port = NULL) const
274  {return Socket::getIPV6Sender(port);}
275 
279  void reject(void);
280 
284  inline IPV6Host getLocal(tpport_t *port = NULL) const
285  {return Socket::getIPV6Local(port);}
286 
292  inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */
293  {return Socket::isPending(Socket::pendingInput, timeout);}
294 
298  virtual ~TCPV6Socket();
299 };
300 #endif
301 
315 class __EXPORT TCPStream : protected std::streambuf, public Socket, public std::iostream
316 {
317 private:
318  int doallocate();
319 
320  void segmentBuffering(unsigned mss);
321 
322  friend TCPStream& crlf(TCPStream&);
323  friend TCPStream& lfcr(TCPStream&);
324 
325  // no copy constructor...
326  TCPStream(const TCPStream &source);
327 
328 
329 protected:
330  timeout_t timeout;
331  size_t bufsize;
332  Family family;
333  char *gbuf, *pbuf;
334 
335 public:
340  TCPStream(Family family = IPV4, bool throwflag = true, timeout_t to = 0);
341 
345  void disconnect(void);
346 
350  int getSegmentSize(void);
351 
352 protected:
359  void allocate(size_t size);
360 
365  void endStream(void);
366 
373  int underflow();
374 
383  int uflow();
384 
392  int overflow(int ch);
393 
402  void connect(const IPV4Host &host, tpport_t port, unsigned mss = 536);
403 #ifdef CCXX_IPV6
404  void connect(const IPV6Host &host, tpport_t port, unsigned mss = 536);
405 #endif
406 
414  void connect(const char *name, unsigned mss = 536);
415 
423  std::iostream *tcp(void)
424  {return ((std::iostream *)this);};
425 
426 public:
436  TCPStream(TCPSocket &server, bool throwflag = true, timeout_t timeout = 0);
437 #ifdef CCXX_IPV6
438  TCPStream(TCPV6Socket &server, bool throwflag = true, timeout_t timeout = 0);
439 #endif
440 
446  void connect(TCPSocket &server);
447 #ifdef CCXX_IPV6
448  void connect(TCPV6Socket &server);
449 #endif
450 
461  TCPStream(const IPV4Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);
462 #ifdef CCXX_IPV6
463  TCPStream(const IPV6Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);
464 #endif
465 
475  TCPStream(const char *name, Family family = IPV4, unsigned mss = 536, bool throwflag = false, timeout_t timer = 0);
476 
482  inline void setTimeout(timeout_t timer)
483  {timeout = timer;};
484 
485 
490  virtual ~TCPStream();
491 
498  int sync(void);
499 
506  size_t printf(const char *format, ...);
507 
515  bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);
516 
524  inline ssize_t peek(void *buf, size_t len)
525  {return ::recv(so, (char *)buf, len, MSG_PEEK);};
526 
532  inline size_t getBufferSize(void) const
533  {return bufsize;};
534 };
535 
546 class __EXPORT TCPSession : public Thread, public TCPStream
547 {
548 private:
549  TCPSession(const TCPSession &rhs); // not defined
550 protected:
563  int waitConnection(timeout_t timeout = TIMEOUT_INF);
564 
571  void initial(void);
572 
573 public:
584  TCPSession(const IPV4Host &host,
585  tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);
586 #ifdef CCXX_IPV6
587  TCPSession(const IPV6Host &host,
588  tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);
589 #endif
590 
600  TCPSession(TCPSocket &server, int pri = 0, size_t stack = 0);
601 #ifdef CCXX_IPV6
602  TCPSession(TCPV6Socket &server, int pri = 0, size_t stack = 0);
603 #endif
604 
608  virtual ~TCPSession();
609 };
610 
611 END_NAMESPACE
612 
613 #endif