-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteServer.cpp
220 lines (181 loc) · 4.64 KB
/
RemoteServer.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
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include <cstring>
#include <string>
#include <iostream>
#include "RemoteServer.h"
#include "ServerThread.h"
RemoteServer::~RemoteServer() {
if (running) {
running = false;
sendMsg("exit");
}
if (loop_th.joinable()) {
if (loop_th.get_id() != std::this_thread::get_id()) {
loop_th.join();
} else {
loop_th.detach();
}
}
close(sockfd);
}
void RemoteServer::init(const char* host, const char* port) {
if (running)
return;
struct addrinfo hints, *result, *rp;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int s = getaddrinfo(host, port, &hints, &result);
if (s != 0)
throw std::string(gai_strerror(s));
// Try to connect to the list of addresses returned by getaddrinfo()
for (rp = result; rp != NULL; rp = rp->ai_next) {
sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sockfd == -1)
continue;
if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
break;
close(sockfd);
}
if (rp == NULL)
throw std::string("Couldn't connect to server.");
freeaddrinfo(result);
int flag = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(int)) < 0)
throw std::string("Setting TCP_NODELAY option failed.");
running = true;
loop_th = std::thread(&RemoteServer::loop, this);
}
void RemoteServer::loop() {
char buf[BUFSIZE];
std::string buffer;
uint32_t head;
size_t size(0);
ssize_t len;
int* data;
struct pollfd pfd;
pfd.fd = sockfd;
pfd.events = POLLIN | POLLHUP;
while (running) {
if (poll(&pfd, 1, 100) > 0) {
if ((len = recv(sockfd, buf, BUFSIZE, MSG_DONTWAIT)) <= 0) {
running = false;
ct.delServer(this);
return;
}
buffer.append(buf, len);
}
if (!running)
return;
if (size == 0 && buffer.size() >= 8) {
data = (int*) buffer.data();
head = ntohl(data[0]);
size = ntohl(data[1]);
}
if (buffer.size() >= size + 8) {
data = (int*) (buffer.data() + 8);
switch (head) {
case INIT_CLIENT:
ct.initView(ntohl(data[0]), ntohl(data[1]));
break;
case UPDATE_STATE:
ct.update(buffer.data() + 8, size);
break;
case CREATE_SERVER:
if (!createServer(ntohl(data[0]), buffer.data() + 12, size - 4)) {
running = false;
ct.delServer(this);
return;
}
break;
case NEW_SERVER:
newServer(buffer.data() + 8, buffer.data() + buffer.find('\0', 8) + 1);
break;
case PLAYER_MOVED:
ct.moveDone();
break;
case STATE_ACQUIRED:
ct.stateAcquired(true);
break;
case NOT_OWNER:
ct.stateAcquired(false);
break;
}
buffer.erase(0, size + 8);
size = 0;
}
}
}
bool RemoteServer::move(char dir) {
std::string msg;
switch (dir) {
case 'N':
case 'E':
case 'W':
case 'S':
msg += dir;
break;
default:
msg = "NoMove";
break;
}
return sendMsg(msg);
}
bool RemoteServer::join() {
return sendMsg("join");
}
bool RemoteServer::connectSrv(int id) {
std::string msg = "connect";
uint32_t nId = htonl(id);
msg.append((char*) &nId, 4);
return sendMsg(msg);
}
bool RemoteServer::createServer(int N, const char* state, size_t size) {
const ServerThread* st = ct.startServer(N, state, size);
std::string msg = "server";
msg += '\0';
msg += st ? st->getPort() : "";
// Send the new backup server address and port to the main server
return sendMsg(msg);
}
void RemoteServer::newServer(const char* host, const char* port) {
// Connect to the new received server
RemoteServer* serv = new RemoteServer(ct);
if (serv) {
try {
serv->init(host, port);
ct.addServer(serv);
} catch (const std::string& e) {
std::cerr << "Error: " << e << std::endl;
delete serv;
}
}
}
bool RemoteServer::movePlayer(int id, char dir) {
std::string msg = MOVE_PLAYER;
msg += dir;
uint32_t n_id = htonl(id);
msg.append((char*) &n_id, 4);
return sendMsg(msg);
}
bool RemoteServer::requestStateOwnership() {
return sendMsg(REQUEST_STATE);
}
bool RemoteServer::sendMsg(const std::string& msg) {
ssize_t n;
size_t len = msg.size() + 1;
const char* p = msg.data();
std::lock_guard<std::mutex> sock_lck(sock_mtx);
while ((n = send(sockfd, p, len, 0)) > 0) {
p += n;
len -= n;
}
if (n < 0)
return false;
return true;
}