diff options
author | Loic Blot <loic.blot@unix-experience.fr> | 2015-03-31 10:35:51 +0200 |
---|---|---|
committer | Loic Blot <loic.blot@unix-experience.fr> | 2015-03-31 11:01:08 +0200 |
commit | 1fe4256462826c218ed9bf171be4c07e0db33e25 (patch) | |
tree | 8c07a04333c23599376327e847d030cfe6bec162 /src/network/connection.cpp | |
parent | ab77bf98ee320835e5dc50ed9b013442221f96e8 (diff) | |
download | minetest-1fe4256462826c218ed9bf171be4c07e0db33e25.tar.gz minetest-1fe4256462826c218ed9bf171be4c07e0db33e25.tar.bz2 minetest-1fe4256462826c218ed9bf171be4c07e0db33e25.zip |
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
Diffstat (limited to 'src/network/connection.cpp')
-rw-r--r-- | src/network/connection.cpp | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/network/connection.cpp b/src/network/connection.cpp index b808c3ab6..dd69df5bb 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serialization.h" #include "log.h" #include "porting.h" +#include "network/networkpacket.h" #include "util/serialize.h" #include "util/numeric.h" #include "util/string.h" @@ -2884,30 +2885,36 @@ void Connection::Disconnect() putCommand(c); } -u32 Connection::Receive(u16 &peer_id, SharedBuffer<u8> &data) +void Connection::Receive(NetworkPacket* pkt) { for(;;) { ConnectionEvent e = waitEvent(m_bc_receive_timeout); if (e.type != CONNEVENT_NONE) - LOG(dout_con<<getDesc()<<": Receive: got event: " - <<e.describe()<<std::endl); + LOG(dout_con << getDesc() << ": Receive: got event: " + << e.describe() << std::endl); switch(e.type) { case CONNEVENT_NONE: throw NoIncomingDataException("No incoming data"); case CONNEVENT_DATA_RECEIVED: - peer_id = e.peer_id; - data = SharedBuffer<u8>(e.data); - return e.data.getSize(); + // Data size is lesser than command size, ignoring packet + if (e.data.getSize() < 2) { + continue; + } + + pkt->putRawPacket(*e.data, e.data.getSize(), e.peer_id); + return; case CONNEVENT_PEER_ADDED: { UDPPeer tmp(e.peer_id, e.address, this); if (m_bc_peerhandler) m_bc_peerhandler->peerAdded(&tmp); - continue; } + continue; + } case CONNEVENT_PEER_REMOVED: { UDPPeer tmp(e.peer_id, e.address, this); if (m_bc_peerhandler) m_bc_peerhandler->deletingPeer(&tmp, e.timeout); - continue; } + continue; + } case CONNEVENT_BIND_FAILED: throw ConnectionBindFailed("Failed to bind socket " "(port already in use?)"); |