From 9be877c68503c35f9f72c8b92bd11454e4fcad97 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Tue, 8 Dec 2009 15:41:50 +0100 Subject: ZMQII-26: Use zero-copy for large messages --- src/zmq_engine.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/zmq_engine.cpp') diff --git a/src/zmq_engine.cpp b/src/zmq_engine.cpp index baa0eee..e8e689d 100644 --- a/src/zmq_engine.cpp +++ b/src/zmq_engine.cpp @@ -26,17 +26,19 @@ zmq::zmq_engine_t::zmq_engine_t (io_thread_t *parent_, fd_t fd_) : io_object_t (parent_), + inbuf (NULL), insize (0), inpos (0), + outbuf (NULL), outsize (0), outpos (0), inout (NULL) { // Allocate read & write buffer. - inbuf = (unsigned char*) malloc (in_batch_size); - zmq_assert (inbuf); - outbuf = (unsigned char*) malloc (out_batch_size); - zmq_assert (outbuf); + inbuf_storage = (unsigned char*) malloc (in_batch_size); + zmq_assert (inbuf_storage); + outbuf_storage = (unsigned char*) malloc (out_batch_size); + zmq_assert (outbuf_storage); // Initialise the underlying socket. int rc = tcp_socket.open (fd_); @@ -45,8 +47,8 @@ zmq::zmq_engine_t::zmq_engine_t (io_thread_t *parent_, fd_t fd_) : zmq::zmq_engine_t::~zmq_engine_t () { - free (outbuf); - free (inbuf); + free (outbuf_storage); + free (inbuf_storage); } void zmq::zmq_engine_t::plug (i_inout *inout_) @@ -80,11 +82,12 @@ void zmq::zmq_engine_t::in_event () if (inpos == insize) { // Read as much data as possible to the read buffer. + inbuf = inbuf_storage; insize = tcp_socket.read (inbuf, in_batch_size); inpos = 0; // Check whether the peer has closed the connection. - if (insize == -1) { + if (insize == (size_t) -1) { insize = 0; error (); return; @@ -111,7 +114,9 @@ void zmq::zmq_engine_t::out_event () // If write buffer is empty, try to read new data from the encoder. if (outpos == outsize) { - outsize = encoder.read (outbuf, out_batch_size); + outbuf = outbuf_storage; + outsize = out_batch_size; + encoder.read (&outbuf, &outsize); outpos = 0; // If there is no data to send, stop polling for output. -- cgit v1.2.3