Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ba8510dd84fcd480a26bd6e33cbf8eb063d465a3 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
   net/netserver.h
   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 "core/gameserver.h"
#include "core/netclient.h"
#include "core/netserver.h"
#include "core/cvar.h"
#include "core/func.h"
#include "core/core.h"

namespace core
{

NetServer::NetServer(std::string const host, unsigned int const port) :
                net::TCPServer(host, port)
{
	//con_print << "Initializing network server..." << std::endl;
	FD_ZERO(&serverset);
	// add the listening socket to the file descriptor set
	FD_SET(fd(), &serverset);

	fdmax=fd();
}

NetServer::~NetServer()
{
	con_print << "Shutting down network server..." << std::endl;

	// delete all clients
	std::list<NetClient *>:: iterator it;
	for (it = clients.begin(); it != clients.end(); it++) {
		// notify the game server
		server()->player_disconnect((*it)->player());

		con_print << "  " << (*it)->host() << ":" << (*it)->port() << " disconnected.\n";

		delete (*it);
	}
	clients.clear();
}

void NetServer::client_connect(int const clientfd, std::string const host, int const port)
{
	NetClient *client = new NetClient(clientfd, host, port);
	if (client->error()) {
		con_warn << client->host() << ":" << client->port() << " connection failed!\n";
		delete(client);
		return;
	}

	clients.push_back(client);
	FD_SET(client->fd(), &serverset);
	
	con_print << client->host() << ":" << client->port() << " connected.\n";

	// notify the game server
	server()->player_connect(client->player());

}

// remove disconnected clients
void NetServer::reap()
{
        std::list<NetClient *>:: iterator it;
        for (it = clients.begin(); it != clients.end(); it++) {
                NetClient *client = *it;
                if (client->error()) {
			FD_CLR(client->fd(), &serverset);
			
			// notify the game server
			server()->player_disconnect((*it)->player());
			con_print << client->host() << ":" << client->port() << " disconnected.\n";

			// remove the client
                        clients.erase(it);
                        delete client;
                        it=clients.begin();	
			
                }
        }
}


void NetServer::frame(float seconds) {
	if (error())
		return;

	timeval timeout;
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	fd_set readset = serverset;

	int nb = select(fdmax+1, &readset, NULL, NULL, &timeout);
	if (nb == 0)
		return;

	if (nb == -1) {
		con_error << "Network error on select()" << std::endl;
		//perror("select");
		abort();
	}
	
	// accept incoming connections
	if(FD_ISSET(this->fd(), &readset)) {
		accept();
	}

	// get messages from clients
	for (std::list<NetClient *>::iterator it = clients.begin(); it != clients.end(); it++) {
		NetClient *client = *it;
		if (client->fd() > fdmax)
			fdmax = client->fd();
		if (FD_ISSET(client->fd(), &readset) && !client->error()) {
			client->receive();
			while (client->has_messages()) {
				std::string message;
				client->retreive(message);
				parse_incoming_message(client, message);
			}
		}
	}
	
	// remove dead connections
	reap();
}

void NetServer::send(NetClient * client, std::string const & message)
{
	client->send(message);
}

// send a message to all clients
void NetServer::broadcast(std::string const & message, int ignorefd) 
{
	//std::cout << "NetServer: " << osstream.str();
	for (std::list<NetClient *>::iterator it = clients.begin(); it != clients.end(); it++) {
		NetClient *client = *it;
		if (!client->error() && client->fd() != ignorefd)
			client->send(message);
	}
}

// find the client corresponding to a player id
NetClient *NetServer::find_client(Player const *player)
{
	for (std::list<NetClient *>::iterator it = clients.begin(); it != clients.end(); it++) {
		if ((*it)->fd() == (int) player->id()) {
			return (*it);
		}
	}
	return 0;
}

// parse server messages
/**
 * The following incoming messages are parsed;
 *
 * disconnect
 * help
 * list_players
 * name
 * say <text>
 * 
 */
void NetServer::parse_incoming_message(NetClient *client, const std::string & message) 
{
	std::stringstream msgstream(message);
	
	std::string command;
	msgstream >> command;
	
	// disconnect
	if (command == "disconnect") {
		client->abort();
		return;
	}

	// say
	if (command == "say") {		
		if (message.size() > command.size()+1) {
			std::ostringstream osstream;
			osstream << "msg public " << client->player()->name() << " " << message.substr(command.size()+1) << "\n";
			broadcast(osstream.str());
			con_print << client->player()->name() << " " << message.substr(command.size()+1) << std::endl;
		}
		return;
	}
	
	// name is an alias for set name
	if (command == "name") {
		std::string name;
		if (msgstream >> name) {
			if (name.size() > 16) 
				name = name.substr(0,16);
			if (name != client->player()->name()) {
				std::ostringstream osstream;
				osstream << "msg info " << client->player()->name() << " renamed to " << name << "\n";
				broadcast(osstream.str());				
				con_print << client->player()->name() << " renamed to " << name << std::endl;
				client->player()->player_name = name;	
			}
		}
		return;
	}
	
	if (command == "list_players") {
		std::ostringstream osstream;
		for (std::list<NetClient *>::iterator it = clients.begin(); it != clients.end(); it++) {
			osstream << "msg info " << (*it)->player()->name() << " " << (*it)->host() << ":" << (*it)->port() << "\n";
		}
		osstream << "msg info " << clients.size() << " connected players\n" << std::endl;
		send(client, osstream.str());
	}

	if (command == "help") {
		send(client, "msg info Available commands:\n");
		send(client, "msg info  help             - shows this help message\n");
		send(client, "msg info  name nickname    - changes your nickname\n");
		send(client, "msg info  say text         - say something on the public channel\n");
		send(client, "msg info  list_players     - shows a list of connected players\n");
		send(client, "msg info  disconnect       - disconnect\n");
	}

	// execute game functions
	Func *function = Func::find(command);
	if (function ) {
		std::string args;
		char c;
		if (msgstream >> args)
			while (msgstream >> c)
				args += c;
		if (function ->flags() && Func::Game) {	
			function->exec(client->player(), args);
		} else {
			// FIXME instant rcon
			function->exec(args);
		}
	}

}

}