2020-02-20 07:12:36 -05:00
|
|
|
/*
|
2021-06-14 08:45:51 -04:00
|
|
|
* Copyright (C) 2020-2021 Luciano Iam <oss@lucianoiam.com>
|
2020-02-20 07:12:36 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2020-02-23 10:13:22 -05:00
|
|
|
#ifndef _ardour_surface_websockets_server_h_
|
|
|
|
#define _ardour_surface_websockets_server_h_
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2024-10-18 20:23:13 -04:00
|
|
|
#include <unordered_map>
|
2020-02-22 07:50:24 -05:00
|
|
|
#include <glibmm.h>
|
2020-02-20 07:12:36 -05:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR < 3
|
2020-05-10 10:01:06 -04:00
|
|
|
// older <libwebsockets.h> includes <uv.h> which in turn includes
|
2020-02-20 07:12:36 -05:00
|
|
|
// /usr/include/asm-generic/param.h on linux which defines HZ
|
|
|
|
// and conflicts with libs/ardour/ardour/parameter_descriptor.h
|
|
|
|
#undef HZ
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "client.h"
|
2020-02-23 10:03:59 -05:00
|
|
|
#include "component.h"
|
2020-02-20 07:12:36 -05:00
|
|
|
#include "message.h"
|
2020-02-23 10:03:59 -05:00
|
|
|
#include "state.h"
|
2020-04-09 09:57:09 -04:00
|
|
|
#include "resources.h"
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-04-22 05:26:51 -04:00
|
|
|
// TO DO: make this configurable
|
2020-04-09 17:26:06 -04:00
|
|
|
#define WEBSOCKET_LISTEN_PORT 3818
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-08-30 15:15:07 -04:00
|
|
|
namespace ArdourSurface {
|
|
|
|
|
2020-02-20 07:12:36 -05:00
|
|
|
class WebsocketsServer : public SurfaceComponent
|
|
|
|
{
|
2020-02-23 10:03:59 -05:00
|
|
|
public:
|
|
|
|
WebsocketsServer (ArdourSurface::ArdourWebsockets&);
|
|
|
|
virtual ~WebsocketsServer (){};
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-02-23 10:03:59 -05:00
|
|
|
int start ();
|
|
|
|
int stop ();
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-02-23 10:03:59 -05:00
|
|
|
void update_client (Client, const NodeState&, bool);
|
|
|
|
void update_all_clients (const NodeState&, bool);
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-02-23 10:03:59 -05:00
|
|
|
private:
|
2020-04-14 16:54:41 -04:00
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR < 3
|
|
|
|
struct lws_protocol_vhost_options _lws_vhost_opt;
|
|
|
|
#endif
|
|
|
|
struct lws_protocols _lws_proto[2];
|
2020-04-17 05:58:53 -04:00
|
|
|
struct lws_http_mount _lws_mnt_root;
|
2020-04-14 16:54:41 -04:00
|
|
|
struct lws_http_mount _lws_mnt_user;
|
|
|
|
struct lws_context_creation_info _lws_info;
|
|
|
|
struct lws_context* _lws_context;
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2024-10-18 20:23:13 -04:00
|
|
|
typedef std::unordered_map<Client, ClientContext> ClientContextMap;
|
2020-02-23 10:03:59 -05:00
|
|
|
ClientContextMap _client_ctx;
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-04-09 09:57:09 -04:00
|
|
|
ServerResources _resources;
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-04-09 09:57:09 -04:00
|
|
|
int add_client (Client);
|
|
|
|
int del_client (Client);
|
|
|
|
int recv_client (Client, void*, size_t);
|
|
|
|
int write_client (Client);
|
2020-04-12 04:42:46 -04:00
|
|
|
int send_availsurf_hdr (Client);
|
|
|
|
int send_availsurf_body (Client);
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2021-06-13 17:49:13 -04:00
|
|
|
void request_write (Client);
|
|
|
|
|
2020-05-10 10:01:06 -04:00
|
|
|
static int lws_callback (struct lws*, enum lws_callback_reasons, void*, void*, size_t);
|
|
|
|
|
2021-06-13 12:21:24 -04:00
|
|
|
/* Glib event loop integration that requires LWS_WITH_EXTERNAL_POLL */
|
|
|
|
|
|
|
|
struct LwsPollFdGlibSource {
|
|
|
|
struct lws_pollfd lws_pfd;
|
|
|
|
Glib::RefPtr<Glib::IOChannel> g_channel;
|
|
|
|
Glib::RefPtr<Glib::IOSource> rg_iosrc;
|
|
|
|
Glib::RefPtr<Glib::IOSource> wg_iosrc;
|
|
|
|
};
|
|
|
|
|
2020-05-10 10:01:06 -04:00
|
|
|
Glib::RefPtr<Glib::IOChannel> _channel;
|
|
|
|
|
2024-10-18 20:23:13 -04:00
|
|
|
typedef std::unordered_map<lws_sockfd_type, LwsPollFdGlibSource> LwsPollFdGlibSourceMap;
|
2021-06-13 12:21:24 -04:00
|
|
|
LwsPollFdGlibSourceMap _fd_ctx;
|
|
|
|
|
|
|
|
bool _fd_callbacks;
|
|
|
|
|
|
|
|
bool fd_callbacks () { return _fd_callbacks; }
|
2020-05-10 10:01:06 -04:00
|
|
|
|
|
|
|
int add_poll_fd (struct lws_pollargs*);
|
|
|
|
int mod_poll_fd (struct lws_pollargs*);
|
|
|
|
int del_poll_fd (struct lws_pollargs*);
|
|
|
|
|
2020-02-23 10:03:59 -05:00
|
|
|
bool io_handler (Glib::IOCondition, lws_sockfd_type);
|
2020-02-20 07:12:36 -05:00
|
|
|
|
2020-02-23 10:03:59 -05:00
|
|
|
Glib::IOCondition events_to_ioc (int);
|
|
|
|
int ioc_to_events (Glib::IOCondition);
|
2021-06-13 12:21:24 -04:00
|
|
|
|
|
|
|
/* Glib event loop integration that does NOT require LWS_WITH_EXTERNAL_POLL
|
|
|
|
but needs a secondary thread for notifying the server when there is
|
|
|
|
pending data for writing. Unfortunately libwesockets' own approach to
|
|
|
|
Glib integration cannot be copied because it relies on file descriptors
|
|
|
|
that are hidden by the 'lws' opaque type. See feedback.cc . */
|
|
|
|
|
|
|
|
GSource* _g_source;
|
|
|
|
|
2021-06-13 14:54:43 -04:00
|
|
|
static gboolean glib_idle_callback (void *);
|
|
|
|
|
|
|
|
public:
|
2021-06-13 17:49:13 -04:00
|
|
|
bool read_blocks_event_loop () { return _g_source != 0; }
|
2021-06-13 12:21:24 -04:00
|
|
|
|
2020-02-20 07:12:36 -05:00
|
|
|
};
|
|
|
|
|
2020-08-30 15:15:07 -04:00
|
|
|
} // namespace ArdourSurface
|
|
|
|
|
2020-04-09 09:49:06 -04:00
|
|
|
#endif // _ardour_surface_websockets_server_h_
|