tcp_socket.cpp 5.68 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

    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 <http://www.gnu.org/licenses/>.
*/

#include "tcp_socket.hpp"
#include "platform.hpp"
#include "err.hpp"

Martin Sustrik's avatar
Martin Sustrik committed
24
#ifdef ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
25

unknown's avatar
unknown committed
26 27 28 29 30 31 32 33 34 35 36
zmq::tcp_socket_t::tcp_socket_t () :
    s (retired_fd)
{
}

zmq::tcp_socket_t::~tcp_socket_t ()
{
    if (s != retired_fd)
        close ();
}

37
int zmq::tcp_socket_t::open (fd_t fd_, uint64_t sndbuf_, uint64_t rcvbuf_)
unknown's avatar
unknown committed
38 39 40
{
    zmq_assert (s == retired_fd);
    s = fd_;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    if (sndbuf_) {
        int sz = (int) sndbuf_;
        int rc = setsockopt (s, SOL_SOCKET, SO_SNDBUF,
            (char*) &sz, sizeof (int));
        errno_assert (rc == 0);
    }

    if (rcvbuf_) {
        int sz = (int) rcvbuf_;
        int rc = setsockopt (s, SOL_SOCKET, SO_RCVBUF,
            (char*) &sz, sizeof (int));
        errno_assert (rc == 0);
    }

unknown's avatar
unknown committed
56 57 58 59 60 61 62 63 64 65 66 67
    return 0;
}

int zmq::tcp_socket_t::close ()
{
    zmq_assert (s != retired_fd);
    int rc = closesocket (s);
    wsa_assert (rc != SOCKET_ERROR);
    s = retired_fd;
    return 0;
}

68 69 70 71 72
zmq::fd_t zmq::tcp_socket_t::get_fd ()
{
    return s;
}

unknown's avatar
unknown committed
73 74 75 76 77 78 79 80 81 82
int zmq::tcp_socket_t::write (const void *data, int size)
{
    int nbytes = send (s, (char*) data, 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.
83 84 85 86 87 88 89
    if (nbytes == -1 && (
          WSAGetLastError () == WSAENETDOWN ||
          WSAGetLastError () == WSAENETRESET ||
          WSAGetLastError () == WSAEHOSTUNREACH ||
          WSAGetLastError () == WSAECONNABORTED ||
          WSAGetLastError () == WSAETIMEDOUT ||
          WSAGetLastError () == WSAECONNRESET))
unknown's avatar
unknown committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        return -1;

    wsa_assert (nbytes != SOCKET_ERROR);

    return (size_t) nbytes;
}

int zmq::tcp_socket_t::read (void *data, int size)
{
    int nbytes = recv (s, (char*) data, 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 && (
108 109 110 111
          WSAGetLastError () == WSAENETDOWN ||
          WSAGetLastError () == WSAENETRESET ||
          WSAGetLastError () == WSAECONNABORTED ||
          WSAGetLastError () == WSAETIMEDOUT ||
unknown's avatar
unknown committed
112 113 114 115 116 117 118 119 120 121 122 123 124
          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;
}
Martin Sustrik's avatar
Martin Sustrik committed
125 126 127 128 129 130 131 132 133 134 135

#else

#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>

Martin Sustrik's avatar
Martin Sustrik committed
136
zmq::tcp_socket_t::tcp_socket_t () :
Martin Sustrik's avatar
Martin Sustrik committed
137 138 139 140
    s (retired_fd)
{
}

Martin Sustrik's avatar
Martin Sustrik committed
141
zmq::tcp_socket_t::~tcp_socket_t ()
Martin Sustrik's avatar
Martin Sustrik committed
142 143 144 145 146
{
    if (s != retired_fd)
        close ();
}

147
int zmq::tcp_socket_t::open (fd_t fd_, uint64_t sndbuf_, uint64_t rcvbuf_)
Martin Sustrik's avatar
Martin Sustrik committed
148 149 150
{
    assert (s == retired_fd);
    s = fd_;
151 152 153 154 155 156 157 158 159 160 161 162 163

    if (sndbuf_) {
        int sz = (int) sndbuf_;
        int rc = setsockopt (s, SOL_SOCKET, SO_SNDBUF, &sz, sizeof (int));
        errno_assert (rc == 0);
    }

    if (rcvbuf_) {
        int sz = (int) rcvbuf_;
        int rc = setsockopt (s, SOL_SOCKET, SO_RCVBUF, &sz, sizeof (int));
        errno_assert (rc == 0);
    }

Martin Sustrik's avatar
Martin Sustrik committed
164 165 166
    return 0;
}

Martin Sustrik's avatar
Martin Sustrik committed
167
int zmq::tcp_socket_t::close ()
Martin Sustrik's avatar
Martin Sustrik committed
168
{
Martin Sustrik's avatar
Martin Sustrik committed
169
    zmq_assert (s != retired_fd);
Martin Sustrik's avatar
Martin Sustrik committed
170 171 172 173 174 175 176
    int rc = ::close (s);
    if (rc != 0)
        return -1;
    s = retired_fd;
    return 0;
}

Martin Sustrik's avatar
Martin Sustrik committed
177
zmq::fd_t zmq::tcp_socket_t::get_fd ()
Martin Sustrik's avatar
Martin Sustrik committed
178 179 180 181
{
    return s;
}

Martin Sustrik's avatar
Martin Sustrik committed
182
int zmq::tcp_socket_t::write (const void *data, int size)
Martin Sustrik's avatar
Martin Sustrik committed
183 184 185 186 187 188 189 190 191 192 193
{
    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.
Martin Sustrik's avatar
Martin Sustrik committed
194
    if (nbytes == -1 && (errno == ECONNRESET || errno == EPIPE))
Martin Sustrik's avatar
Martin Sustrik committed
195 196 197 198 199 200
        return -1;

    errno_assert (nbytes != -1);
    return (size_t) nbytes;
}

Martin Sustrik's avatar
Martin Sustrik committed
201
int zmq::tcp_socket_t::read (void *data, int size)
Martin Sustrik's avatar
Martin Sustrik committed
202 203 204 205 206 207 208 209 210 211 212
{
    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.
213
    if (nbytes == -1 && (errno == ECONNRESET || errno == ECONNREFUSED ||
214
          errno == ETIMEDOUT || errno == EHOSTUNREACH))
Martin Sustrik's avatar
Martin Sustrik committed
215 216 217 218 219 220 221 222 223 224 225
        return -1;

    errno_assert (nbytes != -1);

    //  Orderly shutdown by the other peer.
    if (nbytes == 0)
        return -1;

    return (size_t) nbytes;
}

malosek's avatar
malosek committed
226 227
#endif