2017-03-02 09:28:07 +00:00
|
|
|
#include "ipc.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
JsonMessage* as_message(char* ptr) {
|
|
|
|
return reinterpret_cast<JsonMessage*>(ptr);
|
|
|
|
}
|
2017-03-03 06:16:28 +00:00
|
|
|
|
|
|
|
std::string NameToServerName(const std::string& name) {
|
2017-03-04 01:45:20 +00:00
|
|
|
return name + "server";
|
2017-03-03 06:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string NameToClientName(const std::string& name, int client_id) {
|
2017-03-04 01:45:20 +00:00
|
|
|
return name + "client" + std::to_string(client_id);
|
2017-03-03 06:16:28 +00:00
|
|
|
}
|
2017-03-02 09:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* JsonMessage::payload() {
|
|
|
|
return reinterpret_cast<const char*>(this) + sizeof(JsonMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonMessage::SetPayload(size_t payload_size, const char* payload) {
|
|
|
|
char* payload_dest = reinterpret_cast<char*>(this) + sizeof(JsonMessage);
|
|
|
|
this->payload_size = payload_size;
|
|
|
|
memcpy(payload_dest, payload, payload_size);
|
|
|
|
}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
void BaseIpcMessageElided::Serialize(Writer& writer) {}
|
2017-03-03 08:43:54 +00:00
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
void BaseIpcMessageElided::Deserialize(Reader& reader) {}
|
2017-03-02 09:28:07 +00:00
|
|
|
|
2017-03-03 08:43:54 +00:00
|
|
|
IpcRegistry* IpcRegistry::instance_ = nullptr;
|
2017-03-02 09:28:07 +00:00
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
std::unique_ptr<BaseIpcMessageElided> IpcRegistry::Allocate(int id) {
|
|
|
|
return std::unique_ptr<BaseIpcMessageElided>((*allocators)[id]());
|
2017-03-02 09:28:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 06:16:28 +00:00
|
|
|
IpcDirectionalChannel::IpcDirectionalChannel(const std::string& name) {
|
2017-03-02 09:28:07 +00:00
|
|
|
local_block = new char[shmem_size];
|
2017-03-04 01:45:20 +00:00
|
|
|
shared = CreatePlatformSharedMemory(name + "memory");
|
|
|
|
mutex = CreatePlatformMutex(name + "mutex");
|
2017-03-02 09:28:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 06:16:28 +00:00
|
|
|
IpcDirectionalChannel::~IpcDirectionalChannel() {
|
2017-03-02 09:28:07 +00:00
|
|
|
delete[] local_block;
|
|
|
|
}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
void IpcDirectionalChannel::PushMessage(BaseIpcMessageElided* message) {
|
2017-03-02 09:28:07 +00:00
|
|
|
rapidjson::StringBuffer output;
|
|
|
|
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(output);
|
|
|
|
writer.SetFormatOptions(
|
|
|
|
rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
|
|
|
|
writer.SetIndent(' ', 2);
|
|
|
|
message->Serialize(writer);
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
//std::cout << "Sending message with id " << message->runtime_id() << " (hash " << message->hashed_runtime_id() << ")" << std::endl;
|
|
|
|
|
2017-03-02 09:28:07 +00:00
|
|
|
size_t payload_size = strlen(output.GetString());
|
|
|
|
assert(payload_size < shmem_size && "Increase shared memory size, payload will never fit");
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
bool did_log = false;
|
|
|
|
while (true) {
|
|
|
|
if (!first) {
|
|
|
|
if (!did_log) {
|
|
|
|
std::cout << "[info]: shmem full, waiting" << std::endl; // TODO: remove
|
|
|
|
did_log = true;
|
|
|
|
}
|
2017-03-04 01:45:20 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(16));
|
2017-03-02 09:28:07 +00:00
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
std::unique_ptr<PlatformScopedMutexLock> lock = CreatePlatformScopedMutexLock(mutex.get());
|
|
|
|
|
|
|
|
// Try again later when there is room in shared memory.
|
|
|
|
if ((*shared->shared_bytes_used + sizeof(JsonMessage) + payload_size) >= shmem_size)
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
get_free_message()->message_id = message->hashed_runtime_id();
|
2017-03-02 09:28:07 +00:00
|
|
|
get_free_message()->SetPayload(payload_size, output.GetString());
|
|
|
|
|
|
|
|
*shared->shared_bytes_used += sizeof(JsonMessage) + get_free_message()->payload_size;
|
|
|
|
assert(*shared->shared_bytes_used < shmem_size);
|
2017-03-03 08:12:11 +00:00
|
|
|
get_free_message()->message_id = -1;
|
2017-03-02 09:28:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
std::vector<std::unique_ptr<BaseIpcMessageElided>> IpcDirectionalChannel::TakeMessages() {
|
2017-03-02 09:28:07 +00:00
|
|
|
size_t remaining_bytes = 0;
|
|
|
|
// Move data from shared memory into a local buffer. Do this
|
|
|
|
// before parsing the blocks so that other processes can begin
|
|
|
|
// posting data as soon as possible.
|
|
|
|
{
|
|
|
|
std::unique_ptr<PlatformScopedMutexLock> lock = CreatePlatformScopedMutexLock(mutex.get());
|
|
|
|
remaining_bytes = *shared->shared_bytes_used;
|
|
|
|
|
|
|
|
memcpy(local_block, shared->shared_start, *shared->shared_bytes_used);
|
|
|
|
*shared->shared_bytes_used = 0;
|
2017-03-03 08:12:11 +00:00
|
|
|
get_free_message()->message_id = -1;
|
2017-03-02 09:28:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
std::vector<std::unique_ptr<BaseIpcMessageElided>> result;
|
2017-03-02 09:28:07 +00:00
|
|
|
|
|
|
|
char* message = local_block;
|
|
|
|
while (remaining_bytes > 0) {
|
2017-03-05 02:16:23 +00:00
|
|
|
std::unique_ptr<BaseIpcMessageElided> base_message = IpcRegistry::instance()->Allocate(as_message(message)->message_id);
|
2017-03-02 09:28:07 +00:00
|
|
|
|
|
|
|
rapidjson::Document document;
|
|
|
|
document.Parse(as_message(message)->payload(), as_message(message)->payload_size);
|
|
|
|
bool has_error = document.HasParseError();
|
|
|
|
auto error = document.GetParseError();
|
|
|
|
|
|
|
|
base_message->Deserialize(document);
|
|
|
|
|
|
|
|
result.emplace_back(std::move(base_message));
|
|
|
|
|
|
|
|
remaining_bytes -= sizeof(JsonMessage) + as_message(message)->payload_size;
|
|
|
|
message = message + sizeof(JsonMessage) + as_message(message)->payload_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2017-03-03 06:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IpcServer::IpcServer(const std::string& name)
|
|
|
|
: name_(name), server_(NameToServerName(name)) {}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
void IpcServer::SendToClient(int client_id, BaseIpcMessageElided* message) {
|
2017-03-03 06:16:28 +00:00
|
|
|
// Find or create the client.
|
|
|
|
auto it = clients_.find(client_id);
|
|
|
|
if (it == clients_.end())
|
2017-03-04 01:45:20 +00:00
|
|
|
clients_[client_id] = MakeUnique<IpcDirectionalChannel>(NameToClientName(name_, client_id));
|
2017-03-03 06:16:28 +00:00
|
|
|
|
|
|
|
clients_[client_id]->PushMessage(message);
|
|
|
|
}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
std::vector<std::unique_ptr<BaseIpcMessageElided>> IpcServer::TakeMessages() {
|
2017-03-03 06:16:28 +00:00
|
|
|
return server_.TakeMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
IpcClient::IpcClient(const std::string& name, int client_id)
|
|
|
|
: server_(NameToServerName(name)), client_(NameToClientName(name, client_id)) {}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
void IpcClient::SendToServer(BaseIpcMessageElided* message) {
|
2017-03-03 06:16:28 +00:00
|
|
|
server_.PushMessage(message);
|
|
|
|
}
|
|
|
|
|
2017-03-05 02:16:23 +00:00
|
|
|
std::vector<std::unique_ptr<BaseIpcMessageElided>> IpcClient::TakeMessages() {
|
2017-03-03 06:16:28 +00:00
|
|
|
return client_.TakeMessages();
|
2017-03-04 01:45:20 +00:00
|
|
|
}
|