summaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-01-05 23:04:29 +0300
committerJoursoir <chat@joursoir.net>2021-01-17 16:44:22 +0000
commit94c62c18342f10c3283b6f63bca8acb406f8720b (patch)
tree9c9b959da7f05ca6d792dda2d7ecb8e68a3e8ed4 /src/client
parenta76b00c511db1d3529bba51475a24ca93d12913e (diff)
downloadwant-chat-94c62c18342f10c3283b6f63bca8acb406f8720b.tar.gz
want-chat-94c62c18342f10c3283b6f63bca8acb406f8720b.tar.bz2
want-chat-94c62c18342f10c3283b6f63bca8acb406f8720b.zip
client gui: add non-blocking socket working
Diffstat (limited to 'src/client')
-rw-r--r--src/client/ClientBase.cpp21
-rw-r--r--src/client/ClientBase.hpp2
2 files changed, 16 insertions, 7 deletions
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 <arpa/inet.h> // for iten_aton
#include <sys/types.h> // for bind, connect
#include <sys/socket.h> // for bind, connect
+ #include <fcntl.h>
+ #include <cerrno>
#endif
-#include <fcntl.h>
-#include <cerrno>
-#include <stdio.h>
#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 <winsock.h>
+ #include <winsock2.h>
#endif
#include "../const_vars.hpp"