-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.c
311 lines (247 loc) · 6.67 KB
/
connection.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <limits.h>
#include <ev.h>
#include "common.h"
#include "buffer.h"
#include "log.h"
#include "connection.h"
#include "server.h"
#include "ptrlist.h"
#include "cli.h"
#include "universe.h"
#include "system.h"
#include "star.h"
#include "port.h"
#include "planet.h"
#include "player.h"
#include "mtrandom.h"
int conn_init(struct connection *conn)
{
assert(conn);
memset(conn, 0, sizeof(*conn));
pthread_mutex_init(&conn->worker_lock, NULL);
conn->id = mtrandom_uint(UINT32_MAX);
buffer_init(&conn->send);
buffer_init(&conn->recv);
INIT_LIST_HEAD(&conn->list);
INIT_LIST_HEAD(&conn->work);
return 0;
}
/*
* This function needs to be very safe as it can be called on a
* half-initialized connection structure if something went wrong.
*/
void connection_free(struct connection *conn)
{
if (!conn)
return;
pthread_mutex_destroy(&conn->worker_lock);
if (conn->peerfd)
close(conn->peerfd);
buffer_free(&conn->send);
buffer_free(&conn->recv);
if (conn->pl)
player_free(conn->pl);
}
__attribute__((format(printf, 2, 3)))
void conn_error(struct connection *data, char *fmt, ...)
{
char msg[128];
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
msg[sizeof(msg) - 1] = '\0';
conn_send(data, "Oops! An internal error occured: %s.\nYour current state is NOT saved and you are being forcibly disconnected.\nSorry!", msg);
server_disconnect_nicely(data);
}
#define PROMPT "yastg> "
void* connection_worker(void *_w)
{
struct conn_worker_list *w = _w;
struct conn_data *data = w->conn_data;
struct connection *conn;
do {
/*
* Remember: The locking here need to be synchronized with
* the locking in disconnect_peer() to avoid nasty races
* in case a connection is terminated when work is about to start.
*/
pthread_mutex_lock(&data->workers_lock);
while (list_empty(&data->work_items) && !w->terminate)
pthread_cond_wait(&data->workers_cond, &data->workers_lock);
if (w->terminate) {
pthread_mutex_unlock(&data->workers_lock);
break;
}
conn = list_first_entry(&data->work_items, struct connection, work);
list_del_init(&conn->work);
/*
* This needs to be in this order to avoid a race when
* server_disconnect_cb() is running _now_.
*/
pthread_mutex_lock(&conn->worker_lock);
pthread_mutex_unlock(&data->workers_lock);
if (conn->terminate) {
pthread_mutex_unlock(&conn->worker_lock);
continue;
}
if (conn->worker) {
pthread_mutex_unlock(&conn->worker_lock);
log_printfn(LOG_CONN, "Got new data too fast -- old worker is still busy");
continue;
}
conn->worker = 1;
pthread_mutex_unlock(&conn->worker_lock);
if (conn->recv.buf[0] != '\0' && cli_run_cmd(&conn->pl->cli, conn->recv.buf) < 0)
conn_send(conn, "Unknown command or syntax error: \"%s\"\n", conn->recv.buf);
buffer_reset(&conn->recv);
conn_send(conn, PROMPT);
pthread_mutex_lock(&conn->worker_lock);
conn->worker = 0;
pthread_mutex_unlock(&conn->worker_lock);
} while(1);
return NULL;
}
int conn_fulfixinit(struct connection *data)
{
log_printfn(LOG_CONN, "initializing connection from peer %s", data->peer);
if (list_len(&univ.ship_types) == 0)
return -1;
data->pl = malloc(sizeof(*data->pl));
if (!data->pl)
return -1;
if (player_init(data->pl))
return -1;
data->pl->conn = data;
if (new_ship_to_player(list_first_entry(&univ.ship_types, struct ship_type, list), data->pl))
return -1;
data->pl->pos = list_first_entry(&data->pl->ships, struct ship, list);
data->pl->postype = SHIP;
data->pl->credits = 100000;
player_go(data->pl, SYSTEM, ptrlist_entry(&univ.systems, 0));
conn_send(data, PROMPT);
log_printfn(LOG_CONN, "peer %s successfully logged in as %s", data->peer, data->pl->name);
return 0;
}
void conn_do_work(struct conn_data *data, struct connection *conn)
{
pthread_mutex_lock(&data->workers_lock);
list_add_tail(&conn->work, &data->work_items);
pthread_cond_signal(&data->workers_cond);
pthread_mutex_unlock(&data->workers_lock);
}
void conn_send_buffer(struct connection * const data)
{
assert(data);
assert(data->peerfd);
if (write_buffer_into_fd(data->peerfd, &data->send) < 1) {
log_printfn(LOG_CONN,
"send error (connection %x), terminating connection",
data->id);
server_disconnect_nicely(data);
}
}
static int start_new_worker(struct conn_data *data)
{
struct conn_worker_list *w;
sigset_t old, new;
sigfillset(&new);
w = malloc(sizeof(*w));
if (!w)
return -1;
memset(w, 0, sizeof(*w));
w->conn_data = data;
if (pthread_sigmask(SIG_SETMASK, &new, &old))
goto err_free;
if (pthread_create(&w->thread, NULL, connection_worker, w))
goto err_free;
if (pthread_sigmask(SIG_SETMASK, &old, NULL)) {
pthread_cancel(w->thread);
goto err_free;
}
list_add(&w->list, &data->workers);
return 0;
err_free:
free(w);
return -1;
}
#define NUM_WORKERS 4
static int initialize_workers(struct conn_data *data)
{
INIT_LIST_HEAD(&data->workers);
for (int i = 0; i < NUM_WORKERS; i++) {
if (start_new_worker(data))
goto err;
}
return 0;
struct conn_worker_list *w;
err:
list_for_each_entry(w, &data->workers, list)
free(w);
return -1;
}
int conndata_init(struct conn_data *data)
{
memset(data, 0, sizeof(*data));
INIT_LIST_HEAD(&data->work_items);
if (pthread_mutex_init(&data->workers_lock, NULL))
return -1;
if (pthread_cond_init(&data->workers_cond, NULL))
goto err_mutex;
if (initialize_workers(data))
goto err_cond;
return 0;
err_cond:
pthread_cond_destroy(&data->workers_cond);
err_mutex:
pthread_mutex_destroy(&data->workers_lock);
return -1;
}
void conn_shutdown(struct conn_data *data)
{
struct conn_worker_list *w;
pthread_mutex_lock(&data->workers_lock);
list_for_each_entry(w, &data->workers, list)
w->terminate = 1;
pthread_cond_broadcast(&data->workers_cond);
pthread_mutex_unlock(&data->workers_lock);
list_for_each_entry(w, &data->workers, list)
pthread_join(w->thread, NULL);
}
void conn_destroy(struct conn_data *data)
{
pthread_cond_destroy(&data->workers_cond);
pthread_mutex_destroy(&data->workers_lock);
struct conn_worker_list *w, *p;
list_for_each_entry_safe(w, p, &data->workers, list)
free(w);
}
__attribute__((format(printf, 2, 3)))
void conn_send(void *_conn, const char *fmt, ...)
{
struct connection *conn = _conn;
va_list ap;
assert(conn);
if (conn->terminate)
return;
va_start(ap, fmt);
vbufprintf(&conn->send, fmt, ap);
va_end(ap);
conn_send_buffer(conn);
}