Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 7b5d5545baead40fe92090252f204162fa3b3c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
   net/netclient.cc
   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;
	osstream << "Player" << clientfd;
	client_player.player_name = osstream.str();
	client_player.player_id = (unsigned int) clientfd;

	client_host = host;
	client_port = port;
}

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();
}

}