Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: a7e75bdc47f88b2fd38c09bcfbfcc1cfada77f78 (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
/*
   core/netserver.h
   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_NETSERVER_H__
#define __INCLUDED_CORE_NETSERVER_H__

#include <list>
#include <string>

#include "core/net.h"
#include "core/netclient.h"
#include "core/player.h"
#include "core/info.h"

namespace core
{

/**
 * Handles server-side network messages
 * */
class NetServer
{
public:
	/// type definition for a list of network clients
	typedef std::list<NetClient *> Clients;

	/// create a new network server, listening on host:port
	NetServer(std::string const host, unsigned int const port);

	/// disconnect clients and shutdown the network server
	~NetServer();

	/*----- inspectors ------------------------------------------------ */

	/// Returns true if the TCP server has a valid file descriptor
	inline bool valid() const {
		return netserver_fd != -1;
	}

	/// Returns true if the TCP server has an invalid file descriptor
	inline bool invalid() const {
		return netserver_fd == -1;
	}

	/// Returns the error state of the TCP server
	inline bool error() const {
		return netserver_error;
	}

	/// Return the socket file descriptor
	inline int fd() const {
		return netserver_fd;
	}

	/*----- mutators -------------------------------------------------- */

	/// run a network server frame
	void frame(unsigned long timestamp);
	
	/// transmit pending packets to all clients
	void transmit();

	/// receive data from clients
	void receive();

	/// disconnect a client
	void send_disconnect(NetClient *client);

	/// find the client corresponding to a player
	NetClient *find_client(Player const *player);

	/// send a message on a specified channel
	void send_message(NetClient *client, const Message::Channel channel, const std::string & message);
	
	/// send a messagebox to a single client
	void send_messagebox(NetClient *client, const std::string & text, const std::string &label1, const std::string command1, const std::string &label2, const std::string command2);

protected:

	/// send a server frame marker
	void send_frame_marker(NetClient *client, unsigned long timestamp);

	/// send a create entity event
	void send_entity_create(NetClient *client, Entity *entity);

	/// broadcast a delete entity event
	void send_entity_delete(NetClient *client, Entity *entity);

	/// broadcast a update entity event
	void send_entity_update(NetClient *client, Entity *entity);

	/// send a zone update event
	void send_zone_update(NetClient *client, Zone *zone);

	/// send an update player information message
	void send_player_update(NetClient *client);

	/// send an update player reputation message
	void send_player_reputation(NetClient *client);

	/// send a general update player information message
	void send_player_update(NetClient *client, Player *player);
	
	/// send player disconnect information message
	void send_player_disconnect_info(NetClient *client, Player *player);

	/// send info types
	void send_infotypes(NetClient *client);
	
	/// send player an info record
	void send_info_update(NetClient *client, Info *info);
	
	/// send player an inventory update
	void send_inventory_update(NetClient *client, Entity *entity, const unsigned long timestamp);

	/// set the error state
	void abort();

	/// called when a new client connects
	NetClient *client_connect(std::string const host, int const port);

	/// called when a client has send the intial handshake
	void client_initialize(NetClient *client);

	/// remove terminated clients
	void reap();

	/// parse incoming client messages
	void parse_incoming_message(NetClient *client, const std::string & message);

	/// send a server frame to a single client
	void client_frame(NetClient *client, unsigned long timestamp);

private:
	bool			netserver_error;
	int			netserver_fd;
	struct sockaddr_in 	netserver_addr;

	fd_set			serverset;

	char			recbuf[FRAMESIZE];

	Clients			clients;
};

}

#endif // __INCLUDED_CORE_NETSERVER_H__