Files @ r11679:85c8ce63beba
Branch filter:

Location: cpp/openttd-patchpack/source/src/network/network_udp.cpp - annotation

translators
(svn r16069) -Update: WebTranslator2 update to 2009-04-16 17:52:36
brazilian_portuguese - 12 fixed by tucalipe (12)
korean - 8 fixed by darkttd (8)
slovenian - 10 fixed by Necrolyte (10)
  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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
r5584:545d748cc681
r5584:545d748cc681
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r6121:a8ff6abe7fb2
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r8140:9424f012f6a2
r8139:4fc34c1bc2fb
r5584:545d748cc681
r10828:d8d68d002525
r5584:545d748cc681
r9844:cd80b865ae80
r8132:f5cfeb0d46c0
r10824:4036289a0b3d
r8214:9a3935f9ef4e
r10208:ef8fcc3dc4ca
r8270:c2ec53375c9d
r10824:4036289a0b3d
r9451:01b0b6cc547f
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10824:4036289a0b3d
r10824:4036289a0b3d
r11584:8dbc3c7df3bd
r11584:8dbc3c7df3bd
r11584:8dbc3c7df3bd
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10476:e0a6350eb358
r10476:e0a6350eb358
r10476:e0a6350eb358
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11584:8dbc3c7df3bd
r5619:40c6c3e3d5f7
r11606:d8d6a74590cd
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r7372:a3bbd9f434e7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11637:3f8eca297664
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11584:8dbc3c7df3bd
r11584:8dbc3c7df3bd
r11584:8dbc3c7df3bd
r11584:8dbc3c7df3bd
r11584:8dbc3c7df3bd
r11584:8dbc3c7df3bd
r5619:40c6c3e3d5f7
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11593:8dd431018ab1
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5584:545d748cc681
r11363:6906c490a00e
r9451:01b0b6cc547f
r5584:545d748cc681
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r10207:a1fc2f2a33db
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r9451:01b0b6cc547f
r10310:bfae42dc59ac
r10310:bfae42dc59ac
r10310:bfae42dc59ac
r5584:545d748cc681
r5898:2191f99d1ffb
r9451:01b0b6cc547f
r5584:545d748cc681
r11363:6906c490a00e
r5898:2191f99d1ffb
r5584:545d748cc681
r11545:27d3f758c0c0
r5584:545d748cc681
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5584:545d748cc681
r11363:6906c490a00e
r5584:545d748cc681
r5584:545d748cc681
r5898:2191f99d1ffb
r5584:545d748cc681
r5584:545d748cc681
r5900:b72b667bb359
r10207:a1fc2f2a33db
r5584:545d748cc681
r10457:8667446e8fd4
r10457:8667446e8fd4
r10457:8667446e8fd4
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r10457:8667446e8fd4
r5584:545d748cc681
r5584:545d748cc681
r5898:2191f99d1ffb
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r9146:d873efafcd56
r5584:545d748cc681
r11545:27d3f758c0c0
r5584:545d748cc681
r5900:b72b667bb359
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5638:174d9d13ce21
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5898:2191f99d1ffb
r5900:b72b667bb359
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10310:bfae42dc59ac
r10310:bfae42dc59ac
r6492:225d1a5ff6b7
r5900:b72b667bb359
r5584:545d748cc681
r5584:545d748cc681
r5898:2191f99d1ffb
r5584:545d748cc681
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11363:6906c490a00e
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11545:27d3f758c0c0
r5619:40c6c3e3d5f7
r11363:6906c490a00e
r11546:a4422f41f18b
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r6229:a69564e4eb2e
r8970:a7dc16677ffd
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5898:2191f99d1ffb
r5619:40c6c3e3d5f7
r5900:b72b667bb359
r5619:40c6c3e3d5f7
r5898:2191f99d1ffb
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11546:a4422f41f18b
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11611:ffe270ad19c0
r11545:27d3f758c0c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r6178:fc8bd2bde93a
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11604:033abec750d4
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r11604:033abec750d4
r5619:40c6c3e3d5f7
r11604:033abec750d4
r10822:adfba97cb1d1
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r5624:8f8d1f8d3a74
r5624:8f8d1f8d3a74
r5624:8f8d1f8d3a74
r11611:ffe270ad19c0
r11611:ffe270ad19c0
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11545:27d3f758c0c0
r5584:545d748cc681
r5900:b72b667bb359
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5900:b72b667bb359
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5638:174d9d13ce21
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5584:545d748cc681
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r6424:17d8688db313
r6424:17d8688db313
r5584:545d748cc681
r6424:17d8688db313
r6424:17d8688db313
r6424:17d8688db313
r5584:545d748cc681
r7931:4c17a74c399e
r5584:545d748cc681
r5584:545d748cc681
r11363:6906c490a00e
r5619:40c6c3e3d5f7
r5584:545d748cc681
r11591:dcfe7b4a6015
r5898:2191f99d1ffb
r5584:545d748cc681
r11591:dcfe7b4a6015
r5584:545d748cc681
r11593:8dd431018ab1
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11363:6906c490a00e
r6247:96e840dbefcc
r5584:545d748cc681
r5898:2191f99d1ffb
r11545:27d3f758c0c0
r5584:545d748cc681
r11363:6906c490a00e
r5900:b72b667bb359
r11604:033abec750d4
r5584:545d748cc681
r11597:39dbd2f0af04
r5584:545d748cc681
r11545:27d3f758c0c0
r5584:545d748cc681
r5584:545d748cc681
r11363:6906c490a00e
r6247:96e840dbefcc
r5584:545d748cc681
r11363:6906c490a00e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11547:d8613f4c59f9
r11609:9bf9c36c3172
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11546:a4422f41f18b
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10822:adfba97cb1d1
r5584:545d748cc681
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r7331:9df263aec9d0
r5584:545d748cc681
r5584:545d748cc681
r10824:4036289a0b3d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11545:27d3f758c0c0
r5584:545d748cc681
r5584:545d748cc681
r5898:2191f99d1ffb
r5584:545d748cc681
r5900:b72b667bb359
r9420:7c6f91f6791c
r10824:4036289a0b3d
r10824:4036289a0b3d
r11597:39dbd2f0af04
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11637:3f8eca297664
r11637:3f8eca297664
r11637:3f8eca297664
r11637:3f8eca297664
r11637:3f8eca297664
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11637:3f8eca297664
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11545:27d3f758c0c0
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r11584:8dbc3c7df3bd
r10824:4036289a0b3d
r10824:4036289a0b3d
r11597:39dbd2f0af04
r10824:4036289a0b3d
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r9481:666e41b8d7be
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10824:4036289a0b3d
r10824:4036289a0b3d
r10824:4036289a0b3d
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r11608:fbeed55c850b
r11608:fbeed55c850b
r11608:fbeed55c850b
r11606:d8d6a74590cd
r10476:e0a6350eb358
r10476:e0a6350eb358
r10824:4036289a0b3d
r11593:8dd431018ab1
r11629:97018c71b598
r11629:97018c71b598
r11593:8dd431018ab1
r11629:97018c71b598
r11593:8dd431018ab1
r11606:d8d6a74590cd
r11629:97018c71b598
r11629:97018c71b598
r11606:d8d6a74590cd
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10824:4036289a0b3d
r5584:545d748cc681
r5584:545d748cc681
r11606:d8d6a74590cd
r5619:40c6c3e3d5f7
r10824:4036289a0b3d
r11606:d8d6a74590cd
r11606:d8d6a74590cd
r11606:d8d6a74590cd
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r10476:e0a6350eb358
r10476:e0a6350eb358
r10476:e0a6350eb358
r10824:4036289a0b3d
r11606:d8d6a74590cd
r11606:d8d6a74590cd
r11606:d8d6a74590cd
r11606:d8d6a74590cd
r5619:40c6c3e3d5f7
r5619:40c6c3e3d5f7
r5584:545d748cc681
/* $Id$ */

/**
 * @file network_udp.cpp This file handles the UDP related communication.
 *
 * This is the GameServer <-> MasterServer and GameServer <-> GameClient
 * communication before the game is being joined.
 */

#ifdef ENABLE_NETWORK

#include "../stdafx.h"
#include "../date_func.h"
#include "../map_func.h"
#include "network_gamelist.h"
#include "network_internal.h"
#include "network_udp.h"
#include "core/host.h"
#include "../core/endian_func.hpp"
#include "../core/alloc_func.hpp"
#include "../string_func.h"
#include "../company_base.h"
#include "../settings_type.h"
#include "../thread.h"
#include "../rev.h"

#include "core/udp.h"

ThreadMutex *_network_udp_mutex = ThreadMutex::New();

/** Session key to register ourselves to the master server */
static uint64 _session_key = 0;

enum {
	ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes)
	ADVERTISE_RETRY_INTERVAL  =   300, // readvertise when no response after this many ticks (9 seconds)
	ADVERTISE_RETRY_TIMES     =     3  // give up readvertising after this much failed retries
};

NetworkUDPSocketHandler *_udp_client_socket = NULL; ///< udp client socket
NetworkUDPSocketHandler *_udp_server_socket = NULL; ///< udp server socket
NetworkUDPSocketHandler *_udp_master_socket = NULL; ///< udp master socket

///*** Communication with the masterserver ***/

class MasterNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
protected:
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_SESSION_KEY);
public:
	MasterNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
	virtual ~MasterNetworkUDPSocketHandler() {}
};

DEF_UDP_RECEIVE_COMMAND(Master, PACKET_UDP_MASTER_ACK_REGISTER)
{
	_network_advertise_retries = 0;
	DEBUG(net, 2, "[udp] advertising on master server successful");

	/* We are advertised, but we don't want to! */
	if (!_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise(false);
}

DEF_UDP_RECEIVE_COMMAND(Master, PACKET_UDP_MASTER_SESSION_KEY)
{
	_session_key = p->Recv_uint64();
	DEBUG(net, 2, "[udp] received new session key from master server");
}

///*** Communication with clients (we are server) ***/

class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
protected:
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
public:
	ServerNetworkUDPSocketHandler(NetworkAddressList *addresses) : NetworkUDPSocketHandler(addresses) {}
	virtual ~ServerNetworkUDPSocketHandler() {}
};

DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_FIND_SERVER)
{
	/* Just a fail-safe.. should never happen */
	if (!_network_udp_server) {
		return;
	}

	NetworkGameInfo ngi;

	/* Update some game_info */
	ngi.clients_on     = _network_game_info.clients_on;
	ngi.start_date     = _network_game_info.start_date;

	ngi.server_lang    = _settings_client.network.server_lang;
	ngi.use_password   = !StrEmpty(_settings_client.network.server_password);
	ngi.clients_max    = _settings_client.network.max_clients;
	ngi.companies_on   = ActiveCompanyCount();
	ngi.companies_max  = _settings_client.network.max_companies;
	ngi.spectators_on  = NetworkSpectatorCount();
	ngi.spectators_max = _settings_client.network.max_spectators;
	ngi.game_date      = _date;
	ngi.map_width      = MapSizeX();
	ngi.map_height     = MapSizeY();
	ngi.map_set        = _settings_game.game_creation.landscape;
	ngi.dedicated      = _network_dedicated;
	ngi.grfconfig      = _grfconfig;

	strecpy(ngi.map_name, _network_game_info.map_name, lastof(ngi.map_name));
	strecpy(ngi.server_name, _settings_client.network.server_name, lastof(ngi.server_name));
	strecpy(ngi.server_revision, _openttd_revision, lastof(ngi.server_revision));

	Packet packet(PACKET_UDP_SERVER_RESPONSE);
	this->Send_NetworkGameInfo(&packet, &ngi);

	/* Let the client know that we are here */
	this->SendPacket(&packet, client_addr);

	DEBUG(net, 2, "[udp] queried from '%s'", client_addr->GetHostname());
}

DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO)
{
	/* Just a fail-safe.. should never happen */
	if (!_network_udp_server) return;

	Packet packet(PACKET_UDP_SERVER_DETAIL_INFO);

	/* Send the amount of active companies */
	packet.Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
	packet.Send_uint8 (ActiveCompanyCount());

	/* Fetch the latest version of the stats */
	NetworkCompanyStats company_stats[MAX_COMPANIES];
	NetworkPopulateCompanyStats(company_stats);

	Company *company;
	/* Go through all the companies */
	FOR_ALL_COMPANIES(company) {
		/* Send the information */
		this->Send_CompanyInformation(&packet, company, &company_stats[company->index]);
	}

	this->SendPacket(&packet, client_addr);
}

/**
 * A client has requested the names of some NewGRFs.
 *
 * Replying this can be tricky as we have a limit of SEND_MTU bytes
 * in the reply packet and we can send up to 100 bytes per NewGRF
 * (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name).
 * As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it
 * could be that a packet overflows. To stop this we only reply
 * with the first N NewGRFs so that if the first N + 1 NewGRFs
 * would be sent, the packet overflows.
 * in_reply and in_reply_count are used to keep a list of GRFs to
 * send in the reply.
 */
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_GET_NEWGRFS)
{
	uint8 num_grfs;
	uint i;

	const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT];
	uint8 in_reply_count = 0;
	size_t packet_len = 0;

	DEBUG(net, 6, "[udp] newgrf data request from %s", client_addr->GetAddressAsString());

	num_grfs = p->Recv_uint8 ();
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;

	for (i = 0; i < num_grfs; i++) {
		GRFConfig c;
		const GRFConfig *f;

		this->Recv_GRFIdentifier(p, &c);

		/* Find the matching GRF file */
		f = FindGRFConfig(c.grfid, c.md5sum);
		if (f == NULL) continue; // The GRF is unknown to this server

		/* If the reply might exceed the size of the packet, only reply
		 * the current list and do not send the other data.
		 * The name could be an empty string, if so take the filename. */
		packet_len += sizeof(c.grfid) + sizeof(c.md5sum) +
				min(strlen((f->name != NULL && !StrEmpty(f->name)) ? f->name : f->filename) + 1, (size_t)NETWORK_GRF_NAME_LENGTH);
		if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply
			break;
		}
		in_reply[in_reply_count] = f;
		in_reply_count++;
	}

	if (in_reply_count == 0) return;

	Packet packet(PACKET_UDP_SERVER_NEWGRFS);
	packet.Send_uint8(in_reply_count);
	for (i = 0; i < in_reply_count; i++) {
		char name[NETWORK_GRF_NAME_LENGTH];

		/* The name could be an empty string, if so take the filename */
		strecpy(name, (in_reply[i]->name != NULL && !StrEmpty(in_reply[i]->name)) ?
				in_reply[i]->name : in_reply[i]->filename, lastof(name));
		this->Send_GRFIdentifier(&packet, in_reply[i]);
		packet.Send_string(name);
	}

	this->SendPacket(&packet, client_addr);
}

///*** Communication with servers (we are client) ***/

class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
protected:
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
	virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config);
public:
	virtual ~ClientNetworkUDPSocketHandler() {}
};

DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
{
	NetworkGameList *item;

	/* Just a fail-safe.. should never happen */
	if (_network_udp_server) return;

	DEBUG(net, 4, "[udp] server response from %s", client_addr->GetAddressAsString());

	/* Find next item */
	item = NetworkGameListAddItem(*client_addr);

	this->Recv_NetworkGameInfo(p, &item->info);

	item->info.compatible = true;
	{
		/* Checks whether there needs to be a request for names of GRFs and makes
		 * the request if necessary. GRFs that need to be requested are the GRFs
		 * that do not exist on the clients system and we do not have the name
		 * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
		 * The in_request array and in_request_count are used so there is no need
		 * to do a second loop over the GRF list, which can be relatively expensive
		 * due to the string comparisons. */
		const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
		const GRFConfig *c;
		uint in_request_count = 0;

		for (c = item->info.grfconfig; c != NULL; c = c->next) {
			if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
			if (c->status != GCS_NOT_FOUND || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
			in_request[in_request_count] = c;
			in_request_count++;
		}

		if (in_request_count > 0) {
			/* There are 'unknown' GRFs, now send a request for them */
			uint i;
			Packet packet(PACKET_UDP_CLIENT_GET_NEWGRFS);

			packet.Send_uint8(in_request_count);
			for (i = 0; i < in_request_count; i++) {
				this->Send_GRFIdentifier(&packet, in_request[i]);
			}

			this->SendPacket(&packet, &item->address);
		}
	}

	if (item->info.hostname[0] == '\0') {
		snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", client_addr->GetHostname());
	}

	if (client_addr->GetAddress()->ss_family == AF_INET6) {
		strecat(item->info.server_name, " (IPv6)", lastof(item->info.server_name));
	}

	/* Check if we are allowed on this server based on the revision-match */
	item->info.version_compatible = IsNetworkCompatibleVersion(item->info.server_revision);
	item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs

	item->online = true;

	UpdateNetworkGameWindow(false);
}

DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_MASTER_RESPONSE_LIST)
{
	/* packet begins with the protocol version (uint8)
	 * then an uint16 which indicates how many
	 * ip:port pairs are in this packet, after that
	 * an uint32 (ip) and an uint16 (port) for each pair.
	 */

	ServerListType type = (ServerListType)(p->Recv_uint8() - 1);

	if (type < SLT_END) {
		for (int i = p->Recv_uint16(); i != 0 ; i--) {
			sockaddr_storage addr_storage;
			memset(&addr_storage, 0, sizeof(addr_storage));

			if (type == SLT_IPv4) {
				addr_storage.ss_family = AF_INET;
				((sockaddr_in*)&addr_storage)->sin_addr.s_addr = TO_LE32(p->Recv_uint32());
			} else {
				assert(type == SLT_IPv6);
				addr_storage.ss_family = AF_INET6;
				byte *addr = (byte*)&((sockaddr_in6*)&addr_storage)->sin6_addr;
				for (uint i = 0; i < sizeof(in6_addr); i++) *addr++ = p->Recv_uint8();
			}
			NetworkAddress addr(addr_storage, type == SLT_IPv4 ? sizeof(sockaddr_in) : sizeof(sockaddr_in6));
			addr.SetPort(p->Recv_uint16());

			/* Somehow we reached the end of the packet */
			if (this->HasClientQuit()) return;

			NetworkUDPQueryServer(addr);
		}
	}
}

/** The return of the client's request of the names of some NewGRFs */
DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_NEWGRFS)
{
	uint8 num_grfs;
	uint i;

	DEBUG(net, 6, "[udp] newgrf data reply from %s", client_addr->GetAddressAsString());

	num_grfs = p->Recv_uint8 ();
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;

	for (i = 0; i < num_grfs; i++) {
		char *unknown_name;
		char name[NETWORK_GRF_NAME_LENGTH];
		GRFConfig c;

		this->Recv_GRFIdentifier(p, &c);
		p->Recv_string(name, sizeof(name));

		/* An empty name is not possible under normal circumstances
		 * and causes problems when showing the NewGRF list. */
		if (StrEmpty(name)) continue;

		/* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple.
		 * If it exists and not resolved yet, then name of the fake GRF is
		 * overwritten with the name from the reply. */
		unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
		if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
			ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH);
		}
	}
}

void ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
{
	/* Find the matching GRF file */
	const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum);
	if (f == NULL) {
		/* Don't know the GRF, so mark game incompatible and the (possibly)
		 * already resolved name for this GRF (another server has sent the
		 * name of the GRF already */
		config->name   = FindUnknownGRFName(config->grfid, config->md5sum, true);
		config->status = GCS_NOT_FOUND;
	} else {
		config->filename  = f->filename;
		config->name      = f->name;
		config->info      = f->info;
	}
	SetBit(config->flags, GCF_COPY);
}

/* Broadcast to all ips */
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
{
	for (NetworkAddress *addr = _broadcast_list.Begin(); addr != _broadcast_list.End(); addr++) {
		Packet p(PACKET_UDP_CLIENT_FIND_SERVER);

		DEBUG(net, 4, "[udp] broadcasting to %s", addr->GetHostname());

		socket->SendPacket(&p, addr, true, true);
	}
}


/* Request the the server-list from the master server */
void NetworkUDPQueryMasterServer()
{
	Packet p(PACKET_UDP_CLIENT_GET_LIST);
	NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);

	/* packet only contains protocol version */
	p.Send_uint8(NETWORK_MASTER_SERVER_VERSION);
	p.Send_uint8(SLT_AUTODETECT);

	_udp_client_socket->SendPacket(&p, &out_addr, true);

	DEBUG(net, 2, "[udp] master server queried at %s", out_addr.GetAddressAsString());
}

/* Find all servers */
void NetworkUDPSearchGame()
{
	/* We are still searching.. */
	if (_network_udp_broadcast > 0) return;

	DEBUG(net, 0, "[udp] searching server");

	NetworkUDPBroadCast(_udp_client_socket);
	_network_udp_broadcast = 300; // Stay searching for 300 ticks
}

/** Simpler wrapper struct for NetworkUDPQueryServerThread */
struct NetworkUDPQueryServerInfo : NetworkAddress {
	bool manually; ///< Did we connect manually or not?
	NetworkUDPQueryServerInfo(const NetworkAddress &address, bool manually) :
		NetworkAddress(address),
		manually(manually)
	{
	}
};

/**
 * Threaded part for resolving the IP of a server and querying it.
 * @param pntr the NetworkUDPQueryServerInfo.
 */
void NetworkUDPQueryServerThread(void *pntr)
{
	NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr;

	/* Clear item in gamelist */
	NetworkGameList *item = CallocT<NetworkGameList>(1);
	item->address = *info;
	info->GetAddressAsString(item->info.server_name, lastof(item->info.server_name));
	strecpy(item->info.hostname, info->GetHostname(), lastof(item->info.hostname));
	item->manually = info->manually;
	NetworkGameListAddItemDelayed(item);

	_network_udp_mutex->BeginCritical();
	/* Init the packet */
	Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
	if (_udp_client_socket != NULL) _udp_client_socket->SendPacket(&p, info);
	_network_udp_mutex->EndCritical();

	delete info;
}

void NetworkUDPQueryServer(NetworkAddress address, bool manually)
{
	NetworkUDPQueryServerInfo *info = new NetworkUDPQueryServerInfo(address, manually);
	if (address.IsResolved() || !ThreadObject::New(NetworkUDPQueryServerThread, info)) {
		NetworkUDPQueryServerThread(info);
	}
}

void NetworkUDPRemoveAdvertiseThread(void *pntr)
{
	DEBUG(net, 1, "[udp] removing advertise from master server");

	/* Find somewhere to send */
	NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);

	/* Send the packet */
	Packet p(PACKET_UDP_SERVER_UNREGISTER);
	/* Packet is: Version, server_port */
	p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
	p.Send_uint16(_settings_client.network.server_port);

	_network_udp_mutex->BeginCritical();
	if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
	_network_udp_mutex->EndCritical();
}

/**
 * Remove our advertise from the master-server.
 * @param blocking whether to wait until the removal has finished.
 */
void NetworkUDPRemoveAdvertise(bool blocking)
{
	/* Check if we are advertising */
	if (!_networking || !_network_server || !_network_udp_server) return;

	if (blocking || !ThreadObject::New(NetworkUDPRemoveAdvertiseThread, NULL)) {
		NetworkUDPRemoveAdvertiseThread(NULL);
	}
}

void NetworkUDPAdvertiseThread(void *pntr)
{
	/* Find somewhere to send */
	NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);

	DEBUG(net, 1, "[udp] advertising to master server");

	/* Send the packet */
	Packet p(PACKET_UDP_SERVER_REGISTER);
	/* Packet is: WELCOME_MESSAGE, Version, server_port */
	p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
	p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
	p.Send_uint16(_settings_client.network.server_port);
	p.Send_uint64(_session_key);

	_network_udp_mutex->BeginCritical();
	if (_udp_master_socket != NULL) _udp_master_socket->SendPacket(&p, &out_addr, true);
	_network_udp_mutex->EndCritical();
}

/* Register us to the master server
     This function checks if it needs to send an advertise */
void NetworkUDPAdvertise()
{
	/* Check if we should send an advertise */
	if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise)
		return;

	if (_network_need_advertise) {
		_network_need_advertise = false;
		_network_advertise_retries = ADVERTISE_RETRY_TIMES;
	} else {
		/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
		if (_network_advertise_retries == 0) {
			if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter)
				return;
			_network_advertise_retries = ADVERTISE_RETRY_TIMES;
		}

		if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter)
			return;
	}

	_network_advertise_retries--;
	_network_last_advertise_frame = _frame_counter;

	if (!ThreadObject::New(NetworkUDPAdvertiseThread, NULL)) {
		NetworkUDPAdvertiseThread(NULL);
	}
}

void NetworkUDPInitialize()
{
	/* If not closed, then do it. */
	if (_udp_server_socket != NULL) NetworkUDPClose();

	DEBUG(net, 1, "[udp] initializing listeners");
	assert(_udp_client_socket == NULL && _udp_server_socket == NULL && _udp_master_socket == NULL);

	_network_udp_mutex->BeginCritical();

	_udp_client_socket = new ClientNetworkUDPSocketHandler();

	NetworkAddressList server;
	GetBindAddresses(&server, _settings_client.network.server_port);
	_udp_server_socket = new ServerNetworkUDPSocketHandler(&server);

	server.Clear();
	GetBindAddresses(&server, 0);
	_udp_master_socket = new MasterNetworkUDPSocketHandler(&server);

	_network_udp_server = false;
	_network_udp_broadcast = 0;
	_network_udp_mutex->EndCritical();
}

void NetworkUDPClose()
{
	_network_udp_mutex->BeginCritical();
	_udp_server_socket->Close();
	_udp_master_socket->Close();
	_udp_client_socket->Close();
	delete _udp_client_socket;
	delete _udp_server_socket;
	delete _udp_master_socket;
	_udp_client_socket = NULL;
	_udp_server_socket = NULL;
	_udp_master_socket = NULL;
	_network_udp_mutex->EndCritical();

	_network_udp_server = false;
	_network_udp_broadcast = 0;
	DEBUG(net, 1, "[udp] closed listeners");
}

#endif /* ENABLE_NETWORK */