Commit 70f65382 authored by Pekka Pessi's avatar Pekka Pessi

su.h, su.c: added su_is_blocking(). Using it in tport.

darcs-hash:20060920180904-65a35-ff54411addb20d739e0b2c05681d3dd5965008bc.gz
parent 55dd2eac
......@@ -244,8 +244,13 @@ SOFIAPUBFUN int su_close(su_socket_t s);
/** Control socket. */
SOFIAPUBFUN int su_ioctl(su_socket_t s, int request, ...);
/** Checks if the previous call failed because it would have blocked. */
SOFIAPUBFUN int su_isblocking(void);
/** Checks if the @a errcode indicates that the socket call failed because
* it would have blocked.
*
* New in @VERSION_1_12_2. Defined as macro with POSIX sockets.
*/
SOFIAPUBFUN int su_is_blocking(int errcode);
/** Set/reset blocking option. */
SOFIAPUBFUN int su_setblocking(su_socket_t s, int blocking);
/** Set/reset address reusing option. */
......@@ -270,7 +275,12 @@ SOFIAPUBFUN int su_getlocalip(su_sockaddr_t *sin);
#if SU_HAVE_BSDSOCK
#define su_ioctl ioctl
#define su_isblocking() (su_errno() == EAGAIN || su_errno() == EWOULDBLOCK)
/*
* Note: before 1.12.2, there was su_isblocking() which did not take argument
* and which was missing from WINSOCK
*/
#define su_is_blocking(e) \
((e) == EINPROGRESS || (e) == EAGAIN || (e) == EWOULDBLOCK)
#endif
#if SU_HAVE_WINSOCK
......
......@@ -144,6 +144,11 @@ int su_ioctl(su_socket_t s, int request, ...)
return retval;
}
int su_is_blocking(int errcode)
{
return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS;
}
int su_setblocking(su_socket_t s, int blocking)
{
unsigned long nonBlock = !blocking;
......
......@@ -924,7 +924,7 @@ tport_t *tport_base_connect(tport_primary_t *pri,
if (connect(s, ai->ai_addr, (socklen_t)(ai->ai_addrlen)) == SOCKET_ERROR) {
err = su_errno();
if (err != EINPROGRESS && err != EAGAIN && err != EWOULDBLOCK)
if (!su_is_blocking(err))
TPORT_CONNECT_ERROR(err, connect);
events = SU_WAIT_CONNECT | SU_WAIT_ERR;
wakeup = tport_connected;
......@@ -2618,7 +2618,7 @@ void tport_recv_event(tport_t *self)
if (again < 0) {
int error = su_errno();
if (error != EAGAIN && error != EWOULDBLOCK) {
if (!su_is_blocking(error)) {
tport_error_report(self, error, NULL);
/* Failure: shutdown socket */
if (tport_is_connected(self))
......@@ -2886,7 +2886,7 @@ ssize_t tport_recv_iovec(tport_t const *self,
int tport_recv_error_report(tport_t *self)
{
if (su_errno() == EAGAIN && su_errno() != EWOULDBLOCK)
if (su_is_blocking(su_errno()))
return 1;
/* Report error */
......@@ -3267,7 +3267,7 @@ int tport_send_error(tport_t *self, msg_t *msg,
/*Xyzzy*/
}
if (error == EAGAIN || error == EWOULDBLOCK) {
if (su_is_blocking(error)) {
SU_DEBUG_5(("tport_vsend(%p): %s with (s=%d %s/%s:%s%s)\n",
self, "EAGAIN", (int)self->tp_socket,
tpn->tpn_proto, tpn->tpn_host, tpn->tpn_port, comp));
......
......@@ -387,7 +387,7 @@ static int tport_recv_sigcomp_r(tport_t *self,
char const *pn = self->tp_protoname;
int err = su_errno();
if (err == EAGAIN || err == EWOULDBLOCK) {
if (su_is_blocking(err)) {
SU_DEBUG_7(("%s(%p): recv from %s: EAGAIN\n", __func__, self, pn));
return 1;
}
......@@ -639,7 +639,7 @@ int vsc_send_sigcomp(tport_t const *self,
if (m == -1) {
int error = su_errno();
if (error != EAGAIN && error != EWOULDBLOCK) {
if (su_is_blocking(error)) {
sigcomp_compressor_free(c);
sc->sc_compressor = NULL;
sc->sc_output = NULL; sc->sc_input = NULL;
......
......@@ -376,7 +376,7 @@ static int thrp_udp_recv_deliver(threadpool_t *thrp,
if (thrp_udp_recv(thrp, tpd) < 0) {
tpd->tpd_errorcode = su_errno();
assert(tpd->tpd_errorcode);
if (tpd->tpd_errorcode == EAGAIN || tpd->tpd_errorcode == EWOULDBLOCK)
if (su_is_blocking(tpd->tpd_errorcode))
return 0;
}
else if (tpd->tpd_msg) {
......@@ -443,7 +443,7 @@ int thrp_udp_recv(threadpool_t *thrp, thrp_udp_deliver_t *tpd)
N = recv(s, sample, sizeof sample, MSG_PEEK | MSG_TRUNC);
if (N < 0) {
if (su_errno() == EAGAIN || su_errno() == EWOULDBLOCK)
if (su_is_blocking(su_errno()))
N = 0;
}
else if (N <= 1) {
......
......@@ -223,10 +223,7 @@ int tport_recv_sctp(tport_t *self)
N = su_vrecv(self->tp_socket, iovec, 1, 0, NULL, NULL);
if (N == SOCKET_ERROR) {
int err = su_errno();
if (err == EAGAIN || err == EWOULDBLOCK)
return 1;
return -1;
return su_is_blocking(su_errno()) ? 1 : -1;
}
if (N == 0) {
......
......@@ -363,8 +363,7 @@ int tport_tls_recv(tport_t *self)
if (N == 0) /* End-of-stream */
return 0;
else if (N == -1) {
int err = su_errno();
if (err == EAGAIN || err == EWOULDBLOCK) {
if (su_is_blocking(su_errno()) {
tport_tls_set_events(self);
return 1;
}
......@@ -467,9 +466,9 @@ ssize_t tport_tls_send(tport_t const *self,
if (n < 0) {
int err = su_errno();
if (err == EAGAIN || err == EWOULDBLOCK)
if (su_is_blocking(err))
break;
SU_DEBUG_3(("tls_write: %s\n", strerror(errno)));
SU_DEBUG_3(("tls_write: %s\n", strerror(err)));
return -1;
}
......
......@@ -248,7 +248,7 @@ int tport_recv_dgram(tport_t *self)
msg_destroy(msg); self->tp_msg = NULL;
su_seterrno(error);
if (error == EAGAIN || error == EWOULDBLOCK)
if (su_is_blocking(error))
return 0;
else
return -1;
......@@ -347,7 +347,7 @@ int tport_udp_error(tport_t const *self, su_sockaddr_t name[1])
if (n < 0) {
int err = su_errno();
if (err != EAGAIN && err != EWOULDBLOCK)
if (!su_is_blocking(err))
SU_DEBUG_1(("%s: recvmsg: %s\n", __func__, su_strerror(err)));
return 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment