Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: cb04ce2eb84f4bfdb939ba46196fdb2cb4c58df9 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
   net/netconnection.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#ifndef __INCLUDED_CORE_NETCONNECTION_H__
#define __INCLUDED_CORE_NETCONNECTION_H__

#include <unistd.h>
#include <errno.h>

#ifndef _WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <sys/select.h>
#include <netdb.h>
#else
#include <windows.h>
#endif

#include <string>
#include <deque>
#include <map>

#include "core/entity.h"
#include "core/net.h"

namespace core
{

/// a client to server connection
class NetConnection
{
public:
	NetConnection();
	virtual ~NetConnection();
	
	/// connect to a remote host
	virtual void connect(std::string const &to_host, int to_port);
	
	/// disconnect from a remote host
	virtual void disconnect();

	/// process pending incoming messages
	void frame(float seconds);

	/// send a connect message to the remote server
	void send_connect();

	/// send a player info message to the remote server
	void send_playerinfo();

	/// send a client update message to the remote server
	void send_clientupdate(Entity *entity);

	/// send a chat message
	void send_say(std::string const &text);

	/// send a private message
	void send_private_message(std::string const &text);

	/// send a command line to the remote server
	void send_command(std::string const &cmdline);

	/// transmit messages in the send queue to the remote server
	void transmit();

	void abort();

	inline int fd() const { return connection_fd; }
	
	inline std::string host() const { return connection_host; }

	inline int port() const { return connection_port; }

	inline bool valid() const { return (connection_fd != -1); }

	inline bool invalid() const { return (connection_fd == -1); }

	inline bool error() const { return connection_error; }

	inline bool connected() const {	return ((connection_fd != -1) && !connection_error); }

	enum State {Connecting=0, Pending=1, Connected=2};

	inline State state() const { return connection_state; }
	
	State connection_state;

protected:
	/// add a raw network message to the send queue
	void send_raw(std::string const &msg);

	/// receive incoming data and store messages
	void receive();

	/// return true if there are incoming messages
	bool has_messages() const;

	/// retreive an incoming message
	void retreive(std::string & message);

	/// parse an incoming message
	void parse_incoming_message(const std::string & message);
	
private:
	std::string		messageblock;	
	std::deque<std::string>	recvq;
	std::string		sendq;
	fd_set			clientset;
	
	float			connection_timeout;
	float			connection_keepalive;
	int			connection_fd;
	bool			connection_error;
	std::string		connection_host;
	int			connection_port;
	struct sockaddr_in 	server_addr;
	char			recvbuf[BLOCKSIZE];

	bool			receive_compressed;
	size_t			received_compressed_size;
	size_t			compressed_size;
	char			zrecvbuf[BLOCKSIZE];
};

}

#endif // __INCLUDED_CORE_NETCONNECTION_H__