-
Notifications
You must be signed in to change notification settings - Fork 4
/
TastySocket.cpp
executable file
·257 lines (223 loc) · 7.34 KB
/
TastySocket.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "TastySocket.h"
#include <cassert>
#include <cstring>
#include <signal.h>
/******************************************************/
CTastySocket::CTastySocket(socket_t fd, unsigned int clientPort, string clientIPAddress) :
_ipAddress(clientIPAddress),
_port(clientPort),
_socketFD(fd)
/******************************************************/
{
}
/******************************************************/
CTastySocket::~CTastySocket()
/******************************************************/
{
}
/******************************************************/
CTastySocket* CTastySocket::Accept()
/******************************************************/
{
#ifdef WIN32
#define TASTY_SIZE_TYPE int
#else
#define TASTY_SIZE_TYPE unsigned int
#endif
sockaddr_in clientSockAddrInfo;
TASTY_SIZE_TYPE clientSockAddrInfoSize = sizeof(clientSockAddrInfo);
socket_t newSocket = accept(SocketFD(), (sockaddr*)(&clientSockAddrInfo), &(clientSockAddrInfoSize));
#ifndef WIN32
if(newSocket < 0)
#else
if(newSocket == INVALID_SOCKET)
#endif
return(NULL);
else
{
string clientIPAddress(inet_ntoa(clientSockAddrInfo.sin_addr));
int clientPort = clientSockAddrInfo.sin_port;
return(new CTastySocket(newSocket, clientPort, clientIPAddress));
}
}
/******************************************************/
int CTastySocket::Bind()
/******************************************************/
{
sockaddr_in sockAddrInfo = FormSockAddrStructure();
return(bind(SocketFD(), (sockaddr*)(&sockAddrInfo), sizeof(sockAddrInfo)));
}
/******************************************************/
int CTastySocket::Close()
/******************************************************/
{
#ifndef WIN32
return(close(SocketFD()));
#else
return(closesocket(SocketFD()));
#endif
}
/******************************************************/
int CTastySocket::Connect()
/******************************************************/
{
sockaddr_in sockAddrInfo = FormSockAddrStructure();
return(connect(SocketFD(), (sockaddr*)(&sockAddrInfo), sizeof(sockAddrInfo)));
}
/******************************************************/
CTastySocket* CTastySocket::Create(string ipAddress, unsigned int port)
/******************************************************/
{
socket_t fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(fd>0)
{
int on = 1;
#ifndef WIN32
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
#else
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&bOptVal, bOptLen);
#endif
return(new CTastySocket(fd, port, ipAddress));
}
else
return(NULL);
}
/************************************************************/
fd_set CTastySocket::FormFDSetForMe()
/************************************************************/
{
fd_set set;
FD_ZERO(&set);
FD_SET(SocketFD(), &set);
return(set);
}
/******************************************************/
sockaddr_in CTastySocket::FormSockAddrStructure()
/******************************************************/
{
sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(IPAddress().c_str());
sa.sin_port = htons(Port());
return(sa);
}
/************************************************************/
timeval CTastySocket::FormTimevalStruct(unsigned int timeInMicroseconds)
/************************************************************/
{
timeval toReturn;
toReturn.tv_sec = 0;
toReturn.tv_usec = timeInMicroseconds;
return(toReturn);
}
/******************************************************/
string CTastySocket::IPAddress()
/******************************************************/
{
return(_ipAddress);
}
/******************************************************/
int CTastySocket::Listen(int maxNumberOfClients)
/******************************************************/
{
if(maxNumberOfClients < 1)
maxNumberOfClients = 1;
return(listen(SocketFD(), maxNumberOfClients));
}
/******************************************************/
int CTastySocket::Receive(char* buffer, unsigned int numBytes)
/******************************************************/
{
#ifndef WIN32
return(recv(SocketFD(), buffer, numBytes, MSG_DONTWAIT));
#else
return(recv(SocketFD(), buffer, numBytes, 0));
#endif
}
/******************************************************/
unsigned int CTastySocket::Port()
/******************************************************/
{
return(_port);
}
/************************************************************/
int CTastySocket::Select(socket_t largestFD, fd_set* read, fd_set* write, fd_set* except, unsigned int timeInMicroseconds)
/************************************************************/
{
timeval tm = FormTimevalStruct(timeInMicroseconds);
return(select(largestFD+1, read, write, except, &(tm)));
}
/************************************************************/
int CTastySocket::SelectRead(unsigned int timeoutInMicroseconds)
/************************************************************/
{
fd_set readSet = FormFDSetForMe();
return(Select(SocketFD(), &(readSet), NULL, NULL, timeoutInMicroseconds));
}
/************************************************************/
int CTastySocket::SelectWrite(unsigned int timeoutInMicroseconds)
/************************************************************/
{
fd_set writeSet = FormFDSetForMe();
return(Select(SocketFD(), NULL, &(writeSet), NULL, timeoutInMicroseconds));
}
/************************************************************/
int CTastySocket::SelectExcept(unsigned int timeoutInMicroseconds)
/************************************************************/
{
fd_set exceptSet = FormFDSetForMe();
return(Select(SocketFD(), NULL, NULL, &(exceptSet), timeoutInMicroseconds));
}
/******************************************************/
int CTastySocket::Send(const char* buffer, unsigned int numBytes)
/******************************************************/
{
#ifndef WIN32
return(send(SocketFD(), buffer, numBytes, MSG_DONTWAIT));
#else
return(send(SocketFD(), buffer, numBytes, 0));
#endif
}
/******************************************************/
socket_t CTastySocket::SocketFD()
/******************************************************/
{
return(_socketFD);
}
/******************************************************/
void CTastySocket::SocketFD(socket_t fd)
/******************************************************/
{
_socketFD = fd;
}
/******************************************************/
/******************************************************/
/******************************************************/
/******************************************************/
/******************************************************/
//Global calls below
/***********************************************************/
bool InitializeTheTastiness()
/***********************************************************/
{
#ifndef WIN32
sigignore(SIGPIPE); //In Linux, it is possible to get a broken pipe signal on a socket before writing is finished. This ignores the signal...error codes within TastySocket handle...
#else
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
return(iResult == NO_ERROR);
#endif
}
/***********************************************************/
bool ShutdownTheTastiness()
/***********************************************************/
{
#ifdef WIN32
WSACleanup();
#endif
return(true);
}