From 083c8c734e8af037d8ad3a1a35475a5d4756572c Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Fri, 13 Mar 2015 22:01:49 +0100 Subject: [PATCH] Replace NetworkPacket pointers to references --- src/client.cpp | 153 ++++++------ src/clientiface.cpp | 7 +- src/clientiface.h | 2 +- src/network/connection.cpp | 5 +- src/network/packethandlers/client.cpp | 8 +- src/network/packethandlers/server.cpp | 6 +- src/server.cpp | 346 ++++++++++++-------------- src/test.cpp | 26 +- 8 files changed, 257 insertions(+), 296 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 8e0f657e2..dce89eb1c 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -407,15 +407,15 @@ void Client::step(float dtime) snprintf(pName, PLAYERNAME_SIZE, "%s", myplayer->getName()); snprintf(pPassword, PASSWORD_SIZE, "%s", m_password.c_str()); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_INIT_LEGACY, + NetworkPacket pkt(TOSERVER_INIT_LEGACY, 1 + PLAYERNAME_SIZE + PASSWORD_SIZE + 2 + 2); - *pkt << (u8) SER_FMT_VER_HIGHEST_READ; - pkt->putRawString(pName,PLAYERNAME_SIZE); - pkt->putRawString(pPassword, PASSWORD_SIZE); - *pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX; + pkt << (u8) SER_FMT_VER_HIGHEST_READ; + pkt.putRawString(pName,PLAYERNAME_SIZE); + pkt.putRawString(pPassword, PASSWORD_SIZE); + pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX; - Send(pkt); + Send(&pkt); } // Not connected, return @@ -455,19 +455,19 @@ void Client::step(float dtime) [3+6] v3s16 pos_1 ... */ - NetworkPacket* pkt = new NetworkPacket(TOSERVER_DELETEDBLOCKS, 1 + sizeof(v3s16) * sendlist.size()); + NetworkPacket pkt(TOSERVER_DELETEDBLOCKS, 1 + sizeof(v3s16) * sendlist.size()); - *pkt << (u8) sendlist.size(); + pkt << (u8) sendlist.size(); u32 k = 0; for(std::vector::iterator j = sendlist.begin(); j != sendlist.end(); ++j) { - *pkt << *j; + pkt << *j; k++; } - Send(pkt); + Send(&pkt); if(i == deleted_blocks.end()) break; @@ -575,9 +575,9 @@ void Client::step(float dtime) [0] u8 count [1] v3s16 pos_0 */ - NetworkPacket* pkt = new NetworkPacket(TOSERVER_GOTBLOCKS, 1 + 6); - *pkt << (u8) 1 << r.p; - Send(pkt); + NetworkPacket pkt(TOSERVER_GOTBLOCKS, 1 + 6); + pkt << (u8) 1 << r.p; + Send(&pkt); } } @@ -666,15 +666,15 @@ void Client::step(float dtime) size_t server_ids = removed_server_ids.size(); assert(server_ids <= 0xFFFF); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_REMOVED_SOUNDS, 2 + server_ids * 4); + NetworkPacket pkt(TOSERVER_REMOVED_SOUNDS, 2 + server_ids * 4); - *pkt << (u16) (server_ids & 0xFFFF); + pkt << (u16) (server_ids & 0xFFFF); for(std::set::iterator i = removed_server_ids.begin(); i != removed_server_ids.end(); i++) - *pkt << *i; + pkt << *i; - Send(pkt); + Send(&pkt); } } @@ -794,27 +794,27 @@ void Client::request_media(const std::vector &file_requests) FATAL_ERROR_IF(file_requests_size > 0xFFFF, "Unsupported number of file requests"); // Packet dynamicly resized - NetworkPacket* pkt = new NetworkPacket(TOSERVER_REQUEST_MEDIA, 2 + 0); + NetworkPacket pkt(TOSERVER_REQUEST_MEDIA, 2 + 0); - *pkt << (u16) (file_requests_size & 0xFFFF); + pkt << (u16) (file_requests_size & 0xFFFF); for(std::vector::const_iterator i = file_requests.begin(); i != file_requests.end(); ++i) { - *pkt << (*i); + pkt << (*i); } - Send(pkt); + Send(&pkt); - infostream<<"Client: Sending media request list to server (" - <getCommand(); + ToClientCommand command = (ToClientCommand) pkt.getCommand(); //infostream<<"Client: received command="<::const_iterator i = fields.begin(); i != fields.end(); i++) { const std::string &name = i->first; const std::string &value = i->second; - *pkt << name; - pkt->putLongString(value); + pkt << name; + pkt.putLongString(value); } - Send(pkt); + Send(&pkt); } void Client::sendInventoryFields(const std::string &formname, @@ -1016,18 +1011,18 @@ void Client::sendInventoryFields(const std::string &formname, size_t fields_size = fields.size(); FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of inventory fields"); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_INVENTORY_FIELDS, 0); - *pkt << formname << (u16) (fields_size & 0xFFFF); + NetworkPacket pkt(TOSERVER_INVENTORY_FIELDS, 0); + pkt << formname << (u16) (fields_size & 0xFFFF); for(std::map::const_iterator i = fields.begin(); i != fields.end(); i++) { const std::string &name = i->first; const std::string &value = i->second; - *pkt << name; - pkt->putLongString(value); + pkt << name; + pkt.putLongString(value); } - Send(pkt); + Send(&pkt); } void Client::sendInventoryAction(InventoryAction *a) @@ -1039,19 +1034,19 @@ void Client::sendInventoryAction(InventoryAction *a) // Make data buffer std::string s = os.str(); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_INVENTORY_ACTION, s.size()); - pkt->putRawString(s.c_str(),s.size()); + NetworkPacket pkt(TOSERVER_INVENTORY_ACTION, s.size()); + pkt.putRawString(s.c_str(),s.size()); - Send(pkt); + Send(&pkt); } void Client::sendChatMessage(const std::wstring &message) { - NetworkPacket* pkt = new NetworkPacket(TOSERVER_CHAT_MESSAGE, 2 + message.size() * sizeof(u16)); + NetworkPacket pkt(TOSERVER_CHAT_MESSAGE, 2 + message.size() * sizeof(u16)); - *pkt << message; + pkt << message; - Send(pkt); + Send(&pkt); } void Client::sendChangePassword(const std::wstring &oldpassword, @@ -1065,17 +1060,17 @@ void Client::sendChangePassword(const std::wstring &oldpassword, std::string oldpwd = translatePassword(playername, oldpassword); std::string newpwd = translatePassword(playername, newpassword); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_PASSWORD_LEGACY, 2 * PASSWORD_SIZE); + NetworkPacket pkt(TOSERVER_PASSWORD_LEGACY, 2 * PASSWORD_SIZE); for(u8 i = 0; i < PASSWORD_SIZE; i++) { - *pkt << (u8) (i < oldpwd.length() ? oldpwd[i] : 0); + pkt << (u8) (i < oldpwd.length() ? oldpwd[i] : 0); } for(u8 i = 0; i < PASSWORD_SIZE; i++) { - *pkt << (u8) (i < newpwd.length() ? newpwd[i] : 0); + pkt << (u8) (i < newpwd.length() ? newpwd[i] : 0); } - Send(pkt); + Send(&pkt); } @@ -1083,40 +1078,40 @@ void Client::sendDamage(u8 damage) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_DAMAGE, sizeof(u8)); - *pkt << damage; - Send(pkt); + NetworkPacket pkt(TOSERVER_DAMAGE, sizeof(u8)); + pkt << damage; + Send(&pkt); } void Client::sendBreath(u16 breath) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_BREATH, sizeof(u16)); - *pkt << breath; - Send(pkt); + NetworkPacket pkt(TOSERVER_BREATH, sizeof(u16)); + pkt << breath; + Send(&pkt); } void Client::sendRespawn() { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_RESPAWN, 0); - Send(pkt); + NetworkPacket pkt(TOSERVER_RESPAWN, 0); + Send(&pkt); } void Client::sendReady() { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_CLIENT_READY, + NetworkPacket pkt(TOSERVER_CLIENT_READY, 1 + 1 + 1 + 1 + 2 + sizeof(char) * strlen(minetest_version_hash)); - *pkt << (u8) VERSION_MAJOR << (u8) VERSION_MINOR << (u8) VERSION_PATCH_ORIG + pkt << (u8) VERSION_MAJOR << (u8) VERSION_MINOR << (u8) VERSION_PATCH_ORIG << (u8) 0 << (u16) strlen(minetest_version_hash); - pkt->putRawString(minetest_version_hash, (u16) strlen(minetest_version_hash)); - Send(pkt); + pkt.putRawString(minetest_version_hash, (u16) strlen(minetest_version_hash)); + Send(&pkt); } void Client::sendPlayerPos() @@ -1168,11 +1163,11 @@ void Client::sendPlayerPos() [12+12+4+4] u32 keyPressed */ - NetworkPacket* pkt = new NetworkPacket(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4); + NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4); - *pkt << position << speed << pitch << yaw << keyPressed; + pkt << position << speed << pitch << yaw << keyPressed; - Send(pkt); + Send(&pkt); } void Client::sendPlayerItem(u16 item) @@ -1188,11 +1183,11 @@ void Client::sendPlayerItem(u16 item) myplayer->peer_id = our_peer_id; assert(myplayer->peer_id == our_peer_id); - NetworkPacket* pkt = new NetworkPacket(TOSERVER_PLAYERITEM, 2); + NetworkPacket pkt(TOSERVER_PLAYERITEM, 2); - *pkt << item; + pkt << item; - Send(pkt); + Send(&pkt); } void Client::removeNode(v3s16 p) diff --git a/src/clientiface.cpp b/src/clientiface.cpp index 126979897..3171e7795 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -626,12 +626,9 @@ void ClientInterface::UpdatePlayerList() } void ClientInterface::send(u16 peer_id, u8 channelnum, - NetworkPacket* pkt, bool reliable, bool deletepkt) + NetworkPacket* pkt, bool reliable) { m_con->Send(peer_id, channelnum, pkt, reliable); - - if (deletepkt) - delete pkt; } void ClientInterface::sendToAll(u16 channelnum, @@ -647,8 +644,6 @@ void ClientInterface::sendToAll(u16 channelnum, m_con->Send(client->peer_id, channelnum, pkt, reliable); } } - - delete pkt; } RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min) diff --git a/src/clientiface.h b/src/clientiface.h index 2fd293de2..54b250265 100644 --- a/src/clientiface.h +++ b/src/clientiface.h @@ -400,7 +400,7 @@ public: std::vector getPlayerNames(); /* send message to client */ - void send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable, bool deletepkt=true); + void send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable); /* send to all clients */ void sendToAll(u16 channelnum, NetworkPacket* pkt, bool reliable); diff --git a/src/network/connection.cpp b/src/network/connection.cpp index fb31a07f0..69deb4dd5 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -1725,9 +1725,8 @@ void ConnectionSendThread::connect(Address address) // Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT m_connection->SetPeerID(PEER_ID_INEXISTENT); - NetworkPacket* pkt = new NetworkPacket(0,0); - m_connection->Send(PEER_ID_SERVER, 0, pkt, true); - delete pkt; + NetworkPacket pkt(0,0); + m_connection->Send(PEER_ID_SERVER, 0, &pkt, true); } void ConnectionSendThread::disconnect() diff --git a/src/network/packethandlers/client.cpp b/src/network/packethandlers/client.cpp index 838c85989..5acf04f91 100644 --- a/src/network/packethandlers/client.cpp +++ b/src/network/packethandlers/client.cpp @@ -77,8 +77,8 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt) << m_recommended_send_interval<getPeerId()); - *resp_pkt << (u8) deployed << (v3s16) floatToInt(v3f(0,0,0), BS) + resp_pkt << (u8) deployed << (v3s16) floatToInt(v3f(0,0,0), BS) << (u64) m_env->getServerMap().getSeed() << g_settings->getFloat("dedicated_server_step"); - Send(resp_pkt); + Send(&resp_pkt); m_clients.event(pkt->getPeerId(), CSE_Init); } diff --git a/src/server.cpp b/src/server.cpp index 7e0ee7d03..8f2ec05ba 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -758,16 +758,16 @@ void Server::AsyncRunStep(bool initial_step) obj->m_known_by_count++; } - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, 0, client->peer_id); - pkt->putRawString(data_buffer.c_str(), data_buffer.size()); + NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, 0, client->peer_id); + pkt.putRawString(data_buffer.c_str(), data_buffer.size()); verbosestream << "Server: Sent object remove/add: " << removed_objects.size() << " removed, " << added_objects.size() << " added, " - << "packet size is " << pkt->getSize() << std::endl; + << "packet size is " << pkt.getSize() << std::endl; - Send(pkt); + Send(&pkt); } m_clients.Unlock(); } @@ -846,19 +846,19 @@ void Server::AsyncRunStep(bool initial_step) Send them. */ if(reliable_data.size() > 0) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_MESSAGES, + NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_MESSAGES, 0, client->peer_id); - pkt->putRawString(reliable_data.c_str(), reliable_data.size()); - Send(pkt); + pkt.putRawString(reliable_data.c_str(), reliable_data.size()); + Send(&pkt); } if(unreliable_data.size() > 0) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_MESSAGES, + NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_MESSAGES, 0, client->peer_id); - pkt->putRawString(unreliable_data.c_str(), unreliable_data.size()); - Send(pkt); + pkt.putRawString(unreliable_data.c_str(), unreliable_data.size()); + Send(&pkt); } } m_clients.Unlock(); @@ -1194,9 +1194,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) if(datasize < 2) return; - NetworkPacket* pkt = new NetworkPacket(data, datasize, peer_id); + NetworkPacket pkt(data, datasize, peer_id); - ToServerCommand command = (ToServerCommand) pkt->getCommand(); + ToServerCommand command = (ToServerCommand) pkt.getCommand(); // Command must be handled into ToServerCommandHandler if (command >= TOSERVER_NUM_MSG_TYPES) { @@ -1205,8 +1205,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) } if (toServerCommandTable[command].state == TOSERVER_STATE_NOT_CONNECTED) { - handleCommand(pkt); - delete pkt; + handleCommand(&pkt); return; } @@ -1216,15 +1215,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) errorstream << "Server::ProcessData(): Cancelling: Peer" " serialization format invalid or not initialized." " Skipping incoming command=" << command << std::endl; - - delete pkt; return; } /* Handle commands related to client startup */ if (toServerCommandTable[command].state == TOSERVER_STATE_STARTUP) { - handleCommand(pkt); - delete pkt; + handleCommand(&pkt); return; } @@ -1234,14 +1230,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) errorstream << "Got packet command: " << command << " for peer id " << peer_id << " but client isn't active yet. Dropping packet " << std::endl; - - delete pkt; return; } - handleCommand(pkt); - delete pkt; - + handleCommand(&pkt); } catch(SendFailedException &e) { errorstream << "Server::ProcessData(): SendFailedException: " @@ -1466,40 +1458,40 @@ void Server::SendMovement(u16 peer_id) DSTACK(__FUNCTION_NAME); std::ostringstream os(std::ios_base::binary); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MOVEMENT, 12 * sizeof(float), peer_id); + NetworkPacket pkt(TOCLIENT_MOVEMENT, 12 * sizeof(float), peer_id); + + pkt << g_settings->getFloat("movement_acceleration_default"); + pkt << g_settings->getFloat("movement_acceleration_air"); + pkt << g_settings->getFloat("movement_acceleration_fast"); + pkt << g_settings->getFloat("movement_speed_walk"); + pkt << g_settings->getFloat("movement_speed_crouch"); + pkt << g_settings->getFloat("movement_speed_fast"); + pkt << g_settings->getFloat("movement_speed_climb"); + pkt << g_settings->getFloat("movement_speed_jump"); + pkt << g_settings->getFloat("movement_liquid_fluidity"); + pkt << g_settings->getFloat("movement_liquid_fluidity_smooth"); + pkt << g_settings->getFloat("movement_liquid_sink"); + pkt << g_settings->getFloat("movement_gravity"); - *pkt << g_settings->getFloat("movement_acceleration_default"); - *pkt << g_settings->getFloat("movement_acceleration_air"); - *pkt << g_settings->getFloat("movement_acceleration_fast"); - *pkt << g_settings->getFloat("movement_speed_walk"); - *pkt << g_settings->getFloat("movement_speed_crouch"); - *pkt << g_settings->getFloat("movement_speed_fast"); - *pkt << g_settings->getFloat("movement_speed_climb"); - *pkt << g_settings->getFloat("movement_speed_jump"); - *pkt << g_settings->getFloat("movement_liquid_fluidity"); - *pkt << g_settings->getFloat("movement_liquid_fluidity_smooth"); - *pkt << g_settings->getFloat("movement_liquid_sink"); - *pkt << g_settings->getFloat("movement_gravity"); - - Send(pkt); + Send(&pkt); } void Server::SendHP(u16 peer_id, u8 hp) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HP, 1, peer_id); - *pkt << hp; - Send(pkt); + NetworkPacket pkt(TOCLIENT_HP, 1, peer_id); + pkt << hp; + Send(&pkt); } void Server::SendBreath(u16 peer_id, u16 breath) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_BREATH, 2, peer_id); - *pkt << (u16) breath; - Send(pkt); + NetworkPacket pkt(TOCLIENT_BREATH, 2, peer_id); + pkt << (u16) breath; + Send(&pkt); } void Server::SendAccessDenied(u16 peer_id, AccessDeniedCode reason, const std::wstring &custom_reason) @@ -1519,9 +1511,9 @@ void Server::SendAccessDenied_Legacy(u16 peer_id,const std::wstring &reason) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACCESS_DENIED_LEGACY, 0, peer_id); - *pkt << reason; - Send(pkt); + NetworkPacket pkt(TOCLIENT_ACCESS_DENIED_LEGACY, 0, peer_id); + pkt << reason; + Send(&pkt); } void Server::SendDeathscreen(u16 peer_id,bool set_camera_point_target, @@ -1529,9 +1521,9 @@ void Server::SendDeathscreen(u16 peer_id,bool set_camera_point_target, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DEATHSCREEN, 1 + sizeof(v3f), peer_id); - *pkt << set_camera_point_target << camera_point_target; - Send(pkt); + NetworkPacket pkt(TOCLIENT_DEATHSCREEN, 1 + sizeof(v3f), peer_id); + pkt << set_camera_point_target << camera_point_target; + Send(&pkt); } void Server::SendItemDef(u16 peer_id, @@ -1539,7 +1531,7 @@ void Server::SendItemDef(u16 peer_id, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ITEMDEF, 0, peer_id); + NetworkPacket pkt(TOCLIENT_ITEMDEF, 0, peer_id); /* u16 command @@ -1550,13 +1542,13 @@ void Server::SendItemDef(u16 peer_id, itemdef->serialize(tmp_os, protocol_version); std::ostringstream tmp_os2(std::ios::binary); compressZlib(tmp_os.str(), tmp_os2); - pkt->putLongString(tmp_os2.str()); + pkt.putLongString(tmp_os2.str()); // Make data buffer verbosestream << "Server: Sending item definitions to id(" << peer_id - << "): size=" << pkt->getSize() << std::endl; + << "): size=" << pkt.getSize() << std::endl; - Send(pkt); + Send(&pkt); } void Server::SendNodeDef(u16 peer_id, @@ -1564,7 +1556,7 @@ void Server::SendNodeDef(u16 peer_id, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_NODEDEF, 0, peer_id); + NetworkPacket pkt(TOCLIENT_NODEDEF, 0, peer_id); /* u16 command @@ -1576,13 +1568,13 @@ void Server::SendNodeDef(u16 peer_id, std::ostringstream tmp_os2(std::ios::binary); compressZlib(tmp_os.str(), tmp_os2); - pkt->putLongString(tmp_os2.str()); + pkt.putLongString(tmp_os2.str()); // Make data buffer verbosestream << "Server: Sending node definitions to id(" << peer_id - << "): size=" << pkt->getSize() << std::endl; + << "): size=" << pkt.getSize() << std::endl; - Send(pkt); + Send(&pkt); } /* @@ -1599,30 +1591,29 @@ void Server::SendInventory(PlayerSAO* playerSAO) Serialize it */ - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_INVENTORY, 0, - playerSAO->getPeerID()); + NetworkPacket pkt(TOCLIENT_INVENTORY, 0, playerSAO->getPeerID()); std::ostringstream os; playerSAO->getInventory()->serialize(os); std::string s = os.str(); - pkt->putRawString(s.c_str(), s.size()); - Send(pkt); + pkt.putRawString(s.c_str(), s.size()); + Send(&pkt); } void Server::SendChatMessage(u16 peer_id, const std::wstring &message) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_CHAT_MESSAGE, 0, peer_id); - *pkt << message; + NetworkPacket pkt(TOCLIENT_CHAT_MESSAGE, 0, peer_id); + pkt << message; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0,pkt,true); + m_clients.sendToAll(0, &pkt, true); } } @@ -1631,12 +1622,12 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec, { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id); + NetworkPacket pkt(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id); - pkt->putLongString(FORMSPEC_VERSION_STRING + formspec); - *pkt << formname; + pkt.putLongString(FORMSPEC_VERSION_STRING + formspec); + pkt << formname; - Send(pkt); + Send(&pkt); } // Spawns a particle on peer with peer_id @@ -1646,18 +1637,18 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_SPAWN_PARTICLE, 0, peer_id); + NetworkPacket pkt(TOCLIENT_SPAWN_PARTICLE, 0, peer_id); - *pkt << pos << velocity << acceleration << expirationtime + pkt << pos << velocity << acceleration << expirationtime << size << collisiondetection; - pkt->putLongString(texture); - *pkt << vertical; + pkt.putLongString(texture); + pkt << vertical; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0,pkt,true); + m_clients.sendToAll(0, &pkt, true); } } @@ -1668,21 +1659,21 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ADD_PARTICLESPAWNER, 0, peer_id); + NetworkPacket pkt(TOCLIENT_ADD_PARTICLESPAWNER, 0, peer_id); - *pkt << amount << spawntime << minpos << maxpos << minvel << maxvel + pkt << amount << spawntime << minpos << maxpos << minvel << maxvel << minacc << maxacc << minexptime << maxexptime << minsize << maxsize << collisiondetection; - pkt->putLongString(texture); + pkt.putLongString(texture); - *pkt << id << vertical; + pkt << id << vertical; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0, pkt, true); + m_clients.sendToAll(0, &pkt, true); } } @@ -1690,124 +1681,124 @@ void Server::SendDeleteParticleSpawner(u16 peer_id, u32 id) { DSTACK(__FUNCTION_NAME); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DELETE_PARTICLESPAWNER_LEGACY, 2, peer_id); + NetworkPacket pkt(TOCLIENT_DELETE_PARTICLESPAWNER_LEGACY, 2, peer_id); // Ugly error in this packet - *pkt << (u16) id; + pkt << (u16) id; if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0, pkt, true); + m_clients.sendToAll(0, &pkt, true); } } void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUDADD, 0 , peer_id); + NetworkPacket pkt(TOCLIENT_HUDADD, 0 , peer_id); - *pkt << id << (u8) form->type << form->pos << form->name << form->scale + pkt << id << (u8) form->type << form->pos << form->name << form->scale << form->text << form->number << form->item << form->dir << form->align << form->offset << form->world_pos << form->size; - Send(pkt); + Send(&pkt); } void Server::SendHUDRemove(u16 peer_id, u32 id) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUDRM, 4, peer_id); - *pkt << id; - Send(pkt); + NetworkPacket pkt(TOCLIENT_HUDRM, 4, peer_id); + pkt << id; + Send(&pkt); } void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUDCHANGE, 0, peer_id); - *pkt << id << (u8) stat; + NetworkPacket pkt(TOCLIENT_HUDCHANGE, 0, peer_id); + pkt << id << (u8) stat; switch (stat) { case HUD_STAT_POS: case HUD_STAT_SCALE: case HUD_STAT_ALIGN: case HUD_STAT_OFFSET: - *pkt << *(v2f *) value; + pkt << *(v2f *) value; break; case HUD_STAT_NAME: case HUD_STAT_TEXT: - *pkt << *(std::string *) value; + pkt << *(std::string *) value; break; case HUD_STAT_WORLD_POS: - *pkt << *(v3f *) value; + pkt << *(v3f *) value; break; case HUD_STAT_SIZE: - *pkt << *(v2s32 *) value; + pkt << *(v2s32 *) value; break; case HUD_STAT_NUMBER: case HUD_STAT_ITEM: case HUD_STAT_DIR: default: - *pkt << *(u32 *) value; + pkt << *(u32 *) value; break; } - Send(pkt); + Send(&pkt); } void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUD_SET_FLAGS, 4 + 4, peer_id); + NetworkPacket pkt(TOCLIENT_HUD_SET_FLAGS, 4 + 4, peer_id); flags &= ~(HUD_FLAG_HEALTHBAR_VISIBLE | HUD_FLAG_BREATHBAR_VISIBLE); - *pkt << flags << mask; + pkt << flags << mask; - Send(pkt); + Send(&pkt); } void Server::SendHUDSetParam(u16 peer_id, u16 param, const std::string &value) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUD_SET_PARAM, 0, peer_id); - *pkt << param << value; - Send(pkt); + NetworkPacket pkt(TOCLIENT_HUD_SET_PARAM, 0, peer_id); + pkt << param << value; + Send(&pkt); } void Server::SendSetSky(u16 peer_id, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms) { - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_SET_SKY, 0, peer_id); - *pkt << bgcolor << type << (u16) params.size(); + NetworkPacket pkt(TOCLIENT_SET_SKY, 0, peer_id); + pkt << bgcolor << type << (u16) params.size(); for(size_t i=0; igetPlayer(peer_id); assert(player); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MOVE_PLAYER, 0, peer_id); - *pkt << player->getPosition() << player->getPitch() << player->getYaw(); + NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); + pkt << player->getPosition() << player->getPitch() << player->getYaw(); { v3f pos = player->getPosition(); f32 pitch = player->getPitch(); f32 yaw = player->getYaw(); - verbosestream<<"Server: Sending TOCLIENT_MOVE_PLAYER" - <<" pos=("<get("remote_media"); + Send(&pkt); } struct SendableMedia @@ -2432,21 +2408,21 @@ void Server::sendRequestedMedia(u16 peer_id, } */ - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MEDIA, 0, peer_id); - *pkt << num_bunches << i << (u32) file_bunches[i].size(); + NetworkPacket pkt(TOCLIENT_MEDIA, 4 + 0, peer_id); + pkt << num_bunches << i << (u32) file_bunches[i].size(); for(std::vector::iterator j = file_bunches[i].begin(); j != file_bunches[i].end(); ++j) { - *pkt << j->name; - pkt->putLongString(j->data); + pkt << j->name; + pkt.putLongString(j->data); } verbosestream << "Server::sendRequestedMedia(): bunch " << i << "/" << num_bunches << " files=" << file_bunches[i].size() - << " size=" << pkt->getSize() << std::endl; - Send(pkt); + << " size=" << pkt.getSize() << std::endl; + Send(&pkt); } } @@ -2465,14 +2441,14 @@ void Server::sendDetachedInventory(const std::string &name, u16 peer_id) // Make data buffer std::string s = os.str(); - NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DETACHED_INVENTORY, 0, peer_id); - pkt->putRawString(s.c_str(), s.size()); + NetworkPacket pkt(TOCLIENT_DETACHED_INVENTORY, 0, peer_id); + pkt.putRawString(s.c_str(), s.size()); if (peer_id != PEER_ID_INEXISTENT) { - Send(pkt); + Send(&pkt); } else { - m_clients.sendToAll(0, pkt, true); + m_clients.sendToAll(0, &pkt, true); } } diff --git a/src/test.cpp b/src/test.cpp index 8c9f26c52..32a6b102f 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -2022,12 +2022,12 @@ struct TestConnection: public TestBase Simple send-receive test */ { - NetworkPacket* pkt = new NetworkPacket((u8*) "Hello World !", 14, 0); + NetworkPacket pkt((u8*) "Hello World !", 14, 0); - SharedBuffer sentdata = pkt->oldForgePacket(); + SharedBuffer sentdata = pkt.oldForgePacket(); infostream<<"** running client.Send()"<getU8Ptr(0))[i])&0xff); + snprintf(buf, 10, "%.2X", ((int)((const char*)pkt.getU8Ptr(0))[i])&0xff); infostream<20) infostream<<"..."; infostream< sentdata = pkt->oldForgePacket(); + SharedBuffer sentdata = pkt.oldForgePacket(); - server.Send(peer_id_client, 0, pkt, true); + server.Send(peer_id_client, 0, &pkt, true); //sleep_ms(3000); @@ -2107,8 +2105,6 @@ struct TestConnection: public TestBase UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0); UASSERT(peer_id == PEER_ID_SERVER); - - delete pkt; } // Check peer handlers -- 2.25.1