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
|
/*
Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <libcxml/cxml.h>
#include "server_finder.h"
#include "exceptions.h"
#include "util.h"
#include "config.h"
#include "cross.h"
#include "ui_signaller.h"
using std::string;
using std::stringstream;
using boost::shared_ptr;
using boost::scoped_array;
ServerFinder::ServerFinder ()
: _broadcast_thread (0)
, _listen_thread (0)
, _terminate (false)
{
_broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_thread, this));
_listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this));
}
ServerFinder::~ServerFinder ()
{
{
boost::mutex::scoped_lock lm (_mutex);
_terminate = true;
}
if (_broadcast_thread && _broadcast_thread->joinable ()) {
_broadcast_thread->join ();
}
delete _broadcast_thread;
if (_listen_thread && _listen_thread->joinable ()) {
_listen_thread->join ();
}
delete _listen_thread;
}
void
ServerFinder::broadcast_thread ()
{
boost::system::error_code error;
boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket (io_service);
socket.open (boost::asio::ip::udp::v4(), error);
if (error) {
throw NetworkError ("failed to set up broadcast socket");
}
socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
socket.set_option (boost::asio::socket_base::broadcast (true));
boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
while (1) {
boost::mutex::scoped_lock lm (_mutex);
if (_terminate) {
socket.close (error);
return;
}
string data = DCPOMATIC_HELLO;
socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
lm.unlock ();
dcpomatic_sleep (10);
}
}
void
ServerFinder::listen_thread ()
{
while (1) {
{
/* See if we need to stop */
boost::mutex::scoped_lock lm (_mutex);
if (_terminate) {
return;
}
}
shared_ptr<Socket> sock (new Socket (10));
try {
sock->accept (Config::instance()->server_port_base() + 1);
} catch (std::exception& e) {
continue;
}
uint32_t length = sock->read_uint32 ();
scoped_array<char> buffer (new char[length]);
sock->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
stringstream s (buffer.get());
shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
xml->read_stream (s);
ui_signaller->emit (boost::bind (boost::ref (ServerFound), ServerDescription (
sock->socket().remote_endpoint().address().to_string (),
xml->number_child<int> ("Threads")
)));
}
}
|