diff options
author | Stijn Buys <ingar@osirion.org> | 2008-02-14 18:04:25 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-02-14 18:04:25 +0000 |
commit | 715d0c3952a3a1d59b64074e472d0a9a3b414351 (patch) | |
tree | a5d0ddd0613caaf4f9fe01f9a3bd34e823651ad5 /src/core/netclient.cc | |
parent | 83e8023c5e46635753a609329cf9805a3520001e (diff) |
dedicated server accepts incoming connections
Diffstat (limited to 'src/core/netclient.cc')
-rw-r--r-- | src/core/netclient.cc | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/core/netclient.cc b/src/core/netclient.cc new file mode 100644 index 0000000..2df024b --- /dev/null +++ b/src/core/netclient.cc @@ -0,0 +1,90 @@ +/* + net/netclient.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include <iostream> +#include <sstream> + +#include "sys/sys.h" +#include "net/net.h" +#include "core/netclient.h" + +namespace core +{ + +NetClient::NetClient(int clientfd, std::string host, int port) : + net::TCPClient(clientfd), + client_host(host) +{ + std::ostringstream osstream; + osstream << host << ":" << port; + client_player.name = osstream.str(); + client_player.id = (unsigned int) clientfd; + + client_host = host; + client_port = port; + //con_debug << "NetClient " << fd() << ": starting." << std::endl; +} + +NetClient::~NetClient() +{ +} + +std::string NetClient::host() const +{ + return client_host; +} + +int NetClient::port() const +{ + return client_port; +} + +Player &NetClient::player() +{ + return client_player; +} + +bool NetClient::has_messages() const { + return (recvq.size() > 0 ); +} + +void NetClient::retreive(std::string & message) { + if (recvq.size() > 0 ) { + message.assign(recvq.front()); + recvq.pop_front(); + } else { + message.clear(); + } +} + +// receive data and decode it into lines +void NetClient::receive() +{ + std::string datablock; + net::TCPClient::receive(datablock); + + 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); + messageblock.clear(); + } + } else { + if (messageblock.size() < net::FRAMESIZE) { + messageblock.append(datablock.substr(0,1)); + } else { + con_warn << "NetClient " << fd() << ": message block overflow" << std::endl; + messageblock.clear(); + } + } + datablock.erase(0,1); + } + datablock.clear(); +} + +} |