ucommon
|
00001 // Copyright (C) 1999-2005 Open Source Telecom Corporation. 00002 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // Common C++. If you copy code from other releases into a copy of GNU 00028 // Common C++, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU Common C++, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 00036 // 00037 00043 #ifndef COMMONCPP_TCP_H_ 00044 #define COMMONCPP_TCP_H_ 00045 00046 #include <cstdio> 00047 00048 #ifndef COMMONCPP_CONFIG_H_ 00049 #include <commoncpp/config.h> 00050 #endif 00051 00052 #ifndef COMMONCPP_STRING_H_ 00053 #include <commoncpp/string.h> 00054 #endif 00055 00056 #ifndef COMMONCPP_ADDRESS_H_ 00057 #include <commoncpp/address.h> 00058 #endif 00059 00060 #ifndef COMMONCPP_SOCKET_H_ 00061 #include <commoncpp/socket.h> 00062 #endif 00063 00064 namespace ost { 00065 00090 class __EXPORT TCPSocket : protected Socket 00091 { 00092 protected: 00093 int segsize; 00094 void setSegmentSize(unsigned mss); 00095 00096 public: 00108 virtual bool onAccept(const IPV4Host &ia, tpport_t port); 00109 00113 inline SOCKET getSocket(void) 00114 {return so;} 00115 00119 inline int getSegmentSize(void) 00120 {return segsize;} 00121 00134 TCPSocket(const IPV4Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536); 00135 00146 TCPSocket(const char *name, unsigned backlog = 5, unsigned mss = 536); 00147 00156 inline IPV4Host getRequest(tpport_t *port = NULL) const 00157 {return Socket::getIPV4Sender(port);} 00158 00162 void reject(void); 00163 00167 inline IPV4Host getLocal(tpport_t *port = NULL) const 00168 {return Socket::getIPV4Local(port);} 00169 00175 inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */ 00176 {return Socket::isPending(Socket::pendingInput, timeout);} 00177 00181 virtual ~TCPSocket(); 00182 }; 00183 00184 #ifdef CCXX_IPV6 00185 00209 class __EXPORT TCPV6Socket : protected Socket 00210 { 00211 private: 00212 int segsize; 00213 void setSegmentSize(unsigned mss); 00214 00215 public: 00227 virtual bool onAccept(const IPV6Host &ia, tpport_t port); 00228 00232 inline SOCKET getSocket(void) 00233 {return so;} 00234 00235 inline int getSegmentSize(void) 00236 {return segsize;} 00237 00250 TCPV6Socket(const IPV6Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536); 00251 00262 TCPV6Socket(const char *name, unsigned backlog = 5, unsigned mss = 536); 00263 00272 inline IPV6Host getRequest(tpport_t *port = NULL) const 00273 {return Socket::getIPV6Sender(port);} 00274 00278 void reject(void); 00279 00283 inline IPV6Host getLocal(tpport_t *port = NULL) const 00284 {return Socket::getIPV6Local(port);} 00285 00291 inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */ 00292 {return Socket::isPending(Socket::pendingInput, timeout);} 00293 00297 virtual ~TCPV6Socket(); 00298 }; 00299 #endif 00300 00314 class __EXPORT TCPStream : protected std::streambuf, public Socket, public std::iostream 00315 { 00316 private: 00317 int doallocate(); 00318 00319 void segmentBuffering(unsigned mss); 00320 00321 friend TCPStream& crlf(TCPStream&); 00322 friend TCPStream& lfcr(TCPStream&); 00323 00324 // no copy constructor... 00325 TCPStream(const TCPStream &source); 00326 00327 00328 protected: 00329 timeout_t timeout; 00330 size_t bufsize; 00331 Family family; 00332 char *gbuf, *pbuf; 00333 00334 public: 00339 TCPStream(Family family = IPV4, bool throwflag = true, timeout_t to = 0); 00340 00344 void disconnect(void); 00345 00349 int getSegmentSize(void); 00350 00351 protected: 00358 void allocate(size_t size); 00359 00364 void endStream(void); 00365 00372 int underflow(); 00373 00382 int uflow(); 00383 00391 int overflow(int ch); 00392 00401 void connect(const IPV4Host &host, tpport_t port, unsigned mss = 536); 00402 #ifdef CCXX_IPV6 00403 void connect(const IPV6Host &host, tpport_t port, unsigned mss = 536); 00404 #endif 00405 00413 void connect(const char *name, unsigned mss = 536); 00414 00422 std::iostream *tcp(void) 00423 {return ((std::iostream *)this);} 00424 00425 public: 00435 TCPStream(TCPSocket &server, bool throwflag = true, timeout_t timeout = 0); 00436 #ifdef CCXX_IPV6 00437 TCPStream(TCPV6Socket &server, bool throwflag = true, timeout_t timeout = 0); 00438 #endif 00439 00445 void connect(TCPSocket &server); 00446 #ifdef CCXX_IPV6 00447 void connect(TCPV6Socket &server); 00448 #endif 00449 00460 TCPStream(const IPV4Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0); 00461 #ifdef CCXX_IPV6 00462 TCPStream(const IPV6Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0); 00463 #endif 00464 00474 TCPStream(const char *name, Family family = IPV4, unsigned mss = 536, bool throwflag = false, timeout_t timer = 0); 00475 00481 inline void setTimeout(timeout_t timer) 00482 {timeout = timer;} 00483 00484 00489 virtual ~TCPStream(); 00490 00497 int sync(void); 00498 00505 size_t printf(const char *format, ...); 00506 00514 bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF); 00515 00523 inline ssize_t peek(void *buf, size_t len) 00524 {return ::recv(so, (char *)buf, len, MSG_PEEK);} 00525 00531 inline size_t getBufferSize(void) const 00532 {return bufsize;} 00533 }; 00534 00545 class __EXPORT TCPSession : public Thread, public TCPStream 00546 { 00547 private: 00548 TCPSession(const TCPSession &rhs); // not defined 00549 protected: 00562 int waitConnection(timeout_t timeout = TIMEOUT_INF); 00563 00570 void initial(void); 00571 00572 public: 00583 TCPSession(const IPV4Host &host, 00584 tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0); 00585 #ifdef CCXX_IPV6 00586 TCPSession(const IPV6Host &host, 00587 tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0); 00588 #endif 00589 00599 TCPSession(TCPSocket &server, int pri = 0, size_t stack = 0); 00600 #ifdef CCXX_IPV6 00601 TCPSession(TCPV6Socket &server, int pri = 0, size_t stack = 0); 00602 #endif 00603 00607 virtual ~TCPSession(); 00608 }; 00609 00610 } // namespace ost 00611 00612 #endif