2019-02-25 19:17:13 +00:00
|
|
|
#include "wsfs/provider/client_protocol_intern.h"
|
2019-02-18 22:30:39 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <libwebsockets.h>
|
2019-02-24 17:03:22 +00:00
|
|
|
#include <jansson.h>
|
2019-02-18 22:30:39 +00:00
|
|
|
|
2019-02-24 18:16:29 +00:00
|
|
|
|
2019-03-04 20:17:37 +00:00
|
|
|
#include "wsfs/provider/provider.h"
|
2019-02-18 22:30:39 +00:00
|
|
|
#include "wsfs/util.h"
|
2019-02-26 14:42:59 +00:00
|
|
|
#include "wsfs/message.h"
|
2019-02-18 22:30:39 +00:00
|
|
|
|
2019-02-24 17:03:22 +00:00
|
|
|
static void wsfsp_client_protocol_respond(
|
|
|
|
json_t * response,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-03-03 11:48:58 +00:00
|
|
|
struct wsfsp_client_protocol * protocol = (struct wsfsp_client_protocol *) user_data;
|
2019-02-24 17:03:22 +00:00
|
|
|
|
2019-03-03 11:48:58 +00:00
|
|
|
struct wsfs_message * message = wsfs_message_create(response);
|
|
|
|
if (NULL != message)
|
2019-02-24 17:03:22 +00:00
|
|
|
{
|
2019-03-03 11:48:58 +00:00
|
|
|
wsfs_message_queue_push(&protocol->queue, message);
|
|
|
|
lws_callback_on_writable(protocol->wsi);
|
2019-02-24 17:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wsfsp_client_protocol_process_request(
|
|
|
|
struct wsfsp_client_protocol * protocol,
|
|
|
|
char const * message,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
json_t * request = json_loadb(message, length, 0, NULL);
|
|
|
|
if (NULL != request)
|
|
|
|
{
|
|
|
|
struct wsfsp_invokation_context context =
|
|
|
|
{
|
|
|
|
.provider = &protocol->provider,
|
|
|
|
.user_data = protocol->user_data,
|
|
|
|
.request = &protocol->request
|
|
|
|
};
|
|
|
|
|
|
|
|
wsfsp_provider_invoke(&context, request);
|
|
|
|
json_decref(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-18 22:30:39 +00:00
|
|
|
static int wsfsp_client_protocol_callback(
|
2019-02-24 17:03:22 +00:00
|
|
|
struct lws * wsi,
|
2019-02-23 15:20:30 +00:00
|
|
|
enum lws_callback_reasons reason,
|
2019-02-18 22:30:39 +00:00
|
|
|
void * WSFS_UNUSED_PARAM(user),
|
2019-02-24 17:03:22 +00:00
|
|
|
void * in,
|
|
|
|
size_t len)
|
2019-02-18 22:30:39 +00:00
|
|
|
{
|
2019-02-24 17:03:22 +00:00
|
|
|
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
|
|
|
struct wsfsp_client_protocol * protocol = (NULL != ws_protocol) ? ws_protocol->user: NULL;
|
2019-02-23 15:20:30 +00:00
|
|
|
|
2019-02-24 17:03:22 +00:00
|
|
|
if (NULL != protocol)
|
2019-02-23 15:20:30 +00:00
|
|
|
{
|
2019-02-24 17:03:22 +00:00
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
|
|
|
protocol->provider.connected(protocol->user_data);
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
|
|
|
protocol->provider.disconnected(protocol->user_data);
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLIENT_CLOSED:
|
|
|
|
protocol->provider.connected(protocol->user_data);
|
|
|
|
break;
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
|
|
|
wsfsp_client_protocol_process_request(protocol, in, len);
|
|
|
|
break;
|
2019-03-03 11:48:58 +00:00
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
// fall-through
|
2019-02-26 14:42:59 +00:00
|
|
|
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
|
|
|
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
|
|
|
|
{
|
|
|
|
struct wsfs_message * message = wsfs_message_queue_pop(&protocol->queue);
|
|
|
|
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
|
|
|
wsfs_message_dispose(message);
|
2019-03-03 11:48:58 +00:00
|
|
|
|
|
|
|
if (!wsfs_message_queue_empty(&protocol->queue))
|
|
|
|
{
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
|
|
|
|
}
|
2019-02-26 14:42:59 +00:00
|
|
|
}
|
2019-03-03 11:48:58 +00:00
|
|
|
break;
|
2019-02-24 17:03:22 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-02-23 15:20:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 22:30:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wsfsp_client_protocol_init(
|
|
|
|
struct wsfsp_client_protocol * protocol,
|
|
|
|
struct wsfsp_provider const * provider,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2019-02-26 14:42:59 +00:00
|
|
|
wsfs_message_queue_init(&protocol->queue);
|
|
|
|
|
|
|
|
protocol->wsi = NULL;
|
|
|
|
|
2019-02-24 17:03:22 +00:00
|
|
|
protocol->request.respond = &wsfsp_client_protocol_respond;
|
|
|
|
protocol->request.user_data = protocol;
|
|
|
|
|
2019-02-18 22:30:39 +00:00
|
|
|
protocol->user_data = user_data;
|
2019-03-04 20:17:37 +00:00
|
|
|
wsfsp_provider_init_from_prototype(&protocol->provider, provider);
|
2019-02-18 22:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_protocol_cleanup(
|
|
|
|
struct wsfsp_client_protocol * protocol)
|
|
|
|
{
|
2019-02-26 14:42:59 +00:00
|
|
|
wsfs_message_queue_cleanup(&protocol->queue);
|
2019-02-18 22:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct wsfsp_client_protocol * wsfsp_client_protocol_create(
|
|
|
|
struct wsfsp_provider const * provider,
|
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
struct wsfsp_client_protocol * protocol = malloc(sizeof(struct wsfsp_client_protocol));
|
|
|
|
if (NULL != protocol)
|
|
|
|
{
|
|
|
|
wsfsp_client_protocol_init(protocol, provider, user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return protocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_protocol_dispose(
|
|
|
|
struct wsfsp_client_protocol * protocol)
|
|
|
|
{
|
|
|
|
wsfsp_client_protocol_cleanup(protocol);
|
|
|
|
free(protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wsfsp_client_protocol_init_lws(
|
|
|
|
struct wsfsp_client_protocol * protocol,
|
|
|
|
struct lws_protocols * lws_protocol)
|
|
|
|
{
|
|
|
|
lws_protocol->callback = &wsfsp_client_protocol_callback;
|
|
|
|
lws_protocol->per_session_data_size = 0;
|
|
|
|
lws_protocol->user = protocol;
|
|
|
|
}
|