From 680f23369060abe2650f6982bb1da3ff03de031d Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sun, 29 Oct 2017 11:22:44 +0100 Subject: [PATCH] Implement the sleep function for IrDA and bluetooth --- src/bluetooth.c | 2 +- src/irda.c | 2 +- src/socket.c | 22 ++++++++++++++++++++++ src/socket.h | 4 ++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/bluetooth.c b/src/bluetooth.c index 088d027..922ea4e 100644 --- a/src/bluetooth.c +++ b/src/bluetooth.c @@ -110,7 +110,7 @@ static const dc_iostream_vtable_t dc_bluetooth_vtable = { dc_socket_write, /* write */ NULL, /* flush */ NULL, /* purge */ - NULL, /* sleep */ + dc_socket_sleep, /* sleep */ dc_socket_close, /* close */ }; diff --git a/src/irda.c b/src/irda.c index eedb703..a78ff31 100644 --- a/src/irda.c +++ b/src/irda.c @@ -102,7 +102,7 @@ static const dc_iostream_vtable_t dc_irda_vtable = { dc_socket_write, /* write */ NULL, /* flush */ NULL, /* purge */ - NULL, /* sleep */ + dc_socket_sleep, /* sleep */ dc_socket_close, /* close */ }; #endif diff --git a/src/socket.c b/src/socket.c index 948aa46..cbbb684 100644 --- a/src/socket.c +++ b/src/socket.c @@ -293,3 +293,25 @@ out: return status; } + +dc_status_t +dc_socket_sleep (dc_iostream_t *abstract, unsigned int timeout) +{ +#ifdef _WIN32 + Sleep (timeout); +#else + struct timespec ts; + ts.tv_sec = (timeout / 1000); + ts.tv_nsec = (timeout % 1000) * 1000000; + + while (nanosleep (&ts, &ts) != 0) { + int errcode = errno; + if (errcode != EINTR ) { + SYSERROR (abstract->context, errcode); + return dc_socket_syserror (errcode); + } + } +#endif + + return DC_STATUS_SUCCESS; +} diff --git a/src/socket.h b/src/socket.h index 5f5421d..280c486 100644 --- a/src/socket.h +++ b/src/socket.h @@ -34,6 +34,7 @@ #include // select #include // ioctl #include +#include #endif #include @@ -113,6 +114,9 @@ dc_socket_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual dc_status_t dc_socket_write (dc_iostream_t *iostream, const void *data, size_t size, size_t *actual); +dc_status_t +dc_socket_sleep (dc_iostream_t *abstract, unsigned int timeout); + dc_status_t dc_socket_close (dc_iostream_t *iostream);