mirror of
https://github.com/falk-werner/webfuse
synced 2025-06-13 12:54:15 +00:00
moves some logic from server_protocol to session
This commit is contained in:
parent
d9fd8773e1
commit
a125af7a54
@ -34,31 +34,31 @@ static int wsfs_server_protocol_callback(
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LWS_CALLBACK_ESTABLISHED:
|
case LWS_CALLBACK_ESTABLISHED:
|
||||||
|
session = wsfs_session_manager_add(
|
||||||
|
&protocol->session_manager,
|
||||||
|
wsi,
|
||||||
|
&protocol->authenticators,
|
||||||
|
&protocol->rpc);
|
||||||
|
|
||||||
|
if (NULL != session)
|
||||||
{
|
{
|
||||||
bool is_authenticated = wsfs_authenticators_authenticate(&protocol->authenticators, NULL);
|
wsfs_session_authenticate(session, NULL);
|
||||||
wsfs_session_manager_add(&protocol->session_manager, wsi, is_authenticated);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LWS_CALLBACK_CLOSED:
|
case LWS_CALLBACK_CLOSED:
|
||||||
{
|
|
||||||
wsfs_session_manager_remove(&protocol->session_manager, wsi);
|
wsfs_session_manager_remove(&protocol->session_manager, wsi);
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||||
if ((NULL != session) && (!wsfs_message_queue_empty(&session->queue)))
|
if (NULL != session)
|
||||||
{
|
{
|
||||||
struct wsfs_message * message = wsfs_message_queue_pop(&session->queue);
|
wsfs_session_onwritable(session);
|
||||||
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
|
||||||
wsfs_message_dispose(message);
|
|
||||||
|
|
||||||
if (!wsfs_message_queue_empty(&session->queue))
|
|
||||||
{
|
|
||||||
lws_callback_on_writable(wsi);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LWS_CALLBACK_RECEIVE:
|
case LWS_CALLBACK_RECEIVE:
|
||||||
wsfs_jsonrpc_server_onresult(&protocol->rpc, in, len);
|
if (NULL != session)
|
||||||
|
{
|
||||||
|
wsfs_session_receive(session, in, len);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case LWS_CALLBACK_RAW_RX_FILE:
|
case LWS_CALLBACK_RAW_RX_FILE:
|
||||||
wsfs_filesystem_process_request(&protocol->filesystem);
|
wsfs_filesystem_process_request(&protocol->filesystem);
|
||||||
@ -74,21 +74,11 @@ static bool wsfs_server_protocol_invoke(
|
|||||||
void * user_data,
|
void * user_data,
|
||||||
json_t const * request)
|
json_t const * request)
|
||||||
{
|
{
|
||||||
bool result = false;
|
|
||||||
struct wsfs_server_protocol * protocol = user_data;
|
struct wsfs_server_protocol * protocol = user_data;
|
||||||
struct wsfs_session * session = &protocol->session_manager.session;
|
struct wsfs_session * session = &protocol->session_manager.session;
|
||||||
|
|
||||||
if ((session->is_authenticated) && (NULL != session->wsi))
|
|
||||||
{
|
|
||||||
struct wsfs_message * message = wsfs_message_create(request);
|
struct wsfs_message * message = wsfs_message_create(request);
|
||||||
if (NULL != message)
|
|
||||||
{
|
|
||||||
wsfs_message_queue_push(&session->queue, message);
|
|
||||||
lws_callback_on_writable(session->wsi);
|
|
||||||
|
|
||||||
result = true;
|
bool const result = wsfs_session_send(session, message);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
#include "wsfs/adapter/session.h"
|
#include "wsfs/adapter/session.h"
|
||||||
|
#include "wsfs/adapter/authenticators.h"
|
||||||
|
#include "wsfs/message_queue.h"
|
||||||
|
#include "wsfs/message.h"
|
||||||
|
#include "wsfs/adapter/jsonrpc/server.h"
|
||||||
|
|
||||||
|
#include <libwebsockets.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
void wsfs_session_init(
|
void wsfs_session_init(
|
||||||
struct wsfs_session * session,
|
struct wsfs_session * session,
|
||||||
struct lws * wsi,
|
struct lws * wsi,
|
||||||
bool is_authenticated)
|
struct wsfs_authenticators * authenticators,
|
||||||
|
struct wsfs_jsonrpc_server * rpc)
|
||||||
{
|
{
|
||||||
session->wsi = wsi;
|
session->wsi = wsi;
|
||||||
session->is_authenticated = is_authenticated;
|
session->is_authenticated = false;
|
||||||
|
session->authenticators = authenticators;
|
||||||
|
session->rpc = rpc;
|
||||||
wsfs_message_queue_init(&session->queue);
|
wsfs_message_queue_init(&session->queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,4 +26,59 @@ void wsfs_session_cleanup(
|
|||||||
wsfs_message_queue_cleanup(&session->queue);
|
wsfs_message_queue_cleanup(&session->queue);
|
||||||
session->is_authenticated = false;
|
session->is_authenticated = false;
|
||||||
session->wsi = NULL;
|
session->wsi = NULL;
|
||||||
|
session->authenticators = NULL;
|
||||||
|
session->rpc = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wsfs_session_authenticate(
|
||||||
|
struct wsfs_session * session,
|
||||||
|
struct wsfs_credentials * creds)
|
||||||
|
{
|
||||||
|
session->is_authenticated = wsfs_authenticators_authenticate(session->authenticators, creds);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wsfs_session_send(
|
||||||
|
struct wsfs_session * session,
|
||||||
|
struct wsfs_message * message)
|
||||||
|
{
|
||||||
|
bool result = (session->is_authenticated) && (NULL != session->wsi);
|
||||||
|
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
wsfs_message_queue_push(&session->queue, message);
|
||||||
|
lws_callback_on_writable(session->wsi);
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wsfs_message_dispose(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wsfs_session_onwritable(
|
||||||
|
struct wsfs_session * session)
|
||||||
|
{
|
||||||
|
if (!wsfs_message_queue_empty(&session->queue))
|
||||||
|
{
|
||||||
|
struct wsfs_message * message = wsfs_message_queue_pop(&session->queue);
|
||||||
|
lws_write(session->wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
||||||
|
wsfs_message_dispose(message);
|
||||||
|
|
||||||
|
if (!wsfs_message_queue_empty(&session->queue))
|
||||||
|
{
|
||||||
|
lws_callback_on_writable(session->wsi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void wsfs_session_receive(
|
||||||
|
struct wsfs_session * session,
|
||||||
|
char const * data,
|
||||||
|
size_t length)
|
||||||
|
{
|
||||||
|
wsfs_jsonrpc_server_onresult(session->rpc, data, length);
|
||||||
}
|
}
|
@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
#ifndef __cplusplus
|
#ifndef __cplusplus
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#else
|
||||||
|
#include <cstddef>
|
||||||
|
using std::size_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "wsfs/message_queue.h"
|
#include "wsfs/message_queue.h"
|
||||||
@ -13,18 +17,41 @@ extern "C"
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct lws;
|
struct lws;
|
||||||
|
struct wsfs_message;
|
||||||
|
struct wsfs_credentials;
|
||||||
|
struct wsfs_authenticators;
|
||||||
|
struct wsfs_jsonrpc_server;
|
||||||
|
|
||||||
struct wsfs_session
|
struct wsfs_session
|
||||||
{
|
{
|
||||||
struct lws * wsi;
|
struct lws * wsi;
|
||||||
bool is_authenticated;
|
bool is_authenticated;
|
||||||
struct wsfs_message_queue queue;
|
struct wsfs_message_queue queue;
|
||||||
|
struct wsfs_authenticators * authenticators;
|
||||||
|
struct wsfs_jsonrpc_server * rpc;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void wsfs_session_init(
|
extern void wsfs_session_init(
|
||||||
struct wsfs_session * session,
|
struct wsfs_session * session,
|
||||||
struct lws * wsi,
|
struct lws * wsi,
|
||||||
bool is_authenticated);
|
struct wsfs_authenticators * authenticators,
|
||||||
|
struct wsfs_jsonrpc_server * rpc);
|
||||||
|
|
||||||
|
extern void wsfs_session_authenticate(
|
||||||
|
struct wsfs_session * session,
|
||||||
|
struct wsfs_credentials * creds);
|
||||||
|
|
||||||
|
extern bool wsfs_session_send(
|
||||||
|
struct wsfs_session * session,
|
||||||
|
struct wsfs_message * message);
|
||||||
|
|
||||||
|
extern void wsfs_session_receive(
|
||||||
|
struct wsfs_session * session,
|
||||||
|
char const * data,
|
||||||
|
size_t length);
|
||||||
|
|
||||||
|
extern void wsfs_session_onwritable(
|
||||||
|
struct wsfs_session * session);
|
||||||
|
|
||||||
extern void wsfs_session_cleanup(
|
extern void wsfs_session_cleanup(
|
||||||
struct wsfs_session * session);
|
struct wsfs_session * session);
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
void wsfs_session_manager_init(
|
void wsfs_session_manager_init(
|
||||||
struct wsfs_session_manager * manager)
|
struct wsfs_session_manager * manager)
|
||||||
{
|
{
|
||||||
wsfs_session_init(&manager->session, NULL, false);
|
wsfs_session_init(&manager->session, NULL, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wsfs_session_manager_cleanup(
|
void wsfs_session_manager_cleanup(
|
||||||
@ -13,18 +13,20 @@ void wsfs_session_manager_cleanup(
|
|||||||
wsfs_session_cleanup(&manager->session);
|
wsfs_session_cleanup(&manager->session);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wsfs_session_manager_add(
|
struct wsfs_session * wsfs_session_manager_add(
|
||||||
struct wsfs_session_manager * manager,
|
struct wsfs_session_manager * manager,
|
||||||
struct lws * wsi,
|
struct lws * wsi,
|
||||||
bool is_authenticated)
|
struct wsfs_authenticators * authenticators,
|
||||||
|
struct wsfs_jsonrpc_server * rpc)
|
||||||
{
|
{
|
||||||
bool const result = (NULL == manager->session.wsi);
|
struct wsfs_session * session = NULL;
|
||||||
if (result)
|
if (NULL == manager->session.wsi)
|
||||||
{
|
{
|
||||||
wsfs_session_init(&manager->session, wsi, is_authenticated);
|
session = &manager->session;
|
||||||
|
wsfs_session_init(&manager->session, wsi, authenticators, rpc);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct wsfs_session * wsfs_session_manager_get(
|
struct wsfs_session * wsfs_session_manager_get(
|
||||||
|
@ -25,10 +25,11 @@ extern void wsfs_session_manager_init(
|
|||||||
extern void wsfs_session_manager_cleanup(
|
extern void wsfs_session_manager_cleanup(
|
||||||
struct wsfs_session_manager * manager);
|
struct wsfs_session_manager * manager);
|
||||||
|
|
||||||
extern bool wsfs_session_manager_add(
|
extern struct wsfs_session * wsfs_session_manager_add(
|
||||||
struct wsfs_session_manager * manager,
|
struct wsfs_session_manager * manager,
|
||||||
struct lws * wsi,
|
struct lws * wsi,
|
||||||
bool is_authenticated);
|
struct wsfs_authenticators * authenticators,
|
||||||
|
struct wsfs_jsonrpc_server * rpc);
|
||||||
|
|
||||||
extern struct wsfs_session * wsfs_session_manager_get(
|
extern struct wsfs_session * wsfs_session_manager_get(
|
||||||
struct wsfs_session_manager * manager,
|
struct wsfs_session_manager * manager,
|
||||||
|
Loading…
Reference in New Issue
Block a user