Skip to content

Commit bc8a3f8

Browse files
committed
[ADDED] Shutdown the server socket before closing it.
[ADDED] onError functions to handle errors with each method. [FIXED] Some bugs (#define -> constexprs...) Works much more stable now.
1 parent f304ea8 commit bc8a3f8

File tree

9 files changed

+48
-48
lines changed

9 files changed

+48
-48
lines changed

easysocket/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ SRCEXT := .cpp
2121
OBJEXT := .o
2222

2323
#Compiler and Linker
24-
BUILD ?= debug
24+
BUILD ?= release
2525
BITS ?= 64
2626
CC := g++
2727

2828
#Flags, Libraries and Includes
2929
CFLAGS.common := -std=c++17 -fPIC -m$(BITS) -Wall -Wextra -DBUILD_EASYSOCKET
3030
CFLAGS.debug := $(CFLAGS.common) -g
31-
CFLAGS.release := $(CFLAGS.common) -Werror
31+
CFLAGS.release := $(CFLAGS.common) -Werror -O3
3232
CFLAGS ?= $(CFLAGS.$(BUILD))
3333
LIB := -L$(TARGETDIR)
3434
INC := -I$(INCDIR)

easysocket/include/tcpserver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EASYSOCKET_API TCPServer : public BaseSocket
1212
{
1313
public:
1414
// Event Listeners:
15-
std::function<void(TCPSocket *)> onNewConnection;
15+
std::function<void(TCPSocket *)> onNewConnection = [](TCPSocket* sock){FDR_UNUSED(sock)};
1616

1717
TCPServer(FDR_ON_ERROR);
1818

@@ -23,6 +23,9 @@ class EASYSOCKET_API TCPServer : public BaseSocket
2323
// Start listening the server.
2424
void Listen(FDR_ON_ERROR);
2525

26+
// Overriding Close to add shutdown():
27+
void Close();
28+
2629
private:
2730
static void Accept(TCPServer *server, FDR_ON_ERROR);
2831
};

easysocket/include/tcpsocket.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
#include <functional>
99
#include <thread>
1010

11-
#define BUFFER_SIZE 0xFFFF
11+
constexpr uint16_t BUFFER_SIZE = 0xFFFF;
1212

1313
class EASYSOCKET_API TCPSocket : public BaseSocket
1414
{
1515
public:
1616
// Event Listeners:
17-
std::function<void(std::string)> onMessageReceived;
18-
std::function<void()> onSocketClosed;
17+
std::function<void(std::string)> onMessageReceived = [](std::string msg) { FDR_UNUSED(msg) };
18+
std::function<void()> onSocketClosed = [](){};
1919

2020
TCPSocket(FDR_ON_ERROR, int socketId = -1);
2121

2222
int Send(std::string message);
2323
int Send(const char *bytes, size_t byteslength);
2424

25-
void Connect(std::string ipv4, uint16_t port, std::function<void()> onConnected = []{}, FDR_ON_ERROR);
26-
void Connect(uint32_t IPv4, uint16_t port, std::function<void()> onConnected = []{}, FDR_ON_ERROR);
25+
void Connect(std::string ipv4, uint16_t port, std::function<void()> onConnected = [](){}, FDR_ON_ERROR);
26+
void Connect(uint32_t IPv4, uint16_t port, std::function<void()> onConnected = [](){}, FDR_ON_ERROR);
2727

2828
void Listen();
2929

easysocket/include/udpserver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EASYSOCKET_API UDPServer : public UDPSocket
1212
UDPServer();
1313

1414
void Bind(int port, FDR_ON_ERROR);
15-
void Bind(const char *address, std::uint16_t port, FDR_ON_ERROR);
15+
void Bind(std::string IPv4, std::uint16_t port, FDR_ON_ERROR);
1616
};
1717

1818
#endif

easysocket/include/udpsocket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <functional>
99
#include <thread>
1010

11-
#define BUFFER_SIZE 0xFFFF
11+
constexpr uint16_t BUFFER_SIZE = 0xFFFF;
1212

1313
class EASYSOCKET_API UDPSocket : public BaseSocket
1414
{

easysocket/src/basesocket.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ BaseSocket::BaseSocket(std::function<void(int, std::string)> onError, SocketType
2121

2222
void BaseSocket::Close()
2323
{
24+
if(isClosed) return;
25+
2426
isClosed = true;
25-
close(this->sock);
27+
close(this->sock);
2628
}
2729

2830
std::string BaseSocket::remoteAddress()

easysocket/src/tcpserver.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
TCPServer::TCPServer(std::function<void(int, std::string)> onError) : BaseSocket(onError, TCP)
44
{
5-
5+
int opt = 1;
6+
setsockopt(this->sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(int));
7+
setsockopt(this->sock,SOL_SOCKET,SO_REUSEPORT,&opt,sizeof(int));
68
}
79

810
void TCPServer::Bind(int port, std::function<void(int, std::string)> onError)
911
{
1012
this->Bind("0.0.0.0", port, onError);
1113
}
1214

13-
void TCPServer::Bind(const char* address, uint16_t port, std::function<void(int, std::string)> onError)
15+
void TCPServer::Bind(const char *address, uint16_t port, std::function<void(int, std::string)> onError)
1416
{
1517
if (inet_pton(AF_INET, address, &this->address.sin_addr) <= 0)
1618
{
@@ -40,24 +42,36 @@ void TCPServer::Listen(std::function<void(int, std::string)> onError)
4042
acceptThread.detach();
4143
}
4244

45+
void TCPServer::Close()
46+
{
47+
shutdown(this->sock, SHUT_RDWR);
48+
49+
BaseSocket::Close();
50+
}
51+
4352
void TCPServer::Accept(TCPServer *server, std::function<void(int, std::string)> onError)
4453
{
4554
sockaddr_in newSocketInfo;
4655
socklen_t newSocketInfoLength = sizeof(newSocketInfo);
4756

4857
int newSock;
49-
while (true)
58+
while (!server->isClosed)
5059
{
51-
if ((newSock = accept(server->sock, (sockaddr *)&newSocketInfo, &newSocketInfoLength)) < 0)
60+
while ((newSock = accept(server->sock, (sockaddr *)&newSocketInfo, &newSocketInfoLength)) < 0)
5261
{
62+
if (errno == EBADF || errno == EINVAL) return;
63+
5364
onError(errno, "Error while accepting a new connection.");
5465
return;
5566
}
5667

57-
TCPSocket *newSocket = new TCPSocket([](int e, std::string er){}, newSock);
58-
newSocket->setAddressStruct(newSocketInfo);
68+
if (!server->isClosed && newSock >= 0)
69+
{
70+
TCPSocket *newSocket = new TCPSocket([](int e, std::string er) { FDR_UNUSED(e); FDR_UNUSED(er); }, newSock);
71+
newSocket->setAddressStruct(newSocketInfo);
5972

60-
server->onNewConnection(newSocket);
61-
newSocket->Listen();
73+
server->onNewConnection(newSocket);
74+
newSocket->Listen();
75+
}
6276
}
6377
}

easysocket/src/tcpsocket.cpp

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
#include <tcpsocket.h>
2-
#include <iostream>
3-
42
TCPSocket::TCPSocket(std::function<void(int, std::string)> onError, int socketId) : BaseSocket(onError, TCP, socketId)
53
{
6-
74
}
85

96
int TCPSocket::Send(std::string message)
107
{
118
return this->Send(message.c_str(), message.length());
129
}
1310

14-
int TCPSocket::Send(const char* bytes, size_t byteslength)
11+
int TCPSocket::Send(const char *bytes, size_t byteslength)
1512
{
1613
if (this->isClosed)
17-
{
1814
return -1;
19-
}
2015

2116
int sent = 0;
2217
if ((sent = send(this->sock, bytes, byteslength, 0)) < 0)
@@ -26,7 +21,7 @@ int TCPSocket::Send(const char* bytes, size_t byteslength)
2621
return sent;
2722
}
2823

29-
void TCPSocket::Connect( std::string ipv4, uint16_t port, std::function<void()> onConnected, std::function<void(int, std::string)> onError)
24+
void TCPSocket::Connect(std::string ipv4, uint16_t port, std::function<void()> onConnected, std::function<void(int, std::string)> onError)
3025
{
3126
if (inet_pton(AF_INET, ipv4.c_str(), &this->address.sin_addr) <= 0)
3227
{
@@ -38,8 +33,7 @@ void TCPSocket::Connect( std::string ipv4, uint16_t port, std::function<void()>
3833
this->Connect((uint32_t)this->address.sin_addr.s_addr, port, onConnected, onError);
3934
}
4035

41-
42-
void TCPSocket::Connect( uint32_t IPv4, uint16_t port, std::function<void()> onConnected, std::function<void(int, std::string)> onError)
36+
void TCPSocket::Connect(uint32_t IPv4, uint16_t port, std::function<void()> onConnected, std::function<void(int, std::string)> onError)
4337
{
4438
this->address.sin_family = AF_INET;
4539
this->address.sin_port = htons(port);
@@ -75,25 +69,12 @@ void TCPSocket::Receive(TCPSocket *socket)
7569
{
7670
char tempBuffer[BUFFER_SIZE];
7771
int messageLength;
78-
while ((messageLength = recv(socket->sock, tempBuffer, BUFFER_SIZE, 0)) >= 0)
79-
{
80-
if (messageLength > 0)
81-
{
82-
if (socket->onMessageReceived)
83-
socket->onMessageReceived(std::string(tempBuffer).substr(0, messageLength));
84-
}
85-
else
86-
{
87-
socket->Close();
88-
if (socket->onSocketClosed)
89-
socket->onSocketClosed();
90-
}
91-
}
9272

93-
// If socket crashed:
94-
if (!socket->isClosed)
73+
while ((messageLength = recv(socket->sock, tempBuffer, BUFFER_SIZE, 0)) > 0)
9574
{
96-
std::cerr << "Socket closed with error." << std::endl;
97-
perror("recv");
75+
socket->onMessageReceived(std::string(tempBuffer).substr(0, messageLength));
9876
}
77+
78+
socket->Close();
79+
socket->onSocketClosed();
9980
}

easysocket/src/udpserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ void UDPServer::Bind(int port, std::function<void(int, std::string)> onError) {
88
this->Bind("0.0.0.0", port, onError);
99
}
1010

11-
void UDPServer::Bind(const char* address, std::uint16_t port, std::function<void(int, std::string)> onError)
11+
void UDPServer::Bind(std::string IPv4, std::uint16_t port, std::function<void(int, std::string)> onError)
1212
{
13-
if (inet_pton(AF_INET, address, &this->address.sin_addr) <= 0)
13+
if (inet_pton(AF_INET, IPv4.c_str(), &this->address.sin_addr) <= 0)
1414
{
1515
onError(errno, "Invalid address. Address type not supported.");
1616
return;

0 commit comments

Comments
 (0)