Fix typo in log message.
[dcpomatic.git] / src / lib / server.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "server.h"
23 #include "dcpomatic_socket.h"
24
25
26 #include "i18n.h"
27
28
29 using std::make_shared;
30 using std::shared_ptr;
31
32
33 Server::Server (int port, int timeout)
34         : _terminate (false)
35         , _acceptor (_io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port))
36         , _timeout (timeout)
37 {
38
39 }
40
41
42 Server::~Server ()
43 {
44         {
45                 boost::mutex::scoped_lock lm (_mutex);
46                 _terminate = true;
47         }
48
49         _acceptor.close ();
50         stop ();
51 }
52
53
54 void
55 Server::run ()
56 {
57         start_accept ();
58         _io_service.run ();
59 }
60
61
62 void
63 Server::start_accept ()
64 {
65         {
66                 boost::mutex::scoped_lock lm (_mutex);
67                 if (_terminate) {
68                         return;
69                 }
70         }
71
72         auto socket = make_shared<Socket>(_timeout);
73         _acceptor.async_accept (socket->socket (), boost::bind (&Server::handle_accept, this, socket, boost::asio::placeholders::error));
74 }
75
76
77 void
78 Server::handle_accept (shared_ptr<Socket> socket, boost::system::error_code const & error)
79 {
80         if (error) {
81                 return;
82         }
83
84         handle (socket);
85         start_accept ();
86 }
87
88
89 void
90 Server::stop ()
91 {
92         _io_service.stop ();
93 }