Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

How do you get the port number and detect when a connection has closed? #212

Open
LB-- opened this issue Feb 21, 2018 · 2 comments
Open

Comments

@LB--
Copy link

LB-- commented Feb 21, 2018

I'm implementing a native desktop app that has the user sign in with OAuth. In order for this to be possible, the app has to launch a local webserver on a random available port and use 127.0.0.1:port as the return address so that the app can receive the authentication information (or cancellation). Then when the user is directed to the local webserver, they should see a message telling them to return to the app, and the webserver should shutdown.

I am running into problems with this every step of the way. Firstly, the port number - using port 0 lets the operating system choose a random available port, and then the port can be queried with acceptor->local_endpoint().port(). However, since the acceptor is only initialized in start() and start() is called in a separate thread, there's no way to reliably know when it can be accessed. My current workaround is to just wait 100 milliseconds after starting the server and just hope that that is enough time, which is an obviously horrible hack.

Secondly, there's no way to know when the response has finished being sent. Since the user handler functions must return before the response can be sent, my app has no way of knowing when it is safe to shut down the server. I have tried counting the number of open connections but there is for some reason always one open connection at all times despite setting close_connection_after_response, I don't know what this is from. As a workaround I could include an image in the response body and shut down the server when that image is requested, but that's another obviously horrible hack on a number of levels.

Do you have any suggestions for how to deal with these problems?

@eidheim
Copy link
Owner

eidheim commented Feb 22, 2018

Thank you for reporting these issue. Especially the case when the server is using port 0 has to be fixed.

Regarding your second issue, here is a sample showing how to know if a response has finished or not:

#include "server_http.hpp"

using namespace std;
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;

int main() {
  HttpServer server;
  server.config.port = 8080;

  server.default_resource["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
    response->write("Hello");
    response->send([](const SimpleWeb::error_code &ec) {
      // response has been sent or errored
    });
  };

  server.start();
}

@LB--
Copy link
Author

LB-- commented Feb 22, 2018

here is a sample showing how to know if a response has finished or not:

Thank you, I'm a bit embarrassed I didn't find that myself. I can confirm that works exactly as needed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants