Ardour  9.0-pre0-582-g084a23a80d
server.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020-2021 Luciano Iam <oss@lucianoiam.com>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef _ardour_surface_websockets_server_h_
20 #define _ardour_surface_websockets_server_h_
21 
22 #include <unordered_map>
23 #include <glibmm.h>
24 #include <libwebsockets.h>
25 
26 #if LWS_LIBRARY_VERSION_MAJOR < 3
27 // older <libwebsockets.h> includes <uv.h> which in turn includes
28 // /usr/include/asm-generic/param.h on linux which defines HZ
29 // and conflicts with libs/ardour/ardour/parameter_descriptor.h
30 #undef HZ
31 #endif
32 
33 #include "client.h"
34 #include "component.h"
35 #include "message.h"
36 #include "state.h"
37 #include "resources.h"
38 
39 // TO DO: make this configurable
40 #define WEBSOCKET_LISTEN_PORT 3818
41 
42 namespace ArdourSurface {
43 
45 {
46 public:
48  virtual ~WebsocketsServer (){};
49 
50  int start ();
51  int stop ();
52 
53  void update_client (Client, const NodeState&, bool);
54  void update_all_clients (const NodeState&, bool);
55 
56 private:
57 #if LWS_LIBRARY_VERSION_MAJOR < 3
58  struct lws_protocol_vhost_options _lws_vhost_opt;
59 #endif
60  struct lws_protocols _lws_proto[2];
61  struct lws_http_mount _lws_mnt_root;
62  struct lws_http_mount _lws_mnt_user;
63  struct lws_context_creation_info _lws_info;
64  struct lws_context* _lws_context;
65 
66  typedef std::unordered_map<Client, ClientContext> ClientContextMap;
68 
70 
73  int recv_client (Client, void*, size_t);
77 
79 
80  static int lws_callback (struct lws*, enum lws_callback_reasons, void*, void*, size_t);
81 
82  /* Glib event loop integration that requires LWS_WITH_EXTERNAL_POLL */
83 
85  struct lws_pollfd lws_pfd;
86  Glib::RefPtr<Glib::IOChannel> g_channel;
87  Glib::RefPtr<Glib::IOSource> rg_iosrc;
88  Glib::RefPtr<Glib::IOSource> wg_iosrc;
89  };
90 
91  Glib::RefPtr<Glib::IOChannel> _channel;
92 
93  typedef std::unordered_map<lws_sockfd_type, LwsPollFdGlibSource> LwsPollFdGlibSourceMap;
95 
97 
98  bool fd_callbacks () { return _fd_callbacks; }
99 
100  int add_poll_fd (struct lws_pollargs*);
101  int mod_poll_fd (struct lws_pollargs*);
102  int del_poll_fd (struct lws_pollargs*);
103 
104  bool io_handler (Glib::IOCondition, lws_sockfd_type);
105 
106  Glib::IOCondition events_to_ioc (int);
107  int ioc_to_events (Glib::IOCondition);
108 
109  /* Glib event loop integration that does NOT require LWS_WITH_EXTERNAL_POLL
110  but needs a secondary thread for notifying the server when there is
111  pending data for writing. Unfortunately libwesockets' own approach to
112  Glib integration cannot be copied because it relies on file descriptors
113  that are hidden by the 'lws' opaque type. See feedback.cc . */
114 
115  GSource* _g_source;
116 
117  static gboolean glib_idle_callback (void *);
118 
119 public:
120  bool read_blocks_event_loop () { return _g_source != 0; }
121 
122 };
123 
124 } // namespace ArdourSurface
125 
126 #endif // _ardour_surface_websockets_server_h_
bool io_handler(Glib::IOCondition, lws_sockfd_type)
ServerResources _resources
Definition: server.h:69
LwsPollFdGlibSourceMap _fd_ctx
Definition: server.h:94
struct lws_protocol_vhost_options _lws_vhost_opt
Definition: server.h:58
ClientContextMap _client_ctx
Definition: server.h:67
int recv_client(Client, void *, size_t)
struct lws_context_creation_info _lws_info
Definition: server.h:63
std::unordered_map< Client, ClientContext > ClientContextMap
Definition: server.h:66
int add_poll_fd(struct lws_pollargs *)
static int lws_callback(struct lws *, enum lws_callback_reasons, void *, void *, size_t)
int del_poll_fd(struct lws_pollargs *)
static gboolean glib_idle_callback(void *)
int mod_poll_fd(struct lws_pollargs *)
Glib::IOCondition events_to_ioc(int)
int ioc_to_events(Glib::IOCondition)
struct lws_http_mount _lws_mnt_root
Definition: server.h:61
void update_all_clients(const NodeState &, bool)
struct lws_context * _lws_context
Definition: server.h:64
void update_client(Client, const NodeState &, bool)
std::unordered_map< lws_sockfd_type, LwsPollFdGlibSource > LwsPollFdGlibSourceMap
Definition: server.h:93
Glib::RefPtr< Glib::IOChannel > _channel
Definition: server.h:91
WebsocketsServer(ArdourSurface::ArdourWebsockets &)
struct lws_http_mount _lws_mnt_user
Definition: server.h:62
struct lws_protocols _lws_proto[2]
Definition: server.h:60
struct lws * Client
Definition: client.h:28
Glib::RefPtr< Glib::IOChannel > g_channel
Definition: server.h:86
Glib::RefPtr< Glib::IOSource > wg_iosrc
Definition: server.h:88
Glib::RefPtr< Glib::IOSource > rg_iosrc
Definition: server.h:87