Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: a83229cd092d1db7c6fa575bdb35ec8615c19dd8 (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
/*
   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__

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

#include <list>
#include <string>

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

namespace core 
{

/// Network server
class NetServer
{
public:
        NetServer(std::string const host, unsigned int const port);
        ~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 -------------------------------------------------- */

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

	/// transmit data to clients
	void transmit();

	/// broadcast network data to all clients
	void broadcast(std::string const & message, Player *ignore_player=0);

	/// send network data to one client
	void send(NetClient * client, std::string const & message);

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

 	std::list<NetClient *>	clients;
	
protected:
	/// 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);

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

	fd_set			serverset;
	int			fdmax;

	char			recbuf[FRAMESIZE];
};

}

#endif // __INCLUDED_CORE_NETSERVER_H__