1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

fixed client protocol low level API (#49)

* fixed client protocol low level API: enables usage of providing clients along with other websocket protocols

* fix: made some c'tors explicit
This commit is contained in:
Falk Werner
2020-02-19 22:44:56 +01:00
committed by GitHub
parent 9d83f1687e
commit abd6efe477
11 changed files with 460 additions and 46 deletions

196
test/fake_adapter_server.cc Normal file
View File

@@ -0,0 +1,196 @@
#include "fake_adapter_server.hpp"
#include "timeout_watcher.hpp"
#include "webfuse/core/util.h"
#include <libwebsockets.h>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
using webfuse_test::TimeoutWatcher;
#define DEFAULT_TIMEOUT (std::chrono::milliseconds(5 * 1000))
namespace
{
class IServer
{
public:
virtual ~IServer() = default;
virtual void onConnectionEstablished(struct lws * wsi) = 0;
virtual void onConnectionClosed(struct lws * wsi) = 0;
virtual void onMessageReceived(struct lws * wsi, char const * data, size_t length) = 0;
virtual void onWritable(struct lws * wsi) = 0;
};
}
extern "C"
{
static int wf_test_fake_adapter_server_callback(
struct lws * wsi,
enum lws_callback_reasons reason,
void * WF_UNUSED_PARAM(user),
void * in,
size_t len)
{
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
if (NULL == ws_protocol)
{
return 0;
}
auto * server = reinterpret_cast<IServer*>(ws_protocol->user);
switch(reason)
{
case LWS_CALLBACK_ESTABLISHED:
server->onConnectionEstablished(wsi);
break;
case LWS_CALLBACK_CLOSED:
server->onConnectionClosed(wsi);
break;
case LWS_CALLBACK_RECEIVE:
{
auto * data = reinterpret_cast<char const *>(in);
server->onMessageReceived(wsi, data, len);
}
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
server->onWritable(wsi);
break;
default:
break;
}
return 0;
}
}
namespace webfuse_test
{
class FakeAdapterServer::Private: public IServer
{
public:
explicit Private(int port)
: client_wsi(nullptr)
, message_received(false)
{
memset(ws_protocols, 0, sizeof(struct lws_protocols) * 2);
ws_protocols[0].name = "fs";
ws_protocols[0].callback = &wf_test_fake_adapter_server_callback;
ws_protocols[0].per_session_data_size = 0;
ws_protocols[0].user = reinterpret_cast<void*>(this);
memset(&info, 0, sizeof(struct lws_context_creation_info));
info.port = port;
info.mounts = NULL;
info.protocols =ws_protocols;
info.vhost_name = "localhost";
info.ws_ping_pong_interval = 10;
info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
context = lws_create_context(&info);
}
virtual ~Private()
{
lws_context_destroy(context);
}
void waitForConnection()
{
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
while (nullptr == client_wsi)
{
watcher.check();
lws_service(context, 100);
}
}
void onConnectionEstablished(struct lws * wsi) override
{
client_wsi = wsi;
}
void onConnectionClosed(struct lws * wsi) override
{
if (wsi == client_wsi)
{
client_wsi = nullptr;
}
}
void onMessageReceived(struct lws * wsi, char const * data, size_t length) override
{
if (wsi == client_wsi)
{
last_message.assign(length, *data);
message_received = true;
}
}
void onWritable(struct lws * wsi) override
{
if (!queue.empty())
{
std::string const & message = queue.front();
unsigned char * data = new unsigned char[LWS_PRE + message.size()];
memcpy(&data[LWS_PRE], message.c_str(), message.size());
lws_write(wsi, &data[LWS_PRE], message.size(), LWS_WRITE_TEXT);
delete[] data;
queue.pop();
if (!queue.empty())
{
lws_callback_on_writable(wsi);
}
}
}
private:
void send(std::string const & message)
{
if (nullptr != client_wsi)
{
queue.push(message);
lws_callback_on_writable(client_wsi);
}
}
struct lws * client_wsi;
bool message_received;
struct lws_protocols ws_protocols[2];
struct lws_context_creation_info info;
struct lws_context * context;
std::vector<char> last_message;
std::queue<std::string> queue;
};
FakeAdapterServer::FakeAdapterServer(int port)
: d(new Private(port))
{
}
FakeAdapterServer::~FakeAdapterServer()
{
delete d;
}
void FakeAdapterServer::waitForConnection()
{
d->waitForConnection();
}
}

View File

@@ -0,0 +1,24 @@
#ifndef WF_TEST_FAKE_SERVER_HPP
#define WF_TEST_FAKE_SERVER_HPP
#include <libwebsockets.h>
namespace webfuse_test
{
class FakeAdapterServer
{
FakeAdapterServer(FakeAdapterServer const &) = delete;
FakeAdapterServer & operator=(FakeAdapterServer const &) = delete;
public:
explicit FakeAdapterServer(int port);
~FakeAdapterServer();
void waitForConnection();
private:
class Private;
Private * d;
};
}
#endif

View File

@@ -0,0 +1,72 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <webfuse/provider/client_protocol.h>
#include <webfuse/provider/client_config.h>
#include "fake_adapter_server.hpp"
#include <cstring>
#include <thread>
#include <atomic>
using webfuse_test::FakeAdapterServer;
using testing::_;
namespace
{
struct Context
{
lws_context * context;
std::atomic<bool> isShutdownRequested;
};
void run(Context * context)
{
while (!context->isShutdownRequested)
{
lws_service(context->context, 100);
}
}
}
TEST(client_protocol, connect)
{
FakeAdapterServer server(54321);
wfp_client_config * config = wfp_client_config_create();
wfp_client_protocol * protocol = wfp_client_protocol_create(config);
struct lws_protocols protocols[2];
memset(protocols, 0, sizeof(struct lws_protocols) * 2);
protocols[0].name = "fs";
wfp_client_protocol_init_lws(protocol, &protocols[0]);
struct lws_context_creation_info info;
memset(&info, 0, sizeof(struct lws_context_creation_info));
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.uid = -1;
info.gid = -1;
struct lws_context * context = lws_create_context(&info);
wfp_client_protocol_connect(protocol, context, "ws://localhost:54321/");
Context ctx;
ctx.context = context;
ctx.isShutdownRequested = false;
std::thread client_thread(run, &ctx);
server.waitForConnection();
ctx.isShutdownRequested = true;
client_thread.join();
lws_context_destroy(context);
wfp_client_protocol_dispose(protocol);
wfp_client_config_dispose(config);
}

44
test/timeout_watcher.cc Normal file
View File

@@ -0,0 +1,44 @@
#include "timeout_watcher.hpp"
#include <stdexcept>
using std::chrono::milliseconds;
using std::chrono::duration_cast;
using std::chrono::steady_clock;
namespace
{
milliseconds now()
{
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch());
}
}
namespace webfuse_test
{
TimeoutWatcher::TimeoutWatcher(milliseconds timeout)
: startedAt(now())
, timeout_(timeout)
{
}
TimeoutWatcher::~TimeoutWatcher()
{
}
bool TimeoutWatcher::isTimeout()
{
return (now() - startedAt) > timeout_;
}
void TimeoutWatcher::check()
{
if (isTimeout())
{
throw std::runtime_error("timeout");
}
}
}

26
test/timeout_watcher.hpp Normal file
View File

@@ -0,0 +1,26 @@
#ifndef WF_TEST_TIMEOUT_WATCHER_HPP
#define WF_TEST_TIMEOUT_WATCHER_HPP
#include <chrono>
namespace webfuse_test
{
class TimeoutWatcher final
{
TimeoutWatcher(TimeoutWatcher const & other) = delete;
TimeoutWatcher& operator=(TimeoutWatcher const & other) = delete;
public:
explicit TimeoutWatcher(std::chrono::milliseconds timeout);
~TimeoutWatcher();
bool isTimeout();
void check();
private:
std::chrono::milliseconds startedAt;
std::chrono::milliseconds timeout_;
};
}
#endif