c - Does libuv provide any facilities to attach a buffer to a connection and re use it -
i evaluating libuv library c/c++ server writing. protocol length prefixed can read 32 bit integer stream should able tell size of buffer should allocate. documentation says uv_read_start function might called multiple times.
uv_extern int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb, uv_read_cb read_cb);
since using length prefixed protocol, once know right size of buffer allocate , re use subsequent reads till have received bytes. there easy way libuv? right seems uv_alloc_cb function has take care of this. can associate buffer stream object instead of putting in map or something?
since using length prefixed protocol, not allocate buffer on heap @ till can read first 4 bytes (32 bits). possible me allocate on stack buffer of size 4 , have uv_read_cb function heap allocation? uv_read_cb function invoked synchronously part of uv_read_start function? if seems should able allocate on stack when know don't have buffer attached stream.
answering own question. found answers on libuv mailing list here: https://groups.google.com/forum/#!topic/libuv/frnqv_qggaa
copying details here if link becomes unavailable:
attaching own data structure handle:
the handle has void* data
field yours use. can make point auxiliary structure store length , buffer.
alternatively, can embed uv_tcp_t in structure, embedding structure container_of. it's not standard c macro can find definition , usage examples in libuv/ source tree. benefit pointer arithmetic, saves level of pointer indirection.
stack allocation receiving buffer:
no, that's not possible. proper way of thinking alloc_cb returns buffer libuv fill data sometime in future. stress on "sometime" because there no guarantees when happen; may immediate, may seconds (or minutes) away.
Comments
Post a Comment