Skip to content

Commit d63df4e

Browse files
committedMar 16, 2015
Move client packet generators to dedicated functions for:
* TOSERVER_INIT_LEGACY * TOSERVER_DELETEDBLOCKS * TOSERVER_GOTBLOCKS * TOSERVER_REMOVED_SOUNDS Also use a std::vector instead of std::set for TOSERVER_REMOVED_SOUNDS
1 parent e7736ff commit d63df4e

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed
 

Diff for: ‎src/client.cpp

+59-38
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,7 @@ void Client::step(float dtime)
407407
snprintf(pName, PLAYERNAME_SIZE, "%s", myplayer->getName());
408408
snprintf(pPassword, PASSWORD_SIZE, "%s", m_password.c_str());
409409

410-
NetworkPacket pkt(TOSERVER_INIT_LEGACY,
411-
1 + PLAYERNAME_SIZE + PASSWORD_SIZE + 2 + 2);
412-
413-
pkt << (u8) SER_FMT_VER_HIGHEST_READ;
414-
pkt.putRawString(pName,PLAYERNAME_SIZE);
415-
pkt.putRawString(pPassword, PASSWORD_SIZE);
416-
pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX;
417-
418-
Send(&pkt);
410+
sendLegacyInit(pName, pPassword);
419411
}
420412

421413
// Not connected, return
@@ -455,19 +447,8 @@ void Client::step(float dtime)
455447
[3+6] v3s16 pos_1
456448
...
457449
*/
458-
NetworkPacket pkt(TOSERVER_DELETEDBLOCKS, 1 + sizeof(v3s16) * sendlist.size());
459-
460-
pkt << (u8) sendlist.size();
461-
462-
u32 k = 0;
463-
for(std::vector<v3s16>::iterator
464-
j = sendlist.begin();
465-
j != sendlist.end(); ++j) {
466-
pkt << *j;
467-
k++;
468-
}
469450

470-
Send(&pkt);
451+
sendDeletedBlocks(sendlist);
471452

472453
if(i == deleted_blocks.end())
473454
break;
@@ -575,9 +556,7 @@ void Client::step(float dtime)
575556
[0] u8 count
576557
[1] v3s16 pos_0
577558
*/
578-
NetworkPacket pkt(TOSERVER_GOTBLOCKS, 1 + 6);
579-
pkt << (u8) 1 << r.p;
580-
Send(&pkt);
559+
sendGotBlocks(r.p);
581560
}
582561
}
583562

@@ -646,7 +625,7 @@ void Client::step(float dtime)
646625
if(m_removed_sounds_check_timer >= 2.32) {
647626
m_removed_sounds_check_timer = 0;
648627
// Find removed sounds and clear references to them
649-
std::set<s32> removed_server_ids;
628+
std::vector<s32> removed_server_ids;
650629
for(std::map<s32, int>::iterator
651630
i = m_sounds_server_to_client.begin();
652631
i != m_sounds_server_to_client.end();) {
@@ -657,24 +636,13 @@ void Client::step(float dtime)
657636
m_sounds_server_to_client.erase(server_id);
658637
m_sounds_client_to_server.erase(client_id);
659638
m_sounds_to_objects.erase(client_id);
660-
removed_server_ids.insert(server_id);
639+
removed_server_ids.push_back(server_id);
661640
}
662641
}
663642

664643
// Sync to server
665644
if(!removed_server_ids.empty()) {
666-
size_t server_ids = removed_server_ids.size();
667-
assert(server_ids <= 0xFFFF);
668-
669-
NetworkPacket pkt(TOSERVER_REMOVED_SOUNDS, 2 + server_ids * 4);
670-
671-
pkt << (u16) (server_ids & 0xFFFF);
672-
673-
for(std::set<s32>::iterator i = removed_server_ids.begin();
674-
i != removed_server_ids.end(); i++)
675-
pkt << *i;
676-
677-
Send(&pkt);
645+
sendRemovedSounds(removed_server_ids);
678646
}
679647
}
680648

@@ -983,6 +951,59 @@ void Client::interact(u8 action, const PointedThing& pointed)
983951
Send(&pkt);
984952
}
985953

954+
void Client::sendLegacyInit(const char* playerName, const char* playerPassword)
955+
{
956+
NetworkPacket pkt(TOSERVER_INIT_LEGACY,
957+
1 + PLAYERNAME_SIZE + PASSWORD_SIZE + 2 + 2);
958+
959+
pkt << (u8) SER_FMT_VER_HIGHEST_READ;
960+
pkt.putRawString(playerName,PLAYERNAME_SIZE);
961+
pkt.putRawString(playerPassword, PASSWORD_SIZE);
962+
pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX;
963+
964+
Send(&pkt);
965+
}
966+
967+
void Client::sendDeletedBlocks(std::vector<v3s16> &blocks)
968+
{
969+
NetworkPacket pkt(TOSERVER_DELETEDBLOCKS, 1 + sizeof(v3s16) * blocks.size());
970+
971+
pkt << (u8) blocks.size();
972+
973+
u32 k = 0;
974+
for(std::vector<v3s16>::iterator
975+
j = blocks.begin();
976+
j != blocks.end(); ++j) {
977+
pkt << *j;
978+
k++;
979+
}
980+
981+
Send(&pkt);
982+
}
983+
984+
void Client::sendGotBlocks(v3s16 block)
985+
{
986+
NetworkPacket pkt(TOSERVER_GOTBLOCKS, 1 + 6);
987+
pkt << (u8) 1 << block;
988+
Send(&pkt);
989+
}
990+
991+
void Client::sendRemovedSounds(std::vector<s32> &soundList)
992+
{
993+
size_t server_ids = soundList.size();
994+
assert(server_ids <= 0xFFFF);
995+
996+
NetworkPacket pkt(TOSERVER_REMOVED_SOUNDS, 2 + server_ids * 4);
997+
998+
pkt << (u16) (server_ids & 0xFFFF);
999+
1000+
for(std::vector<s32>::iterator i = soundList.begin();
1001+
i != soundList.end(); i++)
1002+
pkt << *i;
1003+
1004+
Send(&pkt);
1005+
}
1006+
9861007
void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
9871008
const std::map<std::string, std::string> &fields)
9881009
{

Diff for: ‎src/client.h

+5
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
542542
// Send the item number 'item' as player item to the server
543543
void sendPlayerItem(u16 item);
544544

545+
void sendLegacyInit(const char* playerName, const char* playerPassword);
546+
void sendDeletedBlocks(std::vector<v3s16> &blocks);
547+
void sendGotBlocks(v3s16 block);
548+
void sendRemovedSounds(std::vector<s32> &soundList);
549+
545550
float m_packetcounter_timer;
546551
float m_connection_reinit_timer;
547552
float m_avg_rtt_timer;

0 commit comments

Comments
 (0)