1 // Copyright (C) 2008 Ole Laursen
2 // Copyright (C) 2008, 2011, 2015, 2017 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 <stdio.h> //benfix
20 #include "network-server.h"
21 #include <iostream>
22 #include <sigc++/bind.h>
23 #include <giomm.h>
24 
25 #include "network-common.h"
26 #include "network-connection.h"
27 #include "connection-manager.h"
28 
NetworkServer()29 NetworkServer::NetworkServer()
30 {
31   connections.clear();
32 }
33 
~NetworkServer()34 NetworkServer::~NetworkServer()
35 {
36   for (auto i: connections)
37     i->tear_down_connection();
38   connections.clear ();
39   if (server)
40     {
41       if (server->is_active())
42         {
43         server->stop();
44         }
45     }
46 }
47 
startListening(int port)48 void NetworkServer::startListening(int port)
49 {
50   server = Gio::SocketService::create();
51 
52   server->signal_incoming().connect(sigc::hide(sigc::mem_fun(*this, &NetworkServer::gotClientConnection)));
53   try
54     {
55       server->add_inet_port (port);
56     }
57   catch(const Glib::Exception &ex)
58     {
59       server->stop();
60       port_in_use.emit(port);
61       return;
62     }
63 
64   server->start();
65 }
66 
send(void * c,int type,const Glib::ustring & payload)67 void NetworkServer::send(void *c, int type, const Glib::ustring &payload)
68 {
69   NetworkConnection *conn = static_cast<NetworkConnection *>(c);
70   conn->send(type, payload);
71 }
72 
sendFile(void * c,int type,const Glib::ustring & payload)73 void NetworkServer::sendFile(void *c, int type, const Glib::ustring &payload)
74 {
75   NetworkConnection *conn = static_cast<NetworkConnection *>(c);
76   conn->sendFile(type, payload);
77 }
78 
gotClientConnection(const Glib::RefPtr<Gio::SocketConnection> & c)79 bool NetworkServer::gotClientConnection(const Glib::RefPtr<Gio::SocketConnection>& c)
80 {
81   if (c)
82     {
83       c->reference();
84       NetworkConnection *conn = ConnectionManager::create_connection(c);
85       connections.push_back(conn);
86 
87       conn->connection_lost.connect
88         (sigc::bind(sigc::mem_fun
89                     (connection_lost,
90                      &sigc::signal<void, void *>::emit), conn));
91 
92       connection_made.emit(conn);
93 
94       conn->got_message.connect
95         (sigc::bind<0>(sigc::mem_fun(got_message,
96                                      &sigc::signal<bool, void *,
97                                      int, Glib::ustring>::emit), conn));
98 
99       return true;
100     }
101   return false;
102 }
103 
onConnectionLost(void * conn)104 void NetworkServer::onConnectionLost(void *conn)
105 {
106   NetworkConnection *c = reinterpret_cast<NetworkConnection*>(conn);
107   if (std::find (connections.begin(), connections.end(), c) != connections.end())
108     connections.remove(c);
109 }
110 
isListening()111 bool NetworkServer::isListening()
112 {
113   return server->is_active();
114 }
115 
stop()116 void NetworkServer::stop()
117 {
118   server->close();
119   return server->stop();
120 }
121 
get_hostname(void * c)122 Glib::ustring NetworkServer::get_hostname(void *c)
123 {
124   NetworkConnection *conn = static_cast<NetworkConnection *>(c);
125   return conn->get_peer_hostname();
126 }
127 
is_local_connection(void * conn)128 bool NetworkServer::is_local_connection(void *conn)
129 {
130   if (get_hostname(conn) == "127.0.0.1")
131     return true;
132   return false;
133 }
134