Instrument Neutral Distributed Interface INDI  2.0.2
indiwsserver.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2019 Jasem Mutlaq. All rights reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 *******************************************************************************/
18 
19 #pragma once
20 
21 #include <set>
22 #include <websocketpp/config/asio_no_tls.hpp>
23 #include <websocketpp/server.hpp>
24 #include <websocketpp/extensions/permessage_deflate/enabled.hpp>
25 
26 //typedef websocketpp::server<websocketpp::config::asio> server;
27 
28 struct deflate_server_config : public websocketpp::config::asio
29 {
30  // ... additional custom config if you need it for other things
31 
34 
35  typedef websocketpp::extensions::permessage_deflate::enabled
37 };
38 
39 typedef websocketpp::server<deflate_server_config> server;
40 
41 using websocketpp::connection_hdl;
42 using websocketpp::lib::placeholders::_1;
43 using websocketpp::lib::placeholders::_2;
44 using websocketpp::lib::bind;
45 
47 {
48  public:
50 
51  uint16_t generatePort()
52  {
53  m_global_port++;
54  m_port = m_global_port;
55  return m_port ;
56  }
57 
58  void on_open(connection_hdl hdl)
59  {
60  m_connections.insert(hdl);
61  }
62 
63  void on_close(connection_hdl hdl)
64  {
65  m_connections.erase(hdl);
66  }
67 
68  // void on_message(connection_hdl hdl, server::message_ptr msg)
69  // {
70  // for (auto it : m_connections)
71  // {
72  // m_server->send(it,msg);
73  // }
74  // }
75 
76  void send_binary(void const * payload, size_t len)
77  {
78  for (auto it : m_connections)
79  {
80  try
81  {
82  m_server->send(it, payload, len, websocketpp::frame::opcode::binary);
83  }
84  catch (websocketpp::exception const &e)
85  {
86  std::cerr << e.what() << std::endl;
87  }
88  catch (...)
89  {
90  std::cerr << "other exception" << std::endl;
91  }
92 
93  }
94  }
95 
96  void send_text(const std::string &payload)
97  {
98  for (auto it : m_connections)
99  {
100  try
101  {
102  m_server->send(it, payload, websocketpp::frame::opcode::text);
103  }
104  catch (websocketpp::exception const &e)
105  {
106  std::cerr << e.what() << std::endl;
107  }
108  catch (...)
109  {
110  std::cerr << "other exception" << std::endl;
111  }
112 
113  }
114  }
115 
116  void stop()
117  {
118  for (auto it : m_connections)
119  m_server->close(it, websocketpp::close::status::normal, "Switched off by user.");
120 
121  m_connections.clear();
122  m_server->stop();
123  }
124 
125  bool is_running()
126  {
127  return m_server->is_listening();
128  }
129 
130  void run()
131  {
132  try
133  {
134  m_server.reset(new server());
135 
136  m_server->init_asio();
137  m_server->set_reuse_addr(true);
138 
139 
140  m_server->set_open_handler(bind(&INDIWSServer::on_open, this, ::_1));
141  m_server->set_close_handler(bind(&INDIWSServer::on_close, this, ::_1));
142  //m_server->set_message_handler(bind(&INDIWSServer::on_message,this,::_1,::_2));
143 
144  m_server->listen(m_port);
145  m_server->start_accept();
146  m_server->run();
147 
148  }
149  catch (websocketpp::exception const &e)
150  {
151  std::cerr << e.what() << std::endl;
152  }
153  catch (...)
154  {
155  std::cerr << "other exception" << std::endl;
156  }
157 
158  }
159 
160  private:
161  typedef std::set<connection_hdl, std::owner_less<connection_hdl>> con_list;
162 
163  std::unique_ptr<server> m_server;
164  con_list m_connections;
165  uint16_t m_port;
166  static uint16_t m_global_port;
167 };
168 
uint16_t generatePort()
Definition: indiwsserver.h:51
void on_close(connection_hdl hdl)
Definition: indiwsserver.h:63
void on_open(connection_hdl hdl)
Definition: indiwsserver.h:58
bool is_running()
Definition: indiwsserver.h:125
void send_text(const std::string &payload)
Definition: indiwsserver.h:96
void send_binary(void const *payload, size_t len)
Definition: indiwsserver.h:76
websocketpp::server< deflate_server_config > server
Definition: indiwsserver.h:39
websocketpp::extensions::permessage_deflate::enabled< permessage_deflate_config > permessage_deflate_type
Definition: indiwsserver.h:36