-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPraxisServer.cpp
More file actions
205 lines (170 loc) · 4.95 KB
/
PraxisServer.cpp
File metadata and controls
205 lines (170 loc) · 4.95 KB
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
#include "PraxisServer.h"
#ifdef __PRAXIS_WINDOWS__
#include <windows.h>
#include <mmsystem.h>
#include <winuser.h>
#include <winsock.h>
#endif
#ifdef __PRAXIS_LINUX__
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
namespace PraxisServer
{
SOCKET ServerSocket;
SOCKET LastAcceptedClientSocket;
}
bool PraxisServer::Start()
{
ServerSocket = INVALID_SOCKET;
LastAcceptedClientSocket = INVALID_SOCKET;
#ifdef __PRAXIS_WINDOWS__
// Start Winsock up
WSAData wsaData;
int nCode;
if ((nCode = WSAStartup(MAKEWORD(1, 1), &wsaData)) != 0) {
cerr << "WSAStartup() returned error code " << nCode << "." <<
endl;
return false;
}
#endif
// Begin listening for connections
cout << "Establishing the listener..." << endl;
ServerSocket = INVALID_SOCKET;
u_long nInterfaceAddr = inet_addr("127.0.0.1");
if (nInterfaceAddr != INADDR_NONE) {
ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
// Attempting to fix occasional "connection refused" message from putty
// which tends to happen when trying to re-establish the server
// after restarting praxis.
//
// "Pitfall 3: Address in use error (EADDRINUSE)"
// http://www.ibm.com/developerworks/library/l-sockpit/
int on = 1;
int ret = setsockopt( ServerSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) );
if (ServerSocket != INVALID_SOCKET) {
sockaddr_in sinInterface;
sinInterface.sin_family = AF_INET;
sinInterface.sin_addr.s_addr = nInterfaceAddr;
sinInterface.sin_port = htons(4242);
if (bind(ServerSocket, (sockaddr*)&sinInterface,
sizeof(sockaddr_in)) != SOCKET_ERROR) {
listen(ServerSocket, 1);
}
}
}
if (ServerSocket == INVALID_SOCKET)
return false;
#ifdef __PRAXIS_WINDOWS__
u_long iMode = 1;
ioctlsocket(ServerSocket, FIONBIO, &iMode);
#endif
#ifdef __PRAXIS_LINUX__
u_long iMode = 1;
ioctl(ServerSocket, FIONBIO, &iMode);
#endif
return true;
}
SOCKET PraxisServer::Accept()
{
if(ServerSocket == INVALID_SOCKET)
return INVALID_SOCKET;
//cout << "Waiting for a connection..." << flush;
sockaddr_in sinRemote;
int nAddrSize = sizeof(sinRemote);
#ifdef __PRAXIS_WINDOWS__
SOCKET clientSocket = accept(ServerSocket, (sockaddr*)&sinRemote, &nAddrSize);
#endif
#ifdef __PRAXIS_LINUX__
SOCKET clientSocket = accept(ServerSocket, (struct sockaddr*)&sinRemote, (socklen_t *)&nAddrSize);
#endif
if (clientSocket != INVALID_SOCKET) {
cout << "Accepted connection from " <<
inet_ntoa(sinRemote.sin_addr) << ":" <<
ntohs(sinRemote.sin_port) << "." << endl;
#ifdef __PRAXIS_WINDOWS__
u_long iMode = 1;
ioctlsocket(clientSocket, FIONBIO, &iMode);
#endif
#ifdef __PRAXIS_LINUX__
u_long iMode = 1;
ioctl(clientSocket, FIONBIO, &iMode);
#endif
}
LastAcceptedClientSocket = clientSocket;
return clientSocket;
}
std::string PraxisServer::Receive()
{
return Receive(LastAcceptedClientSocket);
}
std::string PraxisServer::Receive(SOCKET s)
{
char buf[65536];
int nBytes = recv(s, buf, 65536, 0);
if(nBytes > 0)
{
// cout << "Received " << nBytes << " bytes." << endl;
buf[nBytes] = '\0';
}
else
{
//cout << "Received no bytes, recv returned " << nBytes << endl;
buf[0] = '\0';
}
return std::string(buf);
}
void PraxisServer::Send(std::string & data)
{
Send(LastAcceptedClientSocket, data);
}
void PraxisServer::Send(SOCKET s, std::string & data)
{
int nReadBytes = data.length();
const char * buf = data.c_str();
//strcpy(buf, data.c_str());
int nSentBytes = 0;
while (nSentBytes < nReadBytes) {
int nTemp = send(s, buf + nSentBytes,
nReadBytes - nSentBytes, 0);
if (nTemp > 0) {
// cout << "Sent " << nTemp << " bytes back to client." << endl;
nSentBytes += nTemp;
}
else if (nTemp == SOCKET_ERROR) {
cout << "Socket error" << endl;
return;
}
else {
// Client closed connection before we could reply to
// all the data it sent, so bomb out early.
cout << "Peer unexpectedly dropped connection!" <<
endl;
return;
}
}
return;
}
bool PraxisServer::SocketIsValid(SOCKET s)
{
return (s != INVALID_SOCKET);
}
void PraxisServer::SetBlockingOption(u_long mode)
{
SetBlockingOption(LastAcceptedClientSocket, mode);
}
void PraxisServer::SetBlockingOption(SOCKET s, u_long mode)
{
#ifdef __PRAXIS_WINDOWS__
ioctlsocket(s, FIONBIO, &mode);
#endif
#ifdef __PRAXIS_LINUX__
ioctl(s, FIONBIO, &mode);
#endif
}