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 libuv+un...@googlegroups.com.
To post to this group, send email to li...@googlegroups.com.
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.
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(uv_write_t *req, int status) {
...
uv_close((uv_handle_t*)req, TCPUVServer::on_close);
uv_close((uv_handle_t*)handle->handle, TCPUVServer::on_close);
void TCPUVServer::on_close(uv_handle_t* handle) {
TCPUVServerLDBG << "on_close ";
free(handle);
}
--
while(run) {
uv_run(loop, UV_RUN_NOWAIT);
usleep(100);
}
is the right way to use this run mode?