From 22e136d88817b64ec904a7e7232a2cf9e80e74bd Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 23 Mar 2008 18:12:56 +0000 Subject: improved network connection handling, keep alive and time out --- src/core/netclient.cc | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/core/netclient.cc') diff --git a/src/core/netclient.cc b/src/core/netclient.cc index c7186f5..c910e26 100644 --- a/src/core/netclient.cc +++ b/src/core/netclient.cc @@ -9,6 +9,7 @@ #include "sys/sys.h" #include "core/net.h" +#include "core/application.h" namespace core { @@ -38,6 +39,9 @@ NetClient::NetClient(std::string host, int port) : sendq.clear(); messageblock.clear(); + client_keepalive = application()->time(); + client_timeout = application()->time(); + client_error = false; } @@ -85,12 +89,15 @@ void NetClient::receive(char *data) std::string datablock; datablock.assign(data); + if (!datablock.size()) + return; + while(datablock.size() > 0 ) { // scan the datablock for enters if (datablock[0] == '\n' || datablock[0] == '\r') { // TODO detect "begin binary block" message for zlib compression if (messageblock.size() > 0 ) { - recvq.push_back(messageblock); + recvq.push_back(messageblock); messageblock.clear(); } } else { @@ -103,6 +110,8 @@ void NetClient::receive(char *data) } datablock.erase(0,1); } + + client_timeout = application()->time(); } void NetClient::send(std::string const &msg) @@ -112,16 +121,23 @@ void NetClient::send(std::string const &msg) void NetClient::transmit(int serverfd) { - if (sendq.size() >= BLOCKSIZE) { - con_warn << "Outgoing message exceeds " << BLOCKSIZE << " bytes!\n"; - sendq.clear(); - return; + + if (!sendq.size()) { + if (client_keepalive + NETTIMEOUT/2 < application()->time()) { + sendq.assign("ping\n"); + } else { + return; + } + } else if (sendq.size() >= FRAMESIZE) { + con_warn << "Outgoing message exceeds " << FRAMESIZE << " bytes!\n"; + //sendq.clear(); + //return; } ssize_t bytes_sent = 0; while (sendq.size() && !error()) { - bytes_sent = sendto(serverfd, sendq.c_str(), sendq.size(), 0, + bytes_sent = ::sendto(serverfd, sendq.c_str(), sendq.size(), 0, (struct sockaddr *)&client_addr, sizeof(client_addr)); if (bytes_sent < 0) { @@ -132,6 +148,8 @@ void NetClient::transmit(int serverfd) sendq.erase(0, bytes_sent); } sendq.clear(); + + client_keepalive = application()->time(); } } -- cgit v1.2.3