to be more clear:
void TCPUVServer::on_write_end(uv_write_t *req, int status) {
TCPUVServerLDBG << "on_write_end ";
//delete the sent data
chaos::common::data::SerializationBuffer *ser =
static_cast<chaos::common::data::SerializationBuffer*>(req->data);
if(ser) delete(ser);
if (status) {
TCPUVServerLERR << "error on_write_end:" << status;
}
uv_close((uv_handle_t*)req->handle, TCPUVServer::on_close);
free(req);
}
on_write_end is the last step on my protocollo (create a server-> accept
more socket for every connection i read and write the ack and the i close
the connection).
is on_write_end method well managed the on_close method do the free action.
Il giorno venerdì 4 luglio 2014 16:35:54 UTC+2, Fedor Indutny ha scritto:
>
> Yes.
>
>
> On Fri, Jul 4, 2014 at 6:34 PM, Claudio Bisegni <[email protected]
> <javascript:>> wrote:
>
>> thank for answer, so if i have mean,
>> i can call free on on_connect(uv_connect_t *connection, int status) for
>> the connection parameter and call onlcose only for the real socket..
>> right?
>>
>> Il giorno mercoledì 2 luglio 2014 11:47:02 UTC+2, Fedor Indutny ha
>> scritto:
>>>
>>> Hello!
>>>
>>> You don't need to `uv_close()` any request, just release the memory in
>>> their callbacks.
>>> This applies to `uv_write_t`, `uv_connect_t` and everything that doesn't
>>> inherit from handle.
>>>
>>> `uv_close()` is closing underlying socket, so please call it only when
>>> you are done with the socket and
>>> do not want to receive or send anything on it.
>>>
>>> Cheers,
>>> Fedor.
>>>
>>>
>>> On Wed, Jul 2, 2014 at 1:40 PM, Claudio Bisegni <[email protected]>
>>> wrote:
>>>
>>>> Hi i have some problem to well understand the libuv functionality on
>>>> when deallocate the handler and stream.
>>>>
>>>> i have a cpp class that have a one uv_tcp_t instance for the client.
>>>> When i need to send data to one server i do the following step:
>>>>
>>>> i use a struct RequestInfo * ri to keep the reference to the
>>>> tcp_connection for close it.
>>>>
>>>> 1) allocate the connection with ri->tcp_connection = (uv_connect_t*)
>>>> malloc(sizeof(uv_connect_t));
>>>> 2) call the uv_tcp_connect(ri->tcp_connection, &client, (const struct
>>>> sockaddr*)&req_addr, TCPUVClient::on_connect); where the 'client' is
>>>> the unique instance of uv_tcp_t;
>>>> 3) below my on connect:
>>>>
>>>> void TCPUVClient::on_connect(uv_connect_t *connection, int status) {
>>>>
>>>> RequestInfo *ri = static_cast<RequestInfo*>(connection->data);
>>>>
>>>> if (status) {
>>>>
>>>> TCPUVClientLERR << "error on_connect" << status;
>>>>
>>>> uv_close((uv_handle_t*)connection,TCPUVClient::on_close);
>>>>
>>>> DEALLOCATE_REQUEST_INFO(ri)
>>>>
>>>> return;
>>>>
>>>> }
>>>>
>>>>
>>>> //get the data to send
>>>>
>>>> ri->serializationBuffer = ri->messageInfo->message->getBSONData();
>>>>
>>>> //initialize uv buffer
>>>>
>>>> uv_buf_t buf = uv_buf_init((char*)ri->serializationBuffer->getBufferPtr(),
>>>> (unsigned int)ri->serializationBuffer->getBufferLen());
>>>>
>>>> //connect data to stream handler
>>>>
>>>> connection->handle->data = connection->data;
>>>>
>>>> uv_write_t request;
>>>>
>>>> uv_write(&request, connection->handle, &buf, 1,
>>>> TCPUVClient::on_write_end);
>>>>
>>>> }
>>>>
>>>>
>>>> 4) after the data is sent i need to read the ack answer with:
>>>>
>>>> void TCPUVClient::on_ack_read(uv_stream_t *stream, ssize_t nread,
>>>> const uv_buf_t* buf) {
>>>>
>>>> //delete the forward info class
>>>>
>>>> RequestInfo *ri = static_cast<RequestInfo*>(stream->data);
>>>>
>>>> stream->data = NULL;
>>>>
>>>> if (nread>0) {
>>>>
>>>> auto_ptr<chaos::common::data::CDataWrapper> ack_result(new
>>>> chaos::common::data::CDataWrapper(buf->base));
>>>>
>>>> LDBG_ << "TCPUVClient::on_ack_read message
>>>> result------------------------------";
>>>>
>>>> LDBG_ << ack_result->getJSONString();
>>>>
>>>> LDBG_ << "TCPUVClient::on_ack_read message
>>>> result------------------------------";
>>>>
>>>> //uv_close((uv_handle_t*) server, TCPUVClient::on_close);
>>>>
>>>> return;
>>>>
>>>> }
>>>>
>>>> uv_close((uv_handle_t*)stream, TCPUVClient::on_close);
>>>>
>>>> free(buf->base);
>>>>
>>>> DEALLOCATE_REQUEST_INFO(ri)
>>>>
>>>> }
>>>>
>>>>
>>>> void TCPUVClient::on_write_end(uv_write_t *req, int status) {
>>>>
>>>> RequestInfo *ri = static_cast<RequestInfo*>(req->data);
>>>>
>>>> if (status) {
>>>>
>>>> TCPUVClientLERR << "error on_write_end ->" << status;
>>>>
>>>> uv_close((uv_handle_t*)ri->tcp_connection,TCPUVClient::on_close);
>>>>
>>>> DEALLOCATE_REQUEST_INFO(ri)
>>>>
>>>> return;
>>>>
>>>> }
>>>>
>>>> uv_read_start(req->handle, TCPUVClient::alloc_buffer,
>>>> TCPUVClient::on_ack_read);
>>>>
>>>> }
>>>>
>>>>
>>>> my question is when i need to call uv_close for:
>>>>
>>>> *uv_write_t *req* contained in on_write_end
>>>>
>>>> *uv_stream_t *stream* contained in on_ack_read
>>>>
>>>> *uv_connect_t** *connection* contained in on_connection
>>>>
>>>>
>>>> the client work but i don't now if i forgot to free memory.
>>>>
>>>>
>>>> thanks in advanced for all group. And there is a good example on how
>>>> work well with libuv?
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "libuv" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To post to this group, send email to [email protected].
>>>>
>>>> Visit this group at http://groups.google.com/group/libuv.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "libuv" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] <javascript:>
>> .
>> Visit this group at http://groups.google.com/group/libuv.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.