1 // Copyright (C) 2008 Ole Laursen
2 // Copyright (C) 2008 Ben Asselstine
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Library General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include "network-server.h"
21 #include <sigc++/bind.h>
24 #include "network-common.h"
25 #include "network-connection.h"
27 NetworkServer::NetworkServer()
32 NetworkServer::~NetworkServer()
34 //for (std::list<NetworkConnection *>::iterator i = connections.begin(),
35 //end = connections.end(); i != end; ++i)
39 gnet_server_delete(server);
43 server_helper(GServer* server, GConn* conn, gpointer user_data)
45 static_cast<NetworkServer *>(user_data)->gotClientConnection(conn);
48 void NetworkServer::startListening(int port)
50 // listen on all interfaces
53 server = gnet_server_new(iface, port, &server_helper, this);
55 ; // FIXME: report error
58 void NetworkServer::send(void *c, MessageType type, const std::string &payload)
60 NetworkConnection *conn = static_cast<NetworkConnection *>(c);
61 if (type == MESSAGE_TYPE_SENDING_MAP)
62 conn->sendFile(type, payload);
64 conn->send(type, payload);
68 void NetworkServer::gotClientConnection(GConn* c)
71 NetworkConnection *conn = new NetworkConnection(c);
72 connections.push_back(conn);
74 conn->connection_lost.connect(
75 sigc::bind(sigc::mem_fun(connection_lost, &sigc::signal<void, void *>::emit), conn));
77 conn->connected.connect(
78 sigc::bind(sigc::mem_fun(connection_made, &sigc::signal<void, void *>::emit), conn));
79 conn->got_message.connect(
80 sigc::bind<0>(sigc::mem_fun(got_message, &sigc::signal<void, void *, MessageType, std::string>::emit), conn));
82 // bind ourselves too, so we can delete the connection
83 conn->connection_lost.connect(
84 sigc::bind(sigc::mem_fun(this, &NetworkServer::onConnectionLost), conn));
87 ; // FIXME: report error
90 void NetworkServer::onConnectionLost(NetworkConnection *conn)
95 bool NetworkServer::isListening()