From 49597c512cfa00a373d4c9f6c2d0a15d6c99486b Mon Sep 17 00:00:00 2001 From: Emin Fedar Date: Fri, 28 Apr 2023 08:45:58 +0300 Subject: [PATCH] Removed isClosed --- async-sockets/include/basesocket.hpp | 4 ---- async-sockets/include/tcpserver.hpp | 4 ++-- async-sockets/include/tcpsocket.hpp | 10 ++++++---- async-sockets/include/udpsocket.hpp | 3 --- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/async-sockets/include/basesocket.hpp b/async-sockets/include/basesocket.hpp index b32872f..95af99f 100644 --- a/async-sockets/include/basesocket.hpp +++ b/async-sockets/include/basesocket.hpp @@ -28,7 +28,6 @@ class BaseSocket UDP = SOCK_DGRAM }; sockaddr_in address; - std::atomic isClosed{false}; constexpr static uint16_t BUFFER_SIZE = 0x1000; // 4096 bytes protected: @@ -59,9 +58,6 @@ class BaseSocket // Methods public: virtual void Close() { - if(isClosed) return; - - isClosed = true; close(this->sock); } diff --git a/async-sockets/include/tcpserver.hpp b/async-sockets/include/tcpserver.hpp index 0b488fc..ec0c905 100644 --- a/async-sockets/include/tcpserver.hpp +++ b/async-sockets/include/tcpserver.hpp @@ -64,7 +64,7 @@ class TCPServer : public BaseSocket socklen_t newSocketInfoLength = sizeof(newSocketInfo); int newSock = -1; - while (!server->isClosed) + while (true) { if ((newSock = accept(server->sock, (sockaddr*)&newSocketInfo, &newSocketInfoLength)) < 0) { @@ -74,7 +74,7 @@ class TCPServer : public BaseSocket return; } - if (!server->isClosed && newSock >= 0) + if (newSock >= 0) { TCPSocket* newSocket = new TCPSocket(onError, newSock); newSocket->deleteAfterClosed = true; diff --git a/async-sockets/include/tcpsocket.hpp b/async-sockets/include/tcpsocket.hpp index 5529c51..39515e7 100644 --- a/async-sockets/include/tcpsocket.hpp +++ b/async-sockets/include/tcpsocket.hpp @@ -19,10 +19,7 @@ class TCPSocket : public BaseSocket // Send TCP Packages int Send(const char* bytes, size_t byteslength) { - if (this->isClosed) - return -1; - - int sent = 0; + int sent = -1; if ((sent = send(this->sock, bytes, byteslength, 0)) < 0) { perror("send"); @@ -31,6 +28,7 @@ class TCPSocket : public BaseSocket } int Send(const std::string& message) { return this->Send(message.c_str(), message.length()); } + void Connect(const char* host, uint16_t port, std::function onConnected = [](){}, FDR_ON_ERROR) { struct addrinfo hints, *res, *it; @@ -81,6 +79,10 @@ class TCPSocket : public BaseSocket // Start listening from server: this->Listen(); } + void Connect(const std::string& host, uint16_t port, std::function onConnected = [](){}, FDR_ON_ERROR) + { + this->Connect(host.c_str(), port, onConnected, onError); + } void Listen() { diff --git a/async-sockets/include/udpsocket.hpp b/async-sockets/include/udpsocket.hpp index 5bcbd0c..f4cd0b3 100644 --- a/async-sockets/include/udpsocket.hpp +++ b/async-sockets/include/udpsocket.hpp @@ -78,9 +78,6 @@ class UDPSocket : public BaseSocket // Send with Connect() int Send(const char* bytes, size_t byteslength) { - if (this->isClosed) - return -1; - int sent = 0; if ((sent = send(this->sock, bytes, byteslength, 0)) < 0) {