00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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_COMMONCPP
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)
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)
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
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);
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 END_NAMESPACE
00611
00612 #endif