From 94c62c18342f10c3283b6f63bca8acb406f8720b Mon Sep 17 00:00:00 2001 From: Joursoir Date: Tue, 5 Jan 2021 23:04:29 +0300 Subject: client gui: add non-blocking socket working --- src/client/ClientBase.cpp | 21 +++++++++++++++------ src/client/ClientBase.hpp | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src/client') diff --git a/src/client/ClientBase.cpp b/src/client/ClientBase.cpp index 2256532..b30b4e0 100644 --- a/src/client/ClientBase.cpp +++ b/src/client/ClientBase.cpp @@ -5,10 +5,9 @@ #include // for iten_aton #include // for bind, connect #include // for bind, connect + #include + #include #endif -#include -#include -#include #include "ClientBase.hpp" @@ -80,6 +79,10 @@ int ClientBase::InitSocket(const char* ip, int port) closesocket(fd); return -1; } + + // nonblocking socket mode: + u_long imode = 1; + ioctlsocket(fd, FIONBIO, &imode); #else // linux socket: struct sockaddr_in server_address; @@ -103,6 +106,7 @@ int ClientBase::InitSocket(const char* ip, int port) return -1; } + // nonblocking socket mode: int flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK); #endif @@ -115,12 +119,17 @@ int ClientBase::Run() return 0; HandleActions(); - int recive = read(fd, out_buffer+out_buf_used, - sizeof(out_buffer)-out_buf_used); + int recive = recv(fd, out_buffer+out_buf_used, + sizeof(out_buffer)-out_buf_used, 0); if(recive < 0) { +#ifdef _WIN32 + if(WSAGetLastError() != WSAEINTR && WSAGetLastError() != WSAEWOULDBLOCK) + return 0; +#else if(errno != EINTR && errno != EAGAIN) return 0; +#endif } else if(recive > 0) out_buf_used += recive; @@ -171,5 +180,5 @@ void ClientBase::SendMessage(const char *msg) strcpy(buf, msg); buf[len-1] = '\n'; - write(fd, buf, len * sizeof(char)); + send(fd, buf, len * sizeof(char), 0); } \ No newline at end of file diff --git a/src/client/ClientBase.hpp b/src/client/ClientBase.hpp index dbe28bd..f2439b8 100644 --- a/src/client/ClientBase.hpp +++ b/src/client/ClientBase.hpp @@ -2,7 +2,7 @@ #define WC_CLIENTBASE_H #ifdef _WIN32 - #include + #include #endif #include "../const_vars.hpp" -- cgit v1.2.3-18-g5258