forked from aitorzip/DeepGTAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
213 lines (187 loc) · 5.52 KB
/
Server.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
#include "Server.h"
#include <thread>
#include "lib/rapidjson/document.h"
#include "lib/rapidjson/stringbuffer.h"
#include "lib/main.h"
using namespace rapidjson;
Server::Server(unsigned int port) {
struct sockaddr_in server;
freopen("deepgtav.log", "w", stdout);
printf("\nInitializing Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("Failed. Error Code: %d", WSAGetLastError());
}
printf("Initialized.\n");
if ((ServerSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Could not create socket: %d", WSAGetLastError());
}
printf("Socket created.\n");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if (bind(ServerSocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Bind failed with error code: %d", WSAGetLastError());
}
printf("Bind done.\n");
printf("Listening...\n");
if (listen(ServerSocket, 1) == SOCKET_ERROR) {
printf("Could not listen: %d", WSAGetLastError());
}
if (ioctlsocket(ServerSocket, FIONBIO, &iMode) != NO_ERROR) {
printf("Server ioctlsocket failed");
}
}
void Server::checkClient(){
SOCKET tmpSocket = SOCKET_ERROR;
tmpSocket = accept(ServerSocket, NULL, NULL);
if (tmpSocket != SOCKET_ERROR) {
printf("Connection accepted.\n");
ClientSocket = tmpSocket;
if (ioctlsocket(ClientSocket, FIONBIO, &iMode) != NO_ERROR) {
printf("Client ioctlsocket failed");
return;
}
clientConnected = true;
}
}
void Server::checkRecvMessage() {
int result;
Document d;
int error;
if (recvMessageLen == 0) {
recv(ClientSocket, (char*)&recvMessageLen, 4, 0); //Receive message len first
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) return;
if (error != 0) {
printf("\nError receiving message length: %d", error);
resetState();
return;
}
}
while (bytesRead < recvMessageLen){
result = recv(ClientSocket, json + bytesRead, recvMessageLen - bytesRead, 0);
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) return;
if (error != 0 || result < 1) {
printf("\nError receiving message: %d", error);
resetState();
return;
}
bytesRead = bytesRead + result;
}
json[bytesRead] = '\0';
bytesRead = 0;
recvMessageLen = 0;
d.Parse(json);
if (d.HasMember("commands")) {
printf("Commands received\n");
const Value& commands = d["commands"];
scenario.setCommands(commands["throttle"].GetFloat(), commands["brake"].GetFloat(), commands["steering"].GetFloat());
}
else if (d.HasMember("config")) {
//Change the message values and keep the others the same
printf("Config received\n");
const Value& config = d["config"];
const Value& sc = config["scenario"];
const Value& dc = config["dataset"];
scenario.config(sc, dc);
}
else if (d.HasMember("start")) {
//Set the message values and randomize the others. Start sending the messages
printf("Start received\n");
const Value& config = d["start"];
const Value& sc = config["scenario"];
const Value& dc = config["dataset"];
scenario.start(sc, dc);
sendOutputs = true;
}
else if (d.HasMember("stop")) {
//Stop sendig messages, keep client connected
printf("Stop received\n");
sendOutputs = false;
scenario.stop();
}
else {
return; //Invalid message
}
}
void Server::checkSendMessage() {
int error;
int r;
if (sendOutputs && (((float)(std::clock() - lastSentMessage) / CLOCKS_PER_SEC) > (1.0 / scenario.rate))) {
if (messageSize == 0) {
message = scenario.generateMessage();
chmessage = message.GetString();
messageSize = message.GetSize();
}
if (!frameSent) {
if (!readyToSend) {
send(ClientSocket, (const char*)&scenario.screenCapturer->length, sizeof(scenario.screenCapturer->length), 0);
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) return;
if (error != 0) {
printf("\nError sending frame length: %d", error);
resetState();
return;
}
readyToSend = true;
sendMessageLen = 0;
}
while (readyToSend && (sendMessageLen < scenario.screenCapturer->length)) {
r = send(ClientSocket, (const char*)(scenario.screenCapturer->pixels + sendMessageLen), scenario.screenCapturer->length - sendMessageLen, 0);
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) return;
if (error != 0 || r <= 1) {
printf("\nError sending frame: %d", error);
resetState();
return;
}
sendMessageLen = sendMessageLen + r;
}
readyToSend = false;
frameSent = true;
}
if (frameSent) {
if (!readyToSend) {
send(ClientSocket, (const char*)&messageSize, sizeof(messageSize), 0);
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) return;
if (error != 0) {
printf("\nError sending message length: %d", error);
resetState();
return;
}
readyToSend = true;
sendMessageLen = 0;
}
while (readyToSend && (sendMessageLen < messageSize)) {
r = send(ClientSocket, (const char*)(chmessage + sendMessageLen), messageSize - sendMessageLen, 0);
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) return;
if (error != 0 || r <= 1) {
printf("\nError sending message: %d", error);
resetState();
return;
}
sendMessageLen = sendMessageLen + r;
}
readyToSend = false;
messageSize = 0;
frameSent = false;
}
lastSentMessage = std::clock();
}
}
void Server::resetState() {
shutdown(ClientSocket, SD_SEND);
closesocket(ClientSocket);
clientConnected = false;
sendOutputs = false;
bytesRead = 0;
recvMessageLen = 0;
sendMessageLen = 0;
readyToSend = false;
frameSent = false;
messageSize = 0;
scenario.stop();
}