std::shared_ptr
[dcpomatic.git] / src / lib / server.h
1 /*
2     Copyright (C) 2015 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 #ifndef DCPOMATIC_SERVER_H
22 #define DCPOMATIC_SERVER_H
23
24 #include <boost/thread.hpp>
25 #include <boost/asio.hpp>
26 #include <boost/thread/condition.hpp>
27 #include <boost/noncopyable.hpp>
28 #include <string>
29
30 class Socket;
31
32 class Server : public boost::noncopyable
33 {
34 public:
35         explicit Server (int port, int timeout = 30);
36         virtual ~Server ();
37
38         virtual void run ();
39         void stop ();
40
41 protected:
42         boost::mutex _mutex;
43         bool _terminate;
44
45 private:
46         virtual void handle (std::shared_ptr<Socket> socket) = 0;
47
48         void start_accept ();
49         void handle_accept (std::shared_ptr<Socket>, boost::system::error_code const &);
50
51         boost::asio::io_service _io_service;
52         boost::asio::ip::tcp::acceptor _acceptor;
53         int _timeout;
54 };
55
56 #endif