1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

refactor: changed signature of wpf_message_create to remove jansson dependency

This commit is contained in:
Falk Werner 2020-07-10 21:17:31 +02:00
parent 32c9e45e1f
commit c066090df2
10 changed files with 50 additions and 67 deletions

View File

@ -221,9 +221,18 @@ static bool wfp_impl_client_protocol_send(
{ {
struct wfp_client_protocol * protocol = user_data; struct wfp_client_protocol * protocol = user_data;
struct wfp_message * message = wfp_message_create(request); size_t length = json_dumpb(request, NULL, 0, JSON_COMPACT);
wfp_slist_append(&protocol->messages, &message->item); if (0 < length)
lws_callback_on_writable(protocol->wsi); {
char * raw_data = malloc(LWS_PRE + length);
char * data = raw_data + LWS_PRE;
json_dumpb(request, data, length, JSON_COMPACT);
struct wfp_message * message = wfp_message_create(data, length);
wfp_slist_append(&protocol->messages, &message->item);
lws_callback_on_writable(protocol->wsi);
}
return true; return true;
} }

View File

@ -172,7 +172,6 @@ wfp_impl_json_writer_take_data(
wfp_impl_json_writer_reserve(writer, 1); wfp_impl_json_writer_reserve(writer, 1);
writer->data[writer->offset] = '\0'; writer->data[writer->offset] = '\0';
char * result = writer->raw_data;
writer->raw_data = NULL; writer->raw_data = NULL;
if (NULL != size) if (NULL != size)
@ -180,7 +179,7 @@ wfp_impl_json_writer_take_data(
*size = writer->offset; *size = writer->offset;
} }
return result; return writer->data;
} }
void void

View File

@ -22,20 +22,6 @@ wfp_jsonrpc_is_request(
} }
struct wfp_jsonrpc_request *
wfp_jsonrpc_request_create(
int id,
wfp_jsonrpc_send_fn * send,
void * user_data)
{
struct wfp_jsonrpc_request * request = malloc(sizeof(struct wfp_jsonrpc_request));
request->id = id;
request->send = send;
request->user_data = user_data;
return request;
}
void void
wfp_jsonrpc_request_dispose( wfp_jsonrpc_request_dispose(
struct wfp_jsonrpc_request * request) struct wfp_jsonrpc_request * request)

View File

@ -24,12 +24,6 @@ struct wfp_jsonrpc_request;
extern bool wfp_jsonrpc_is_request( extern bool wfp_jsonrpc_is_request(
json_t * message); json_t * message);
extern struct wfp_jsonrpc_request *
wfp_jsonrpc_request_create(
int id,
wfp_jsonrpc_send_fn * send,
void * user_data);
extern void wfp_jsonrpc_request_dispose( extern void wfp_jsonrpc_request_dispose(
struct wfp_jsonrpc_request * request); struct wfp_jsonrpc_request * request);

View File

@ -3,17 +3,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <libwebsockets.h> #include <libwebsockets.h>
extern struct wfp_message * wfp_message_create(json_t const * value) extern struct wfp_message * wfp_message_create(
char * data,
size_t length)
{ {
size_t const length = json_dumpb(value, NULL, 0, JSON_COMPACT); struct wfp_message * message = malloc(sizeof(struct wfp_message));
message->data = data;
char * data = malloc(sizeof(struct wfp_message) + LWS_PRE + length);
struct wfp_message * message = (struct wfp_message *) data;
message->data = &data[sizeof(struct wfp_message) + LWS_PRE];
message->length = length; message->length = length;
message->raw_data = NULL; message->raw_data = data - LWS_PRE;
json_dumpb(value, message->data, length, JSON_COMPACT);
return message; return message;
} }

View File

@ -8,7 +8,6 @@
using std::size_t; using std::size_t;
#endif #endif
#include <jansson.h>
#include "webfuse_provider/impl/util/slist.h" #include "webfuse_provider/impl/util/slist.h"
struct wfp_message struct wfp_message
@ -24,8 +23,10 @@ extern "C"
{ {
#endif #endif
extern struct wfp_message * wfp_message_create( extern struct wfp_message *
json_t const * value); wfp_message_create(
char * data,
size_t length);
extern void wfp_message_dispose( extern void wfp_message_dispose(
struct wfp_message * message); struct wfp_message * message);

View File

@ -51,11 +51,10 @@ wfp_impl_message_writer_take_message(
writer->is_finished = true; writer->is_finished = true;
} }
struct wfp_message * message = malloc(sizeof(struct wfp_message)); size_t length;
message->raw_data = wfp_impl_json_writer_take_data(writer->json_writer, &message->length); char * data = wfp_impl_json_writer_take_data(writer->json_writer, &length);
message->data = &(message->raw_data[LWS_PRE]);
return message; return wfp_message_create(data, length);
} }
void void

View File

@ -23,20 +23,6 @@ bool jsonrpc_send(
} }
TEST(wfp_jsonrpc_request, create_dispose)
{
Context context{nullptr};
void * user_data = reinterpret_cast<void*>(&context);
struct wfp_jsonrpc_request * request =
wfp_jsonrpc_request_create(42, &jsonrpc_send, user_data);
ASSERT_NE(nullptr, request);
ASSERT_EQ(user_data, wfp_jsonrpc_request_get_userdata(request));
wfp_jsonrpc_request_dispose(request);
}
TEST(wfp_jsonrpc_request, is_request_object_params) TEST(wfp_jsonrpc_request, is_request_object_params)
{ {
json_t * request = json_object(); json_t * request = json_object();

View File

@ -1,16 +1,22 @@
#include <gtest/gtest.h>
#include <cstring>
#include "webfuse_provider/impl/message.h" #include "webfuse_provider/impl/message.h"
#include <gtest/gtest.h>
#include <libwebsockets.h>
#include <cstdlib>
#include <cstring>
TEST(wfp_message, create) TEST(wfp_message, create)
{ {
json_t * value = json_object(); size_t length = 3;
char * raw_data = reinterpret_cast<char*>(malloc(LWS_PRE + length));
struct wfp_message * message = wfp_message_create(value); char * data = raw_data + LWS_PRE;
snprintf(data, length, "{}");
struct wfp_message * message = wfp_message_create(data, length);
ASSERT_NE(nullptr, message); ASSERT_NE(nullptr, message);
ASSERT_EQ(2, message->length); ASSERT_EQ(3, message->length);
ASSERT_TRUE(0 == strncmp("{}", message->data, 2)); ASSERT_STREQ("{}", message->data);
wfp_message_dispose(message); wfp_message_dispose(message);
json_decref(value);
} }

View File

@ -3,16 +3,22 @@
#include "webfuse_provider/impl/message.h" #include "webfuse_provider/impl/message.h"
#include "webfuse_provider/impl/util/slist.h" #include "webfuse_provider/impl/util/slist.h"
#include <libwebsockets.h>
#include <cstdlib>
#include <cstring>
namespace namespace
{ {
struct wfp_slist_item * create_message(char const * content) struct wfp_slist_item * create_message(char const * content)
{ {
json_t * value = json_object(); size_t length = strlen(content);
json_object_set_new(value, "content", json_string(content)); char * raw_data = reinterpret_cast<char*>(malloc(LWS_PRE + length));
struct wfp_message * message = wfp_message_create(value); char * data = raw_data + LWS_PRE;
memcpy(data, content, length);
struct wfp_message * message = wfp_message_create(data, length);
json_decref(value);
return &message->item; return &message->item;
} }