2012-03-14 09:57:29 -06:00
|
|
|
/*
|
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2011 Petr Mrázek <peterix@gmail.com>
|
|
|
|
|
|
|
|
A thread-safe logging console with a line editor for windows.
|
|
|
|
|
|
|
|
Based on linenoise win32 port,
|
|
|
|
copyright 2010, Jon Griffiths <jon_p_griffiths at yahoo dot com>.
|
|
|
|
All rights reserved.
|
|
|
|
Based on linenoise, copyright 2010, Salvatore Sanfilippo <antirez at gmail dot com>.
|
|
|
|
The original linenoise can be found at: http://github.com/antirez/linenoise
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
to endorse or promote products derived from this software without
|
|
|
|
specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <istream>
|
|
|
|
#include <string>
|
2012-03-15 15:05:33 -06:00
|
|
|
#include <stdint.h>
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
#include "RemoteServer.h"
|
2012-03-17 02:09:30 -06:00
|
|
|
#include "RemoteTools.h"
|
|
|
|
|
2012-03-15 15:05:33 -06:00
|
|
|
#include "PassiveSocket.h"
|
2012-03-14 09:57:29 -06:00
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "MiscUtils.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <memory>
|
2018-07-07 04:13:55 -06:00
|
|
|
#include <thread>
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2018-07-02 21:53:56 -06:00
|
|
|
#include "json/json.h"
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2018-07-02 21:53:56 -06:00
|
|
|
using namespace DFHack;
|
2017-11-24 23:04:02 -07:00
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
using dfproto::CoreTextNotification;
|
|
|
|
using dfproto::CoreTextFragment;
|
|
|
|
using google::protobuf::MessageLite;
|
|
|
|
|
2012-03-17 05:36:42 -06:00
|
|
|
bool readFullBuffer(CSimpleSocket *socket, void *buf, int size);
|
|
|
|
bool sendRemoteMessage(CSimpleSocket *socket, int16_t id,
|
|
|
|
const ::google::protobuf::MessageLite *msg, bool size_ready);
|
|
|
|
|
2018-07-07 04:13:55 -06:00
|
|
|
std::mutex ServerMain::access_{};
|
|
|
|
bool ServerMain::blocked_{};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct BlockedException : std::exception {
|
|
|
|
const char* what() const noexcept override
|
|
|
|
{
|
|
|
|
return "Core has blocked all connection. This should have been catched.";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace DFHack {
|
|
|
|
|
|
|
|
struct BlockGuard {
|
|
|
|
std::lock_guard<std::mutex> lock;
|
|
|
|
BlockGuard() :
|
|
|
|
lock{ServerMain::access_}
|
|
|
|
{
|
|
|
|
if (ServerMain::blocked_)
|
|
|
|
throw BlockedException{};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2012-03-17 05:36:42 -06:00
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
RPCService::RPCService()
|
|
|
|
{
|
|
|
|
owner = NULL;
|
|
|
|
holder = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RPCService::~RPCService()
|
|
|
|
{
|
2012-03-15 03:01:23 -06:00
|
|
|
if (holder)
|
|
|
|
holder->detach_connection(this);
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
for (size_t i = 0; i < functions.size(); i++)
|
|
|
|
delete functions[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerFunctionBase *RPCService::getFunction(const std::string &name)
|
|
|
|
{
|
|
|
|
assert(owner);
|
|
|
|
return lookup[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
void RPCService::finalize(ServerConnection *owner, std::vector<ServerFunctionBase*> *ftable)
|
|
|
|
{
|
|
|
|
this->owner = owner;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < functions.size(); i++)
|
|
|
|
{
|
|
|
|
auto fn = functions[i];
|
|
|
|
|
|
|
|
fn->id = (int16_t)ftable->size();
|
|
|
|
ftable->push_back(fn);
|
|
|
|
|
|
|
|
lookup[fn->name] = fn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:22:04 -06:00
|
|
|
void RPCService::dumpMethods(std::ostream & out) const
|
|
|
|
{
|
|
|
|
for (auto fn : functions)
|
|
|
|
{
|
|
|
|
std::string in_name = fn->p_in_template->GetTypeName();
|
|
|
|
size_t last_dot = in_name.rfind('.');
|
|
|
|
if (last_dot != std::string::npos)
|
|
|
|
in_name = in_name.substr(last_dot + 1);
|
|
|
|
|
|
|
|
std::string out_name = fn->p_out_template->GetTypeName();
|
|
|
|
last_dot = out_name.rfind('.');
|
|
|
|
if (last_dot != std::string::npos)
|
|
|
|
out_name = out_name.substr(last_dot + 1);
|
|
|
|
|
|
|
|
out << "// RPC " << fn->name << " : " << in_name << " -> " << out_name << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
ServerConnection::ServerConnection(CActiveSocket *socket)
|
|
|
|
: socket(socket), stream(this)
|
|
|
|
{
|
|
|
|
in_error = false;
|
|
|
|
|
|
|
|
core_service = new CoreService();
|
|
|
|
core_service->finalize(this, &functions);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerConnection::~ServerConnection()
|
|
|
|
{
|
|
|
|
in_error = true;
|
|
|
|
socket->Close();
|
|
|
|
delete socket;
|
|
|
|
|
|
|
|
for (auto it = plugin_services.begin(); it != plugin_services.end(); ++it)
|
|
|
|
delete it->second;
|
|
|
|
|
|
|
|
delete core_service;
|
|
|
|
}
|
|
|
|
|
2012-03-15 03:01:23 -06:00
|
|
|
ServerFunctionBase *ServerConnection::findFunction(color_ostream &out, const std::string &plugin, const std::string &name)
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
2012-03-15 03:01:23 -06:00
|
|
|
RPCService *svc;
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
if (plugin.empty())
|
2012-03-15 03:01:23 -06:00
|
|
|
svc = core_service;
|
2012-03-14 09:57:29 -06:00
|
|
|
else
|
2012-03-15 03:01:23 -06:00
|
|
|
{
|
|
|
|
svc = plugin_services[plugin];
|
|
|
|
|
|
|
|
if (!svc)
|
|
|
|
{
|
|
|
|
Plugin *plug = Core::getInstance().plug_mgr->getPluginByName(plugin);
|
|
|
|
if (!plug)
|
|
|
|
{
|
|
|
|
out.printerr("No such plugin: %s\n", plugin.c_str());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
svc = plug->rpc_connect(out);
|
|
|
|
if (!svc)
|
|
|
|
{
|
|
|
|
out.printerr("Plugin %s doesn't export any RPC methods.\n", plugin.c_str());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-15 04:06:50 -06:00
|
|
|
svc->finalize(this, &functions);
|
2012-03-15 03:01:23 -06:00
|
|
|
plugin_services[plugin] = svc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc->getFunction(name);
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerConnection::connection_ostream::flush_proxy()
|
|
|
|
{
|
|
|
|
if (owner->in_error)
|
|
|
|
{
|
|
|
|
buffer.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CoreTextNotification msg;
|
|
|
|
|
|
|
|
for (auto it = buffer.begin(); it != buffer.end(); ++it)
|
|
|
|
{
|
|
|
|
auto frag = msg.add_fragments();
|
|
|
|
frag->set_text(it->second);
|
|
|
|
if (it->first >= 0)
|
|
|
|
frag->set_color(CoreTextFragment::Color(it->first));
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
2012-03-17 05:36:42 -06:00
|
|
|
if (!sendRemoteMessage(owner->socket, RPC_REPLY_TEXT, &msg, false))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
owner->in_error = true;
|
|
|
|
Core::printerr("Error writing text into client socket.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 04:13:55 -06:00
|
|
|
void ServerConnection::Accepted(CActiveSocket* socket)
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
2018-07-07 04:13:55 -06:00
|
|
|
std::thread{[](CActiveSocket* socket) {
|
|
|
|
try {
|
|
|
|
ServerConnection(socket).threadFn();
|
|
|
|
} catch (BlockedException e) {
|
|
|
|
}
|
|
|
|
}, socket}.detach();
|
2012-03-18 01:04:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerConnection::threadFn()
|
|
|
|
{
|
2012-03-14 09:57:29 -06:00
|
|
|
color_ostream_proxy out(Core::getInstance().getConsole());
|
|
|
|
|
|
|
|
/* Handshake */
|
|
|
|
|
|
|
|
{
|
|
|
|
RPCHandshakeHeader header;
|
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
if (!readFullBuffer(socket, &header, sizeof(header)))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out << "In RPC server: could not read handshake header." << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(header.magic, RPCHandshakeHeader::REQUEST_MAGIC, sizeof(header.magic)) ||
|
2012-06-14 03:15:37 -06:00
|
|
|
header.version < 1 || header.version > 255)
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out << "In RPC server: invalid handshake header." << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(header.magic, RPCHandshakeHeader::RESPONSE_MAGIC, sizeof(header.magic));
|
|
|
|
header.version = 1;
|
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
if (socket->Send((uint8*)&header, sizeof(header)) != sizeof(header))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out << "In RPC server: could not send handshake response." << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Processing */
|
|
|
|
|
|
|
|
std::cerr << "Client connection established." << endl;
|
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
while (!in_error) {
|
2012-03-15 01:07:43 -06:00
|
|
|
// Read the message
|
2012-03-14 09:57:29 -06:00
|
|
|
RPCMessageHeader header;
|
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
if (!readFullBuffer(socket, &header, sizeof(header)))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out.printerr("In RPC server: I/O error in receive header.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-10 07:19:21 -06:00
|
|
|
if ((DFHack::DFHackReplyCode)header.id == RPC_REQUEST_QUIT)
|
2012-03-14 09:57:29 -06:00
|
|
|
break;
|
|
|
|
|
2012-03-17 05:36:42 -06:00
|
|
|
if (header.size < 0 || header.size > RPCMessageHeader::MAX_MESSAGE_SIZE)
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out.printerr("In RPC server: invalid received size %d.\n", header.size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-04 13:37:04 -06:00
|
|
|
std::unique_ptr<uint8_t[]> buf(new uint8_t[header.size]);
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
if (!readFullBuffer(socket, buf.get(), header.size))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out.printerr("In RPC server: I/O error in receive %d bytes of data.\n", header.size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//out.print("Handling %d:%d\n", header.id, header.size);
|
|
|
|
|
2012-03-15 01:07:43 -06:00
|
|
|
// Find and call the function
|
|
|
|
int in_size = header.size;
|
2018-07-07 04:13:55 -06:00
|
|
|
BlockGuard lock;
|
2012-03-15 01:07:43 -06:00
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
ServerFunctionBase *fn = vector_get(functions, header.id);
|
2012-03-14 09:57:29 -06:00
|
|
|
MessageLite *reply = NULL;
|
|
|
|
command_result res = CR_FAILURE;
|
|
|
|
|
|
|
|
if (!fn)
|
|
|
|
{
|
2012-03-18 01:04:15 -06:00
|
|
|
stream.printerr("RPC call of invalid id %d\n", header.id);
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-03 22:18:12 -06:00
|
|
|
if (((fn->flags & SF_ALLOW_REMOTE) != SF_ALLOW_REMOTE) && strcmp(socket->GetClientAddr(), "127.0.0.1") != 0)
|
|
|
|
{
|
|
|
|
stream.printerr("In call to %s: forbidden host: %s\n", fn->name, socket->GetClientAddr());
|
|
|
|
}
|
2017-10-02 08:25:16 -06:00
|
|
|
else if (!fn->in()->ParseFromArray(buf.get(), header.size))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
2012-03-18 01:04:15 -06:00
|
|
|
stream.printerr("In call to %s: could not decode input args.\n", fn->name);
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 05:36:42 -06:00
|
|
|
buf.reset();
|
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
reply = fn->out();
|
2012-03-18 01:04:15 -06:00
|
|
|
|
|
|
|
if (fn->flags & SF_DONT_SUSPEND)
|
|
|
|
{
|
|
|
|
res = fn->execute(stream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CoreSuspender suspend;
|
|
|
|
res = fn->execute(stream);
|
|
|
|
}
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 01:07:43 -06:00
|
|
|
// Flush all text output
|
2012-03-18 01:04:15 -06:00
|
|
|
if (in_error)
|
2012-03-14 09:57:29 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
//out.print("Answer %d:%d\n", res, reply);
|
|
|
|
|
2012-03-15 01:07:43 -06:00
|
|
|
// Send reply
|
2012-03-17 05:36:42 -06:00
|
|
|
int out_size = (reply ? reply->ByteSize() : 0);
|
|
|
|
|
|
|
|
if (out_size > RPCMessageHeader::MAX_MESSAGE_SIZE)
|
|
|
|
{
|
2012-03-18 01:04:15 -06:00
|
|
|
stream.printerr("In call to %s: reply too large: %d.\n",
|
2012-03-17 05:36:42 -06:00
|
|
|
(fn ? fn->name : "UNKNOWN"), out_size);
|
|
|
|
res = CR_LINK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
stream.flush();
|
2012-03-15 01:07:43 -06:00
|
|
|
|
2012-03-14 09:57:29 -06:00
|
|
|
if (res == CR_OK && reply)
|
|
|
|
{
|
2012-03-18 01:04:15 -06:00
|
|
|
if (!sendRemoteMessage(socket, RPC_REPLY_RESULT, reply, true))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out.printerr("In RPC server: I/O error in send result.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
header.id = RPC_REPLY_FAIL;
|
|
|
|
header.size = res;
|
|
|
|
|
2012-03-18 01:04:15 -06:00
|
|
|
if (socket->Send((uint8_t*)&header, sizeof(header)) != sizeof(header))
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
|
|
|
out.printerr("In RPC server: I/O error in send failure code.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 01:07:43 -06:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
if (fn)
|
|
|
|
{
|
2012-03-17 10:04:15 -06:00
|
|
|
fn->reset((fn->flags & SF_CALLED_ONCE) ||
|
|
|
|
(out_size > 128*1024 || in_size > 32*1024));
|
2012-03-15 01:07:43 -06:00
|
|
|
}
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Shutting down client connection." << endl;
|
|
|
|
}
|
|
|
|
|
2018-07-07 04:13:55 -06:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct ServerMainImpl : public ServerMain {
|
|
|
|
CPassiveSocket socket;
|
|
|
|
static void threadFn(std::promise<bool> promise, int port);
|
|
|
|
ServerMainImpl(std::promise<bool> promise, int port);
|
|
|
|
~ServerMainImpl();
|
|
|
|
};
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-07 04:13:55 -06:00
|
|
|
ServerMainImpl::ServerMainImpl(std::promise<bool> promise, int port) :
|
|
|
|
socket{}
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
2018-07-07 04:13:55 -06:00
|
|
|
socket.Initialize();
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2017-11-24 23:04:02 -07:00
|
|
|
std::string filename("dfhack-config/remote-server.json");
|
|
|
|
|
|
|
|
Json::Value configJson;
|
2017-10-03 22:18:12 -06:00
|
|
|
|
2017-11-24 23:04:02 -07:00
|
|
|
std::ifstream inFile(filename, std::ios_base::in);
|
2018-02-13 07:32:42 -07:00
|
|
|
|
|
|
|
bool allow_remote = false;
|
|
|
|
|
2017-11-24 23:04:02 -07:00
|
|
|
if (inFile.is_open())
|
2017-10-03 22:18:12 -06:00
|
|
|
{
|
2017-11-24 23:04:02 -07:00
|
|
|
inFile >> configJson;
|
|
|
|
inFile.close();
|
|
|
|
|
2018-02-13 07:32:42 -07:00
|
|
|
allow_remote = configJson.get("allow_remote", "false").asBool();
|
|
|
|
}
|
2017-11-24 23:04:02 -07:00
|
|
|
|
2018-07-02 21:53:56 -06:00
|
|
|
// rewrite/normalize config file
|
2017-11-24 23:04:02 -07:00
|
|
|
configJson["allow_remote"] = allow_remote;
|
2018-07-02 21:53:56 -06:00
|
|
|
configJson["port"] = configJson.get("port", RemoteClient::DEFAULT_PORT);
|
2017-11-24 23:04:02 -07:00
|
|
|
|
|
|
|
std::ofstream outFile(filename, std::ios_base::trunc);
|
|
|
|
|
|
|
|
if (outFile.is_open())
|
|
|
|
{
|
|
|
|
outFile << configJson;
|
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
|
2018-07-02 21:53:56 -06:00
|
|
|
std::cerr << "Listening on port " << port << (allow_remote ? " (remote enabled)" : "") << std::endl;
|
2018-07-07 04:13:55 -06:00
|
|
|
const char* addr = allow_remote ? NULL : "127.0.0.1";
|
|
|
|
if (!socket.Listen(addr, port)) {
|
|
|
|
promise.set_value(false);
|
|
|
|
return;
|
2017-10-03 22:18:12 -06:00
|
|
|
}
|
2018-07-07 04:13:55 -06:00
|
|
|
promise.set_value(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerMainImpl::~ServerMainImpl()
|
|
|
|
{
|
|
|
|
socket.Close();
|
|
|
|
}
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2018-07-07 04:13:55 -06:00
|
|
|
std::future<bool> ServerMain::listen(int port)
|
|
|
|
{
|
|
|
|
std::promise<bool> promise;
|
|
|
|
auto rv = promise.get_future();
|
|
|
|
std::thread{&ServerMainImpl::threadFn, std::move(promise), port}.detach();
|
|
|
|
return rv;
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
|
2018-07-07 04:13:55 -06:00
|
|
|
void ServerMainImpl::threadFn(std::promise<bool> promise, int port)
|
2012-03-14 09:57:29 -06:00
|
|
|
{
|
2018-07-07 04:13:55 -06:00
|
|
|
ServerMainImpl server{std::move(promise), port};
|
2012-03-14 09:57:29 -06:00
|
|
|
|
2018-07-07 04:13:55 -06:00
|
|
|
CActiveSocket *client = nullptr;
|
|
|
|
|
|
|
|
try {
|
|
|
|
while ((client = server.socket.Accept()) != NULL)
|
|
|
|
{
|
|
|
|
BlockGuard lock;
|
|
|
|
ServerConnection::Accepted(client);
|
|
|
|
client = nullptr;
|
|
|
|
}
|
|
|
|
} catch(BlockedException e) {
|
|
|
|
if (client)
|
|
|
|
client->Close();
|
|
|
|
delete client;
|
2012-03-14 09:57:29 -06:00
|
|
|
}
|
|
|
|
}
|
2018-07-07 04:13:55 -06:00
|
|
|
|
|
|
|
void ServerMain::block()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock{access_};
|
|
|
|
blocked_ = true;
|
|
|
|
}
|