00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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