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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
|
/*
net/netconnection.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include <zlib.h>
#include <string.h>
#include <sstream>
#include "sys/sys.h"
#include "core/application.h"
#include "core/entity.h"
#include "core/entityglobe.h"
#include "core/gameconnection.h"
#include "core/netconnection.h"
#include "core/player.h"
#include "core/stats.h"
namespace core
{
NetConnection::NetConnection()
{
connection_timeout = core::application()->time();
connection_state = Connecting;
connection_timestamp = 0;
receive_compressed = false;
received_compressed_size = 0;
compressed_size = 0;
}
NetConnection::~NetConnection()
{
disconnect();
}
void NetConnection::abort()
{
connection_error = true;
}
void NetConnection::connect(std::string const &to_host, int to_port)
{
connection_error = false;
connection_fd = -1;
connection_state = Connecting;
if (valid())
return;
// resolve serverhostname
struct hostent *serverhostent;
serverhostent = gethostbyname(to_host.c_str());
if (!serverhostent) {
con_warn << "Could not resolve '" << to_host.c_str() << "'" << std::endl;
abort();
return;
}
// Get a socket file descriptor
connection_fd = socket(PF_INET, SOCK_DGRAM, 0);
if (connection_fd == -1) {
//con_error << "Network socket() failed!" << std::endl;
abort();
return;
}
// make the connection
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(to_port);
// FIXME inet_addr can still fail
server_addr.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *)serverhostent->h_addr)));
memset(server_addr.sin_zero, '\0', sizeof server_addr.sin_zero);
if (server_addr.sin_addr.s_addr == INADDR_NONE) {
con_error << "Network invalid address " << to_host << "!" << std::endl;
abort();
return;
}
connection_host = to_host;
connection_port = to_port;
connection_error = false;
FD_ZERO(&clientset);
#ifdef _WIN32
FD_SET((unsigned int) fd(), &clientset);
#else
FD_SET(fd(), &clientset);
#endif
connection_timeout = application()->time();
connection_keepalive = application()->time();
connection_state = Pending;
game()->localplayer()->set_dirty();
std::stringstream str;
str << "Connecting to " << inet_ntoa(*((struct in_addr *)serverhostent->h_addr)) << ":" << to_port << "...";
con_print << str.str() << std::endl;
application()->notify_loader(str.str());
}
void NetConnection::disconnect()
{
if (connection_fd != -1) {
sendq.clear();
sendq.assign("disconnect\n");
transmit();
FD_ZERO(&clientset);
#ifdef _WIN32
closesocket(connection_fd);
#else
close(connection_fd);
#endif
}
connection_fd = -1;
connection_error = false;
connection_host.clear();
connection_port = 0;
connection_state = Connecting;
}
bool NetConnection::has_messages() const
{
return (recvq.size() > 0);
}
void NetConnection::retreive(std::string & message)
{
if (recvq.size() > 0) {
message.assign(recvq.front());
recvq.pop_front();
} else {
message.clear();
}
}
// receive data and decode it into lines
void NetConnection::receive()
{
// TODO: binary mode data transfer
if (error() || invalid())
return;
ssize_t bytes_received;
memset(recvbuf, 0, BLOCKSIZE);
bytes_received = ::recv(connection_fd, recvbuf, BLOCKSIZE - 1, 0);
Stats::network_bytes_received += bytes_received;
connection_timeout = core::application()->time();
if (bytes_received == 0) {
con_print << "^BDisconnected." << std::endl;
abort();
return;
} else if (bytes_received < 0) {
con_error << "Network receive() error!" << std::endl;
//perror("recv");
abort();
return;
}
const char *c = recvbuf;
while (bytes_received) {
if (receive_compressed) {
zrecvbuf[received_compressed_size] = *c;
received_compressed_size++;
if (compressed_size == received_compressed_size) {
// uncompress
char zunbuf[BLOCKSIZE*2];
memset(zunbuf, 0, sizeof(zunbuf));
uLong zunbuf_size = (uLong) BLOCKSIZE * 2 - 1;
int status = uncompress((Bytef *) zunbuf, &zunbuf_size, (Bytef *) zrecvbuf, (uLong) compressed_size);
if (status != Z_OK) {
con_warn << "zlib error " << status << " uncompressing incoming datablock!\n";
} else {
const char *zc = zunbuf;
while (*zc) {
if ((*zc == '\n') || (*zc == '\r')) {
if (messageblock.size()) {
recvq.push_back(messageblock);
messageblock.clear();
}
} else {
if (messageblock.size() < FRAMESIZE) {
messageblock += *zc;
} else {
con_warn << "Incoming uncompressed message exceeds " << FRAMESIZE << " bytes!\n";
messageblock.clear();
}
}
zc++;
}
}
// reset
receive_compressed = false;
received_compressed_size = 0;
compressed_size = 0;
}
} else if (!messageblock.size() && (bytes_received > 3) && (*c == '\xff') && (*(c + 1) == '\xff')) {
receive_compressed = true;
received_compressed_size = 0;
compressed_size = (uLong)(*(unsigned char *)(c + 2) + (*(unsigned char *)(c + 3) << 8));
c += 3;
bytes_received -= 3;
memset(zrecvbuf, 0, sizeof(zrecvbuf));
if (compressed_size > BLOCKSIZE) {
con_warn << "Incoming compressed datablock exceeds " << BLOCKSIZE << " bytes!\n";
receive_compressed = false;
}
} else if ((*c == '\n') || (*c == '\r')) {
if (messageblock.size()) {
recvq.push_back(messageblock);
messageblock.clear();
}
} else {
if (messageblock.size() < FRAMESIZE) {
messageblock += *c;
} else {
con_warn << "Incoming message exceeds " << FRAMESIZE << " bytes!\n";
messageblock.clear();
}
}
c++;
bytes_received--;
}
}
void NetConnection::frame()
{
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
fd_set readset = clientset;
int nb = select(fd() + 1, &readset, NULL, NULL, &timeout);
if (nb == 0) {
if (connection_timeout + NETTIMEOUT < core::application()->time()) {
con_error << "Connection timeout!\n";
abort();
}
return;
}
if (nb == -1) {
con_error << "Network error on select()" << std::endl;
//perror("select");
abort();
}
while (FD_ISSET(this->fd(), &readset) && !error()) {
receive();
while (has_messages()) {
std::string message;
retreive(message);
parse_incoming_message(message);
}
nb = select(fd() + 1, &readset, NULL, NULL, &timeout);
if (nb == 0) {
return;
}
if (nb == -1) {
con_error << "Network error on select()" << std::endl;
//perror("select");
abort();
}
}
}
// transmit all queued messages
void NetConnection::transmit()
{
if (error() || invalid())
return;
if (!sendq.size()) {
if (connection_keepalive + NETTIMEOUT / 2 < application()->time()) {
sendq.assign("ping\n");
} else {
return;
}
} else if (sendq.size() >= BLOCKSIZE - 16) {
con_warn << "Outgoing data exceeds " << BLOCKSIZE - 16 << " bytes!\n";
sendq.clear();
return;
}
ssize_t bytes_sent = 0;
while (sendq.size()) {
bytes_sent = ::sendto(connection_fd, sendq.c_str(), sendq.size(), 0, (struct sockaddr *) & server_addr, sizeof(server_addr));
if (bytes_sent <= 0) {
con_error << "Network send() error!" << std::endl;
//perror("send");
abort();
return;
}
// assert (bytes_sent <= sendbuf.size());
sendq.erase(0, bytes_sent);
Stats::network_bytes_sent += bytes_sent;
}
connection_keepalive = application()->time();
}
// queue a mmessage to the server
void NetConnection::send_raw(std::string const &msg)
{
sendq.append(msg);
}
// functions for outgoing messages
/**
* the following outgoing messages can be send
*
* connect <protocol version>
* pif <player data>
* cup <id> <entity data>
* cmd <text>
* say <text>
* priv <text>
*/
// send a "connect" message to the server
void NetConnection::send_connect()
{
std::ostringstream msg;
msg << "connect " << PROTOCOLVERSION << " " << localplayer()->guid().str() << "\n";
this->send_raw(msg.str());
}
// send a "pif" player info message to the server
void NetConnection::send_playerinfo()
{
localplayer()->update_info();
std::ostringstream msg;
msg << "pif ";
localplayer()->serialize_client_update(msg);
msg << '\n';
this->send_raw(msg.str());
localplayer()->set_dirty(false);
}
// send a "cup" client update message to the server
void NetConnection::send_client_update(Entity *entity)
{
// cup <id> <entity data>
std::ostringstream msg;
msg << "cup " << entity->id() << " ";
entity->serialize_client_update(msg);
msg << '\n';
this->send_raw(msg.str());
}
// send a "req" entity request
void NetConnection::send_entity_request(Entity *entity)
{
// req <id>
std::ostringstream msg;
msg << "req " << entity->id() << '\n';
this->send_raw(msg.str());
}
// send a "cmd" command line message to the server
void NetConnection::send_command(std::string const &cmdline)
{
std::string msg("cmd ");
msg.append(cmdline);
msg += '\n';
this->send_raw(msg);
}
// send a "rcon" command line message to the server
void NetConnection::send_rcon(std::string const &cmdline)
{
std::string msg("rcon ");
msg.append(cmdline);
msg += '\n';
this->send_raw(msg);
}
// send a "shout" chat message message to the server
void NetConnection::send_shout(std::string const &text)
{
std::string msg("msg public ");
msg.append(text);
msg += '\n';
this->send_raw(msg);
}
// send a "say" chat message message to the server
void NetConnection::send_say(std::string const &text)
{
std::string msg("msg local ");
msg.append(text);
msg += '\n';
this->send_raw(msg);
}
// send a "priv" private message to the server
void NetConnection::send_private_message(std::string const &text)
{
std::string msg("msg private ");
msg.append(text);
msg += '\n';
this->send_raw(msg);
}
// send a ping reply
// ping timestamp
void NetConnection::send_ping_reply(unsigned long timestamp)
{
std::ostringstream msg;
msg << "ping " << timestamp << '\n';
this->send_raw(msg.str());
}
// send an info record request
// inf type.label info.label
void NetConnection::send_info_request(Info *info)
{
std::ostringstream msg;
if (!info->id()) {
return;
}
msg << "inf " << info->id() << "\n";
this->send_raw(msg.str());
info->set_timestamp(application()->timestamp());
}
// send an inventory update request
void NetConnection::send_inventory_request(Entity *entity)
{
if (!entity || !entity->inventory())
return;
std::ostringstream msg;
msg << "inv " << entity->id() << " " << entity->inventory()->timestamp() << "\n";
this->send_raw(msg.str());
}
// parse incoming client messages
/**
* The following incoming messages are parsed;
*
* connect
* disconnect
* msg info <text>
* msg public <name> <text>
* msg private <name> <text>
* msg rcon <text>
* msg snd <soundname>
* die <id>
* ent <id> <typeid>
* frame
* sup <id> <typeid>
* pif <id>
* pid <id>
* inf <id>
* inv <id> <timestamp>
* zone
*/
void NetConnection::parse_incoming_message(const std::string & message)
{
std::istringstream msgstream(message);
std::string command;
msgstream >> command;
if (command.compare("sup") == 0) {
// entity server update: sup <id> <typeid>
if (connection_state == Connected) {
unsigned int type;
unsigned int id;
if ((msgstream >> id) && (msgstream >> type)) {
if (!id) {
return;
}
Entity *entity = Entity::find(id);
if (entity) {
// validate entity type
if (entity->type() != type) {
// type mismatch, delete the entity
if (localcontrol() == entity)
localplayer()->set_control(0);
Entity::erase(id);
entity = 0;
}
}
if (!entity) {
switch (type) {
case Entity::Default:
entity = new Entity(msgstream);
break;
case Entity::Dynamic:
entity = new EntityDynamic(msgstream);
break;
case Entity::Controlable:
entity = new EntityControlable(msgstream);
break;
case Entity::Globe:
entity = new EntityGlobe(msgstream);
break;
default:
con_warn << "Received update for unknown entity type " << type << "!" << std::endl;
return;
break;
}
Entity::add(entity, id);
send_entity_request(entity);
}
// receive update
entity->receive_server_update(msgstream);
}
}
} else if (command.compare("ent") == 0) {
// entity create message
unsigned int type = 0;
unsigned int id = 0;
if ((msgstream >> id) && (msgstream >> type)) {
if (!id) {
con_warn << "Received create for NULL entity!" << std::endl;
return;
}
//con_debug << "Received create entity id " << id << " type " << type << std::endl;
Entity *entity = Entity::find(id);
if (entity) {
// validate entity type
if (entity->type() != type) {
// type mismatch, delete the entity
if (localcontrol() == entity)
localplayer()->set_control(0);
Entity::erase(id);
entity = 0;
}
}
if (!entity) {
switch (type) {
case Entity::Default:
entity = new Entity(msgstream);
break;
case Entity::Dynamic:
entity = new EntityDynamic(msgstream);
break;
case Entity::Controlable:
entity = new EntityControlable(msgstream);
break;
case Entity::Globe:
entity = new EntityGlobe(msgstream);
break;
default:
con_warn << "Received create for unknown entity type " << type << "!" << std::endl;
return;
break;
}
Entity::add(entity, id);
}
entity->receive_server_create(msgstream);
}
} else if (command.compare("die") == 0) {
unsigned int id;
if (msgstream >> id) {
//con_debug << "Received die entity id " << id << std::endl;
Entity *e = Entity::find(id);
if (localcontrol() == e)
localplayer()->set_control(0);
if (e)
Entity::erase(id);
}
} else if (command.compare("msg") == 0) {
// text message: msg <level>
std::string level;
if (msgstream >> level) {
if (level == "info") {
if (message.size() > 9) {
application()->notify_message(Message::Info, message.substr(9));
}
} else if (level == "rcon") {
if (message.size() > 9) {
application()->notify_message(Message::RCon, message.substr(9));
}
} else if (level == "local") {
// FIXME - separate zone and sender nickname
if (message.size() > 10) {
application()->notify_message(Message::Local, message.substr(10));
}
} else if (level == "public") {
// FIXME - separate sender nickname
if (message.size() > 11) {
application()->notify_message(Message::Public, message.substr(11));
}
} else if (level == "private") {
// FIXME - separate sender nickname
if (message.size() > 12) {
application()->notify_message(Message::Private, message.substr(12));
}
} else if (level == "snd") {
if (message.size() > 8) {
application()->notify_message(Message::Sound, message.substr(8));
}
}
}
} else if (command.compare("connect") == 0) {
if (connection_state == Pending) {
send_playerinfo();
connection_state = Connected;
con_print << "^BConnected." << std::endl;
}
return;
} else if (command.compare("disconnect") == 0) {
con_error << "Server disconnected!" << std::endl;
abort();
} else if (command.compare("ping") == 0) {
unsigned long timestamp;
if ((msgstream >> timestamp)) {
send_ping_reply(timestamp);
}
} else if (command.compare("frame") == 0) {
unsigned long timestamp;
if ((msgstream >> timestamp)) {
send_ping_reply(timestamp);
connection_timestamp = timestamp;
}
} else if (command.compare("menu") == 0) {
unsigned int id = 0;
if (msgstream >> id) {
if (!id) {
con_warn << "Received menu for NULL entity!" << std::endl;
return;
}
Entity *entity = Entity::find(id);
if (!entity) {
con_warn << "Received menu for unknown entity " << id << "!" << std::endl;
return;
}
MenuDescription *menu = Descriptions::receive(msgstream);
if (!menu->label().size()) {
con_warn << "Received menu without label for entity " << id << "!" << std::endl;
delete menu;
return;
}
// remove the menu if it already exists
entity->remove_menu(menu->label());
//con_debug << "receiving menu " << entity->label() << " " << menu->label() << std::endl;
entity->add_menu(menu);
} else {
con_warn << "Received illegal menu message!" << std::endl;
}
} else if (command.compare("zone") == 0) {
unsigned int id;
std::string label;
if (msgstream >> id) {
if (id) {
//con_debug << "Received zone " << id << std::endl;
Zone * zone = Zone::find(id);
// create the zone if necessary
if (!zone) {
zone = new Zone(msgstream);
Zone::add(zone, id);
} else {
zone->receive_server_update(msgstream);
}
}
// control is set to 0 on zone change and is put back by pif
connection()->localplayer()->set_control(0);
}
} else if (command.compare("pif") == 0) {
//con_debug << "Received update player info" << std::endl;
int player_id;
if (!(msgstream >> player_id)) {
con_warn << "Received illegal player info message!" << std::endl;
return;
}
// normal "pif" message about localplayer
if (!player_id) {
Zone *oldzone = connection()->localplayer()->zone();
connection()->localplayer()->receive_server_update(msgstream);
//con_debug << "zone " << ( connection()->localplayer()->zone() ? connection()->localplayer()->zone()->id() : 0) << std::endl;
if (connection()->localplayer()->zonechange() && oldzone && (oldzone != connection()->localplayer()->zone())) {
// notify the applciation to clear none-core zone assets (textures etc)
application()->notify_zonechange();
// delete all entities in the old zone
for (Entity::Registry::iterator it = Entity::registry().begin(); it != Entity::registry().end();) {
Entity *entity = (*it).second;
if ((entity->zone() == oldzone)) {
delete entity;
Entity::registry().erase(it++);
} else {
++it;
}
}
oldzone->content().clear();
}
// short "pif" message about a different player
} else if (player_id != localplayer()->id()) {
// find player
Player *player = 0;
// search other players
for (GameInterface::Players::iterator it = game()->players().begin(); it != game()->players().end() && !player; it++) {
if ((*it)->id() == player_id) {
player = (*it);
}
}
if (!player) {
player = new Player(0);
game()->players().push_back(player);
game()->set_playerlist_timestamp(game()->timestamp());
}
player->receive_short_server_update(msgstream);
player->set_dirty(false);
}
} else if (command.compare("pid") == 0) {
//con_debug << "Received player disconnect info" << std::endl;
int player_id;
if (!(msgstream >> player_id)) {
con_warn << "Received illegal player disconnect message!" << std::endl;
return;
}
// find player
if (player_id == connection()->localplayer()->id()) {
// ignore disconnect messages for local client
return;
}
// search other players
Player *player = 0;
for (GameInterface::Players::iterator it = game()->players().begin(); it != game()->players().end() && !player; it++) {
if ((*it)->id() == player_id) {
game()->players().erase(it);
game()->set_playerlist_timestamp(game()->timestamp());
return;
}
}
} else if (command.compare("inf") == 0) {
// incoming info record
unsigned int id = 0;
std::string typelabelstr;
std::string infolabelstr;
std::string n;
char c;
// read id
if (!(msgstream >> id)) {
con_warn << "Received invalid info record message!" << std::endl;
return;
}
if (id == 0) {
// special case: info type constructor
size_t s;
if (!(msgstream >> s)) {
con_warn << "Received invalid info type message!" << std::endl;
return;
}
for (size_t i = 0; i < s; i++) {
// read infotype label
n.clear();
while ((msgstream.get(c)) && (c != '"'));
while ((msgstream.get(c)) && (c != '"'))
n +=c;
if (n.size() && !InfoType::find(n)) {
// add new infotype
new InfoType(n.c_str());
}
}
return;
}
// read type label
n.clear();
while ((msgstream.get(c)) && (c != '"'));
while ((msgstream.get(c)) && (c != '"'))
n +=c;
typelabelstr.assign(n);
if (!typelabelstr.size()) {
con_warn << "Received invalid info record message!" << std::endl;
return;
}
// read info label
n.clear();
while ((msgstream.get(c)) && (c != '"'));
while ((msgstream.get(c)) && (c != '"'))
n +=c;
infolabelstr.assign(n);
if (!infolabelstr.size()) {
con_warn << "Received invalid info record message for type '" << typelabelstr << "'!" << std::endl;
return;
}
// find the InfoType instance
InfoType *infotype = InfoType::find(typelabelstr);
if (!infotype) {
infotype = new InfoType(typelabelstr.c_str());
}
// find the Info instance
Info *info = Info::find(id);
if (!info) {
info = Info::find(infotype, infolabelstr);
}
// create one if necessary
if (!info) {
info = new Info(id);
}
info->set_type(infotype);
info->set_label(infolabelstr);
info->set_id(id);
info->receive_server_update(msgstream);
//info->clear_timestamp();
info->set_timestamp(application()->timestamp());
//con_debug << "CLIENT info for " << info->id() << " " << info->type()->label() << ":" << info->label() << " timestamp " << info->timestamp() << std::endl;
} else if (command.compare("inv") == 0) {
// received inventory update
unsigned int entity_id = 0;
unsigned long server_timestamp;
// read entity id and server timestamp
if (!(msgstream >> entity_id >> server_timestamp)) {
con_warn << "Received invalid inventory update message!" << std::endl;
return;
}
Entity *entity = Entity::find(entity_id);
if (!entity) {
con_warn << "Received inventory update for non-existing entity " << entity_id << "!" << std::endl;
return;
}
if (!entity->inventory()) {
con_warn << "Received inventory update for entity " << entity_id << " without inventory!" << std::endl;
return;
}
if (server_timestamp < entity->inventory()->timestamp())
return;
unsigned int item_id = 0;
unsigned int info_id = 0;
int full_update = 0;
if (!(msgstream >> full_update)) {
con_warn << "Received inventory update for entity " << entity_id << " without full update marker!" << std::endl;
return;
}
if (full_update == 1) {
// mark all items dirty
for (Inventory::Items::iterator it = entity->inventory()->items().begin(); it != entity->inventory()->items().end(); ++it) {
(*it)->set_dirty();
}
}
size_t nbitems = 0;
if (!(msgstream >> nbitems))
nbitems = 0;
//con_debug << "CLIENT number of items: " << nbitems << std::endl;
// receive item updates
for (size_t i = 0; i < nbitems; i++) {
// read item id
if (!(msgstream >> item_id)) {
con_warn << "Received inventory item update for entity " << entity_id << " without item id!" << std::endl;
return;
}
// read info id
if (!(msgstream >> info_id)) {
con_warn << "Received inventory item update for entity " << entity_id << " without info id!" << std::endl;
return;
}
Info *info = core::game()->request_info(info_id);
Item *item = entity->inventory()->find(item_id);
if (!item) {
item = new Item(info);
entity->inventory()->add(item);
item->set_id(item_id);
} else {
if (item->info() != info) {
item->set_info(info);
}
}
item->receive_server_update(msgstream);
item->set_dirty(false);
}
if (full_update == 1) {
// remove items for which no update was received
for (Inventory::Items::iterator it = entity->inventory()->items().begin(); it != entity->inventory()->items().end();) {
Item *item = (*it);
if (item->dirty()) {
delete (item);
(*it) = 0;
entity->inventory()->items().erase(it++);
} else {
++it;
}
}
}
entity->inventory()->recalculate();
entity->inventory()->set_timestamp(server_timestamp);
//con_debug << "CLIENT inventory updated timestamp " << entity->inventory()->timestamp() << " for " << nbitems << " items" << std::endl;
}
}
}
|