blob: 3277c1b9fc2e974c50baacf28f1447a6e02e7f8e (
plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
*
* (C) 2004-2010 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for furhter details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
*
*/
#ifndef SOCKETS_H
#define SOCKETS_H
#ifdef _WIN32
#define CloseSocket closesocket
#else
#define CloseSocket close
#endif
class SocketException : public CoreException
{
public:
/** Default constructor for socket exceptions
* @param message Error message
*/
SocketException(const std::string &message) : CoreException(message) { }
/** Default destructor
* @throws Nothing
*/
virtual ~SocketException() throw() { }
};
class CoreExport Socket
{
private:
/** Read from the socket
* @param buf Buffer to read to
* @param sz How much to read
* @return Number of bytes recieved
*/
virtual int RecvInternal(char *buf, size_t sz) const;
/** Write to the socket
* @param buf What to write
* @return Number of bytes sent, -1 on error
*/
virtual int SendInternal(const std::string &buf) const;
protected:
/* Socket FD */
int Sock;
/* Host this socket is connected to */
std::string TargetHost;
/* Port we're connected to */
int Port;
/* IP this socket is bound to */
std::string BindHost;
/* Is this an IPv6 socket? */
bool IPv6;
/* Messages to be written to the socket */
std::string WriteBuffer;
/* Part of a message not totally yet recieved */
std::string extrabuf;
/* How much data was recieved from the socket */
size_t RecvLen;
public:
/** Default constructor
* @param nTargetHost Hostname to connect to
* @param nPort Port to connect to
* @param nBindHos Host to bind to when connecting
* @param nIPv6 true to use IPv6
*/
Socket(const std::string &nTargetHost, int nPort, const std::string &nBindHost = "", bool nIPv6 = false);
/** Default destructor
*/
virtual ~Socket();
/** Get the socket FD for this socket
* @return The fd
*/
virtual int GetSock() const;
/** Check if this socket is IPv6
* @return true or false
*/
bool IsIPv6() const;
/** Called when there is something to be read from thie socket
* @return true on success, false to kill this socket
*/
virtual bool ProcessRead();
/** Called when this socket becomes writeable
* @return true on success, false to drop this socket
*/
virtual bool ProcessWrite();
/** Called when there is an error on this socket
*/
virtual void ProcessError();
/** Called with a message recieved from the socket
* @param buf The message
* @return true on success, false to kill this socket
*/
virtual bool Read(const std::string &buf);
/** Write to the socket
* @param message The message to write
*/
void Write(const char *message, ...);
void Write(std::string &message);
/** Get the length of the read buffer
* @return The length of the read buffer
*/
size_t ReadBufferLen() const;
/** Get the length of the write buffer
* @return The length of the write buffer
*/
size_t WriteBufferLen() const;
};
class CoreExport SocketEngine
{
private:
/* List of sockets that need to be deleted */
std::set<Socket *> OldSockets;
/* FDs to read */
fd_set ReadFDs;
/* FDs that want writing */
fd_set WriteFDs;
/* Max FD */
int MaxFD;
/** Unmark a socket as writeable
* @param s The socket
*/
void ClearWriteable(Socket *s);
public:
/* Set of sockets */
std::set<Socket *> Sockets;
/** Constructor
*/
SocketEngine();
/** Destructor
*/
virtual ~SocketEngine();
/** Add a socket to the socket engine
* @param s The socket
*/
void AddSocket(Socket *s);
/** Delete a socket from the socket engine
* @param s The socket
*/
void DelSocket(Socket *s);
/** Mark a socket as wanting to be written to
* @param s The socket
*/
void MarkWriteable(Socket *s);
/** Called to iterate through each socket and check for activity
*/
void Process();
/** Get the last socket error
* @return The error
*/
const std::string GetError() const;
};
#endif
|