Using buffer serialization to send out messages

This commit is contained in:
Pierre Pronchery 2012-12-04 00:47:17 +01:00
parent 81a336cce2
commit 7ffe22d76f

View File

@ -454,31 +454,44 @@ static TCPSocket * _tcp_socket_new_fd(TCP * tcp, int fd)
/* tcp_socket_queue */ /* tcp_socket_queue */
static int _tcp_socket_queue(TCPSocket * tcpsocket, Buffer * buffer) static int _tcp_socket_queue(TCPSocket * tcpsocket, Buffer * buffer)
{ {
size_t inc;
uint32_t len; uint32_t len;
char * p; char * p;
Variable * v;
Buffer * b = NULL;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%d)\n", __func__, tcpsocket->fd); fprintf(stderr, "DEBUG: %s(%d)\n", __func__, tcpsocket->fd);
#endif #endif
/* FIXME serialize the buffer instead */ /* serialize the buffer */
len = buffer_get_size(buffer); v = variable_new(VT_BUFFER, buffer);
if((inc = ((len + sizeof(len)) | (INC - 1)) + 1) < len + sizeof(len)) b = buffer_new(0, NULL);
inc = len + sizeof(len); if(v == NULL || b == NULL || variable_serialize(v, b, 0) != 0)
if((p = realloc(tcpsocket->bufout, tcpsocket->bufout_cnt + inc)) {
== NULL) if(v != NULL)
variable_delete(v);
if(b != NULL)
buffer_delete(b);
return -1; return -1;
}
variable_delete(v);
len = buffer_get_size(b);
/* FIXME queue the serialized buffer directly as a message instead */
if((p = realloc(tcpsocket->bufout, tcpsocket->bufout_cnt + len))
== NULL)
{
buffer_delete(b);
return -1;
}
tcpsocket->bufout = p; tcpsocket->bufout = p;
/* FIXME use the proper endian */ memcpy(&p[tcpsocket->bufout_cnt], buffer_get_data(b), len);
memcpy(tcpsocket->bufout, &len, sizeof(len));
memcpy(&tcpsocket->bufout[sizeof(len)], buffer_get_data(buffer), len);
/* register the callback if necessary */ /* register the callback if necessary */
if(tcpsocket->bufout_cnt == 0) if(tcpsocket->bufout_cnt == 0)
event_register_io_write(tcpsocket->tcp->helper->event, event_register_io_write(tcpsocket->tcp->helper->event,
tcpsocket->fd, tcpsocket->fd,
(EventIOFunc)_tcp_socket_callback_write, (EventIOFunc)_tcp_socket_callback_write,
tcpsocket); tcpsocket);
tcpsocket->bufout_cnt += len + sizeof(len); tcpsocket->bufout_cnt += len;
buffer_delete(b);
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%d) => %d\n", __func__, tcpsocket->fd, 0); fprintf(stderr, "DEBUG: %s(%d) => %d\n", __func__, tcpsocket->fd, 0);
#endif #endif