Fix for issue #100 : Let possible to start the server on an address other than localhost

Add a new function for keeping the API stable
This commit is contained in:
ocack 2024-04-30 11:44:15 +02:00 committed by Ben Clayton
parent 37c744c294
commit 0f943b3357
2 changed files with 17 additions and 2 deletions

View File

@ -44,13 +44,21 @@ class Server {
// create() constructs and returns a new Server. // create() constructs and returns a new Server.
static std::unique_ptr<Server> create(); static std::unique_ptr<Server> create();
// start() begins listening for connections on the given port. // start() begins listening for connections on localhost and the given port.
// callback will be called for each connection. // callback will be called for each connection.
// onError will be called for any connection errors. // onError will be called for any connection errors.
virtual bool start(int port, virtual bool start(int port,
const OnConnect& callback, const OnConnect& callback,
const OnError& onError = ignoreErrors) = 0; const OnError& onError = ignoreErrors) = 0;
// start() begins listening for connections on the given specific address and port.
// callback will be called for each connection.
// onError will be called for any connection errors.
virtual bool start(const char* address,
int port,
const OnConnect& callback,
const OnError& onError = ignoreErrors) = 0;
// stop() stops listening for connections. // stop() stops listening for connections.
// stop() is implicitly called on destruction. // stop() is implicitly called on destruction.
virtual void stop() = 0; virtual void stop() = 0;

View File

@ -32,10 +32,17 @@ class Impl : public dap::net::Server {
bool start(int port, bool start(int port,
const OnConnect& onConnect, const OnConnect& onConnect,
const OnError& onError) override { const OnError& onError) override {
return start("localhost", port, onConnect, onError);
}
bool start(const char* address,
int port,
const OnConnect& onConnect,
const OnError& onError) override {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
stopWithLock(); stopWithLock();
socket = std::unique_ptr<dap::Socket>( socket = std::unique_ptr<dap::Socket>(
new dap::Socket("localhost", std::to_string(port).c_str())); new dap::Socket(address, std::to_string(port).c_str()));
if (!socket->isOpen()) { if (!socket->isOpen()) {
onError("Failed to open socket"); onError("Failed to open socket");