X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fwindows%2Fos-socket.h;h=0c3855558ddeb7b164d151cb3e1e50ca0fa1cdee;hb=2b44051cbfa70deafc30d9767323214debbc1a75;hp=ad98edb59e5c46648abdd8131a2cc4511d787c5a;hpb=996665192725398172263999b88c63663d11db04;p=deb_libcec.git diff --git a/src/lib/platform/windows/os-socket.h b/src/lib/platform/windows/os-socket.h index ad98edb..0c38555 100644 --- a/src/lib/platform/windows/os-socket.h +++ b/src/lib/platform/windows/os-socket.h @@ -31,10 +31,9 @@ * http://www.pulse-eight.net/ */ -#include "../os.h" -#include "../util/timeutils.h" +#include "lib/platform/os.h" +#include "lib/platform/util/timeutils.h" -#pragma comment(lib, "Ws2_32.lib") #include #include #include @@ -74,10 +73,16 @@ namespace PLATFORM inline ssize_t SerialSocketWrite(serial_socket_t socket, int *iError, void* data, size_t len) { + if (len != (DWORD)len) + { + *iError = EINVAL; + return -1; + } + DWORD iBytesWritten(0); if (socket != INVALID_HANDLE_VALUE) { - if (!WriteFile(socket, data, len, &iBytesWritten, NULL)) + if (!WriteFile(socket, data, (DWORD)len, &iBytesWritten, NULL)) { *iError = GetLastError(); return -1; @@ -90,10 +95,16 @@ namespace PLATFORM inline ssize_t SerialSocketRead(serial_socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/) { + if (len != (DWORD)len) + { + *iError = EINVAL; + return -1; + } + DWORD iBytesRead(0); if (socket != INVALID_HANDLE_VALUE) { - if(!ReadFile(socket, data, len, &iBytesRead, NULL) != 0) + if(!ReadFile(socket, data, (DWORD)len, &iBytesRead, NULL) != 0) { *iError = GetLastError(); return -1; @@ -127,15 +138,16 @@ namespace PLATFORM inline ssize_t TcpSocketWrite(tcp_socket_t socket, int *iError, void* data, size_t len) { if (socket == INVALID_SOCKET || - socket == SOCKET_ERROR) + socket == SOCKET_ERROR || + len != (int)len) { *iError = EINVAL; return -1; } - ssize_t iReturn = send(socket, (char*)data, len, 0); + ssize_t iReturn = send(socket, (char*)data, (int)len, 0); if (iReturn < (ssize_t)len) - *iError = errno; + *iError = GetSocketError(); return iReturn; } @@ -146,7 +158,8 @@ namespace PLATFORM *iError = 0; if (socket == INVALID_SOCKET || - socket == SOCKET_ERROR) + socket == SOCKET_ERROR || + len != (int)len) { *iError = EINVAL; return -1; @@ -168,22 +181,33 @@ namespace PLATFORM tv.tv_usec = 1000 * (long)(iTimeoutMs % 1000); FD_ZERO(&fd_read); + #pragma warning(disable:4127) /* disable 'conditional expression is constant' */ FD_SET(socket, &fd_read); + #pragma warning(default:4127) - if (select(socket + 1, &fd_read, NULL, NULL, &tv) == 0) + if (select((int)socket + 1, &fd_read, NULL, NULL, &tv) == 0) + { + *iError = ETIMEDOUT; return ETIMEDOUT; + } TcpSocketSetBlocking(socket, false); } ssize_t iReadResult = (iTimeoutMs > 0) ? - recv(socket, (char*)data + iBytesRead, len - iBytesRead, MSG_WAITALL) : - recv(socket, (char*)data, len, MSG_WAITALL); + recv(socket, (char*)data + iBytesRead, (int)(len - iBytesRead), 0) : + recv(socket, (char*)data, (int)len, MSG_WAITALL); *iError = GetSocketError(); + + if (iTimeoutMs > 0) + { + TcpSocketSetBlocking(socket, true); + iNow = GetTimeMs(); + } + if (iReadResult < 0) { - if (errno == EAGAIN && iTimeoutMs > 0) + if (*iError == EAGAIN && iTimeoutMs > 0) continue; - *iError = errno; return -1; } else if (iReadResult == 0 || (iReadResult != (ssize_t)len && iTimeoutMs == 0)) @@ -193,14 +217,12 @@ namespace PLATFORM } iBytesRead += iReadResult; - - if (iTimeoutMs > 0) - { - TcpSocketSetBlocking(socket, true); - iNow = GetTimeMs(); - } } - return 0; + + if (iBytesRead < (ssize_t)len && *iError == 0) + *iError = ETIMEDOUT; + + return iBytesRead; } inline bool TcpResolveAddress(const char *strHost, uint16_t iPort, int *iError, struct addrinfo **info) @@ -237,7 +259,7 @@ namespace PLATFORM TcpSocketSetBlocking(socket, false); *iError = 0; - int iConnectResult = connect(socket, addr->ai_addr, addr->ai_addrlen); + int iConnectResult = connect(socket, addr->ai_addr, (int)addr->ai_addrlen); if (iConnectResult == -1) { if (GetSocketError() == EINPROGRESS || @@ -250,8 +272,10 @@ namespace PLATFORM FD_ZERO(&fd_write); FD_ZERO(&fd_except); + #pragma warning(disable:4127) /* disable 'conditional expression is constant' */ FD_SET(socket, &fd_write); FD_SET(socket, &fd_except); + #pragma warning(default:4127) int iPollResult = select(sizeof(socket)*8, NULL, &fd_write, &fd_except, &tv); if (iPollResult == 0) @@ -266,7 +290,7 @@ namespace PLATFORM } else { - *iError = errno; + *iError = GetSocketError(); } }