initial commit, lordsawar source, slightly modified
[lordsawar] / src / network-server.cpp
1 // Copyright (C) 2008 Ole Laursen
2 // Copyright (C) 2008 Ben Asselstine
3 //
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.
8 //
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.
13 //
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 
17 //  02110-1301, USA.
18
19 #include "network-server.h"
20 #include <iostream>
21 #include <sigc++/bind.h>
22 #include <gnet.h>
23
24 #include "network-common.h"
25 #include "network-connection.h"
26
27 NetworkServer::NetworkServer()
28 {
29   server = 0;
30 }
31
32 NetworkServer::~NetworkServer()
33 {
34   //for (std::list<NetworkConnection *>::iterator i = connections.begin(),
35          //end = connections.end(); i != end; ++i)
36     //delete *i;
37   
38   if (server)
39     gnet_server_delete(server);
40 }
41
42 static void
43 server_helper(GServer* server, GConn* conn, gpointer user_data)
44 {
45   static_cast<NetworkServer *>(user_data)->gotClientConnection(conn);
46 }
47
48 void NetworkServer::startListening(int port)
49 {
50   // listen on all interfaces
51   GInetAddr *iface = 0;
52
53   server = gnet_server_new(iface, port, &server_helper, this);
54   if (!server)
55     ; // FIXME: report error
56 }
57
58 void NetworkServer::send(void *c, MessageType type, const std::string &payload)
59 {
60   NetworkConnection *conn = static_cast<NetworkConnection *>(c);
61   if (type == MESSAGE_TYPE_SENDING_MAP)
62     conn->sendFile(type, payload);
63   else
64     conn->send(type, payload);
65 }
66
67
68 void NetworkServer::gotClientConnection(GConn* c)
69 {
70   if (c) {
71     NetworkConnection *conn = new NetworkConnection(c);
72     connections.push_back(conn);
73
74     conn->connection_lost.connect(
75       sigc::bind(sigc::mem_fun(connection_lost, &sigc::signal<void, void *>::emit), conn));
76
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));
81
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));
85   }
86   else
87     ; // FIXME: report error
88 }
89
90 void NetworkServer::onConnectionLost(NetworkConnection *conn)
91 {
92   delete conn;
93 }
94   
95 bool NetworkServer::isListening()
96 {
97   if (server)
98     return true;
99   return false;
100 }