Split EncodeServer into that and Server.
[dcpomatic.git] / src / lib / server.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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 2 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "server.h"
21 #include "dcpomatic_socket.h"
22
23 #include "i18n.h"
24
25 using boost::shared_ptr;
26
27 Server::Server (int port)
28         : _terminate (false)
29         , _acceptor (_io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port))
30 {
31
32 }
33
34 Server::~Server ()
35 {
36         boost::mutex::scoped_lock lm (_mutex);
37         _terminate = true;
38         _io_service.stop ();
39 }
40
41 void
42 Server::run ()
43 {
44         start_accept ();
45         _io_service.run ();
46 }
47
48 void
49 Server::start_accept ()
50 {
51         {
52                 boost::mutex::scoped_lock lm (_mutex);
53                 if (_terminate) {
54                         return;
55                 }
56         }
57
58         shared_ptr<Socket> socket (new Socket);
59         _acceptor.async_accept (socket->socket (), boost::bind (&Server::handle_accept, this, socket, boost::asio::placeholders::error));
60 }
61
62 void
63 Server::handle_accept (shared_ptr<Socket> socket, boost::system::error_code const & error)
64 {
65         if (error) {
66                 return;
67         }
68
69         handle (socket);
70         start_accept ();
71 }