64 lines
2.4 KiB
C++
64 lines
2.4 KiB
C++
#ifndef NO_CPP0X
|
|
#include "ServerChannel.h"
|
|
#include "../core/ApiServerInterop.h"
|
|
#include "ApiServer.h"
|
|
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
|
|
namespace bts {
|
|
namespace api {
|
|
|
|
|
|
struct ServerChannel::ServerChannelImpl {
|
|
ServerChannelImpl(Quickbus& qbus, const std::string& service_type, const std::string& qualifier, ApiServer& server):
|
|
_server(server),
|
|
_core(api_server_create(&qbus, service_type.c_str(), qualifier.c_str(), dispatch_trampoline, this))
|
|
{
|
|
}
|
|
//Need to guaruntee this object is stationary for the api_server_create trampoline
|
|
ServerChannelImpl(const ServerChannelImpl& ) = delete;
|
|
ServerChannelImpl(ServerChannelImpl&&) = delete;
|
|
|
|
static void dispatch_trampoline(const uint8_t* buffer, size_t request_size, ReplyHandler* callback, void* extra) {
|
|
assert(extra != nullptr);
|
|
ServerChannelImpl* pimpl = static_cast<ServerChannelImpl*>(extra);
|
|
pimpl->dispatch(buffer, request_size, callback);
|
|
}
|
|
|
|
void dispatch(const uint8_t* request_buffer, size_t request_length, ReplyHandler* callback) {
|
|
assert(request_buffer != nullptr);
|
|
assert(request_length > 1);
|
|
size_t header_length = request_buffer[0];
|
|
assert(request_length >= header_length);
|
|
RequestHeader header;
|
|
if (header.ParsePartialFromArray(request_buffer + 1, header_length - 1)) {
|
|
google::protobuf::io::ArrayInputStream stream(request_buffer + header_length, request_length - header_length);
|
|
_server.dispatch(header, stream, [=](const uint8_t* buffer, size_t length) {
|
|
ReplyHandler* handler = callback;
|
|
api_server_call_callback(&handler, buffer, (int)length);
|
|
});
|
|
}
|
|
}
|
|
|
|
ApiServer& _server;
|
|
ApiServerCore* _core;
|
|
};
|
|
|
|
|
|
ServerChannel::ServerChannel(Quickbus& qbus, const std::string& service_type, const std::string& qualifier, ApiServer& server)
|
|
:_me(new ServerChannelImpl(qbus, service_type, qualifier, server))
|
|
{ }
|
|
ServerChannel::ServerChannel(ServerChannel&& other) : _me(std::move(other._me)) {}
|
|
|
|
ServerChannel::~ServerChannel() { }
|
|
|
|
|
|
TableHandle ServerChannel::create_table(const std::string& method_and_qualifier) const {
|
|
return api_server_create_table(_me->_core, method_and_qualifier.c_str());
|
|
}
|
|
void ServerChannel::publish(TableHandle table, StreamUpdate* changes, int change_count) const {
|
|
api_server_update_table(table, changes, change_count);
|
|
}
|
|
|
|
|
|
}}//namespace
|
|
#endif
|