Avis Client Library: avis/net.h Source File

- Main Page
- Data Structures
- Files

# avis/net.h

Go to the documentation of this file.

    00001 /*
    00002  *  Avis Elvin client library for C.
    00003  *
    00004  *  Copyright (C) 2008 Matthew Phillips <avis@mattp.name>
    00005  *
    00006  *  This program is free software; you can redistribute it and/or
    00007  *  modify it under the terms of version 3 of the GNU Lesser General
    00008  *  Public License as published by the Free Software Foundation.
    00009  *
    00010  *  This program is distributed in the hope that it will be useful,
    00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00013  *  General Public License for more details.
    00014  *
    00015  *  You should have received a copy of the GNU General Public License
    00016  *  along with this program. If not, see <http://www.gnu.org/licenses/>.
    00017  */
    00021 #ifndef AVIS_NET_H_
    00022 #define AVIS_NET_H_
    00023
    00024 #ifdef _WIN32
    00025   #include <winsock2.h>
    00026   #include <ws2tcpip.h>
    00027
    00028   typedef SOCKET socket_t;
    00029
    00030   #define pipe_read(socket, buff, length) recv (socket, buff, length, 0)
    00031   #define pipe_write(socket, buff, length) send (socket, buff, length, 0)
    00032
    00033   #define close_socket(s) (closesocket (s), (s) = -1, WSACleanup ())
    00034
    00035   #define sock_op_timed_out() (WSAGetLastError () == WSAETIMEDOUT)
    00036
    00037   #define elvin_error_from_pipe elvin_error_from_socket
    00038
    00039   #define elvin_error_from_socket(err) \
    00040     elvin_error_set (err, errno_to_elvin_error (WSAGetLastError ()), \
    00041                      "Socket error")
    00042 #else
    00043   #include <errno.h>
    00044   #include <unistd.h>
    00045   #include <sys/socket.h>
    00046   #include <netdb.h>
    00047   #include <netinet/in.h>
    00048   #include <arpa/inet.h>
    00049
    00050   typedef int socket_t;
    00051
    00052   #define pipe_read(socket, buff, length) read (socket, buff, length)
    00053   #define pipe_write(socket, buff, length) write (socket, buff, length)
    00054
    00055   #define close_socket(s) (shutdown ((s), SHUT_RDWR), close (s), (s) = -1)
    00056
    00057   #define sock_op_timed_out() (errno == EAGAIN || errno == EWOULDBLOCK)
    00058
    00059   #define elvin_error_from_pipe elvin_error_from_errno
    00060   #define elvin_error_from_socket elvin_error_from_errno
    00061 #endif
    00062
    00063 #include <avis/errors.h>
    00064 #include <avis/stdtypes.h>
    00065 #include <avis/defs.h>
    00066
    00067 AVIS_PUBLIC
    00068 socket_t avis_open_socket (const char *host, uint16_t port, ElvinError *error);
    00069
    00073 AVIS_PUBLIC
    00074 socket_t avis_select_ready (socket_t socket1, socket_t socket2, ElvinError *error);
    00075
    00079 AVIS_PUBLIC
    00080 bool avis_open_socket_pair (socket_t *socket_read, socket_t *socket_write,
    00081                           ElvinError *error);
    00082
    00083 AVIS_PUBLIC
    00084 void avis_close_socket_pair (socket_t socket_read, socket_t socket_write);
    00085
    00086 #endif /* AVIS_NET_H_ */