From b3bac1760735703a11297df3d0e2a2e5252aa45e Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Tue, 16 Aug 2011 12:44:34 +0200 Subject: tcp_engine renamed to stream engine The engine was not used exclusively for TCP connections. Rather it was used to handle any socket with SOCK_STREAM semantics. The class was renamed to reflect its true function. Signed-off-by: Martin Sustrik --- src/Makefile.am | 4 +- src/ipc_connecter.cpp | 4 +- src/ipc_listener.cpp | 4 +- src/stream_engine.cpp | 374 +++++++++++++++++++++++++++++++++++++++++++++++++ src/stream_engine.hpp | 102 ++++++++++++++ src/tcp_connecter.cpp | 4 +- src/tcp_engine.cpp | 374 ------------------------------------------------- src/tcp_engine.hpp | 99 ------------- src/tcp_listener.cpp | 4 +- src/vtcp_connecter.cpp | 4 +- src/vtcp_listener.cpp | 4 +- 11 files changed, 490 insertions(+), 487 deletions(-) create mode 100644 src/stream_engine.cpp create mode 100644 src/stream_engine.hpp delete mode 100644 src/tcp_engine.cpp delete mode 100644 src/tcp_engine.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 9f5ea65..93c51ed 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -60,9 +60,9 @@ libzmq_la_SOURCES = \ signaler.hpp \ socket_base.hpp \ stdint.hpp \ + stream_engine.hpp \ sub.hpp \ tcp_connecter.hpp \ - tcp_engine.hpp \ tcp_listener.hpp \ thread.hpp \ trie.hpp \ @@ -117,9 +117,9 @@ libzmq_la_SOURCES = \ session.cpp \ signaler.cpp \ socket_base.cpp \ + stream_engine.cpp \ sub.cpp \ tcp_connecter.cpp \ - tcp_engine.cpp \ tcp_listener.cpp \ thread.cpp \ trie.cpp \ diff --git a/src/ipc_connecter.cpp b/src/ipc_connecter.cpp index 4b8f3bf..2862db4 100644 --- a/src/ipc_connecter.cpp +++ b/src/ipc_connecter.cpp @@ -25,7 +25,7 @@ #include #include -#include "tcp_engine.hpp" +#include "stream_engine.hpp" #include "io_thread.hpp" #include "platform.hpp" #include "random.hpp" @@ -99,7 +99,7 @@ void zmq::ipc_connecter_t::out_event () } // Create the engine object for this connection. - tcp_engine_t *engine = new (std::nothrow) tcp_engine_t (fd, options); + stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options); alloc_assert (engine); // Attach the engine to the corresponding session object. diff --git a/src/ipc_listener.cpp b/src/ipc_listener.cpp index 200a2bc..9e35af3 100644 --- a/src/ipc_listener.cpp +++ b/src/ipc_listener.cpp @@ -26,7 +26,7 @@ #include -#include "tcp_engine.hpp" +#include "stream_engine.hpp" #include "io_thread.hpp" #include "session.hpp" #include "config.hpp" @@ -78,7 +78,7 @@ void zmq::ipc_listener_t::in_event () return; // Create the engine object for this connection. - tcp_engine_t *engine = new (std::nothrow) tcp_engine_t (fd, options); + stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options); alloc_assert (engine); // Choose I/O thread to run connecter in. Given that we are already diff --git a/src/stream_engine.cpp b/src/stream_engine.cpp new file mode 100644 index 0000000..15e7c21 --- /dev/null +++ b/src/stream_engine.cpp @@ -0,0 +1,374 @@ +/* + Copyright (c) 2007-2011 iMatix Corporation + Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#include "platform.hpp" +#if defined ZMQ_HAVE_WINDOWS +#include "windows.hpp" +#else +#include +#include +#include +#include +#include +#include +#include +#endif + +#include +#include + +#include "stream_engine.hpp" +#include "io_thread.hpp" +#include "session.hpp" +#include "config.hpp" +#include "err.hpp" +#include "ip.hpp" + +zmq::stream_engine_t::stream_engine_t (fd_t fd_, const options_t &options_) : + s (fd_), + inpos (NULL), + insize (0), + decoder (in_batch_size, options_.maxmsgsize), + outpos (NULL), + outsize (0), + encoder (out_batch_size), + session (NULL), + leftover_session (NULL), + options (options_), + plugged (false) +{ + // Get the socket into non-blocking mode. + unblock_socket (s); + + // Set the socket buffer limits for the underlying socket. + if (options.sndbuf) { + int rc = setsockopt (s, SOL_SOCKET, SO_SNDBUF, + (char*) &options.sndbuf, sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + } + if (options.rcvbuf) { + int rc = setsockopt (s, SOL_SOCKET, SO_RCVBUF, + (char*) &options.rcvbuf, sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + } + +#if defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_FREEBSD + // Make sure that SIGPIPE signal is not generated when writing to a + // connection that was already closed by the peer. + int set = 1; + int rc = setsockopt (s, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof (int)); + errno_assert (rc == 0); +#endif +} + +zmq::stream_engine_t::~stream_engine_t () +{ + zmq_assert (!plugged); + + if (s != retired_fd) { +#ifdef ZMQ_HAVE_WINDOWS + int rc = closesocket (s); + wsa_assert (rc != SOCKET_ERROR); +#else + int rc = close (s); + errno_assert (rc == 0); +#endif + s = retired_fd; + } +} + +void zmq::stream_engine_t::plug (io_thread_t *io_thread_, session_t *session_) +{ + zmq_assert (!plugged); + plugged = true; + leftover_session = NULL; + + // Connect to session object. + zmq_assert (!session); + zmq_assert (session_); + encoder.set_session (session_); + decoder.set_session (session_); + session = session_; + + // Connect to I/O threads poller object. + io_object_t::plug (io_thread_); + handle = add_fd (s); + set_pollin (handle); + set_pollout (handle); + + // Flush all the data that may have been already received downstream. + in_event (); +} + +void zmq::stream_engine_t::unplug () +{ + zmq_assert (plugged); + plugged = false; + + // Cancel all fd subscriptions. + rm_fd (handle); + + // Disconnect from I/O threads poller object. + io_object_t::unplug (); + + // Disconnect from session object. + encoder.set_session (NULL); + decoder.set_session (NULL); + leftover_session = session; + session = NULL; +} + +void zmq::stream_engine_t::terminate () +{ + unplug (); + delete this; +} + +void zmq::stream_engine_t::in_event () +{ + bool disconnection = false; + + // If there's no data to process in the buffer... + if (!insize) { + + // Retrieve the buffer and read as much data as possible. + // Note that buffer can be arbitrarily large. However, we assume + // the underlying TCP layer has fixed buffer size and thus the + // number of bytes read will be always limited. + decoder.get_buffer (&inpos, &insize); + insize = read (inpos, insize); + + // Check whether the peer has closed the connection. + if (insize == (size_t) -1) { + insize = 0; + disconnection = true; + } + } + + // Push the data to the decoder. + size_t processed = decoder.process_buffer (inpos, insize); + + if (unlikely (processed == (size_t) -1)) { + disconnection = true; + } + else { + + // Stop polling for input if we got stuck. + if (processed < insize) { + + // This may happen if queue limits are in effect. + if (plugged) + reset_pollin (handle); + } + + // Adjust the buffer. + inpos += processed; + insize -= processed; + } + + // Flush all messages the decoder may have produced. + // If IO handler has unplugged engine, flush transient IO handler. + if (unlikely (!plugged)) { + zmq_assert (leftover_session); + leftover_session->flush (); + } else { + session->flush (); + } + + if (session && disconnection) + error (); +} + +void zmq::stream_engine_t::out_event () +{ + // If write buffer is empty, try to read new data from the encoder. + if (!outsize) { + + outpos = NULL; + encoder.get_data (&outpos, &outsize); + + // If IO handler has unplugged engine, flush transient IO handler. + if (unlikely (!plugged)) { + zmq_assert (leftover_session); + leftover_session->flush (); + return; + } + + // If there is no data to send, stop polling for output. + if (outsize == 0) { + reset_pollout (handle); + return; + } + } + + // If there are any data to write in write buffer, write as much as + // possible to the socket. Note that amount of data to write can be + // arbitratily large. However, we assume that underlying TCP layer has + // limited transmission buffer and thus the actual number of bytes + // written should be reasonably modest. + int nbytes = write (outpos, outsize); + + // Handle problems with the connection. + if (nbytes == -1) { + error (); + return; + } + + outpos += nbytes; + outsize -= nbytes; +} + +void zmq::stream_engine_t::activate_out () +{ + set_pollout (handle); + + // Speculative write: The assumption is that at the moment new message + // was sent by the user the socket is probably available for writing. + // Thus we try to write the data to socket avoiding polling for POLLOUT. + // Consequently, the latency should be better in request/reply scenarios. + out_event (); +} + +void zmq::stream_engine_t::activate_in () +{ + set_pollin (handle); + + // Speculative read. + in_event (); +} + +void zmq::stream_engine_t::error () +{ + zmq_assert (session); + session->detach (); + unplug (); + delete this; +} + +int zmq::stream_engine_t::write (const void *data_, size_t size_) +{ +#ifdef ZMQ_HAVE_WINDOWS + + int nbytes = send (s, (char*) data_, (int) size_, 0); + + // If not a single byte can be written to the socket in non-blocking mode + // we'll get an error (this may happen during the speculative write). + if (nbytes == SOCKET_ERROR && WSAGetLastError () == WSAEWOULDBLOCK) + return 0; + + // Signalise peer failure. + if (nbytes == -1 && ( + WSAGetLastError () == WSAENETDOWN || + WSAGetLastError () == WSAENETRESET || + WSAGetLastError () == WSAEHOSTUNREACH || + WSAGetLastError () == WSAECONNABORTED || + WSAGetLastError () == WSAETIMEDOUT || + WSAGetLastError () == WSAECONNRESET)) + return -1; + + wsa_assert (nbytes != SOCKET_ERROR); + return (size_t) nbytes; + +#else + + ssize_t nbytes = send (s, data_, size_, 0); + + // Several errors are OK. When speculative write is being done we may not + // be able to write a single byte from the socket. Also, SIGSTOP issued + // by a debugging tool can result in EINTR error. + if (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK || + errno == EINTR)) + return 0; + + // Signalise peer failure. + if (nbytes == -1 && (errno == ECONNRESET || errno == EPIPE)) + return -1; + + errno_assert (nbytes != -1); + return (size_t) nbytes; + +#endif +} + +int zmq::stream_engine_t::read (void *data_, size_t size_) +{ +#ifdef ZMQ_HAVE_WINDOWS + + int nbytes = recv (s, (char*) data_, (int) size_, 0); + + // If not a single byte can be read from the socket in non-blocking mode + // we'll get an error (this may happen during the speculative read). + if (nbytes == SOCKET_ERROR && WSAGetLastError () == WSAEWOULDBLOCK) + return 0; + + // Connection failure. + if (nbytes == -1 && ( + WSAGetLastError () == WSAENETDOWN || + WSAGetLastError () == WSAENETRESET || + WSAGetLastError () == WSAECONNABORTED || + WSAGetLastError () == WSAETIMEDOUT || + WSAGetLastError () == WSAECONNRESET || + WSAGetLastError () == WSAECONNREFUSED || + WSAGetLastError () == WSAENOTCONN)) + return -1; + + wsa_assert (nbytes != SOCKET_ERROR); + + // Orderly shutdown by the other peer. + if (nbytes == 0) + return -1; + + return (size_t) nbytes; + +#else + + ssize_t nbytes = recv (s, data_, size_, 0); + + // Several errors are OK. When speculative read is being done we may not + // be able to read a single byte from the socket. Also, SIGSTOP issued + // by a debugging tool can result in EINTR error. + if (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK || + errno == EINTR)) + return 0; + + // Signalise peer failure. + if (nbytes == -1 && (errno == ECONNRESET || errno == ECONNREFUSED || + errno == ETIMEDOUT || errno == EHOSTUNREACH)) + return -1; + + errno_assert (nbytes != -1); + + // Orderly shutdown by the peer. + if (nbytes == 0) + return -1; + + return (size_t) nbytes; + +#endif +} + diff --git a/src/stream_engine.hpp b/src/stream_engine.hpp new file mode 100644 index 0000000..ac9a5be --- /dev/null +++ b/src/stream_engine.hpp @@ -0,0 +1,102 @@ +/* + Copyright (c) 2007-2011 iMatix Corporation + Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef __ZMQ_STREAM_ENGINE_HPP_INCLUDED__ +#define __ZMQ_STREAM_ENGINE_HPP_INCLUDED__ + +#include + +#include "fd.hpp" +#include "i_engine.hpp" +#include "io_object.hpp" +#include "encoder.hpp" +#include "decoder.hpp" +#include "options.hpp" + +namespace zmq +{ + + // This engine handles any socket with SOCK_STREAM semantics, + // e.g. TCP socket or an UNIX domain socket. + + class stream_engine_t : public io_object_t, public i_engine + { + public: + + stream_engine_t (fd_t fd_, const options_t &options_); + ~stream_engine_t (); + + // i_engine interface implementation. + void plug (class io_thread_t *io_thread_, class session_t *session_); + void unplug (); + void terminate (); + void activate_in (); + void activate_out (); + + // i_poll_events interface implementation. + void in_event (); + void out_event (); + + private: + + // Function to handle network disconnections. + void error (); + + // Writes data to the socket. Returns the number of bytes actually + // written (even zero is to be considered to be a success). In case + // of error or orderly shutdown by the other peer -1 is returned. + int write (const void *data_, size_t size_); + + // Reads data from the socket (up to 'size' bytes). Returns the number + // of bytes actually read (even zero is to be considered to be + // a success). In case of error or orderly shutdown by the other + // peer -1 is returned. + int read (void *data_, size_t size_); + + // Underlying socket. + fd_t s; + + handle_t handle; + + unsigned char *inpos; + size_t insize; + decoder_t decoder; + + unsigned char *outpos; + size_t outsize; + encoder_t encoder; + + // The session this engine is attached to. + class session_t *session; + + // Detached transient session. + class session_t *leftover_session; + + options_t options; + + bool plugged; + + stream_engine_t (const stream_engine_t&); + const stream_engine_t &operator = (const stream_engine_t&); + }; + +} + +#endif diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index 7531445..10e1be6 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -22,7 +22,7 @@ #include #include "tcp_connecter.hpp" -#include "tcp_engine.hpp" +#include "stream_engine.hpp" #include "io_thread.hpp" #include "platform.hpp" #include "random.hpp" @@ -109,7 +109,7 @@ void zmq::tcp_connecter_t::out_event () tune_tcp_socket (fd); // Create the engine object for this connection. - tcp_engine_t *engine = new (std::nothrow) tcp_engine_t (fd, options); + stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options); alloc_assert (engine); // Attach the engine to the corresponding session object. diff --git a/src/tcp_engine.cpp b/src/tcp_engine.cpp deleted file mode 100644 index f938d71..0000000 --- a/src/tcp_engine.cpp +++ /dev/null @@ -1,374 +0,0 @@ -/* - Copyright (c) 2007-2011 iMatix Corporation - Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . -*/ - -#include "platform.hpp" -#if defined ZMQ_HAVE_WINDOWS -#include "windows.hpp" -#else -#include -#include -#include -#include -#include -#include -#include -#endif - -#include -#include - -#include "tcp_engine.hpp" -#include "io_thread.hpp" -#include "session.hpp" -#include "config.hpp" -#include "err.hpp" -#include "ip.hpp" - -zmq::tcp_engine_t::tcp_engine_t (fd_t fd_, const options_t &options_) : - s (fd_), - inpos (NULL), - insize (0), - decoder (in_batch_size, options_.maxmsgsize), - outpos (NULL), - outsize (0), - encoder (out_batch_size), - session (NULL), - leftover_session (NULL), - options (options_), - plugged (false) -{ - // Get the socket into non-blocking mode. - unblock_socket (s); - - // Set the socket buffer limits for the underlying socket. - if (options.sndbuf) { - int rc = setsockopt (s, SOL_SOCKET, SO_SNDBUF, - (char*) &options.sndbuf, sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - } - if (options.rcvbuf) { - int rc = setsockopt (s, SOL_SOCKET, SO_RCVBUF, - (char*) &options.rcvbuf, sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - } - -#if defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_FREEBSD - // Make sure that SIGPIPE signal is not generated when writing to a - // connection that was already closed by the peer. - int set = 1; - int rc = setsockopt (s, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof (int)); - errno_assert (rc == 0); -#endif -} - -zmq::tcp_engine_t::~tcp_engine_t () -{ - zmq_assert (!plugged); - - if (s != retired_fd) { -#ifdef ZMQ_HAVE_WINDOWS - int rc = closesocket (s); - wsa_assert (rc != SOCKET_ERROR); -#else - int rc = close (s); - errno_assert (rc == 0); -#endif - s = retired_fd; - } -} - -void zmq::tcp_engine_t::plug (io_thread_t *io_thread_, session_t *session_) -{ - zmq_assert (!plugged); - plugged = true; - leftover_session = NULL; - - // Connect to session object. - zmq_assert (!session); - zmq_assert (session_); - encoder.set_session (session_); - decoder.set_session (session_); - session = session_; - - // Connect to I/O threads poller object. - io_object_t::plug (io_thread_); - handle = add_fd (s); - set_pollin (handle); - set_pollout (handle); - - // Flush all the data that may have been already received downstream. - in_event (); -} - -void zmq::tcp_engine_t::unplug () -{ - zmq_assert (plugged); - plugged = false; - - // Cancel all fd subscriptions. - rm_fd (handle); - - // Disconnect from I/O threads poller object. - io_object_t::unplug (); - - // Disconnect from session object. - encoder.set_session (NULL); - decoder.set_session (NULL); - leftover_session = session; - session = NULL; -} - -void zmq::tcp_engine_t::terminate () -{ - unplug (); - delete this; -} - -void zmq::tcp_engine_t::in_event () -{ - bool disconnection = false; - - // If there's no data to process in the buffer... - if (!insize) { - - // Retrieve the buffer and read as much data as possible. - // Note that buffer can be arbitrarily large. However, we assume - // the underlying TCP layer has fixed buffer size and thus the - // number of bytes read will be always limited. - decoder.get_buffer (&inpos, &insize); - insize = read (inpos, insize); - - // Check whether the peer has closed the connection. - if (insize == (size_t) -1) { - insize = 0; - disconnection = true; - } - } - - // Push the data to the decoder. - size_t processed = decoder.process_buffer (inpos, insize); - - if (unlikely (processed == (size_t) -1)) { - disconnection = true; - } - else { - - // Stop polling for input if we got stuck. - if (processed < insize) { - - // This may happen if queue limits are in effect. - if (plugged) - reset_pollin (handle); - } - - // Adjust the buffer. - inpos += processed; - insize -= processed; - } - - // Flush all messages the decoder may have produced. - // If IO handler has unplugged engine, flush transient IO handler. - if (unlikely (!plugged)) { - zmq_assert (leftover_session); - leftover_session->flush (); - } else { - session->flush (); - } - - if (session && disconnection) - error (); -} - -void zmq::tcp_engine_t::out_event () -{ - // If write buffer is empty, try to read new data from the encoder. - if (!outsize) { - - outpos = NULL; - encoder.get_data (&outpos, &outsize); - - // If IO handler has unplugged engine, flush transient IO handler. - if (unlikely (!plugged)) { - zmq_assert (leftover_session); - leftover_session->flush (); - return; - } - - // If there is no data to send, stop polling for output. - if (outsize == 0) { - reset_pollout (handle); - return; - } - } - - // If there are any data to write in write buffer, write as much as - // possible to the socket. Note that amount of data to write can be - // arbitratily large. However, we assume that underlying TCP layer has - // limited transmission buffer and thus the actual number of bytes - // written should be reasonably modest. - int nbytes = write (outpos, outsize); - - // Handle problems with the connection. - if (nbytes == -1) { - error (); - return; - } - - outpos += nbytes; - outsize -= nbytes; -} - -void zmq::tcp_engine_t::activate_out () -{ - set_pollout (handle); - - // Speculative write: The assumption is that at the moment new message - // was sent by the user the socket is probably available for writing. - // Thus we try to write the data to socket avoiding polling for POLLOUT. - // Consequently, the latency should be better in request/reply scenarios. - out_event (); -} - -void zmq::tcp_engine_t::activate_in () -{ - set_pollin (handle); - - // Speculative read. - in_event (); -} - -void zmq::tcp_engine_t::error () -{ - zmq_assert (session); - session->detach (); - unplug (); - delete this; -} - -int zmq::tcp_engine_t::write (const void *data_, size_t size_) -{ -#ifdef ZMQ_HAVE_WINDOWS - - int nbytes = send (s, (char*) data_, (int) size_, 0); - - // If not a single byte can be written to the socket in non-blocking mode - // we'll get an error (this may happen during the speculative write). - if (nbytes == SOCKET_ERROR && WSAGetLastError () == WSAEWOULDBLOCK) - return 0; - - // Signalise peer failure. - if (nbytes == -1 && ( - WSAGetLastError () == WSAENETDOWN || - WSAGetLastError () == WSAENETRESET || - WSAGetLastError () == WSAEHOSTUNREACH || - WSAGetLastError () == WSAECONNABORTED || - WSAGetLastError () == WSAETIMEDOUT || - WSAGetLastError () == WSAECONNRESET)) - return -1; - - wsa_assert (nbytes != SOCKET_ERROR); - return (size_t) nbytes; - -#else - - ssize_t nbytes = send (s, data_, size_, 0); - - // Several errors are OK. When speculative write is being done we may not - // be able to write a single byte from the socket. Also, SIGSTOP issued - // by a debugging tool can result in EINTR error. - if (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK || - errno == EINTR)) - return 0; - - // Signalise peer failure. - if (nbytes == -1 && (errno == ECONNRESET || errno == EPIPE)) - return -1; - - errno_assert (nbytes != -1); - return (size_t) nbytes; - -#endif -} - -int zmq::tcp_engine_t::read (void *data_, size_t size_) -{ -#ifdef ZMQ_HAVE_WINDOWS - - int nbytes = recv (s, (char*) data_, (int) size_, 0); - - // If not a single byte can be read from the socket in non-blocking mode - // we'll get an error (this may happen during the speculative read). - if (nbytes == SOCKET_ERROR && WSAGetLastError () == WSAEWOULDBLOCK) - return 0; - - // Connection failure. - if (nbytes == -1 && ( - WSAGetLastError () == WSAENETDOWN || - WSAGetLastError () == WSAENETRESET || - WSAGetLastError () == WSAECONNABORTED || - WSAGetLastError () == WSAETIMEDOUT || - WSAGetLastError () == WSAECONNRESET || - WSAGetLastError () == WSAECONNREFUSED || - WSAGetLastError () == WSAENOTCONN)) - return -1; - - wsa_assert (nbytes != SOCKET_ERROR); - - // Orderly shutdown by the other peer. - if (nbytes == 0) - return -1; - - return (size_t) nbytes; - -#else - - ssize_t nbytes = recv (s, data_, size_, 0); - - // Several errors are OK. When speculative read is being done we may not - // be able to read a single byte from the socket. Also, SIGSTOP issued - // by a debugging tool can result in EINTR error. - if (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK || - errno == EINTR)) - return 0; - - // Signalise peer failure. - if (nbytes == -1 && (errno == ECONNRESET || errno == ECONNREFUSED || - errno == ETIMEDOUT || errno == EHOSTUNREACH)) - return -1; - - errno_assert (nbytes != -1); - - // Orderly shutdown by the peer. - if (nbytes == 0) - return -1; - - return (size_t) nbytes; - -#endif -} - diff --git a/src/tcp_engine.hpp b/src/tcp_engine.hpp deleted file mode 100644 index 6a41883..0000000 --- a/src/tcp_engine.hpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - Copyright (c) 2007-2011 iMatix Corporation - Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . -*/ - -#ifndef __ZMQ_TCP_ENGINE_HPP_INCLUDED__ -#define __ZMQ_TCP_ENGINE_HPP_INCLUDED__ - -#include - -#include "fd.hpp" -#include "i_engine.hpp" -#include "io_object.hpp" -#include "encoder.hpp" -#include "decoder.hpp" -#include "options.hpp" - -namespace zmq -{ - - class tcp_engine_t : public io_object_t, public i_engine - { - public: - - tcp_engine_t (fd_t fd_, const options_t &options_); - ~tcp_engine_t (); - - // i_engine interface implementation. - void plug (class io_thread_t *io_thread_, class session_t *session_); - void unplug (); - void terminate (); - void activate_in (); - void activate_out (); - - // i_poll_events interface implementation. - void in_event (); - void out_event (); - - private: - - // Function to handle network disconnections. - void error (); - - // Writes data to the socket. Returns the number of bytes actually - // written (even zero is to be considered to be a success). In case - // of error or orderly shutdown by the other peer -1 is returned. - int write (const void *data_, size_t size_); - - // Reads data from the socket (up to 'size' bytes). Returns the number - // of bytes actually read (even zero is to be considered to be - // a success). In case of error or orderly shutdown by the other - // peer -1 is returned. - int read (void *data_, size_t size_); - - // Underlying socket. - fd_t s; - - handle_t handle; - - unsigned char *inpos; - size_t insize; - decoder_t decoder; - - unsigned char *outpos; - size_t outsize; - encoder_t encoder; - - // The session this engine is attached to. - class session_t *session; - - // Detached transient session. - class session_t *leftover_session; - - options_t options; - - bool plugged; - - tcp_engine_t (const tcp_engine_t&); - const tcp_engine_t &operator = (const tcp_engine_t&); - }; - -} - -#endif diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 406b4d0..dd654b2 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -24,7 +24,7 @@ #include "platform.hpp" #include "tcp_listener.hpp" -#include "tcp_engine.hpp" +#include "stream_engine.hpp" #include "io_thread.hpp" #include "session.hpp" #include "config.hpp" @@ -90,7 +90,7 @@ void zmq::tcp_listener_t::in_event () tune_tcp_socket (fd); // Create the engine object for this connection. - tcp_engine_t *engine = new (std::nothrow) tcp_engine_t (fd, options); + stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options); alloc_assert (engine); // Choose I/O thread to run connecter in. Given that we are already diff --git a/src/vtcp_connecter.cpp b/src/vtcp_connecter.cpp index f281b23..61e7e84 100644 --- a/src/vtcp_connecter.cpp +++ b/src/vtcp_connecter.cpp @@ -25,7 +25,7 @@ #include #include -#include "tcp_engine.hpp" +#include "stream_engine.hpp" #include "io_thread.hpp" #include "platform.hpp" #include "random.hpp" @@ -141,7 +141,7 @@ void zmq::vtcp_connecter_t::out_event () } // Create the engine object for this connection. - tcp_engine_t *engine = new (std::nothrow) tcp_engine_t (fd, options); + stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options); alloc_assert (engine); // Attach the engine to the corresponding session object. diff --git a/src/vtcp_listener.cpp b/src/vtcp_listener.cpp index 9a897e4..b394833 100644 --- a/src/vtcp_listener.cpp +++ b/src/vtcp_listener.cpp @@ -26,7 +26,7 @@ #include #include -#include "tcp_engine.hpp" +#include "stream_engine.hpp" #include "session.hpp" #include "stdint.hpp" #include "err.hpp" @@ -104,7 +104,7 @@ void zmq::vtcp_listener_t::in_event () tune_tcp_socket (fd); // Create the engine object for this connection. - tcp_engine_t *engine = new (std::nothrow) tcp_engine_t (fd, options); + stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options); alloc_assert (engine); // Choose I/O thread to run connecter in. Given that we are already -- cgit v1.2.3