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:
parent
32c9e45e1f
commit
c066090df2
@ -221,9 +221,18 @@ static bool wfp_impl_client_protocol_send(
|
||||
{
|
||||
struct wfp_client_protocol * protocol = user_data;
|
||||
|
||||
struct wfp_message * message = wfp_message_create(request);
|
||||
wfp_slist_append(&protocol->messages, &message->item);
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
size_t length = json_dumpb(request, NULL, 0, JSON_COMPACT);
|
||||
if (0 < length)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
@ -172,7 +172,6 @@ wfp_impl_json_writer_take_data(
|
||||
wfp_impl_json_writer_reserve(writer, 1);
|
||||
writer->data[writer->offset] = '\0';
|
||||
|
||||
char * result = writer->raw_data;
|
||||
writer->raw_data = NULL;
|
||||
|
||||
if (NULL != size)
|
||||
@ -180,7 +179,7 @@ wfp_impl_json_writer_take_data(
|
||||
*size = writer->offset;
|
||||
}
|
||||
|
||||
return result;
|
||||
return writer->data;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -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
|
||||
wfp_jsonrpc_request_dispose(
|
||||
struct wfp_jsonrpc_request * request)
|
||||
|
@ -24,12 +24,6 @@ struct wfp_jsonrpc_request;
|
||||
extern bool wfp_jsonrpc_is_request(
|
||||
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(
|
||||
struct wfp_jsonrpc_request * request);
|
||||
|
||||
|
@ -3,17 +3,14 @@
|
||||
#include <stdlib.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);
|
||||
|
||||
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];
|
||||
struct wfp_message * message = malloc(sizeof(struct wfp_message));
|
||||
message->data = data;
|
||||
message->length = length;
|
||||
message->raw_data = NULL;
|
||||
|
||||
json_dumpb(value, message->data, length, JSON_COMPACT);
|
||||
message->raw_data = data - LWS_PRE;
|
||||
|
||||
return message;
|
||||
}
|
||||
|
@ -8,7 +8,6 @@
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse_provider/impl/util/slist.h"
|
||||
|
||||
struct wfp_message
|
||||
@ -24,8 +23,10 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern struct wfp_message * wfp_message_create(
|
||||
json_t const * value);
|
||||
extern struct wfp_message *
|
||||
wfp_message_create(
|
||||
char * data,
|
||||
size_t length);
|
||||
|
||||
extern void wfp_message_dispose(
|
||||
struct wfp_message * message);
|
||||
|
@ -51,11 +51,10 @@ wfp_impl_message_writer_take_message(
|
||||
writer->is_finished = true;
|
||||
}
|
||||
|
||||
struct wfp_message * message = malloc(sizeof(struct wfp_message));
|
||||
message->raw_data = wfp_impl_json_writer_take_data(writer->json_writer, &message->length);
|
||||
message->data = &(message->raw_data[LWS_PRE]);
|
||||
size_t length;
|
||||
char * data = wfp_impl_json_writer_take_data(writer->json_writer, &length);
|
||||
|
||||
return message;
|
||||
return wfp_message_create(data, length);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -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)
|
||||
{
|
||||
json_t * request = json_object();
|
||||
|
@ -1,16 +1,22 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstring>
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
TEST(wfp_message, create)
|
||||
{
|
||||
json_t * value = json_object();
|
||||
|
||||
struct wfp_message * message = wfp_message_create(value);
|
||||
size_t length = 3;
|
||||
char * raw_data = reinterpret_cast<char*>(malloc(LWS_PRE + length));
|
||||
char * data = raw_data + LWS_PRE;
|
||||
snprintf(data, length, "{}");
|
||||
|
||||
struct wfp_message * message = wfp_message_create(data, length);
|
||||
ASSERT_NE(nullptr, message);
|
||||
ASSERT_EQ(2, message->length);
|
||||
ASSERT_TRUE(0 == strncmp("{}", message->data, 2));
|
||||
ASSERT_EQ(3, message->length);
|
||||
ASSERT_STREQ("{}", message->data);
|
||||
|
||||
wfp_message_dispose(message);
|
||||
json_decref(value);
|
||||
}
|
||||
|
@ -3,16 +3,22 @@
|
||||
#include "webfuse_provider/impl/message.h"
|
||||
#include "webfuse_provider/impl/util/slist.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct wfp_slist_item * create_message(char const * content)
|
||||
{
|
||||
json_t * value = json_object();
|
||||
json_object_set_new(value, "content", json_string(content));
|
||||
struct wfp_message * message = wfp_message_create(value);
|
||||
size_t length = strlen(content);
|
||||
char * raw_data = reinterpret_cast<char*>(malloc(LWS_PRE + length));
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user