Skip to content

Commit

Permalink
Merge pull request #28 from stewpend0us/master
Browse files Browse the repository at this point in the history
optionally expose the internal buffer size to the user (if they need it)
  • Loading branch information
eminfedar authored May 12, 2023
2 parents 1ecd626 + c3440df commit d66588d
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 32 deletions.
5 changes: 4 additions & 1 deletion async-sockets/include/basesocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#define FDR_UNUSED(expr){ (void)(expr); }
#define FDR_ON_ERROR std::function<void(int, std::string)> onError = [](int errorCode, std::string errorMessage){FDR_UNUSED(errorCode); FDR_UNUSED(errorMessage)}

#ifndef AS_DEFAULT_BUFFER_SIZE
#define AS_DEFAULT_BUFFER_SIZE 0x1000 /*4096 bytes*/
#endif

class BaseSocket
{
public:
Expand All @@ -26,7 +30,6 @@ class BaseSocket
UDP = SOCK_DGRAM
};
sockaddr_in address;
constexpr static uint16_t BUFFER_SIZE = 0x1000; // 4096 bytes

void Close() {
shutdown(this->sock, SHUT_RDWR);
Expand Down
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
5 changes: 3 additions & 2 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 Down Expand Up @@ -95,10 +96,10 @@ class TCPSocket : public BaseSocket
private:
static void Receive(TCPSocket* socket)
{
char tempBuffer[TCPSocket::BUFFER_SIZE];
char tempBuffer[BUFFER_SIZE];
ssize_t messageLength;

while ((messageLength = recv(socket->sock, tempBuffer, TCPSocket::BUFFER_SIZE, 0)) > 0)
while ((messageLength = recv(socket->sock, tempBuffer, BUFFER_SIZE, 0)) > 0)
{
tempBuffer[messageLength] = '\0';
if(socket->onMessageReceived)
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
10 changes: 5 additions & 5 deletions async-sockets/include/udpsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include <thread>

template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
class UDPSocket : public BaseSocket
{
public:
Expand All @@ -14,7 +15,6 @@ class UDPSocket : public BaseSocket
{
if (useConnect)
{

std::thread t(Receive, this); // usage with Connect()
t.detach();
}
Expand Down Expand Up @@ -135,10 +135,10 @@ class UDPSocket : public BaseSocket
private:
static void Receive(UDPSocket* udpSocket)
{
char tempBuffer[UDPSocket::BUFFER_SIZE];
char tempBuffer[BUFFER_SIZE];
ssize_t messageLength;

while ((messageLength = recv(udpSocket->sock, tempBuffer, UDPSocket::BUFFER_SIZE, 0)) != -1)
while ((messageLength = recv(udpSocket->sock, tempBuffer, BUFFER_SIZE, 0)) != -1)
{
tempBuffer[messageLength] = '\0';
if (udpSocket->onMessageReceived)
Expand All @@ -154,10 +154,10 @@ class UDPSocket : public BaseSocket
sockaddr_in hostAddr;
socklen_t hostAddrSize = sizeof(hostAddr);

char tempBuffer[UDPSocket::BUFFER_SIZE];
char tempBuffer[BUFFER_SIZE];
ssize_t messageLength;

while ((messageLength = recvfrom(udpSocket->sock, tempBuffer, UDPSocket::BUFFER_SIZE, 0, (sockaddr* )&hostAddr, &hostAddrSize)) != -1)
while ((messageLength = recvfrom(udpSocket->sock, tempBuffer, BUFFER_SIZE, 0, (sockaddr* )&hostAddr, &hostAddrSize)) != -1)
{
tempBuffer[messageLength] = '\0';
if (udpSocket->onMessageReceived)
Expand Down
22 changes: 12 additions & 10 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
CC := g++
CFLAGS := --std=c++11 -Wall -Wextra -Werror=conversion
LIBS := -lpthread
INC := -I../async-sockets/include
INC := ../async-sockets/include
RM := rm

.PHONY: all clean

all: tcp-client tcp-server udp-client udp-server

tcp-client:
$(CC) $(CFLAGS) tcp-client.cpp $(INC) $(LIBS) -o tcp-client
tcp-client: tcp-client.cpp $(INC)/tcpsocket.hpp
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@

tcp-server:
$(CC) $(CFLAGS) tcp-server.cpp $(INC) $(LIBS) -o tcp-server
tcp-server: tcp-server.cpp $(INC)/tcpserver.hpp
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@

udp-client:
$(CC) $(CFLAGS) udp-client.cpp $(INC) $(LIBS) -o udp-client
udp-client: udp-client.cpp $(INC)/udpsocket.hpp
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@

udp-server:
$(CC) $(CFLAGS) udp-server.cpp $(INC) $(LIBS) -o udp-server
udp-server: udp-server.cpp $(INC)/udpserver.hpp
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@

clean:
$(RM) tcp-client
$(RM) tcp-server
$(RM) udp-client
$(RM) udp-server
$(RM) udp-server
6 changes: 3 additions & 3 deletions examples/tcp-client.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "../async-sockets/include/tcpsocket.hpp"
#include "tcpsocket.hpp"
#include <iostream>

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 @@ -25,7 +25,7 @@ int main()
cout << "Connection closed: " << errorCode << endl;
};

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

Expand Down
6 changes: 3 additions & 3 deletions examples/tcp-server.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "../async-sockets/include/tcpserver.hpp"
#include "tcpserver.hpp"
#include <iostream>

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
4 changes: 2 additions & 2 deletions examples/udp-client.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../async-sockets/include/udpsocket.hpp"
#include "udpsocket.hpp"
#include <iostream>

using namespace std;
Expand All @@ -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
4 changes: 2 additions & 2 deletions examples/udp-server.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "../async-sockets/include/udpserver.hpp"
#include "udpserver.hpp"
#include <iostream>

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 d66588d

Please sign in to comment.