diff --git a/src/client/client.cpp b/src/client/client.cpp index d15c9608a..0c222b111 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -398,8 +398,6 @@ void Client::connect(const Address &address, const std::string &address_name, address.print(infostream); infostream << std::endl; - // Since we use TryReceive() a timeout here would be ineffective anyway - m_con->SetTimeoutMs(0); m_con->Connect(address); initLocalMapSaving(address, m_address_name, is_local_server); diff --git a/src/network/connection.h b/src/network/connection.h index a3665566e..bec6f98f0 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -48,7 +48,6 @@ class IConnection public: virtual ~IConnection() = default; - virtual void SetTimeoutMs(u32 timeout) = 0; virtual void Serve(Address bind_addr) = 0; virtual void Connect(Address address) = 0; virtual bool Connected() = 0; @@ -56,7 +55,6 @@ public: virtual void DisconnectPeer(session_t peer_id) = 0; virtual bool ReceiveTimeoutMs(NetworkPacket *pkt, u32 timeout_ms) = 0; - virtual void Receive(NetworkPacket *pkt) = 0; bool TryReceive(NetworkPacket *pkt) { return ReceiveTimeoutMs(pkt, 0); } diff --git a/src/network/mtp/impl.cpp b/src/network/mtp/impl.cpp index 2d4a99119..00535945e 100644 --- a/src/network/mtp/impl.cpp +++ b/src/network/mtp/impl.cpp @@ -1486,13 +1486,6 @@ bool Connection::ReceiveTimeoutMs(NetworkPacket *pkt, u32 timeout_ms) return false; } -void Connection::Receive(NetworkPacket *pkt) -{ - bool any = ReceiveTimeoutMs(pkt, m_bc_receive_timeout); - if (!any) - throw NoIncomingDataException("No incoming data"); -} - void Connection::Send(session_t peer_id, u8 channelnum, NetworkPacket *pkt, bool reliable) { diff --git a/src/network/mtp/impl.h b/src/network/mtp/impl.h index 0232ebc0d..cc1d4c2ed 100644 --- a/src/network/mtp/impl.h +++ b/src/network/mtp/impl.h @@ -249,13 +249,11 @@ public: void putCommand(ConnectionCommandPtr c); - void SetTimeoutMs(u32 timeout) { m_bc_receive_timeout = timeout; } void Serve(Address bind_addr); void Connect(Address address); bool Connected(); void Disconnect(); bool ReceiveTimeoutMs(NetworkPacket *pkt, u32 timeout_ms); - void Receive(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); @@ -317,7 +315,6 @@ private: // Backwards compatibility PeerHandler *m_bc_peerhandler; - u32 m_bc_receive_timeout = 0; bool m_shutting_down = false; }; diff --git a/src/network/networkexceptions.h b/src/network/networkexceptions.h index 2e9c2a6e8..1810106e5 100644 --- a/src/network/networkexceptions.h +++ b/src/network/networkexceptions.h @@ -51,12 +51,6 @@ public: InvalidIncomingDataException(const char *s) : BaseException(s) {} }; -class NoIncomingDataException : public BaseException -{ -public: - NoIncomingDataException(const char *s) : BaseException(s) {} -}; - } class SocketException : public BaseException diff --git a/src/server.cpp b/src/server.cpp index a78244e99..bad0e70d2 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -551,7 +551,6 @@ void Server::start() m_thread->stop(); // Initialize connection - m_con->SetTimeoutMs(30); m_con->Serve(m_bind_addr); // Start thread diff --git a/src/unittest/test_connection.cpp b/src/unittest/test_connection.cpp index 3c0043389..b7d751dc3 100644 --- a/src/unittest/test_connection.cpp +++ b/src/unittest/test_connection.cpp @@ -160,6 +160,9 @@ void TestConnection::testHelpers() void TestConnection::testConnectSendReceive() { + + constexpr u32 timeout_ms = 100; + /* Test some real connections @@ -210,13 +213,11 @@ void TestConnection::testConnectSendReceive() // Client should not have added client yet UASSERT(hand_client.count == 0); - try { - NetworkPacket pkt; - infostream << "** running client.Receive()" << std::endl; - client.Receive(&pkt); + NetworkPacket pkt; + infostream << "** running client.Receive()" << std::endl; + if (client.ReceiveTimeoutMs(&pkt, timeout_ms)) { infostream << "** Client received: peer_id=" << pkt.getPeerId() << ", size=" << pkt.getSize() << std::endl; - } catch (con::NoIncomingDataException &e) { } // Client should have added server now @@ -227,14 +228,14 @@ void TestConnection::testConnectSendReceive() sleep_ms(100); - try { - NetworkPacket pkt; - infostream << "** running server.Receive()" << std::endl; - server.Receive(&pkt); + NetworkPacket pkt1; + infostream << "** running server.Receive()" << std::endl; + if (server.ReceiveTimeoutMs(&pkt, timeout_ms)) { infostream << "** Server received: peer_id=" << pkt.getPeerId() - << ", size=" << pkt.getSize() - << std::endl; - } catch (con::NoIncomingDataException &e) { + << ", size=" << pkt.getSize() + << std::endl; + } + else { // No actual data received, but the client has // probably been connected } @@ -249,27 +250,23 @@ void TestConnection::testConnectSendReceive() //sleep_ms(50); while (client.Connected() == false) { - try { - NetworkPacket pkt; - infostream << "** running client.Receive()" << std::endl; - client.Receive(&pkt); + NetworkPacket pkt; + infostream << "** running client.Receive()" << std::endl; + if (client.TryReceive(&pkt)) { infostream << "** Client received: peer_id=" << pkt.getPeerId() << ", size=" << pkt.getSize() << std::endl; - } catch (con::NoIncomingDataException &e) { } sleep_ms(50); } sleep_ms(50); - try { - NetworkPacket pkt; - infostream << "** running server.Receive()" << std::endl; - server.Receive(&pkt); + NetworkPacket pkt2; + infostream << "** running server.Receive()" << std::endl; + if (server.ReceiveTimeoutMs(&pkt, timeout_ms)) { infostream << "** Server received: peer_id=" << pkt.getPeerId() - << ", size=" << pkt.getSize() - << std::endl; - } catch (con::NoIncomingDataException &e) { + << ", size=" << pkt.getSize() + << std::endl; } /* @@ -288,7 +285,7 @@ void TestConnection::testConnectSendReceive() NetworkPacket recvpacket; infostream << "** running server.Receive()" << std::endl; - server.Receive(&recvpacket); + UASSERT(server.ReceiveTimeoutMs(&recvpacket, timeout_ms)); infostream << "** Server received: peer_id=" << pkt.getPeerId() << ", size=" << pkt.getSize() << ", data=" << (const char*)pkt.getU8Ptr(0) @@ -338,14 +335,12 @@ void TestConnection::testConnectSendReceive() for (;;) { if (porting::getTimeMs() - timems0 > 5000 || received) break; - try { - NetworkPacket pkt; - client.Receive(&pkt); + NetworkPacket pkt; + if (client.ReceiveTimeoutMs(&pkt, timeout_ms)) { size = pkt.getSize(); peer_id = pkt.getPeerId(); recvdata = pkt.oldForgePacket(); received = true; - } catch (con::NoIncomingDataException &e) { } sleep_ms(10); }