diff options
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/connection.cpp | 23 | ||||
-rw-r--r-- | src/network/connection.h | 4 | ||||
-rw-r--r-- | src/network/networkpacket.cpp | 25 | ||||
-rw-r--r-- | src/network/networkpacket.h | 5 |
4 files changed, 36 insertions, 21 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?)"); diff --git a/src/network/connection.h b/src/network/connection.h index 9c920cc01..f60c66257 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -34,6 +34,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <list> #include <map> +class NetworkPacket; + namespace con { @@ -1025,7 +1027,7 @@ public: void Connect(Address address); bool Connected(); void Disconnect(); - u32 Receive(u16 &peer_id, SharedBuffer<u8> &data); + void Receive(NetworkPacket* pkt); void Send(u16 peer_id, u8 channelnum, NetworkPacket* pkt, bool reliable); u16 GetPeerID() { return m_peer_id; } Address GetPeerAddress(u16 peer_id); diff --git a/src/network/networkpacket.cpp b/src/network/networkpacket.cpp index 85d39d91d..d7487af40 100644 --- a/src/network/networkpacket.cpp +++ b/src/network/networkpacket.cpp @@ -22,17 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "exceptions.h" #include "util/serialize.h" -NetworkPacket::NetworkPacket(u8 *data, u32 datasize, u16 peer_id): -m_read_offset(0), m_peer_id(peer_id) -{ - m_read_offset = 0; - m_datasize = datasize - 2; - - // split command and datas - m_command = readU16(&data[0]); - m_data = std::vector<u8>(&data[2], &data[2 + m_datasize]); -} - NetworkPacket::NetworkPacket(u16 command, u32 datasize, u16 peer_id): m_datasize(datasize), m_read_offset(0), m_command(command), m_peer_id(peer_id) { @@ -50,6 +39,20 @@ NetworkPacket::~NetworkPacket() m_data.clear(); } +void NetworkPacket::putRawPacket(u8 *data, u32 datasize, u16 peer_id) +{ + // If a m_command is already set, we are rewriting on same packet + // This is not permitted + assert(m_command == 0); + + m_datasize = datasize - 2; + m_peer_id = peer_id; + + // split command and datas + m_command = readU16(&data[0]); + m_data = std::vector<u8>(&data[2], &data[2 + m_datasize]); +} + char* NetworkPacket::getString(u32 from_offset) { if (from_offset >= m_datasize) diff --git a/src/network/networkpacket.h b/src/network/networkpacket.h index 4a801b444..0afb1e7e3 100644 --- a/src/network/networkpacket.h +++ b/src/network/networkpacket.h @@ -28,11 +28,14 @@ class NetworkPacket { public: - NetworkPacket(u8 *data, u32 datasize, u16 peer_id); NetworkPacket(u16 command, u32 datasize, u16 peer_id); NetworkPacket(u16 command, u32 datasize); + NetworkPacket(): m_datasize(0), m_read_offset(0), m_command(0), + m_peer_id(0) {} ~NetworkPacket(); + void putRawPacket(u8 *data, u32 datasize, u16 peer_id); + // Getters u32 getSize() { return m_datasize; } u16 getPeerId() { return m_peer_id; } |