Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
tcp_server.hpp
1 // MIT License
2 //
3 // Copyright (c) 2016-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <atomic>
26 #include <cstdint>
27 #include <list>
28 #include <memory>
29 #include <mutex>
30 #include <string>
31 
32 #include <tacopie/network/io_service.hpp>
33 #include <tacopie/network/tcp_client.hpp>
34 #include <tacopie/network/tcp_socket.hpp>
35 #include <tacopie/utils/typedefs.hpp>
36 
37 #define __TACOPIE_CONNECTION_QUEUE_SIZE 1024
38 
39 namespace tacopie {
40 
45 class tcp_server {
46 public:
48  tcp_server(void);
50  ~tcp_server(void);
51 
53  tcp_server(const tcp_server&) = delete;
55  tcp_server& operator=(const tcp_server&) = delete;
56 
57 public:
63  bool operator==(const tcp_server& rhs) const;
64 
70  bool operator!=(const tcp_server& rhs) const;
71 
72 public:
79  typedef std::function<bool(const std::shared_ptr<tcp_client>&)> on_new_connection_callback_t;
80 
88  void start(const std::string& host, std::uint32_t port, const on_new_connection_callback_t& callback = nullptr);
89 
96  void stop(bool wait_for_removal = false, bool recursive_wait_for_removal = true);
97 
101  bool is_running(void) const;
102 
103 public:
107  tcp_socket& get_socket(void);
108 
112  const tcp_socket& get_socket(void) const;
113 
114 public:
118  const std::shared_ptr<tacopie::io_service>& get_io_service(void) const;
119 
120 public:
124  const std::list<std::shared_ptr<tacopie::tcp_client>>& get_clients(void) const;
125 
126 private:
132  void on_read_available(fd_t fd);
133 
140  void on_client_disconnected(const std::shared_ptr<tcp_client>& client);
141 
142 private:
147  std::shared_ptr<io_service> m_io_service;
148 
149  //1
152  tacopie::tcp_socket m_socket;
153 
157  std::atomic<bool> m_is_running = ATOMIC_VAR_INIT(false);
158 
162  std::list<std::shared_ptr<tacopie::tcp_client>> m_clients;
163 
167  std::mutex m_clients_mtx;
168 
172  on_new_connection_callback_t m_on_new_connection_callback;
173 };
174 
175 } // namespace tacopie
tcp_server(void)
ctor
tcp_server & operator=(const tcp_server &)=delete
assignment operator
void stop(bool wait_for_removal=false, bool recursive_wait_for_removal=true)
Definition: tcp_socket.hpp:38
Definition: tcp_server.hpp:45
tcp_socket & get_socket(void)
~tcp_server(void)
dtor
Definition: io_service.hpp:48
std::function< bool(const std::shared_ptr< tcp_client > &)> on_new_connection_callback_t
Definition: tcp_server.hpp:79
bool operator==(const tcp_server &rhs) const
const std::list< std::shared_ptr< tacopie::tcp_client > > & get_clients(void) const
const std::shared_ptr< tacopie::io_service > & get_io_service(void) const
bool operator!=(const tcp_server &rhs) const
void start(const std::string &host, std::uint32_t port, const on_new_connection_callback_t &callback=nullptr)
bool is_running(void) const