From 09b8df5200de1c8c20ea2856a8c6aa76b0811bd1 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 11 Sep 2009 23:13:23 +0200 Subject: Connection: Allow setting a receive limit --- src/Net/Connection.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/Net/Connection.cpp') diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index f1beb35..256bbfe 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -35,7 +35,7 @@ Connection::~Connection() { void Connection::handleHandshake(const boost::system::error_code& error) { if(error) { - application->logf("Error: %s", error.message().c_str()); + application->logf(Core::Logger::LOG_NETWORK, "Error: %s", error.message().c_str()); // TODO Error handling doDisconnect(); @@ -64,7 +64,7 @@ void Connection::handleShutdown(const boost::system::error_code& error) { boost::lock_guard lock(connectionLock); if(error) { - application->logf(Core::Logger::LOG_VERBOSE, "Shutdown error: %s", error.message().c_str()); + application->logf(Core::Logger::LOG_NETWORK, Core::Logger::LOG_VERBOSE, "Shutdown error: %s", error.message().c_str()); } _setState(DISCONNECTED); @@ -90,11 +90,17 @@ void Connection::handleHeaderReceive(const boost::shared_array & header = *reinterpret_cast(data.get()); } - if(header.length == 0) { + boost::uint32_t length = ntohl(header.length); + + if(length == 0) { receiveSignal.emit(boost::shared_ptr(new Packet(ntohs(header.requestId)))); enterReceiveLoop(); } + else if(length > receiveLimit) { + application->log(Core::Logger::LOG_NETWORK, Core::Logger::LOG_WARNING, "Packet size limit exceeded. Disconnecting."); + doDisconnect(); + } else { rawReceive(ntohl(header.length), boost::bind(&Connection::handleDataReceive, thisPtr.lock(), _1)); } @@ -115,7 +121,7 @@ void Connection::handleRead(const boost::system::error_code& error, std::size_t if(error == boost::system::errc::operation_canceled) return; - application->logf(Core::Logger::LOG_DEFAULT, "Read error: %s", error.message().c_str()); + application->logf(Core::Logger::LOG_NETWORK, "Read error: %s", error.message().c_str()); // TODO Error doDisconnect(); @@ -176,7 +182,7 @@ void Connection::rawReceive(std::size_t length, const boost::function1logf(Core::Logger::LOG_VERBOSE, "Write error: %s", error.message().c_str()); + application->logf(Core::Logger::LOG_NETWORK, Core::Logger::LOG_VERBOSE, "Write error: %s", error.message().c_str()); { boost::unique_lock lock(connectionLock); -- cgit v1.2.3