Skip to content

Commit

Permalink
Removed isClosed
Browse files Browse the repository at this point in the history
  • Loading branch information
eminfedar committed Apr 28, 2023
1 parent fb72473 commit 49597c5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
4 changes: 0 additions & 4 deletions async-sockets/include/basesocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class BaseSocket
UDP = SOCK_DGRAM
};
sockaddr_in address;
std::atomic<bool> isClosed{false};
constexpr static uint16_t BUFFER_SIZE = 0x1000; // 4096 bytes

protected:
Expand Down Expand Up @@ -59,9 +58,6 @@ class BaseSocket
// Methods
public:
virtual void Close() {
if(isClosed) return;

isClosed = true;
close(this->sock);
}

Expand Down
4 changes: 2 additions & 2 deletions async-sockets/include/tcpserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions async-sockets/include/tcpsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<void()> onConnected = [](){}, FDR_ON_ERROR)
{
struct addrinfo hints, *res, *it;
Expand Down Expand Up @@ -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<void()> onConnected = [](){}, FDR_ON_ERROR)
{
this->Connect(host.c_str(), port, onConnected, onError);
}

void Listen()
{
Expand Down
3 changes: 0 additions & 3 deletions async-sockets/include/udpsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 49597c5

Please sign in to comment.