uv_write documentation

1,254 views
Skip to first unread message

Ashish

unread,
Aug 9, 2013, 1:06:07 PM8/9/13
to li...@googlegroups.com


Can someone please give me detail info on parameters of uv_write? I searched around a lot but there is no single documentation available on this basic important function.


All I want is to write to client socket. So I should be good to go if am having essential three params: 1. socket handle, 2. data to write and 3. data length
But uv_write seems need five parameters, three of which are structures, and so am getting very confused here.

Also, as I can see in many examples, the buffer being written to socket, is passed in first and third parameter both. Why is so ?

Tnx,
Ashish

Ben Noordhuis

unread,
Aug 9, 2013, 1:35:40 PM8/9/13
to li...@googlegroups.com
I'm not sure what you mean by that but uv_write() is basically used like this:

uv_stream_t* stream = ...;
uv_write_t* req = malloc(sizeof(*req));
uv_buf_t buf = { .base = "test", .len = 4 };
int nbufs = 1;
int err = uv_write(req, stream, &buf, nbufs, write_cb);

Every write has a write request associated with it. Once the data has
been written out or when an error happens, your write callback gets
invoked.

That's all there is to it, really. You can send more than one buffer
with a single call but the common use case seems to be 'one buffer,
one call.'

Ashish

unread,
Aug 9, 2013, 3:12:27 PM8/9/13
to li...@googlegroups.com


thanks! looks simpler to me now :)

In many examples I saw they do:
req->data = buf.base;

why is tht (when buf is already being passed as third param) ?

Ashish

unread,
Aug 10, 2013, 11:01:43 AM8/10/13
to li...@googlegroups.com


Well, I've further issues on this now. I've written simple server that calls uv_queue_work which processes client request and writes response back to the client.
After three/four writes I got error "Assertion failed: handle->write_queue_size >= req->queued_bytes, file src\win\tcp.c, line 1035"
However, if I don't call "uv_queue_work" and process request in "on_read" itself, I didn't get this error.

I searched on github found this: https://github.com/joyent/libuv/issues/409
Where it is mentioned:
  • You should allocate a new uv_write_t for every write to a handle. Do no reuse, free, move, or clobber it's memory until the write callback is made.
  • Ditto for the uv_tcp_t handle; do not reuse, free, move, or clobber it's memory until the close callback is made
 
Now I like to know: How can I use "uv_write" through "uv_queue_work" ?

Tnx,
Ashish

Ashish

unread,
Aug 10, 2013, 12:53:40 PM8/10/13
to li...@googlegroups.com

Ok I found answers to my own questions. Sharing here please verify if my findings are correct:

1. In 'uv_write' it is common practice to pass buffer pointer to 'uv_work_t*' because we get same structure ptr in 'after_write' callback where we can free the buffer allocation

2. When calling 'uv_write' through 'uv_queue_work' we should call it from 'uv_after_work_cb' callback instead of 'uv_work_cb' (Although not sure why but this has worked for me)

Tnx,
Ashish

Ben Noordhuis

unread,
Aug 10, 2013, 3:13:24 PM8/10/13
to li...@googlegroups.com
On Sat, Aug 10, 2013 at 6:53 PM, Ashish <aashee...@gmail.com> wrote:
> Ok I found answers to my own questions. Sharing here please verify if my
> findings are correct:
>
> 1. In 'uv_write' it is common practice to pass buffer pointer to
> 'uv_work_t*' because we get same structure ptr in 'after_write' callback
> where we can free the buffer allocation
>
> 2. When calling 'uv_write' through 'uv_queue_work' we should call it from
> 'uv_after_work_cb' callback instead of 'uv_work_cb' (Although not sure why
> but this has worked for me)

That's because the after_work_cb runs on the event loop thread while
the work_cb runs in another thread.

I admit I'm mystified by what you are doing, combining uv_queue_work()
and uv_write(). One is for running a unit of work in another thread
while the other is for doing asynchronous writes to tcp/tty/pipe
handles.

Ashish

unread,
Aug 11, 2013, 12:42:15 AM8/11/13
to li...@googlegroups.com


>>I admit I'm mystified by what you are doing, combining uv_queue_work()
>>and uv_write().  One is for running a unit of work in another thread
>>while the other is for doing asynchronous writes to tcp/tty/pipe
>>handles.

I was trying to send outcome of the work done in another thread (uv_queue_work), to the client.
However, the mistake I seems doing was calling uv_write from same thread.
Reply all
Reply to author
Forward
0 new messages