-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.cpp
282 lines (277 loc) · 7.53 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <iostream>
#ifndef _WIN32
#include <unistd.h>
#include <arpa/inet.h>
#include <signal.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif
#include <orts/clientext.h>
#include <orts/common.h>
#include <orts/server.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <chrono>
using namespace std;
using namespace ORserver;
bool go=true;
void quit(int sig)
{
go = false;
}
void server_broadcast()
{
char msg[] = "Train Simulator Server";
while(go)
{
int sock = socket(AF_INET,SOCK_DGRAM,0);
if (sock < 0) return;
#ifdef _WIN32
char br = 1;
#else
int br = 1;
#endif
if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &br, sizeof(br))==-1)
{
perror("setsockopt");
goto cleanup;
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &br, sizeof(br));
struct sockaddr_in bind_addr;
bind_addr.sin_family = AF_INET;
bind_addr.sin_port = htons(5091);
bind_addr.sin_addr.s_addr = INADDR_ANY;
if(::bind(sock,(struct sockaddr*)&(bind_addr), sizeof(struct sockaddr_in))==-1)
{
perror("bind");
goto cleanup;
}
while (go)
{
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
fd_set fds;
FD_ZERO(&fds) ;
FD_SET(sock, &fds);
int res = select(sock+1, &fds, nullptr, nullptr, &tv);
if (res < 0) break;
if (res == 1)
{
char buff[25];
recv(sock,buff,25,0);
if (std::string(buff) == "Train Simulator Client")
{
for (int i=0; i<3; i++)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(5091);
addr.sin_addr.s_addr = INADDR_BROADCAST;
sendto(sock, msg, sizeof(msg), 0,(struct sockaddr *)&(addr), sizeof(struct sockaddr_in));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
}
}
}
cleanup:
#ifdef _WIN32
closesocket(sock);
#else
close(sock);
#endif
if (go) std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
class SerialManager
{
set<string> connectable;
set<string> connected;
public:
SerialManager()
{
#ifndef RELEASE
#ifdef WIN32
/*connectable.insert("COM5");
connectable.insert("COM6");
connectable.insert("COM7");
connectable.insert("COM8");
connectable.insert("COM9");
connectable.insert("COM10");*/
#else
/*connectable.insert("/dev/ttyACM0");
connectable.insert("/dev/ttyACM1");
connectable.insert("/dev/ttyACM2");
connectable.insert("/dev/ttyACM3");
connectable.insert("/dev/ttyUSB0");
connectable.insert("/dev/ttyUSB1");
connectable.insert("/dev/ttyUSB2");
connectable.insert("/dev/ttyUSB3");*/
#endif
#endif
}
set<string> available()
{
set<string> avail;
for(auto it=connectable.begin(); it!=connectable.end(); ++it) {
bool exists;
#ifdef WIN32
char buff[500];
exists = QueryDosDevice(it->c_str(), buff, 500) != 0;
#else
exists = access(it->c_str(), 0) == 0;
#endif
if(exists)
{
if(connected.find(*it)==connected.end()) {
connected.insert(*it);
avail.insert(*it);
}
}
else connected.erase(*it);
}
return avail;
}
};
void run_server(Server &srv, bool is_local)
{
#ifdef _WIN32
WSADATA wsa;
WSAStartup(MAKEWORD(2,2), &wsa);
#endif
int server = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serv;
serv.sin_family = AF_INET;
serv.sin_port = htons(5090);
serv.sin_addr.s_addr = INADDR_ANY;
if (server == -1) {
perror("socket");
return;
}
#ifdef _WIN32
char ra = 1;
#else
int ra = 1;
#endif
if(setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &ra, sizeof(ra))==-1) {
perror("setsockopt");
return;
}
if (::bind(server, (struct sockaddr *)&(serv), sizeof(serv)) == -1) {
perror("bind");
return;
}
if (listen(server, SOMAXCONN) == -1) {
perror("listen");
return;
}
evwait *poller = new threadwait();
poller->add({FD, server});
thread broadcast;
if (is_local) srv.AddClient(TCPclient::connect_to_server(poller));
else broadcast = thread(server_broadcast);
#ifdef WIN32
/*string port;
cout<<"Escriba numero de puerto: ";
cin>>port;
auto cl = new SerialClient(port, 115200, poller);
this_thread::sleep_for(chrono::seconds(3));
srv.AddClient(cl);*/
#endif
SerialManager serial;
bool err = false;
while (go) {
auto ser = serial.available();
for(auto it=ser.begin(); it!=ser.end(); ++it)
{
srv.AddClient(new SerialClient(*it, 115200, poller));
cout<<"Added "<<*it<<endl;
}
//auto t1 = chrono::system_clock::now();
int nfds = poller->poll(5000);
//auto t2 = chrono::system_clock::now();
//cout<<chrono::duration_cast<chrono::duration<int, milli>>(t2-t1).count()<<endl;
if (nfds == 0) {
continue;
}
if (nfds == -1) {
perror("poll");
if(err)
break;
else
err = true;
}
err = false;
if(!go)
break;
int revents = poller->get_events({FD, server});
if(revents>0) {
struct sockaddr_in addr;
int c = sizeof(struct sockaddr_in);
int cl = accept(server, (struct sockaddr *)&addr,
#ifdef _WIN32
(int *)
#else
(socklen_t *)
#endif
&c);
if(cl == -1) {
perror("accept");
continue;
}
cout<<"Added TCP client"<<endl;
srv.AddClient(new TCPclient(cl, poller));
}
const std::set<client*> &clients = srv.get_clients();
for (auto it=clients.begin(); it!=clients.end();) {
(*it)->handle();
string s = (*it)->ReadLine();
while (s != "") {
srv.ParseLine(*it, s);
s = (*it)->ReadLine();
}
if(!(*it)->connected) {
client *c = *it;
++it;
srv.RemoveClient(c);
delete c;
} else {
++it;
}
}
}
printf("Waiting for clients to close...\n");
const std::set<client*> &clients = srv.get_clients();
for (auto it=clients.begin(); it!=clients.end(); ++it) {
delete *it;
}
delete poller;
printf("Waiting for server to close...\n");
#ifdef _WIN32
closesocket(server);
#else
close(server);
#endif
if (!is_local) broadcast.join();
}
int main(int argc, char **argv)
{
cout<<"Servidor de comunicación entre periféricos y simulador."<<endl;
cout<<"Por favor, no cierre esta ventana mientras quiera utilizar el simulador"<<endl;
bool local_server = false;
if (argc > 1 && argv[1][0] == 'l')
{
local_server = true;
cout<<"Servidor local"<<endl;
}
#ifndef _WIN32
signal(SIGINT, quit);
signal(SIGPIPE, SIG_IGN);
#endif
Server server;
run_server(server, local_server);
return 0;
}