-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKOsocket.h
141 lines (106 loc) · 2.91 KB
/
KOsocket.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// Name: KOsocket.h
// Description: K.O.'s very own socket library
// Author: K.Olchanski
// Date: 11 Aug 1998
//
// $Id: KOsocket.h,v 1.2 2003/05/07 00:31:55 olchansk Exp $
//
#ifndef KOsocketH
#define KOsocketH
#include <string>
#include <stdlib.h>
typedef unsigned int KOsocketType;
class KOsocketException
{
public:
KOsocketException(const std::string& s) : message(s) { };
const char* toString() const { return message.c_str(); };
private:
std::string message;
};
class KOsocketAddr
{
public: // public data members
std::string fProtocol;
std::string fHostname;
int fPort;
public: // public methods
KOsocketAddr(); // ctor
std::string toString() const;
private: // private data members
};
class KOsocketBase
{
public:
KOsocketBase(); // ctor
virtual ~KOsocketBase(); // dtor
// socket operations
void setSockopt(int opt,int value);
// set the SO_RCVBUF option
void setReceiveBufferSize(int value);
// set the SO_SNDBUF option
void setSendBufferSize(int value);
// set or clear close-on-exec flag
void setCloseOnExec(bool flag);
// read the close-on-exec flag
bool getCloseOnExec() const;
// socket shutdown
void shutdown();
// socket error reporting
int getErrorCode() const;
std::string getErrorString() const;
// socket information reporting
const KOsocketAddr& getLocalAddr() const;
const KOsocketAddr& getRemoteAddr() const;
unsigned int getSocket() { return fSocket; }
protected:
KOsocketAddr fLocalAddr;
KOsocketAddr fRemoteAddr;
int fError;
KOsocketType fSocket;
private: // hide the assignment and copy constructor operators
KOsocketBase(const KOsocketBase&s) { abort(); };
KOsocketBase& operator=(const KOsocketBase&s) { abort(); return *this; };
};
class KOsocket;
class KOserverSocket
: public KOsocketBase
{
public: // public methods
// create listener socket
KOserverSocket(int iport,int backlog = 1); // ctor
// accept a connection
KOsocket* accept();
private:
KOserverSocket(const KOserverSocket&s) { abort(); };
};
class KOsocket
: public KOsocketBase
{
friend class KOserverSocket;
public:
// open a connection to remote host
KOsocket(const std::string& remoteHost,
int remotePort); // ctor
// send as many bytes as socket send() would send
int writeSomeBytes(const char*buffer,int bufferSize);
// Java-style write()
void write(const char*buffer,int bufferSize);
// Java-style read()
int read(char*buffer,int bufferSize);
// Java-style setSoTimeout()
void setSoTimeout(int mstimeout);
// Java-style readFully(), throws exception if failure.
void readFully(char*buffer,int bufferSize);
// Java-style "java.net.Socket->InputStream.available()"
int available();
private:
int fTimeout; // in msec
// these constructors are used
// by KOserverSocket::accept().
KOsocket(KOsocketType fd,
const KOsocketAddr& remoteAddr); // ctor
};
#endif
// end file