-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
277 lines (251 loc) · 6.93 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
/*
* This file is part of btcontrol
*
* Copyright (C) Christian Ferbar
*
* btcontrol is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* btcontrol is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with btcontrol. If not, see <http://www.gnu.org/licenses/>.
*
* TCP - server
*/
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <cxxabi.h>
#include "tcpclient.h"
#include "server.h"
#include "utils.h"
#include "clientthread.h"
#ifdef INCL_X11
#include "clientthread_X11.h"
#endif
#include <stdexcept>
bool Server::isrunning=false;
bool Server::exit=false;
std::map <int,pthread_t> Server::clients;
Server::Server(int port)
: clientID_counter(1)
{
// init socket
int on=1;
int rc;
struct sockaddr_in address;
bzero((char *)&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
this->tcp_so = socket(AF_INET, SOCK_STREAM, 0);
if(this->tcp_so < 0) {
perror("Error in socket ");
abort();
}
setsockopt(this->tcp_so, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
// potentialy not threadsafe !
// struct hostent *hp=gethostbyname("localhost");
struct hostent *hp=gethostbyname("0.0.0.0");
if(! hp) {
perror("invalid IP");
abort();
}
memcpy((char *)&address.sin_addr,(char *)hp->h_addr,(size_t)hp->h_length);
rc = bind(this->tcp_so, (struct sockaddr *)&address, sizeof(address));
if (rc!=0)
{
perror("bind error");
abort();
}
rc = ::listen(this->tcp_so, 20);
if (rc!=0)
{
perror("Error in Listen");
abort();
}
}
int Server::accept()
{
// assert(this->bt_so > 0); - keine BT hardware -> this->bt_so < 0
assert(this->tcp_so > 0); // FD_SET mit -1 macht was hin
fd_set fd;
FD_ZERO(&fd);
#ifdef INCL_BT
if(this->bt_so > 0) {
FD_SET(this->bt_so,&fd);
}
#endif
FD_SET(this->tcp_so,&fd);
int rc=select(FD_SETSIZE, &fd, NULL, NULL, NULL);
printf("Server::accept - select rc=%d\n",rc);
if( rc < 0 ) {
perror("select/accept error");
return -1;
}
#ifdef INCL_BT
if(this->bt_so > 0 && FD_ISSET(this->bt_so,&fd)) {
printf("bt connection\n");
return BTServer::accept();
} else
#endif
if(FD_ISSET(this->tcp_so,&fd)) {
printf("tcp connection (%d)\n",this->tcp_so);
struct sockaddr_in addr2;
socklen_t siz;
siz = (socklen_t)sizeof(addr2);
int csock = ::accept(this->tcp_so, (struct sockaddr *)&addr2, &siz);
if(csock < 0) {
perror("so->accept error:");
throw std::runtime_error("so->accept error");
}
printf("socket: %d\n",csock);
return csock;
} else {
printf("Puit?\n");
abort();
}
/*
struct linger ling;
ling.l_onoff=1;
ling.l_linger=10;
setsockopt(csock, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling)); */
perror("select/accept error");
return -1;
}
/**
* unregister handler
*/
static void unregisterPhoneClient(void *data)
{
startupdata_t *startupData=(startupdata_t *)data;
printf("%d:unregisterPhoneClient\n",startupData->clientID);
Server::clientDone(startupData->clientID);
free(startupData);
}
/**
* für jedes handy ein eigener thread...
*/
static void *phoneClient(void *data)
{
startupdata_t *startupData=(startupdata_t *)data;
NOTICEF("%d:new client (so:%d)",startupData->clientID, startupData->so);
pthread_cleanup_push(unregisterPhoneClient,data);
try {
#ifdef INCL_X11
if(cfg_X11) {
ClientThreadX11 client(startupData->clientID, cfg_tcpTimeout);
client.begin(new TCPClient(startupData->so), true);
client.run();
} else
#endif
{
# warning todo: phoneClient() auf thread.start() umstellen
ClientThread client(startupData->clientID, cfg_tcpTimeout);
client.begin(new TCPClient(startupData->so), true);
// !!! kein start !!!
client.run();
}
} catch(const char *e) {
ERRORF("%d: exception %s - client thread killed", startupData->clientID,e);
} catch(std::RuntimeExceptionWithBacktrace &e) {
ERRORF("%d: Runtime Exception %s - client thread killed", startupData->clientID,e.what());
} catch(std::exception &e) {
ERRORF("%d: exception %s - client thread killed", startupData->clientID,e.what());
} catch (abi::__forced_unwind&) { // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28145
ERRORF("%d: forced unwind exception - client thread killed", startupData->clientID);
// copy &paste:
// printf("%d:client exit\n",startupData->clientID);
// pthread_cleanup_pop(true);
throw; // rethrow exeption bis zum pthread_create, dort isses dann aus
}
DEBUGF("%d:client exit",startupData->clientID);
pthread_cleanup_pop(true);
return NULL;
}
void Server::run()
{
assert(Server::isrunning==false); // darf nur einmal gestartet wern
Server::isrunning=true;
while(1) {
int nsk = this->accept();
if(nsk < 0) { // fehler, kann auch z.b. interrupted system call sein
if(this->exit) {
Server::isrunning=false;
break;
}
continue;
}
// client thread vorbereiten + starten
startupdata_t *startupData=(startupdata_t*) calloc(sizeof(startupdata_t),1);
startupData->clientID=this->clientID_counter++;
startupData->so=nsk;
pthread_t &newThread=Server::clients[startupData->clientID];
bzero(&newThread,sizeof(newThread));
#ifdef NO_THREADS
phoneClient((void *)startupData);
DEBUGF("client func done: %lx\n",newThread);
#else
if(int rc=pthread_create(&newThread, NULL, phoneClient, (void *)startupData) != 0) {
printf("error creating new thread rc=%d\n",rc);
perror("error creating new thread ");
}
printf("new Thread: %lx\n",newThread);
pthread_detach(newThread);
#endif
}
}
/**
* setzt nur das exit - flag!
*/
void Server::setExit() {
Server::exit=true;
for (std::map<int,pthread_t>::iterator it=Server::clients.begin(); it!=Server::clients.end(); ++it) {
if(pthread_cancel(it->second) != 0) {
perror("error sending pthread cancel");
}
}
}
/**
* wartet drauf dass alle threads weg sind:
* mainThread geht nur weg wenna ein INTR bekommt (vom ctrl+c z.b.)
*/
void Server::waitExit() {
printf("waiting for main thread exit\n"); //
while(Server::isrunning){
printf(".");
sleep(1);
}
// lock ??
printf("waiting for client threads\n");
while(true) {
if(Server::clients.empty()) {
break;
}
for (std::map<int,pthread_t>::iterator it=Server::clients.begin(); it!=Server::clients.end(); ++it) {
printf("%d:%lu\n",it->first,it->second);
}
sleep(1);
}
printf("done\n");
}
void Server::clientDone(int clientID) {
Server::clients.erase(clientID);
}