Open evgeshkastrong opened 5 years ago
can I send data to connected client from separate thread (not from loop callbacks/threads) ? [..] is it normal to disconnect client from separate thread [..] can i create in the same application in separate threads, tcpserver thread [..] each use own loop
Libuv's event loop APIs are - by design - not thread-safe, with few exceptions, all documented.
If you only want do some computing that doesn't touch libuv, you can use uv_async_send()
to coordinate between your worker thread and the event loop thread.
You can send handles to other event loops with uv_write2()
to a connected uv_pipe_t
handle. That works not just across threads but also processes. See test/benchmark-multi-accept.c for an example.
What is best way to extract unique clientId of new connected one for my ProtocolTcpServer [..] it is uv_tcp_t* client
Depends on how unique it needs to be.
Since you're calling malloc() you presumably also call free() and therefore the address need not be unique across the lifetime of the program; it's only unique for the lifetime of that handle.
Not really a libuv question, though.
how much threads use default loop ? can i increase this count manually ?
1 and no, IIUC. uv_loop_init()
calls CreateIoCompletionPort()
with NumberOfConcurrentThreads == 1
.
If you have a pressing need for more threads, please open an issue over at libuv/libuv to discuss. You should motivate why though.
thanks for answers ! more things became clear now.
in most cases the practical way to dev serious app is to create more higher level abstraction above libuv class - to realize protocol layer ProtocolTcpServer above UvTcpServer, so
1) can I send data to connected client from separate thread (not from loop callbacks/threads) ?
write_req_t req = (write_req_t)malloc(sizeof(write_req_t)); req->buf = uv_buf_init(buf->base, nread); uv_write((uv_write_t*)req, client, &req->buf, 1, echo_write);
if no - what is the best strategy to do it in my case ?
2) What is best way to extract unique clientId of new connected one for my ProtocolTcpServer (add new client and info to collection of connected clients ) as i can see: uv_tcp_t client = (uv_tcp_t)malloc(sizeof(uv_tcp_t)); uv_tcp_init(loop, client); if (uv_accept(server, (uv_stream_t*)client) == 0) {
it is uv_tcp_t client
yes ? is it normal to disconnect client from separate thread uv_close((uv_handle_t)client, NULL);
3) how much threads use default loop ? can i increase this count manually ? previously i had experience in dev native windows iocp tcp server, as a rule - threads count = number_of_cores when creating iocompletion port, in libuv case - if i have much connected clients, does libuv automatically allocate new native threads or it uses something like threads count = number_of_cores ? in boost::asio i could do
for(int i = 0; i < threads_count; i++) { create_thread(... io_service.run() ...); // boost::asio loop in some threads run }
what the situation with libuv loop run ? i cant find information about this
4) can i create in the same application in separate threads, tcpserver thread tcpclient thread pipeserver thread each use own loop, to work at the same time and not intersect with each other or there can be created only obe loop per application ?