Skip to content

Commit

Permalink
change over to class templates
Browse files Browse the repository at this point in the history
  • Loading branch information
stewpend0us committed May 12, 2023
1 parent 09637b1 commit c3440df
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 25 deletions.
7 changes: 4 additions & 3 deletions async-sockets/include/tcpserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include "tcpsocket.hpp"
#include <thread>

template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
class TCPServer : public BaseSocket
{
public:
// Event Listeners:
std::function<void(TCPSocket*)> onNewConnection = [](TCPSocket* sock){FDR_UNUSED(sock)};
std::function<void(TCPSocket<BUFFER_SIZE>*)> onNewConnection = [](TCPSocket<BUFFER_SIZE>* sock){FDR_UNUSED(sock)};

explicit TCPServer(FDR_ON_ERROR): BaseSocket(onError, SocketType::TCP)
{
Expand Down Expand Up @@ -57,7 +58,7 @@ class TCPServer : public BaseSocket
}

private:
static void Accept(TCPServer* server, FDR_ON_ERROR)
static void Accept(TCPServer<BUFFER_SIZE>* server, FDR_ON_ERROR)
{
sockaddr_in newSocketInfo;
socklen_t newSocketInfoLength = sizeof(newSocketInfo);
Expand All @@ -75,7 +76,7 @@ class TCPServer : public BaseSocket
return;
}

TCPSocket* newSocket = new TCPSocket(onError, newSocketFileDescriptor);
TCPSocket<BUFFER_SIZE>* newSocket = new TCPSocket<BUFFER_SIZE>(onError, newSocketFileDescriptor);
newSocket->deleteAfterClosed = true;
newSocket->setAddressStruct(newSocketInfo);

Expand Down
14 changes: 5 additions & 9 deletions async-sockets/include/tcpsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <functional>
#include <thread>

template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
class TCPSocket : public BaseSocket
{
public:
Expand All @@ -22,7 +23,6 @@ class TCPSocket : public BaseSocket
ssize_t Send(const std::string& message) { return this->Send(message.c_str(), message.length()); }

// Connect to a TCP Server with `uint32_t ipv4` & `uint16_t port` values
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
void Connect(uint32_t ipv4, uint16_t port, std::function<void()> onConnected = [](){}, FDR_ON_ERROR)
{
this->address.sin_family = AF_INET;
Expand All @@ -46,10 +46,9 @@ class TCPSocket : public BaseSocket
onConnected();

// Start listening from server:
this->Listen<BUFFER_SIZE>();
this->Listen();
}
// Connect to a TCP Server with `const char* host` & `uint16_t port` values
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
void Connect(const char* host, uint16_t port, std::function<void()> onConnected = [](){}, FDR_ON_ERROR)
{
struct addrinfo hints, *res, *it;
Expand All @@ -74,20 +73,18 @@ class TCPSocket : public BaseSocket

freeaddrinfo(res);

this->Connect<BUFFER_SIZE>((uint32_t)this->address.sin_addr.s_addr, port, onConnected, onError);
this->Connect((uint32_t)this->address.sin_addr.s_addr, port, onConnected, onError);
}
// Connect to a TCP Server with `const std::string& ipv4` & `uint16_t port` values
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
void Connect(const std::string& host, uint16_t port, std::function<void()> onConnected = [](){}, FDR_ON_ERROR)
{
this->Connect<BUFFER_SIZE>(host.c_str(), port, onConnected, onError);
this->Connect(host.c_str(), port, onConnected, onError);
}

// Start another thread to listen the socket
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
void Listen()
{
std::thread t(TCPSocket::Receive<BUFFER_SIZE>, this);
std::thread t(TCPSocket::Receive, this);
t.detach();
}

Expand All @@ -97,7 +94,6 @@ class TCPSocket : public BaseSocket
bool deleteAfterClosed = false;

private:
template <uint16_t BUFFER_SIZE>
static void Receive(TCPSocket* socket)
{
char tempBuffer[BUFFER_SIZE];
Expand Down
3 changes: 2 additions & 1 deletion async-sockets/include/udpserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "udpsocket.hpp"
#include <thread>

class UDPServer : public UDPSocket
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
class UDPServer : public UDPSocket<BUFFER_SIZE>
{
public:
// Bind the custom address & port of the server.
Expand Down
9 changes: 3 additions & 6 deletions async-sockets/include/udpsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
#include <string.h>
#include <thread>

template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
class UDPSocket : public BaseSocket
{
public:
std::function<void(std::string, std::string, std::uint16_t)> onMessageReceived;
std::function<void(const char*, ssize_t, std::string, std::uint16_t)> onRawMessageReceived;

template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
explicit UDPSocket(bool useConnect = false, FDR_ON_ERROR, int socketId = -1): BaseSocket(onError, SocketType::UDP, socketId)
{
if (useConnect)
{

std::thread t(Receive<BUFFER_SIZE>, this); // usage with Connect()
std::thread t(Receive, this); // usage with Connect()
t.detach();
}
else
{
std::thread t(ReceiveFrom<BUFFER_SIZE>, this);
std::thread t(ReceiveFrom, this);
t.detach();
}
}
Expand Down Expand Up @@ -134,7 +133,6 @@ class UDPSocket : public BaseSocket
void Connect(const std::string& host, uint16_t port, FDR_ON_ERROR) { this->Connect(host.c_str(), port, onError); }

private:
template <uint16_t BUFFER_SIZE>
static void Receive(UDPSocket* udpSocket)
{
char tempBuffer[BUFFER_SIZE];
Expand All @@ -151,7 +149,6 @@ class UDPSocket : public BaseSocket
}
}

template <uint16_t BUFFER_SIZE>
static void ReceiveFrom(UDPSocket* udpSocket)
{
sockaddr_in hostAddr;
Expand Down
4 changes: 2 additions & 2 deletions examples/tcp-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace std;
int main()
{
// Initialize socket.
TCPSocket tcpSocket([](int errorCode, std::string errorMessage){
TCPSocket<> tcpSocket([](int errorCode, std::string errorMessage){
cout << "Socket creation error:" << errorCode << " : " << errorMessage << endl;
});

Expand All @@ -26,7 +26,7 @@ int main()
};

// Connect to the host (with a custom buffer size).
tcpSocket.Connect<0xFFFF>("localhost", 8888, [&] {
tcpSocket.Connect("localhost", 8888, [&] {
cout << "Connected to the server successfully." << endl;

// Send String:
Expand Down
4 changes: 2 additions & 2 deletions examples/tcp-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ using namespace std;
int main()
{
// Initialize server socket..
TCPServer tcpServer;
TCPServer<> tcpServer;

// When a new client connected:
tcpServer.onNewConnection = [&](TCPSocket *newClient) {
tcpServer.onNewConnection = [&](TCPSocket<> *newClient) {
cout << "New client: [";
cout << newClient->remoteAddress() << ":" << newClient->remotePort() << "]" << endl;

Expand Down
2 changes: 1 addition & 1 deletion examples/udp-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main()
constexpr uint16_t PORT = 8888;

// Initialize socket.
UDPSocket udpSocket(true); // "true" to use Connection on UDP. Default is "false".
UDPSocket<100> udpSocket(true); // "true" to use Connection on UDP. Default is "false".
udpSocket.Connect(IP, PORT);

// Send String:
Expand Down
2 changes: 1 addition & 1 deletion examples/udp-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace std;
int main()
{
// Initialize server socket..
UDPServer udpServer;
UDPServer<> udpServer;

// onMessageReceived will run when a message received with information of ip & port of sender:
/*udpServer.onMessageReceived = [&](string message, string ipv4, uint16_t port) {
Expand Down

0 comments on commit c3440df

Please sign in to comment.