00001 #ifndef MYTCP_H_INCLUDED
00002 #define MYTCP_H_INCLUDED
00003
00004 #include "kolsocket.h"
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 namespace kekonline
00017 {
00018 class TcpBuffer
00019 {
00020 public:
00021 TcpBuffer();
00022 TcpBuffer(int domain, int type, int protocol=0);
00023 virtual ~TcpBuffer();
00024 virtual int close();
00025 virtual int get();
00026 TcpBuffer& getline(char* buf, std::streamsize maxlen);
00027 TcpBuffer& ignore(std::streamsize len=1);
00028 TcpBuffer& read(char* buf, std::streamsize len);
00029 TcpBuffer& put(int c);
00030 TcpBuffer& write(const void* buf, std::streamsize len);
00031 TcpBuffer& flush();
00032 virtual int shutdown(int how=SHUT_RDWR);
00033 int getsockopt(int level, int optname, void* optval, socklen_t* optlen) const;
00034 int setsockopt(int level, int optname, const void* optval, socklen_t optlen);
00035 std::streamsize gcount() const { return m_gcount; }
00036 bool good() const { return (m_iostate == goodbit); }
00037 bool eof() const { return ((m_iostate & eofbit) != 0); }
00038 bool fail() const { return ((m_iostate & (failbit | badbit)) != 0); }
00039 bool bad() const { return ((m_iostate & badbit) != 0); }
00040 operator void*() const
00041 { if(fail()) return 0;return (void*)this; }
00042 bool operator!() const { return fail(); }
00043 protected:
00044 int sync();
00045 void initparams();
00046 int recv_all(unsigned char* buf, int nbytes);
00047 int send_all(const unsigned char* buf, int nbytes);
00048 private:
00049 enum { bufsize = 1024 };
00050 enum { goodbit = 0, eofbit = 1, failbit = 2, badbit = 4 };
00051
00052 protected:
00053 Socket m_socket;
00054 std::streamsize m_gcount;
00055 int m_iostate;
00056 size_t m_rbufmax;
00057 size_t m_rbuflen;
00058 size_t m_rbufnxt;
00059 unsigned char m_rbuf[bufsize];
00060 size_t m_sbufmax;
00061 size_t m_sbuflen;
00062 size_t m_sbufnxt;
00063 unsigned char m_sbuf[bufsize];
00064 };
00065
00066 class TcpSocket : public TcpBuffer
00067 {
00068 public:
00069 TcpSocket();
00070 TcpSocket(const Socket& sock);
00071 virtual ~TcpSocket();
00072 };
00073
00074 class TcpClient : public TcpBuffer
00075 {
00076 public:
00077 TcpClient();
00078 TcpClient(const char* host, int port);
00079 virtual ~TcpClient();
00080 };
00081
00082 class TcpServer : public TcpBuffer
00083 {
00084 public:
00085 TcpServer();
00086 TcpServer(int port);
00087 virtual ~TcpServer();
00088 virtual TcpSocket accept();
00089 };
00090 }
00091
00092 #endif
00093