better threading and allow more then 1 port

This commit is contained in:
Green Sky 2022-12-19 17:31:08 +01:00
parent e0938690c7
commit 7177d90c44
No known key found for this signature in database

View File

@ -511,28 +511,43 @@ int main(void) {
std::cout << "waiting for agent id\n"; std::cout << "waiting for agent id\n";
ctx.agent_set.get_future().wait(); ctx.agent_set.get_future().wait();
if (ctx.should_quit) { if (ctx.should_quit) {
tox_thread.join(); // wait for thread
return -1; return -1;
} }
std::cout << "starting vim ipc server\n"; std::cout << "starting vim ipc server\n";
if (zed_net_init() != 0) { if (zed_net_init() != 0) {
ctx.should_quit.store(true);
std::cerr << "zed_net_init failed: " << zed_net_get_error() << "\n"; std::cerr << "zed_net_init failed: " << zed_net_get_error() << "\n";
tox_thread.join(); // wait for thread
return -1; return -1;
} }
std::cout << "initialized zed_net\n"; std::cout << "initialized zed_net\n";
const uint16_t port {1337}; const uint16_t port_start {1337};
const uint16_t port_end {1437};
uint16_t port = port_start;
zed_net_socket_t listen_socket; zed_net_socket_t listen_socket;
bool found_free_port {false};
for (; port <= port_end; port++) {
if (zed_net_tcp_socket_open( if (zed_net_tcp_socket_open(
&listen_socket, &listen_socket,
port, // port port, // port
0, // non blocking 0, // non blocking
1 // listen 1 // listen
) != 0) { ) == 0) {
found_free_port = true;
break;
}
}
if (!found_free_port) {
ctx.should_quit.store(true);
std::cerr << "zed_net_tcp_socket_open failed: " << zed_net_get_error() << "\n"; std::cerr << "zed_net_tcp_socket_open failed: " << zed_net_get_error() << "\n";
zed_net_shutdown(); zed_net_shutdown();
tox_thread.join(); // wait for thread
return -1; return -1;
} }
@ -546,9 +561,11 @@ int main(void) {
zed_net_socket_t remote_socket; zed_net_socket_t remote_socket;
zed_net_address_t remote_address; zed_net_address_t remote_address;
if (zed_net_tcp_accept(&listen_socket, &remote_socket, &remote_address) != 0) { if (zed_net_tcp_accept(&listen_socket, &remote_socket, &remote_address) != 0) {
ctx.should_quit.store(true);
std::cerr << "zed_net_tcp_accept failed: " << zed_net_get_error() << "\n"; std::cerr << "zed_net_tcp_accept failed: " << zed_net_get_error() << "\n";
zed_net_socket_close(&listen_socket); zed_net_socket_close(&listen_socket);
zed_net_shutdown(); zed_net_shutdown();
tox_thread.join(); // wait for thread
return -1; return -1;
} }
@ -568,10 +585,12 @@ int main(void) {
int64_t bytes_received {0}; int64_t bytes_received {0};
bytes_received = zed_net_tcp_socket_receive(&remote_socket, buffer->data(), buffer->size()); bytes_received = zed_net_tcp_socket_receive(&remote_socket, buffer->data(), buffer->size());
if (bytes_received < 0) { if (bytes_received < 0) {
ctx.should_quit.store(true);
std::cerr << "zed_net_tcp_socket_receive failed: " << zed_net_get_error() << "\n"; std::cerr << "zed_net_tcp_socket_receive failed: " << zed_net_get_error() << "\n";
zed_net_socket_close(&remote_socket); zed_net_socket_close(&remote_socket);
zed_net_socket_close(&listen_socket); zed_net_socket_close(&listen_socket);
zed_net_shutdown(); zed_net_shutdown();
tox_thread.join(); // wait for thread
return -1; return -1;
} else if (bytes_received == 0) { } else if (bytes_received == 0) {
std::cout << "got 0 bytes?\n"; std::cout << "got 0 bytes?\n";