mirror of
https://gitlab.xiph.org/xiph/icecast-common.git
synced 2024-11-03 04:17:20 -05:00
Cleanup: Decluttered public socket API
This commit is contained in:
parent
a75e064256
commit
2fb3f2a051
@ -77,22 +77,12 @@ struct iovec
|
|||||||
#define igloo_SOCK_ERROR (igloo_sock_t)-1
|
#define igloo_SOCK_ERROR (igloo_sock_t)-1
|
||||||
#define igloo_SOCK_TIMEOUT -2
|
#define igloo_SOCK_TIMEOUT -2
|
||||||
|
|
||||||
/* sock connect macro */
|
|
||||||
#define igloo_sock_connect(h, p) igloo_sock_connect_wto(h, p, 0)
|
|
||||||
|
|
||||||
/* Misc socket functions */
|
/* Misc socket functions */
|
||||||
char *igloo_sock_get_localip(char *buff, int len);
|
|
||||||
int igloo_sock_error(void);
|
int igloo_sock_error(void);
|
||||||
int igloo_sock_recoverable(int error);
|
|
||||||
int igloo_sock_stalled(int error);
|
|
||||||
int igloo_sock_valid_socket(igloo_sock_t sock);
|
|
||||||
int igloo_sock_active (igloo_sock_t sock);
|
int igloo_sock_active (igloo_sock_t sock);
|
||||||
int igloo_sock_set_blocking(igloo_sock_t sock, int block);
|
int igloo_sock_set_blocking(igloo_sock_t sock, int block);
|
||||||
int igloo_sock_set_nolinger(igloo_sock_t sock);
|
|
||||||
int igloo_sock_set_keepalive(igloo_sock_t sock);
|
|
||||||
int igloo_sock_set_nodelay(igloo_sock_t sock);
|
int igloo_sock_set_nodelay(igloo_sock_t sock);
|
||||||
void igloo_sock_set_send_buffer (igloo_sock_t sock, int win_size);
|
void igloo_sock_set_send_buffer (igloo_sock_t sock, int win_size);
|
||||||
void igloo_sock_set_error(int val);
|
|
||||||
int igloo_sock_close(igloo_sock_t sock);
|
int igloo_sock_close(igloo_sock_t sock);
|
||||||
|
|
||||||
/* Connection related socket functions */
|
/* Connection related socket functions */
|
||||||
@ -105,8 +95,6 @@ int igloo_sock_connected(igloo_sock_t sock, int timeout);
|
|||||||
int igloo_sock_write_bytes(igloo_sock_t sock, const void *buff, size_t len);
|
int igloo_sock_write_bytes(igloo_sock_t sock, const void *buff, size_t len);
|
||||||
int igloo_sock_write(igloo_sock_t sock, const char *fmt, ...);
|
int igloo_sock_write(igloo_sock_t sock, const char *fmt, ...);
|
||||||
int igloo_sock_write_fmt(igloo_sock_t sock, const char *fmt, va_list ap);
|
int igloo_sock_write_fmt(igloo_sock_t sock, const char *fmt, va_list ap);
|
||||||
int igloo_sock_write_string(igloo_sock_t sock, const char *buff);
|
|
||||||
ssize_t igloo_sock_writev (igloo_sock_t sock, const struct iovec *iov, size_t count);
|
|
||||||
|
|
||||||
|
|
||||||
/* Socket read functions */
|
/* Socket read functions */
|
||||||
|
104
net/sock.c
104
net/sock.c
@ -97,26 +97,6 @@ void igloo_sock_shutdown(void)
|
|||||||
igloo_resolver_shutdown();
|
igloo_resolver_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* igloo_sock_get_localip
|
|
||||||
**
|
|
||||||
** gets the local ip address for the machine
|
|
||||||
** the ip it returns *should* be on the internet.
|
|
||||||
** in any case, it's as close as we can hope to get
|
|
||||||
** unless someone has better ideas on how to do this
|
|
||||||
*/
|
|
||||||
char *igloo_sock_get_localip(char *buff, int len)
|
|
||||||
{
|
|
||||||
char temp[1024];
|
|
||||||
|
|
||||||
if (gethostname(temp, sizeof(temp)) != 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (igloo_resolver_getip(temp, buff, len))
|
|
||||||
return buff;
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* igloo_sock_error
|
/* igloo_sock_error
|
||||||
**
|
**
|
||||||
** returns the last socket error
|
** returns the last socket error
|
||||||
@ -130,7 +110,7 @@ int igloo_sock_error(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void igloo_sock_set_error(int val)
|
static void igloo_sock_set_error(int val)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WSASetLastError (val);
|
WSASetLastError (val);
|
||||||
@ -144,7 +124,7 @@ void igloo_sock_set_error(int val)
|
|||||||
** determines if the socket error is recoverable
|
** determines if the socket error is recoverable
|
||||||
** in terms of non blocking sockets
|
** in terms of non blocking sockets
|
||||||
*/
|
*/
|
||||||
int igloo_sock_recoverable(int error)
|
static int igloo_sock_recoverable(int error)
|
||||||
{
|
{
|
||||||
switch (error)
|
switch (error)
|
||||||
{
|
{
|
||||||
@ -170,31 +150,6 @@ int igloo_sock_recoverable(int error)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int igloo_sock_stalled (int error)
|
|
||||||
{
|
|
||||||
switch (error)
|
|
||||||
{
|
|
||||||
case EAGAIN:
|
|
||||||
case EINPROGRESS:
|
|
||||||
case EALREADY:
|
|
||||||
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
||||||
case EWOULDBLOCK:
|
|
||||||
#endif
|
|
||||||
#if defined (WSAEWOULDBLOCK) && WSAEWOULDBLOCK != EWOULDBLOCK
|
|
||||||
case WSAEWOULDBLOCK:
|
|
||||||
#endif
|
|
||||||
#if defined (WSAEINPROGRESS) && WSAEINPROGRESS != EINPROGRESS
|
|
||||||
case WSAEINPROGRESS:
|
|
||||||
#endif
|
|
||||||
#ifdef ERESTART
|
|
||||||
case ERESTART:
|
|
||||||
#endif
|
|
||||||
return 1;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int sock_connect_pending (int error)
|
static int sock_connect_pending (int error)
|
||||||
{
|
{
|
||||||
@ -205,7 +160,7 @@ static int sock_connect_pending (int error)
|
|||||||
**
|
**
|
||||||
** determines if a igloo_sock_t represents a valid socket
|
** determines if a igloo_sock_t represents a valid socket
|
||||||
*/
|
*/
|
||||||
int igloo_sock_valid_socket(igloo_sock_t sock)
|
static int igloo_sock_valid_socket(igloo_sock_t sock)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
int optval;
|
int optval;
|
||||||
@ -279,7 +234,7 @@ int igloo_sock_set_blocking(igloo_sock_t sock, int block)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int igloo_sock_set_nolinger(igloo_sock_t sock)
|
static int igloo_sock_set_nolinger(igloo_sock_t sock)
|
||||||
{
|
{
|
||||||
struct linger lin = { 0, 0 };
|
struct linger lin = { 0, 0 };
|
||||||
return setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&lin,
|
return setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&lin,
|
||||||
@ -294,7 +249,7 @@ int igloo_sock_set_nodelay(igloo_sock_t sock)
|
|||||||
sizeof(int));
|
sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
||||||
int igloo_sock_set_keepalive(igloo_sock_t sock)
|
static int igloo_sock_set_keepalive(igloo_sock_t sock)
|
||||||
{
|
{
|
||||||
int keepalive = 1;
|
int keepalive = 1;
|
||||||
return setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive,
|
return setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive,
|
||||||
@ -314,45 +269,6 @@ int igloo_sock_close(igloo_sock_t sock)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* igloo_sock_writev
|
|
||||||
*
|
|
||||||
* write multiple buffers at once, return bytes actually written
|
|
||||||
*/
|
|
||||||
#ifdef HAVE_WRITEV
|
|
||||||
|
|
||||||
ssize_t igloo_sock_writev (igloo_sock_t sock, const struct iovec *iov, size_t count)
|
|
||||||
{
|
|
||||||
return writev (sock, iov, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
ssize_t igloo_sock_writev (igloo_sock_t sock, const struct iovec *iov, size_t count)
|
|
||||||
{
|
|
||||||
int i = count, accum = 0, ret;
|
|
||||||
const struct iovec *v = iov;
|
|
||||||
|
|
||||||
while (i)
|
|
||||||
{
|
|
||||||
if (v->iov_base && v->iov_len)
|
|
||||||
{
|
|
||||||
ret = igloo_sock_write_bytes (sock, v->iov_base, v->iov_len);
|
|
||||||
if (ret == -1 && accum==0)
|
|
||||||
return -1;
|
|
||||||
if (ret == -1)
|
|
||||||
ret = 0;
|
|
||||||
accum += ret;
|
|
||||||
if (ret < (int)v->iov_len)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
v++;
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
return accum;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* igloo_sock_write_bytes
|
/* igloo_sock_write_bytes
|
||||||
**
|
**
|
||||||
** write bytes to the socket
|
** write bytes to the socket
|
||||||
@ -372,16 +288,6 @@ int igloo_sock_write_bytes(igloo_sock_t sock, const void *buff, size_t len)
|
|||||||
return send(sock, buff, len, 0);
|
return send(sock, buff, len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* igloo_sock_write_string
|
|
||||||
**
|
|
||||||
** writes a string to a socket
|
|
||||||
** This function must only be called with a blocking socket.
|
|
||||||
*/
|
|
||||||
int igloo_sock_write_string(igloo_sock_t sock, const char *buff)
|
|
||||||
{
|
|
||||||
return (igloo_sock_write_bytes(sock, buff, strlen(buff)) > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* igloo_sock_write
|
/* igloo_sock_write
|
||||||
**
|
**
|
||||||
** write a formatted string to the socket
|
** write a formatted string to the socket
|
||||||
|
Loading…
Reference in New Issue
Block a user