1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-11-10 11:25:07 -05:00

Replace NetworkPacket pointers to references

This commit is contained in:
Loic Blot 2015-03-13 22:01:49 +01:00
parent 74d34da6cb
commit 083c8c734e
8 changed files with 256 additions and 295 deletions

View File

@ -407,15 +407,15 @@ void Client::step(float dtime)
snprintf(pName, PLAYERNAME_SIZE, "%s", myplayer->getName()); snprintf(pName, PLAYERNAME_SIZE, "%s", myplayer->getName());
snprintf(pPassword, PASSWORD_SIZE, "%s", m_password.c_str()); 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); 1 + PLAYERNAME_SIZE + PASSWORD_SIZE + 2 + 2);
*pkt << (u8) SER_FMT_VER_HIGHEST_READ; pkt << (u8) SER_FMT_VER_HIGHEST_READ;
pkt->putRawString(pName,PLAYERNAME_SIZE); pkt.putRawString(pName,PLAYERNAME_SIZE);
pkt->putRawString(pPassword, PASSWORD_SIZE); pkt.putRawString(pPassword, PASSWORD_SIZE);
*pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX; pkt << (u16) CLIENT_PROTOCOL_VERSION_MIN << (u16) CLIENT_PROTOCOL_VERSION_MAX;
Send(pkt); Send(&pkt);
} }
// Not connected, return // Not connected, return
@ -455,19 +455,19 @@ void Client::step(float dtime)
[3+6] v3s16 pos_1 [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; u32 k = 0;
for(std::vector<v3s16>::iterator for(std::vector<v3s16>::iterator
j = sendlist.begin(); j = sendlist.begin();
j != sendlist.end(); ++j) { j != sendlist.end(); ++j) {
*pkt << *j; pkt << *j;
k++; k++;
} }
Send(pkt); Send(&pkt);
if(i == deleted_blocks.end()) if(i == deleted_blocks.end())
break; break;
@ -575,9 +575,9 @@ void Client::step(float dtime)
[0] u8 count [0] u8 count
[1] v3s16 pos_0 [1] v3s16 pos_0
*/ */
NetworkPacket* pkt = new NetworkPacket(TOSERVER_GOTBLOCKS, 1 + 6); NetworkPacket pkt(TOSERVER_GOTBLOCKS, 1 + 6);
*pkt << (u8) 1 << r.p; pkt << (u8) 1 << r.p;
Send(pkt); Send(&pkt);
} }
} }
@ -666,15 +666,15 @@ void Client::step(float dtime)
size_t server_ids = removed_server_ids.size(); size_t server_ids = removed_server_ids.size();
assert(server_ids <= 0xFFFF); 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<s32>::iterator i = removed_server_ids.begin(); for(std::set<s32>::iterator i = removed_server_ids.begin();
i != removed_server_ids.end(); i++) 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<std::string> &file_requests)
FATAL_ERROR_IF(file_requests_size > 0xFFFF, "Unsupported number of file requests"); FATAL_ERROR_IF(file_requests_size > 0xFFFF, "Unsupported number of file requests");
// Packet dynamicly resized // 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<std::string>::const_iterator i = file_requests.begin(); for(std::vector<std::string>::const_iterator i = file_requests.begin();
i != file_requests.end(); ++i) { i != file_requests.end(); ++i) {
*pkt << (*i); pkt << (*i);
} }
Send(pkt); Send(&pkt);
infostream<<"Client: Sending media request list to server (" infostream << "Client: Sending media request list to server ("
<<file_requests.size()<<" files. packet size)"<<std::endl; << file_requests.size() << " files. packet size)" << std::endl;
} }
void Client::received_media() void Client::received_media()
{ {
NetworkPacket* pkt = new NetworkPacket(TOSERVER_RECEIVED_MEDIA, 0); NetworkPacket pkt(TOSERVER_RECEIVED_MEDIA, 0);
Send(pkt); Send(&pkt);
infostream<<"Client: Notifying server that we received all media" infostream << "Client: Notifying server that we received all media"
<<std::endl; << std::endl;
} }
void Client::initLocalMapSaving(const Address &address, void Client::initLocalMapSaving(const Address &address,
@ -891,9 +891,9 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
return; return;
} }
NetworkPacket* pkt = new NetworkPacket(data, datasize, sender_peer_id); NetworkPacket pkt(data, datasize, sender_peer_id);
ToClientCommand command = (ToClientCommand) pkt->getCommand(); ToClientCommand command = (ToClientCommand) pkt.getCommand();
//infostream<<"Client: received command="<<command<<std::endl; //infostream<<"Client: received command="<<command<<std::endl;
m_packetcounter.add((u16)command); m_packetcounter.add((u16)command);
@ -906,7 +906,6 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
infostream << "Client::ProcessData(): Discarding data not " infostream << "Client::ProcessData(): Discarding data not "
"coming from server: peer_id=" << sender_peer_id "coming from server: peer_id=" << sender_peer_id
<< std::endl; << std::endl;
delete pkt;
return; return;
} }
@ -922,8 +921,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
* as a byte mask * as a byte mask
*/ */
if(toClientCommandTable[command].state == TOCLIENT_STATE_NOT_CONNECTED) { if(toClientCommandTable[command].state == TOCLIENT_STATE_NOT_CONNECTED) {
handleCommand(pkt); handleCommand(&pkt);
delete pkt;
return; return;
} }
@ -931,7 +929,6 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
infostream << "Client: Server serialization" infostream << "Client: Server serialization"
" format invalid or not initialized." " format invalid or not initialized."
" Skipping incoming command=" << command << std::endl; " Skipping incoming command=" << command << std::endl;
delete pkt;
return; return;
} }
@ -939,8 +936,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
Handle runtime commands Handle runtime commands
*/ */
handleCommand(pkt); handleCommand(&pkt);
delete pkt;
} }
void Client::Send(NetworkPacket* pkt) void Client::Send(NetworkPacket* pkt)
@ -949,7 +945,6 @@ void Client::Send(NetworkPacket* pkt)
serverCommandFactoryTable[pkt->getCommand()].channel, serverCommandFactoryTable[pkt->getCommand()].channel,
pkt, pkt,
serverCommandFactoryTable[pkt->getCommand()].reliable); serverCommandFactoryTable[pkt->getCommand()].reliable);
delete pkt;
} }
void Client::interact(u8 action, const PointedThing& pointed) void Client::interact(u8 action, const PointedThing& pointed)
@ -975,17 +970,17 @@ void Client::interact(u8 action, const PointedThing& pointed)
4: use item 4: use item
*/ */
NetworkPacket* pkt = new NetworkPacket(TOSERVER_INTERACT, 1 + 2 + 0); NetworkPacket pkt(TOSERVER_INTERACT, 1 + 2 + 0);
*pkt << action; pkt << action;
*pkt << (u16)getPlayerItem(); pkt << (u16)getPlayerItem();
std::ostringstream tmp_os(std::ios::binary); std::ostringstream tmp_os(std::ios::binary);
pointed.serialize(tmp_os); pointed.serialize(tmp_os);
pkt->putLongString(tmp_os.str()); pkt.putLongString(tmp_os.str());
Send(pkt); Send(&pkt);
} }
void Client::sendNodemetaFields(v3s16 p, const std::string &formname, void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
@ -995,19 +990,19 @@ void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of nodemeta fields"); FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of nodemeta fields");
NetworkPacket* pkt = new NetworkPacket(TOSERVER_NODEMETA_FIELDS, 0); NetworkPacket pkt(TOSERVER_NODEMETA_FIELDS, 0);
*pkt << p << formname << (u16) (fields_size & 0xFFFF); pkt << p << formname << (u16) (fields_size & 0xFFFF);
for(std::map<std::string, std::string>::const_iterator for(std::map<std::string, std::string>::const_iterator
i = fields.begin(); i != fields.end(); i++) { i = fields.begin(); i != fields.end(); i++) {
const std::string &name = i->first; const std::string &name = i->first;
const std::string &value = i->second; const std::string &value = i->second;
*pkt << name; pkt << name;
pkt->putLongString(value); pkt.putLongString(value);
} }
Send(pkt); Send(&pkt);
} }
void Client::sendInventoryFields(const std::string &formname, void Client::sendInventoryFields(const std::string &formname,
@ -1016,18 +1011,18 @@ void Client::sendInventoryFields(const std::string &formname,
size_t fields_size = fields.size(); size_t fields_size = fields.size();
FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of inventory fields"); FATAL_ERROR_IF(fields_size > 0xFFFF, "Unsupported number of inventory fields");
NetworkPacket* pkt = new NetworkPacket(TOSERVER_INVENTORY_FIELDS, 0); NetworkPacket pkt(TOSERVER_INVENTORY_FIELDS, 0);
*pkt << formname << (u16) (fields_size & 0xFFFF); pkt << formname << (u16) (fields_size & 0xFFFF);
for(std::map<std::string, std::string>::const_iterator for(std::map<std::string, std::string>::const_iterator
i = fields.begin(); i != fields.end(); i++) { i = fields.begin(); i != fields.end(); i++) {
const std::string &name = i->first; const std::string &name = i->first;
const std::string &value = i->second; const std::string &value = i->second;
*pkt << name; pkt << name;
pkt->putLongString(value); pkt.putLongString(value);
} }
Send(pkt); Send(&pkt);
} }
void Client::sendInventoryAction(InventoryAction *a) void Client::sendInventoryAction(InventoryAction *a)
@ -1039,19 +1034,19 @@ void Client::sendInventoryAction(InventoryAction *a)
// Make data buffer // Make data buffer
std::string s = os.str(); std::string s = os.str();
NetworkPacket* pkt = new NetworkPacket(TOSERVER_INVENTORY_ACTION, s.size()); NetworkPacket pkt(TOSERVER_INVENTORY_ACTION, s.size());
pkt->putRawString(s.c_str(),s.size()); pkt.putRawString(s.c_str(),s.size());
Send(pkt); Send(&pkt);
} }
void Client::sendChatMessage(const std::wstring &message) 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, 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 oldpwd = translatePassword(playername, oldpassword);
std::string newpwd = translatePassword(playername, newpassword); 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++) { 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++) { 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); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOSERVER_DAMAGE, sizeof(u8)); NetworkPacket pkt(TOSERVER_DAMAGE, sizeof(u8));
*pkt << damage; pkt << damage;
Send(pkt); Send(&pkt);
} }
void Client::sendBreath(u16 breath) void Client::sendBreath(u16 breath)
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOSERVER_BREATH, sizeof(u16)); NetworkPacket pkt(TOSERVER_BREATH, sizeof(u16));
*pkt << breath; pkt << breath;
Send(pkt); Send(&pkt);
} }
void Client::sendRespawn() void Client::sendRespawn()
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOSERVER_RESPAWN, 0); NetworkPacket pkt(TOSERVER_RESPAWN, 0);
Send(pkt); Send(&pkt);
} }
void Client::sendReady() void Client::sendReady()
{ {
DSTACK(__FUNCTION_NAME); 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)); 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); << (u8) 0 << (u16) strlen(minetest_version_hash);
pkt->putRawString(minetest_version_hash, (u16) strlen(minetest_version_hash)); pkt.putRawString(minetest_version_hash, (u16) strlen(minetest_version_hash));
Send(pkt); Send(&pkt);
} }
void Client::sendPlayerPos() void Client::sendPlayerPos()
@ -1168,11 +1163,11 @@ void Client::sendPlayerPos()
[12+12+4+4] u32 keyPressed [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) void Client::sendPlayerItem(u16 item)
@ -1188,11 +1183,11 @@ void Client::sendPlayerItem(u16 item)
myplayer->peer_id = our_peer_id; myplayer->peer_id = our_peer_id;
assert(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) void Client::removeNode(v3s16 p)

View File

@ -626,12 +626,9 @@ void ClientInterface::UpdatePlayerList()
} }
void ClientInterface::send(u16 peer_id, u8 channelnum, 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); m_con->Send(peer_id, channelnum, pkt, reliable);
if (deletepkt)
delete pkt;
} }
void ClientInterface::sendToAll(u16 channelnum, void ClientInterface::sendToAll(u16 channelnum,
@ -647,8 +644,6 @@ void ClientInterface::sendToAll(u16 channelnum,
m_con->Send(client->peer_id, channelnum, pkt, reliable); m_con->Send(client->peer_id, channelnum, pkt, reliable);
} }
} }
delete pkt;
} }
RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min) RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min)

View File

@ -400,7 +400,7 @@ public:
std::vector<std::string> getPlayerNames(); std::vector<std::string> getPlayerNames();
/* send message to client */ /* 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 */ /* send to all clients */
void sendToAll(u16 channelnum, NetworkPacket* pkt, bool reliable); void sendToAll(u16 channelnum, NetworkPacket* pkt, bool reliable);

View File

@ -1725,9 +1725,8 @@ void ConnectionSendThread::connect(Address address)
// Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT // Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT
m_connection->SetPeerID(PEER_ID_INEXISTENT); m_connection->SetPeerID(PEER_ID_INEXISTENT);
NetworkPacket* pkt = new NetworkPacket(0,0); NetworkPacket pkt(0,0);
m_connection->Send(PEER_ID_SERVER, 0, pkt, true); m_connection->Send(PEER_ID_SERVER, 0, &pkt, true);
delete pkt;
} }
void ConnectionSendThread::disconnect() void ConnectionSendThread::disconnect()

View File

@ -77,8 +77,8 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt)
<< m_recommended_send_interval<<std::endl; << m_recommended_send_interval<<std::endl;
// Reply to server // Reply to server
NetworkPacket* resp_pkt = new NetworkPacket(TOSERVER_INIT2, 0); NetworkPacket resp_pkt(TOSERVER_INIT2, 0);
Send(resp_pkt); Send(&resp_pkt);
m_state = LC_Init; m_state = LC_Init;
} }
@ -128,8 +128,8 @@ void Client::handleCommand_InitLegacy(NetworkPacket* pkt)
} }
// Reply to server // Reply to server
NetworkPacket* resp_pkt = new NetworkPacket(TOSERVER_INIT2, 0); NetworkPacket resp_pkt(TOSERVER_INIT2, 0);
Send(resp_pkt); Send(&resp_pkt);
m_state = LC_Init; m_state = LC_Init;
} }

View File

@ -617,14 +617,14 @@ void Server::handleCommand_Init_Legacy(NetworkPacket* pkt)
Answer with a TOCLIENT_INIT Answer with a TOCLIENT_INIT
*/ */
NetworkPacket* resp_pkt = new NetworkPacket(TOCLIENT_INIT_LEGACY, 1 + 6 + 8 + 4, NetworkPacket resp_pkt(TOCLIENT_INIT_LEGACY, 1 + 6 + 8 + 4,
pkt->getPeerId()); pkt->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() << (u64) m_env->getServerMap().getSeed()
<< g_settings->getFloat("dedicated_server_step"); << g_settings->getFloat("dedicated_server_step");
Send(resp_pkt); Send(&resp_pkt);
m_clients.event(pkt->getPeerId(), CSE_Init); m_clients.event(pkt->getPeerId(), CSE_Init);
} }

View File

@ -758,16 +758,16 @@ void Server::AsyncRunStep(bool initial_step)
obj->m_known_by_count++; obj->m_known_by_count++;
} }
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, 0, client->peer_id); NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, 0, client->peer_id);
pkt->putRawString(data_buffer.c_str(), data_buffer.size()); pkt.putRawString(data_buffer.c_str(), data_buffer.size());
verbosestream << "Server: Sent object remove/add: " verbosestream << "Server: Sent object remove/add: "
<< removed_objects.size() << " removed, " << removed_objects.size() << " removed, "
<< added_objects.size() << " added, " << 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(); m_clients.Unlock();
} }
@ -846,19 +846,19 @@ void Server::AsyncRunStep(bool initial_step)
Send them. Send them.
*/ */
if(reliable_data.size() > 0) { if(reliable_data.size() > 0) {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_MESSAGES, NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_MESSAGES,
0, client->peer_id); 0, client->peer_id);
pkt->putRawString(reliable_data.c_str(), reliable_data.size()); pkt.putRawString(reliable_data.c_str(), reliable_data.size());
Send(pkt); Send(&pkt);
} }
if(unreliable_data.size() > 0) { if(unreliable_data.size() > 0) {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACTIVE_OBJECT_MESSAGES, NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_MESSAGES,
0, client->peer_id); 0, client->peer_id);
pkt->putRawString(unreliable_data.c_str(), unreliable_data.size()); pkt.putRawString(unreliable_data.c_str(), unreliable_data.size());
Send(pkt); Send(&pkt);
} }
} }
m_clients.Unlock(); m_clients.Unlock();
@ -1194,9 +1194,9 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
if(datasize < 2) if(datasize < 2)
return; 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 // Command must be handled into ToServerCommandHandler
if (command >= TOSERVER_NUM_MSG_TYPES) { 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) { if (toServerCommandTable[command].state == TOSERVER_STATE_NOT_CONNECTED) {
handleCommand(pkt); handleCommand(&pkt);
delete pkt;
return; return;
} }
@ -1216,15 +1215,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
errorstream << "Server::ProcessData(): Cancelling: Peer" errorstream << "Server::ProcessData(): Cancelling: Peer"
" serialization format invalid or not initialized." " serialization format invalid or not initialized."
" Skipping incoming command=" << command << std::endl; " Skipping incoming command=" << command << std::endl;
delete pkt;
return; return;
} }
/* Handle commands related to client startup */ /* Handle commands related to client startup */
if (toServerCommandTable[command].state == TOSERVER_STATE_STARTUP) { if (toServerCommandTable[command].state == TOSERVER_STATE_STARTUP) {
handleCommand(pkt); handleCommand(&pkt);
delete pkt;
return; return;
} }
@ -1234,14 +1230,10 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
errorstream << "Got packet command: " << command << " for peer id " errorstream << "Got packet command: " << command << " for peer id "
<< peer_id << " but client isn't active yet. Dropping packet " << peer_id << " but client isn't active yet. Dropping packet "
<< std::endl; << std::endl;
delete pkt;
return; return;
} }
handleCommand(pkt); handleCommand(&pkt);
delete pkt;
} }
catch(SendFailedException &e) { catch(SendFailedException &e) {
errorstream << "Server::ProcessData(): SendFailedException: " errorstream << "Server::ProcessData(): SendFailedException: "
@ -1466,40 +1458,40 @@ void Server::SendMovement(u16 peer_id)
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
std::ostringstream os(std::ios_base::binary); 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_default");
*pkt << g_settings->getFloat("movement_acceleration_air"); pkt << g_settings->getFloat("movement_acceleration_air");
*pkt << g_settings->getFloat("movement_acceleration_fast"); pkt << g_settings->getFloat("movement_acceleration_fast");
*pkt << g_settings->getFloat("movement_speed_walk"); pkt << g_settings->getFloat("movement_speed_walk");
*pkt << g_settings->getFloat("movement_speed_crouch"); pkt << g_settings->getFloat("movement_speed_crouch");
*pkt << g_settings->getFloat("movement_speed_fast"); pkt << g_settings->getFloat("movement_speed_fast");
*pkt << g_settings->getFloat("movement_speed_climb"); pkt << g_settings->getFloat("movement_speed_climb");
*pkt << g_settings->getFloat("movement_speed_jump"); pkt << g_settings->getFloat("movement_speed_jump");
*pkt << g_settings->getFloat("movement_liquid_fluidity"); pkt << g_settings->getFloat("movement_liquid_fluidity");
*pkt << g_settings->getFloat("movement_liquid_fluidity_smooth"); pkt << g_settings->getFloat("movement_liquid_fluidity_smooth");
*pkt << g_settings->getFloat("movement_liquid_sink"); pkt << g_settings->getFloat("movement_liquid_sink");
*pkt << g_settings->getFloat("movement_gravity"); pkt << g_settings->getFloat("movement_gravity");
Send(pkt); Send(&pkt);
} }
void Server::SendHP(u16 peer_id, u8 hp) void Server::SendHP(u16 peer_id, u8 hp)
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HP, 1, peer_id); NetworkPacket pkt(TOCLIENT_HP, 1, peer_id);
*pkt << hp; pkt << hp;
Send(pkt); Send(&pkt);
} }
void Server::SendBreath(u16 peer_id, u16 breath) void Server::SendBreath(u16 peer_id, u16 breath)
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_BREATH, 2, peer_id); NetworkPacket pkt(TOCLIENT_BREATH, 2, peer_id);
*pkt << (u16) breath; pkt << (u16) breath;
Send(pkt); Send(&pkt);
} }
void Server::SendAccessDenied(u16 peer_id, AccessDeniedCode reason, const std::wstring &custom_reason) 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); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ACCESS_DENIED_LEGACY, 0, peer_id); NetworkPacket pkt(TOCLIENT_ACCESS_DENIED_LEGACY, 0, peer_id);
*pkt << reason; pkt << reason;
Send(pkt); Send(&pkt);
} }
void Server::SendDeathscreen(u16 peer_id,bool set_camera_point_target, 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); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DEATHSCREEN, 1 + sizeof(v3f), peer_id); NetworkPacket pkt(TOCLIENT_DEATHSCREEN, 1 + sizeof(v3f), peer_id);
*pkt << set_camera_point_target << camera_point_target; pkt << set_camera_point_target << camera_point_target;
Send(pkt); Send(&pkt);
} }
void Server::SendItemDef(u16 peer_id, void Server::SendItemDef(u16 peer_id,
@ -1539,7 +1531,7 @@ void Server::SendItemDef(u16 peer_id,
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ITEMDEF, 0, peer_id); NetworkPacket pkt(TOCLIENT_ITEMDEF, 0, peer_id);
/* /*
u16 command u16 command
@ -1550,13 +1542,13 @@ void Server::SendItemDef(u16 peer_id,
itemdef->serialize(tmp_os, protocol_version); itemdef->serialize(tmp_os, protocol_version);
std::ostringstream tmp_os2(std::ios::binary); std::ostringstream tmp_os2(std::ios::binary);
compressZlib(tmp_os.str(), tmp_os2); compressZlib(tmp_os.str(), tmp_os2);
pkt->putLongString(tmp_os2.str()); pkt.putLongString(tmp_os2.str());
// Make data buffer // Make data buffer
verbosestream << "Server: Sending item definitions to id(" << peer_id 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, void Server::SendNodeDef(u16 peer_id,
@ -1564,7 +1556,7 @@ void Server::SendNodeDef(u16 peer_id,
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_NODEDEF, 0, peer_id); NetworkPacket pkt(TOCLIENT_NODEDEF, 0, peer_id);
/* /*
u16 command u16 command
@ -1576,13 +1568,13 @@ void Server::SendNodeDef(u16 peer_id,
std::ostringstream tmp_os2(std::ios::binary); std::ostringstream tmp_os2(std::ios::binary);
compressZlib(tmp_os.str(), tmp_os2); compressZlib(tmp_os.str(), tmp_os2);
pkt->putLongString(tmp_os2.str()); pkt.putLongString(tmp_os2.str());
// Make data buffer // Make data buffer
verbosestream << "Server: Sending node definitions to id(" << peer_id 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 Serialize it
*/ */
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_INVENTORY, 0, NetworkPacket pkt(TOCLIENT_INVENTORY, 0, playerSAO->getPeerID());
playerSAO->getPeerID());
std::ostringstream os; std::ostringstream os;
playerSAO->getInventory()->serialize(os); playerSAO->getInventory()->serialize(os);
std::string s = os.str(); std::string s = os.str();
pkt->putRawString(s.c_str(), s.size()); pkt.putRawString(s.c_str(), s.size());
Send(pkt); Send(&pkt);
} }
void Server::SendChatMessage(u16 peer_id, const std::wstring &message) void Server::SendChatMessage(u16 peer_id, const std::wstring &message)
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_CHAT_MESSAGE, 0, peer_id); NetworkPacket pkt(TOCLIENT_CHAT_MESSAGE, 0, peer_id);
*pkt << message; pkt << message;
if (peer_id != PEER_ID_INEXISTENT) { if (peer_id != PEER_ID_INEXISTENT) {
Send(pkt); Send(&pkt);
} }
else { 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); 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.putLongString(FORMSPEC_VERSION_STRING + formspec);
*pkt << formname; pkt << formname;
Send(pkt); Send(&pkt);
} }
// Spawns a particle on peer with peer_id // 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); 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; << size << collisiondetection;
pkt->putLongString(texture); pkt.putLongString(texture);
*pkt << vertical; pkt << vertical;
if (peer_id != PEER_ID_INEXISTENT) { if (peer_id != PEER_ID_INEXISTENT) {
Send(pkt); Send(&pkt);
} }
else { 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); 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 << minacc << maxacc << minexptime << maxexptime << minsize
<< maxsize << collisiondetection; << maxsize << collisiondetection;
pkt->putLongString(texture); pkt.putLongString(texture);
*pkt << id << vertical; pkt << id << vertical;
if (peer_id != PEER_ID_INEXISTENT) { if (peer_id != PEER_ID_INEXISTENT) {
Send(pkt); Send(&pkt);
} }
else { 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); 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 // Ugly error in this packet
*pkt << (u16) id; pkt << (u16) id;
if (peer_id != PEER_ID_INEXISTENT) { if (peer_id != PEER_ID_INEXISTENT) {
Send(pkt); Send(&pkt);
} }
else { else {
m_clients.sendToAll(0, pkt, true); m_clients.sendToAll(0, &pkt, true);
} }
} }
void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form) 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->text << form->number << form->item << form->dir
<< form->align << form->offset << form->world_pos << form->size; << form->align << form->offset << form->world_pos << form->size;
Send(pkt); Send(&pkt);
} }
void Server::SendHUDRemove(u16 peer_id, u32 id) void Server::SendHUDRemove(u16 peer_id, u32 id)
{ {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUDRM, 4, peer_id); NetworkPacket pkt(TOCLIENT_HUDRM, 4, peer_id);
*pkt << id; pkt << id;
Send(pkt); Send(&pkt);
} }
void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value) void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value)
{ {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUDCHANGE, 0, peer_id); NetworkPacket pkt(TOCLIENT_HUDCHANGE, 0, peer_id);
*pkt << id << (u8) stat; pkt << id << (u8) stat;
switch (stat) { switch (stat) {
case HUD_STAT_POS: case HUD_STAT_POS:
case HUD_STAT_SCALE: case HUD_STAT_SCALE:
case HUD_STAT_ALIGN: case HUD_STAT_ALIGN:
case HUD_STAT_OFFSET: case HUD_STAT_OFFSET:
*pkt << *(v2f *) value; pkt << *(v2f *) value;
break; break;
case HUD_STAT_NAME: case HUD_STAT_NAME:
case HUD_STAT_TEXT: case HUD_STAT_TEXT:
*pkt << *(std::string *) value; pkt << *(std::string *) value;
break; break;
case HUD_STAT_WORLD_POS: case HUD_STAT_WORLD_POS:
*pkt << *(v3f *) value; pkt << *(v3f *) value;
break; break;
case HUD_STAT_SIZE: case HUD_STAT_SIZE:
*pkt << *(v2s32 *) value; pkt << *(v2s32 *) value;
break; break;
case HUD_STAT_NUMBER: case HUD_STAT_NUMBER:
case HUD_STAT_ITEM: case HUD_STAT_ITEM:
case HUD_STAT_DIR: case HUD_STAT_DIR:
default: default:
*pkt << *(u32 *) value; pkt << *(u32 *) value;
break; break;
} }
Send(pkt); Send(&pkt);
} }
void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask) 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); 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) void Server::SendHUDSetParam(u16 peer_id, u16 param, const std::string &value)
{ {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_HUD_SET_PARAM, 0, peer_id); NetworkPacket pkt(TOCLIENT_HUD_SET_PARAM, 0, peer_id);
*pkt << param << value; pkt << param << value;
Send(pkt); Send(&pkt);
} }
void Server::SendSetSky(u16 peer_id, const video::SColor &bgcolor, void Server::SendSetSky(u16 peer_id, const video::SColor &bgcolor,
const std::string &type, const std::vector<std::string> &params) const std::string &type, const std::vector<std::string> &params)
{ {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_SET_SKY, 0, peer_id); NetworkPacket pkt(TOCLIENT_SET_SKY, 0, peer_id);
*pkt << bgcolor << type << (u16) params.size(); pkt << bgcolor << type << (u16) params.size();
for(size_t i=0; i<params.size(); i++) for(size_t i=0; i<params.size(); i++)
*pkt << params[i]; pkt << params[i];
Send(pkt); Send(&pkt);
} }
void Server::SendOverrideDayNightRatio(u16 peer_id, bool do_override, void Server::SendOverrideDayNightRatio(u16 peer_id, bool do_override,
float ratio) float ratio)
{ {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO, NetworkPacket pkt(TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO,
1 + 2, peer_id); 1 + 2, peer_id);
*pkt << do_override << (u16) (ratio * 65535); pkt << do_override << (u16) (ratio * 65535);
Send (pkt); Send(&pkt);
} }
void Server::SendTimeOfDay(u16 peer_id, u16 time, f32 time_speed) void Server::SendTimeOfDay(u16 peer_id, u16 time, f32 time_speed)
{ {
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_TIME_OF_DAY, 0, peer_id); NetworkPacket pkt(TOCLIENT_TIME_OF_DAY, 0, peer_id);
*pkt << time << time_speed; pkt << time << time_speed;
if (peer_id == PEER_ID_INEXISTENT) { if (peer_id == PEER_ID_INEXISTENT) {
m_clients.sendToAll(0, pkt, true); m_clients.sendToAll(0, &pkt, true);
} }
else { else {
Send(pkt); Send(&pkt);
} }
} }
@ -1841,39 +1832,39 @@ void Server::SendMovePlayer(u16 peer_id)
Player *player = m_env->getPlayer(peer_id); Player *player = m_env->getPlayer(peer_id);
assert(player); assert(player);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MOVE_PLAYER, 0, peer_id); NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id);
*pkt << player->getPosition() << player->getPitch() << player->getYaw(); pkt << player->getPosition() << player->getPitch() << player->getYaw();
{ {
v3f pos = player->getPosition(); v3f pos = player->getPosition();
f32 pitch = player->getPitch(); f32 pitch = player->getPitch();
f32 yaw = player->getYaw(); f32 yaw = player->getYaw();
verbosestream<<"Server: Sending TOCLIENT_MOVE_PLAYER" verbosestream << "Server: Sending TOCLIENT_MOVE_PLAYER"
<<" pos=("<<pos.X<<","<<pos.Y<<","<<pos.Z<<")" << " pos=(" << pos.X << "," << pos.Y << "," << pos.Z << ")"
<<" pitch="<<pitch << " pitch=" << pitch
<<" yaw="<<yaw << " yaw=" << yaw
<<std::endl; << std::endl;
} }
Send(pkt); Send(&pkt);
} }
void Server::SendLocalPlayerAnimations(u16 peer_id, v2s32 animation_frames[4], f32 animation_speed) void Server::SendLocalPlayerAnimations(u16 peer_id, v2s32 animation_frames[4], f32 animation_speed)
{ {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_LOCAL_PLAYER_ANIMATIONS, 0, NetworkPacket pkt(TOCLIENT_LOCAL_PLAYER_ANIMATIONS, 0,
peer_id); peer_id);
*pkt << animation_frames[0] << animation_frames[1] << animation_frames[2] pkt << animation_frames[0] << animation_frames[1] << animation_frames[2]
<< animation_frames[3] << animation_speed; << animation_frames[3] << animation_speed;
Send(pkt); Send(&pkt);
} }
void Server::SendEyeOffset(u16 peer_id, v3f first, v3f third) void Server::SendEyeOffset(u16 peer_id, v3f first, v3f third)
{ {
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_EYE_OFFSET, 0, peer_id); NetworkPacket pkt(TOCLIENT_EYE_OFFSET, 0, peer_id);
*pkt << first << third; pkt << first << third;
Send(pkt); Send(&pkt);
} }
void Server::SendPlayerPrivileges(u16 peer_id) void Server::SendPlayerPrivileges(u16 peer_id)
{ {
@ -1885,15 +1876,15 @@ void Server::SendPlayerPrivileges(u16 peer_id)
std::set<std::string> privs; std::set<std::string> privs;
m_script->getAuth(player->getName(), NULL, &privs); m_script->getAuth(player->getName(), NULL, &privs);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_PRIVILEGES, 0, peer_id); NetworkPacket pkt(TOCLIENT_PRIVILEGES, 0, peer_id);
*pkt << (u16) privs.size(); pkt << (u16) privs.size();
for(std::set<std::string>::const_iterator i = privs.begin(); for(std::set<std::string>::const_iterator i = privs.begin();
i != privs.end(); i++) { i != privs.end(); i++) {
*pkt << (*i); pkt << (*i);
} }
Send(pkt); Send(&pkt);
} }
void Server::SendPlayerInventoryFormspec(u16 peer_id) void Server::SendPlayerInventoryFormspec(u16 peer_id)
@ -1903,9 +1894,9 @@ void Server::SendPlayerInventoryFormspec(u16 peer_id)
if(player->peer_id == PEER_ID_INEXISTENT) if(player->peer_id == PEER_ID_INEXISTENT)
return; return;
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_INVENTORY_FORMSPEC, 0, peer_id); NetworkPacket pkt(TOCLIENT_INVENTORY_FORMSPEC, 0, peer_id);
pkt->putLongString(FORMSPEC_VERSION_STRING + player->inventory_formspec); pkt.putLongString(FORMSPEC_VERSION_STRING + player->inventory_formspec);
Send(pkt); Send(&pkt);
} }
s32 Server::playSound(const SimpleSoundSpec &spec, s32 Server::playSound(const SimpleSoundSpec &spec,
@ -1963,16 +1954,15 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
ServerPlayingSound &psound = m_playing_sounds[id]; ServerPlayingSound &psound = m_playing_sounds[id];
psound.params = params; psound.params = params;
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_PLAY_SOUND, 0); NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0);
*pkt << id << spec.name << (float) (spec.gain * params.gain) pkt << id << spec.name << (float) (spec.gain * params.gain)
<< (u8) params.type << pos << params.object << params.loop; << (u8) params.type << pos << params.object << params.loop;
for(std::vector<u16>::iterator i = dst_clients.begin(); for(std::vector<u16>::iterator i = dst_clients.begin();
i != dst_clients.end(); i++) { i != dst_clients.end(); i++) {
psound.clients.insert(*i); psound.clients.insert(*i);
m_clients.send(*i, 0, pkt, true, false); m_clients.send(*i, 0, &pkt, true);
} }
delete pkt;
return id; return id;
} }
void Server::stopSound(s32 handle) void Server::stopSound(s32 handle)
@ -1984,15 +1974,14 @@ void Server::stopSound(s32 handle)
return; return;
ServerPlayingSound &psound = i->second; ServerPlayingSound &psound = i->second;
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_STOP_SOUND, 4); NetworkPacket pkt(TOCLIENT_STOP_SOUND, 4);
*pkt << handle; pkt << handle;
for(std::set<u16>::iterator i = psound.clients.begin(); for(std::set<u16>::iterator i = psound.clients.begin();
i != psound.clients.end(); i++) { i != psound.clients.end(); i++) {
// Send as reliable // Send as reliable
m_clients.send(*i, 0, pkt, true, false); m_clients.send(*i, 0, &pkt, true);
} }
delete pkt;
// Remove sound reference // Remove sound reference
m_playing_sounds.erase(i); m_playing_sounds.erase(i);
} }
@ -2003,8 +1992,8 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
float maxd = far_d_nodes*BS; float maxd = far_d_nodes*BS;
v3f p_f = intToFloat(p, BS); v3f p_f = intToFloat(p, BS);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_REMOVENODE, 6); NetworkPacket pkt(TOCLIENT_REMOVENODE, 6);
*pkt << p; pkt << p;
std::vector<u16> clients = m_clients.getClientIDs(); std::vector<u16> clients = m_clients.getClientIDs();
for(std::vector<u16>::iterator i = clients.begin(); for(std::vector<u16>::iterator i = clients.begin();
@ -2022,10 +2011,8 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
} }
// Send as reliable // Send as reliable
m_clients.send(*i, 0, pkt, true, false); m_clients.send(*i, 0, &pkt, true);
} }
// This loop needs the deletion of the packet here
delete pkt;
} }
void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id, void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
@ -2051,11 +2038,11 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
} }
} }
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ADDNODE, 6 + 2 + 1 + 1 + 1); NetworkPacket pkt(TOCLIENT_ADDNODE, 6 + 2 + 1 + 1 + 1);
m_clients.Lock(); m_clients.Lock();
RemoteClient* client = m_clients.lockedGetClientNoEx(*i); RemoteClient* client = m_clients.lockedGetClientNoEx(*i);
if (client != 0) { if (client != 0) {
*pkt << p << n.param0 << n.param1 << n.param2 pkt << p << n.param0 << n.param1 << n.param2
<< (u8) (remove_metadata ? 0 : 1); << (u8) (remove_metadata ? 0 : 1);
if (!remove_metadata) { if (!remove_metadata) {
@ -2069,8 +2056,8 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
m_clients.Unlock(); m_clients.Unlock();
// Send as reliable // Send as reliable
if (pkt->getSize() > 0) if (pkt.getSize() > 0)
m_clients.send(*i, 0, pkt, true); m_clients.send(*i, 0, &pkt, true);
} }
} }
@ -2101,12 +2088,11 @@ void Server::SendBlockNoLock(u16 peer_id, MapBlock *block, u8 ver, u16 net_proto
block->serializeNetworkSpecific(os, net_proto_version); block->serializeNetworkSpecific(os, net_proto_version);
std::string s = os.str(); std::string s = os.str();
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_BLOCKDATA, NetworkPacket pkt(TOCLIENT_BLOCKDATA, 2 + 2 + 2 + 2 + s.size(), peer_id);
2 + 2 + 2 + 2 + s.size(), peer_id);
*pkt << p; pkt << p;
pkt->putRawString(s.c_str(), s.size()); pkt.putRawString(s.c_str(), s.size());
Send(pkt); Send(&pkt);
} }
void Server::SendBlocks(float dtime) void Server::SendBlocks(float dtime)
@ -2306,27 +2292,17 @@ void Server::sendMediaAnnouncement(u16 peer_id)
// Make packet // Make packet
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
/* NetworkPacket pkt(TOCLIENT_ANNOUNCE_MEDIA, 0, peer_id);
u32 number of files pkt << (u16) file_announcements.size();
for each texture {
u16 length of name
string name
u16 length of sha1_digest
string sha1_digest
}
*/
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ANNOUNCE_MEDIA, 0, peer_id);
*pkt << (u16) file_announcements.size();
for (std::vector<SendableMediaAnnouncement>::iterator for (std::vector<SendableMediaAnnouncement>::iterator
j = file_announcements.begin(); j = file_announcements.begin();
j != file_announcements.end(); ++j) { j != file_announcements.end(); ++j) {
*pkt << j->name << j->sha1_digest; pkt << j->name << j->sha1_digest;
} }
*pkt << g_settings->get("remote_media"); pkt << g_settings->get("remote_media");
Send(pkt); Send(&pkt);
} }
struct SendableMedia struct SendableMedia
@ -2432,21 +2408,21 @@ void Server::sendRequestedMedia(u16 peer_id,
} }
*/ */
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_MEDIA, 0, peer_id); NetworkPacket pkt(TOCLIENT_MEDIA, 4 + 0, peer_id);
*pkt << num_bunches << i << (u32) file_bunches[i].size(); pkt << num_bunches << i << (u32) file_bunches[i].size();
for(std::vector<SendableMedia>::iterator for(std::vector<SendableMedia>::iterator
j = file_bunches[i].begin(); j = file_bunches[i].begin();
j != file_bunches[i].end(); ++j) { j != file_bunches[i].end(); ++j) {
*pkt << j->name; pkt << j->name;
pkt->putLongString(j->data); pkt.putLongString(j->data);
} }
verbosestream << "Server::sendRequestedMedia(): bunch " verbosestream << "Server::sendRequestedMedia(): bunch "
<< i << "/" << num_bunches << i << "/" << num_bunches
<< " files=" << file_bunches[i].size() << " files=" << file_bunches[i].size()
<< " size=" << pkt->getSize() << std::endl; << " size=" << pkt.getSize() << std::endl;
Send(pkt); Send(&pkt);
} }
} }
@ -2465,14 +2441,14 @@ void Server::sendDetachedInventory(const std::string &name, u16 peer_id)
// Make data buffer // Make data buffer
std::string s = os.str(); std::string s = os.str();
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_DETACHED_INVENTORY, 0, peer_id); NetworkPacket pkt(TOCLIENT_DETACHED_INVENTORY, 0, peer_id);
pkt->putRawString(s.c_str(), s.size()); pkt.putRawString(s.c_str(), s.size());
if (peer_id != PEER_ID_INEXISTENT) { if (peer_id != PEER_ID_INEXISTENT) {
Send(pkt); Send(&pkt);
} }
else { else {
m_clients.sendToAll(0, pkt, true); m_clients.sendToAll(0, &pkt, true);
} }
} }

View File

@ -2022,12 +2022,12 @@ struct TestConnection: public TestBase
Simple send-receive test Simple send-receive test
*/ */
{ {
NetworkPacket* pkt = new NetworkPacket((u8*) "Hello World !", 14, 0); NetworkPacket pkt((u8*) "Hello World !", 14, 0);
SharedBuffer<u8> sentdata = pkt->oldForgePacket(); SharedBuffer<u8> sentdata = pkt.oldForgePacket();
infostream<<"** running client.Send()"<<std::endl; infostream<<"** running client.Send()"<<std::endl;
client.Send(PEER_ID_SERVER, 0, pkt, true); client.Send(PEER_ID_SERVER, 0, &pkt, true);
sleep_ms(50); sleep_ms(50);
@ -2037,12 +2037,10 @@ struct TestConnection: public TestBase
u32 size = server.Receive(peer_id, recvdata); u32 size = server.Receive(peer_id, recvdata);
infostream << "** Server received: peer_id=" << peer_id infostream << "** Server received: peer_id=" << peer_id
<< ", size=" << size << ", size=" << size
<< ", data=" << (const char*)pkt->getU8Ptr(0) << ", data=" << (const char*)pkt.getU8Ptr(0)
<< std::endl; << std::endl;
UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0); UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
delete pkt;
} }
u16 peer_id_client = 2; u16 peer_id_client = 2;
@ -2051,25 +2049,25 @@ struct TestConnection: public TestBase
*/ */
{ {
const int datasize = 30000; const int datasize = 30000;
NetworkPacket* pkt = new NetworkPacket(0, datasize); NetworkPacket pkt(0, datasize);
for(u16 i=0; i<datasize; i++){ for (u16 i=0; i<datasize; i++) {
*pkt << (u8) i/4; pkt << (u8) i/4;
} }
infostream<<"Sending data (size="<<datasize<<"):"; infostream<<"Sending data (size="<<datasize<<"):";
for(int i=0; i<datasize && i<20; i++){ for(int i=0; i<datasize && i<20; i++) {
if(i%2==0) infostream<<" "; if(i%2==0) infostream<<" ";
char buf[10]; char buf[10];
snprintf(buf, 10, "%.2X", ((int)((const char*)pkt->getU8Ptr(0))[i])&0xff); snprintf(buf, 10, "%.2X", ((int)((const char*)pkt.getU8Ptr(0))[i])&0xff);
infostream<<buf; infostream<<buf;
} }
if(datasize>20) if(datasize>20)
infostream<<"..."; infostream<<"...";
infostream<<std::endl; infostream<<std::endl;
SharedBuffer<u8> sentdata = pkt->oldForgePacket(); SharedBuffer<u8> sentdata = pkt.oldForgePacket();
server.Send(peer_id_client, 0, pkt, true); server.Send(peer_id_client, 0, &pkt, true);
//sleep_ms(3000); //sleep_ms(3000);
@ -2107,8 +2105,6 @@ struct TestConnection: public TestBase
UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0); UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
UASSERT(peer_id == PEER_ID_SERVER); UASSERT(peer_id == PEER_ID_SERVER);
delete pkt;
} }
// Check peer handlers // Check peer handlers