Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: dc5dc2742cf5cb53324303782cf673bc531df3cd (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
   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 "core/net.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>
#endif

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

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

namespace core
{

/**
 * @brief client-side network methods
 * This class contains the necessary methods for the client-side
 * of network communication. It handles both sending and receiving.
 **/
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();

	/// 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_client_update(Entity *entity);

	/// send an entity request
	void send_entity_request(Entity *entity);
	
	/// send a local chat message
	void send_say(std::string const &text);
	
	/// send a public chat message
	void send_shout(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);

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

	/// send an info request
	void send_info_request(Info *info);
	
	/// send an inventory udpate request
	void send_inventory_request(Entity *entity);

	/// 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;

	/// return the current game time
	inline unsigned long timestamp() const {
		return connection_timestamp;
	}

protected:
	/// send a ping reply
	void send_ping_reply(unsigned long timestamp);

	/// 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];

	unsigned long		connection_timestamp;
};

}

#endif // __INCLUDED_CORE_NETCONNECTION_H__