From 4ed70a930202b103e7e80b8dc925e0aaa4622595 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Wed, 29 Jul 2009 12:07:54 +0200 Subject: initial commit --- src/tcp_socket.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/tcp_socket.cpp (limited to 'src/tcp_socket.cpp') diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp new file mode 100644 index 0000000..70fde96 --- /dev/null +++ b/src/tcp_socket.cpp @@ -0,0 +1,116 @@ +/* + Copyright (c) 2007-2009 FastMQ Inc. + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the Lesser GNU 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 + Lesser GNU General Public License for more details. + + You should have received a copy of the Lesser GNU General Public License + along with this program. If not, see . +*/ + +#include "tcp_socket.hpp" +#include "platform.hpp" +#include "err.hpp" + +#ifdef ZS_HAVE_WINDOWS + +#include "windows.hpp" +#error + +#else + +#include +#include +#include +#include +#include +#include +#include + +zs::tcp_socket_t::tcp_socket_t () : + s (retired_fd) +{ +} + +zs::tcp_socket_t::~tcp_socket_t () +{ + if (s != retired_fd) + close (); +} + +int zs::tcp_socket_t::open (fd_t fd_) +{ + assert (s == retired_fd); + s = fd_; + return 0; +} + +int zs::tcp_socket_t::close () +{ + zs_assert (s != retired_fd); + int rc = ::close (s); + if (rc != 0) + return -1; + s = retired_fd; + return 0; +} + +zs::fd_t zs::tcp_socket_t::get_fd () +{ + return s; +} + +int zs::tcp_socket_t::write (const void *data, int size) +{ + 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 to 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) + return -1; + + errno_assert (nbytes != -1); + return (size_t) nbytes; +} + +int zs::tcp_socket_t::read (void *data, int size) +{ + 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 to 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)) + return -1; + + errno_assert (nbytes != -1); + + // Orderly shutdown by the other peer. + if (nbytes == 0) + return -1; + + return (size_t) nbytes; +} + +#endif + -- cgit v1.2.3