uNetworking / uSockets

Miniscule cross-platform eventing, networking & crypto for async applications
Apache License 2.0
1.29k stars 268 forks source link

About hub.connect on windows run as blocking mode on uWebSocket #7

Closed Furujiang closed 5 years ago

Furujiang commented 6 years ago

Hi I could not find a place to quest under uWebsocket, so I post it here: On winodws 10, while calling hub.connect to wws, it will block several seconds. if I add predefine SOCK_CLOEXEC;SOCK_NONBLOCK; to project, error occured with identifier "accept4" not found error.

I am using the hub to connect to multi wws, so I need this "connect" function excute as nonblocking, could you please advice? thank you!

ghost commented 6 years ago

It blocks on DNS lookup, that's the only thing blocking. It's a valid issue for uSockets too, I haven't solved it yet. DNS lookup should happen in a thread pool.

Furujiang commented 6 years ago

Thank you for quick response, I changed my design to use uws in multithread because I found it don't work for creating multi group and make connect calls, only last connect call works. It works well in each thread for 1 hub. Thank you again!

ghost commented 6 years ago

You shouldn't hack with threading. If you require nonblocking DNS lookup then do that part yourself with a third party library and pass the IP to connect

Furujiang commented 6 years ago

the main problem for single thread is: below code only connect to url2 will work fine, no callback recieved from url1, is this a bug? thank you!

sample code: hub = new Hub(); group1 = hub->createGroup(); group1->onConnection([&](WebSocket ws, HttpRequest req) {xxx}); group1->onError([&](void self) {xxx}; group1->onDisconnection([&](WebSocket ws, int code, char data, sizet length) {xxx}); hub->connect(url_1, nullptr, {}, 5000, group1);

group2 = hub_->createGroup(); group2->onConnection([&](WebSocket ws, HttpRequest req) {xxx}); group2->onError([&](void self) {xxx}; group2->onDisconnection([&](WebSocket ws, int code, char data, sizet length) {xxx}); hub->connect(url_2, nullptr, {}, 5000, group2); hub->run();

ghost commented 6 years ago

Well since the first connection will block on DNS resolution the second connection might time out? Both should work.