Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-03-23 18:12:56 +0000
committerStijn Buys <ingar@osirion.org>2008-03-23 18:12:56 +0000
commit22e136d88817b64ec904a7e7232a2cf9e80e74bd (patch)
treeea369bba78930ff55325412760d14102d326f85b /src/core/netclient.cc
parent8485d43feca5597c4b412c6912aadcd9586e3cde (diff)
improved network connection handling, keep alive and time out
Diffstat (limited to 'src/core/netclient.cc')
-rw-r--r--src/core/netclient.cc30
1 files changed, 24 insertions, 6 deletions
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();
}
}