aboutsummaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2019-11-14 17:38:15 +0100
committerrubenwardy <rw@rubenwardy.com>2019-12-31 21:31:53 +0000
commit1c61fe5ed930720485ef42571739e5efc39a14c9 (patch)
tree66e72a976a8aa7c5a88343181e69a3bfa21ebcc9 /src/network
parent57409ef382509a4a293683b78b44816ee29bb8d4 (diff)
downloadminetest-1c61fe5ed930720485ef42571739e5efc39a14c9.tar.gz
minetest-1c61fe5ed930720485ef42571739e5efc39a14c9.tar.bz2
minetest-1c61fe5ed930720485ef42571739e5efc39a14c9.zip
Rework packet receiving in ServerThread
Notably it tries to receive all queued packets between server steps, not just one.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/connection.cpp27
-rw-r--r--src/network/connection.h3
-rw-r--r--src/network/networkpacket.cpp9
-rw-r--r--src/network/networkpacket.h1
4 files changed, 35 insertions, 5 deletions
diff --git a/src/network/connection.cpp b/src/network/connection.cpp
index 0bc13a2f0..a99e5b145 100644
--- a/src/network/connection.cpp
+++ b/src/network/connection.cpp
@@ -1323,16 +1323,21 @@ void Connection::Disconnect()
putCommand(c);
}
-void Connection::Receive(NetworkPacket* pkt)
+bool Connection::Receive(NetworkPacket *pkt, u32 timeout)
{
+ /*
+ Note that this function can potentially wait infinitely if non-data
+ events keep happening before the timeout expires.
+ This is not considered to be a problem (is it?)
+ */
for(;;) {
- ConnectionEvent e = waitEvent(m_bc_receive_timeout);
+ ConnectionEvent e = waitEvent(timeout);
if (e.type != CONNEVENT_NONE)
LOG(dout_con << getDesc() << ": Receive: got event: "
<< e.describe() << std::endl);
switch(e.type) {
case CONNEVENT_NONE:
- throw NoIncomingDataException("No incoming data");
+ return false;
case CONNEVENT_DATA_RECEIVED:
// Data size is lesser than command size, ignoring packet
if (e.data.getSize() < 2) {
@@ -1340,7 +1345,7 @@ void Connection::Receive(NetworkPacket* pkt)
}
pkt->putRawPacket(*e.data, e.data.getSize(), e.peer_id);
- return;
+ return true;
case CONNEVENT_PEER_ADDED: {
UDPPeer tmp(e.peer_id, e.address, this);
if (m_bc_peerhandler)
@@ -1358,7 +1363,19 @@ void Connection::Receive(NetworkPacket* pkt)
"(port already in use?)");
}
}
- throw NoIncomingDataException("No incoming data");
+ return false;
+}
+
+void Connection::Receive(NetworkPacket *pkt)
+{
+ bool any = Receive(pkt, m_bc_receive_timeout);
+ if (!any)
+ throw NoIncomingDataException("No incoming data");
+}
+
+bool Connection::TryReceive(NetworkPacket *pkt)
+{
+ return Receive(pkt, 0);
}
void Connection::Send(session_t peer_id, u8 channelnum,
diff --git a/src/network/connection.h b/src/network/connection.h
index 057bd39f6..d7f1e0fe8 100644
--- a/src/network/connection.h
+++ b/src/network/connection.h
@@ -771,6 +771,7 @@ public:
bool Connected();
void Disconnect();
void Receive(NetworkPacket* pkt);
+ bool TryReceive(NetworkPacket *pkt);
void Send(session_t peer_id, u8 channelnum, NetworkPacket *pkt, bool reliable);
session_t GetPeerID() const { return m_peer_id; }
Address GetPeerAddress(session_t peer_id);
@@ -803,6 +804,8 @@ protected:
UDPSocket m_udpSocket;
MutexedQueue<ConnectionCommand> m_command_queue;
+ bool Receive(NetworkPacket *pkt, u32 timeout);
+
void putEvent(ConnectionEvent &e);
void TriggerSend();
diff --git a/src/network/networkpacket.cpp b/src/network/networkpacket.cpp
index 22c035c5b..4d531b611 100644
--- a/src/network/networkpacket.cpp
+++ b/src/network/networkpacket.cpp
@@ -66,6 +66,15 @@ void NetworkPacket::putRawPacket(u8 *data, u32 datasize, session_t peer_id)
memcpy(m_data.data(), &data[2], m_datasize);
}
+void NetworkPacket::clear()
+{
+ m_data.clear();
+ m_datasize = 0;
+ m_read_offset = 0;
+ m_command = 0;
+ m_peer_id = 0;
+}
+
const char* NetworkPacket::getString(u32 from_offset)
{
checkReadOffset(from_offset, 0);
diff --git a/src/network/networkpacket.h b/src/network/networkpacket.h
index a8b741374..fc8617651 100644
--- a/src/network/networkpacket.h
+++ b/src/network/networkpacket.h
@@ -35,6 +35,7 @@ public:
~NetworkPacket();
void putRawPacket(u8 *data, u32 datasize, session_t peer_id);
+ void clear();
// Getters
u32 getSize() const { return m_datasize; }