-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoltcp.h
106 lines (95 loc) · 3.03 KB
/
koltcp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef MYTCP_H_INCLUDED
#define MYTCP_H_INCLUDED
#include "kolsocket.h"
// 16-Nov-2006
// - Constructors of TcpBuffer, TcpClient and TcpServer with Socket are added
// for setting the socket options before bind or connect.
// - The number of backlog is added as an argument of TcpServer.
// 28-July-2006
// - 2nd argument of write() was changed to std::streamsize
// 11-July-2006
// Changed several interfaces close to iostream.
// - putline() was removed.
// - return values of put(), write() and flush() were changed to TcpBuffer&
// - put(const char*) was removed.
// - added good()
// - sync() was removed.
namespace kol
{
class TcpBuffer
{
public:
TcpBuffer();
TcpBuffer(const Socket& s);
TcpBuffer(int domain, int type, int protocol=0);
virtual ~TcpBuffer();
virtual int close();
virtual int get();
TcpBuffer& getline(char* buf, std::streamsize maxlen);
TcpBuffer& ignore(std::streamsize len=1);
TcpBuffer& read(char* buf, std::streamsize len);
TcpBuffer& put(int c);
TcpBuffer& write(const void* buf, std::streamsize len);
TcpBuffer& flush();
virtual int shutdown(int how=SHUT_RDWR);
int getsockopt(int level, int optname, void* optval, socklen_t* optlen) const;
int setsockopt(int level, int optname, const void* optval, socklen_t optlen);
std::streamsize gcount() const { return m_gcount; }
bool good() const { return (m_iostate == goodbit); }
bool eof() const { return ((m_iostate & eofbit) != 0); }
bool fail() const { return ((m_iostate & (failbit | badbit)) != 0); }
bool bad() const { return ((m_iostate & badbit) != 0); }
operator void*() const
{ if(fail()) return 0;return (void*)this; }
bool operator!() const { return fail(); }
protected:
int sync();
void initparams();
int recv_all(unsigned char* buf, int nbytes);
int send_all(const unsigned char* buf, int nbytes);
private:
enum { bufsize = 1024 };
enum { goodbit = 0, eofbit = 1, failbit = 2, badbit = 4 };
protected:
Socket m_socket;
std::streamsize m_gcount;
int m_iostate;
size_t m_rbufmax;
size_t m_rbuflen;
size_t m_rbufnxt;
unsigned char m_rbuf[bufsize];
size_t m_sbufmax;
size_t m_sbuflen;
size_t m_sbufnxt;
unsigned char m_sbuf[bufsize];
};
class TcpSocket : public TcpBuffer
{
public:
TcpSocket();
TcpSocket(const Socket& s);
virtual ~TcpSocket();
};
class TcpClient : public TcpBuffer
{
public:
TcpClient();
TcpClient(const char* host, int port);
TcpClient(const Socket& s, const char* host, int port);
virtual ~TcpClient();
private:
void Start(const char* host, int port);
};
class TcpServer : public TcpBuffer
{
public:
TcpServer();
TcpServer(int port, int backlog=5);
TcpServer(const Socket& s, int port, int backlog=5);
virtual ~TcpServer();
virtual TcpSocket accept();
private:
void Start(int port, int backlog);
};
}
#endif