1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Connection::Receive(): receive Network Packet instead of SharedBuffer<u8>.

Because we get a Buffer<u8> from ConnectionEvent, don't convert it to SharedBuffer<u8> and return it to Server/Client::Receive which will convert it to NetworkPacket
Instead, put the Buffer<u8> directly to NetworkPacket and return it to packet processing
This remove a long existing memory copy
Also check the packet size directly into Connection::Receive instead of packet processing
This commit is contained in:
Loic Blot 2015-03-31 10:35:51 +02:00
parent ab77bf98ee
commit 1fe4256462
9 changed files with 104 additions and 98 deletions

View file

@ -834,10 +834,9 @@ void Client::ReceiveAll()
void Client::Receive()
{
DSTACK(__FUNCTION_NAME);
SharedBuffer<u8> data;
u16 sender_peer_id;
u32 datasize = m_con.Receive(sender_peer_id, data);
ProcessData(*data, datasize, sender_peer_id);
NetworkPacket pkt;
m_con.Receive(&pkt);
ProcessData(&pkt);
}
inline void Client::handleCommand(NetworkPacket* pkt)
@ -849,19 +848,12 @@ inline void Client::handleCommand(NetworkPacket* pkt)
/*
sender_peer_id given to this shall be quaranteed to be a valid peer
*/
void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
void Client::ProcessData(NetworkPacket *pkt)
{
DSTACK(__FUNCTION_NAME);
// Ignore packets that don't even fit a command
if(datasize < 2) {
m_packetcounter.add(60000);
return;
}
NetworkPacket pkt(data, datasize, sender_peer_id);
ToClientCommand command = (ToClientCommand) pkt.getCommand();
ToClientCommand command = (ToClientCommand) pkt->getCommand();
u32 sender_peer_id = pkt->getPeerId();
//infostream<<"Client: received command="<<command<<std::endl;
m_packetcounter.add((u16)command);
@ -889,7 +881,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
* as a byte mask
*/
if(toClientCommandTable[command].state == TOCLIENT_STATE_NOT_CONNECTED) {
handleCommand(&pkt);
handleCommand(pkt);
return;
}
@ -904,7 +896,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
Handle runtime commands
*/
handleCommand(&pkt);
handleCommand(pkt);
}
void Client::Send(NetworkPacket* pkt)