Skip to content

Commit d66588d

Browse files
authored
Merge pull request #28 from stewpend0us/master
optionally expose the internal buffer size to the user (if they need it)
2 parents 1ecd626 + c3440df commit d66588d

10 files changed

Lines changed: 40 additions & 32 deletions

File tree

async-sockets/include/basesocket.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#define FDR_UNUSED(expr){ (void)(expr); }
1818
#define FDR_ON_ERROR std::function<void(int, std::string)> onError = [](int errorCode, std::string errorMessage){FDR_UNUSED(errorCode); FDR_UNUSED(errorMessage)}
1919

20+
#ifndef AS_DEFAULT_BUFFER_SIZE
21+
#define AS_DEFAULT_BUFFER_SIZE 0x1000 /*4096 bytes*/
22+
#endif
23+
2024
class BaseSocket
2125
{
2226
public:
@@ -26,7 +30,6 @@ class BaseSocket
2630
UDP = SOCK_DGRAM
2731
};
2832
sockaddr_in address;
29-
constexpr static uint16_t BUFFER_SIZE = 0x1000; // 4096 bytes
3033

3134
void Close() {
3235
shutdown(this->sock, SHUT_RDWR);

async-sockets/include/tcpserver.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
#include "tcpsocket.hpp"
44
#include <thread>
55

6+
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
67
class TCPServer : public BaseSocket
78
{
89
public:
910
// Event Listeners:
10-
std::function<void(TCPSocket*)> onNewConnection = [](TCPSocket* sock){FDR_UNUSED(sock)};
11+
std::function<void(TCPSocket<BUFFER_SIZE>*)> onNewConnection = [](TCPSocket<BUFFER_SIZE>* sock){FDR_UNUSED(sock)};
1112

1213
explicit TCPServer(FDR_ON_ERROR): BaseSocket(onError, SocketType::TCP)
1314
{
@@ -57,7 +58,7 @@ class TCPServer : public BaseSocket
5758
}
5859

5960
private:
60-
static void Accept(TCPServer* server, FDR_ON_ERROR)
61+
static void Accept(TCPServer<BUFFER_SIZE>* server, FDR_ON_ERROR)
6162
{
6263
sockaddr_in newSocketInfo;
6364
socklen_t newSocketInfoLength = sizeof(newSocketInfo);
@@ -75,7 +76,7 @@ class TCPServer : public BaseSocket
7576
return;
7677
}
7778

78-
TCPSocket* newSocket = new TCPSocket(onError, newSocketFileDescriptor);
79+
TCPSocket<BUFFER_SIZE>* newSocket = new TCPSocket<BUFFER_SIZE>(onError, newSocketFileDescriptor);
7980
newSocket->deleteAfterClosed = true;
8081
newSocket->setAddressStruct(newSocketInfo);
8182

async-sockets/include/tcpsocket.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <functional>
77
#include <thread>
88

9+
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
910
class TCPSocket : public BaseSocket
1011
{
1112
public:
@@ -95,10 +96,10 @@ class TCPSocket : public BaseSocket
9596
private:
9697
static void Receive(TCPSocket* socket)
9798
{
98-
char tempBuffer[TCPSocket::BUFFER_SIZE];
99+
char tempBuffer[BUFFER_SIZE];
99100
ssize_t messageLength;
100101

101-
while ((messageLength = recv(socket->sock, tempBuffer, TCPSocket::BUFFER_SIZE, 0)) > 0)
102+
while ((messageLength = recv(socket->sock, tempBuffer, BUFFER_SIZE, 0)) > 0)
102103
{
103104
tempBuffer[messageLength] = '\0';
104105
if(socket->onMessageReceived)

async-sockets/include/udpserver.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "udpsocket.hpp"
44
#include <thread>
55

6-
class UDPServer : public UDPSocket
6+
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
7+
class UDPServer : public UDPSocket<BUFFER_SIZE>
78
{
89
public:
910
// Bind the custom address & port of the server.

async-sockets/include/udpsocket.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string.h>
55
#include <thread>
66

7+
template <uint16_t BUFFER_SIZE = AS_DEFAULT_BUFFER_SIZE>
78
class UDPSocket : public BaseSocket
89
{
910
public:
@@ -14,7 +15,6 @@ class UDPSocket : public BaseSocket
1415
{
1516
if (useConnect)
1617
{
17-
1818
std::thread t(Receive, this); // usage with Connect()
1919
t.detach();
2020
}
@@ -135,10 +135,10 @@ class UDPSocket : public BaseSocket
135135
private:
136136
static void Receive(UDPSocket* udpSocket)
137137
{
138-
char tempBuffer[UDPSocket::BUFFER_SIZE];
138+
char tempBuffer[BUFFER_SIZE];
139139
ssize_t messageLength;
140140

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

157-
char tempBuffer[UDPSocket::BUFFER_SIZE];
157+
char tempBuffer[BUFFER_SIZE];
158158
ssize_t messageLength;
159159

160-
while ((messageLength = recvfrom(udpSocket->sock, tempBuffer, UDPSocket::BUFFER_SIZE, 0, (sockaddr* )&hostAddr, &hostAddrSize)) != -1)
160+
while ((messageLength = recvfrom(udpSocket->sock, tempBuffer, BUFFER_SIZE, 0, (sockaddr* )&hostAddr, &hostAddrSize)) != -1)
161161
{
162162
tempBuffer[messageLength] = '\0';
163163
if (udpSocket->onMessageReceived)

examples/Makefile

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
CC := g++
22
CFLAGS := --std=c++11 -Wall -Wextra -Werror=conversion
33
LIBS := -lpthread
4-
INC := -I../async-sockets/include
4+
INC := ../async-sockets/include
55
RM := rm
66

7+
.PHONY: all clean
8+
79
all: tcp-client tcp-server udp-client udp-server
810

9-
tcp-client:
10-
$(CC) $(CFLAGS) tcp-client.cpp $(INC) $(LIBS) -o tcp-client
11+
tcp-client: tcp-client.cpp $(INC)/tcpsocket.hpp
12+
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@
1113

12-
tcp-server:
13-
$(CC) $(CFLAGS) tcp-server.cpp $(INC) $(LIBS) -o tcp-server
14+
tcp-server: tcp-server.cpp $(INC)/tcpserver.hpp
15+
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@
1416

15-
udp-client:
16-
$(CC) $(CFLAGS) udp-client.cpp $(INC) $(LIBS) -o udp-client
17+
udp-client: udp-client.cpp $(INC)/udpsocket.hpp
18+
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@
1719

18-
udp-server:
19-
$(CC) $(CFLAGS) udp-server.cpp $(INC) $(LIBS) -o udp-server
20+
udp-server: udp-server.cpp $(INC)/udpserver.hpp
21+
$(CC) $(CFLAGS) $< -I$(INC) $(LIBS) -o $@
2022

2123
clean:
2224
$(RM) tcp-client
2325
$(RM) tcp-server
2426
$(RM) udp-client
25-
$(RM) udp-server
27+
$(RM) udp-server

examples/tcp-client.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#include "../async-sockets/include/tcpsocket.hpp"
1+
#include "tcpsocket.hpp"
22
#include <iostream>
33

44
using namespace std;
55

66
int main()
77
{
88
// Initialize socket.
9-
TCPSocket tcpSocket([](int errorCode, std::string errorMessage){
9+
TCPSocket<> tcpSocket([](int errorCode, std::string errorMessage){
1010
cout << "Socket creation error:" << errorCode << " : " << errorMessage << endl;
1111
});
1212

@@ -25,7 +25,7 @@ int main()
2525
cout << "Connection closed: " << errorCode << endl;
2626
};
2727

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

examples/tcp-server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
#include "../async-sockets/include/tcpserver.hpp"
1+
#include "tcpserver.hpp"
22
#include <iostream>
33

44
using namespace std;
55

66
int main()
77
{
88
// Initialize server socket..
9-
TCPServer tcpServer;
9+
TCPServer<> tcpServer;
1010

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

examples/udp-client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../async-sockets/include/udpsocket.hpp"
1+
#include "udpsocket.hpp"
22
#include <iostream>
33

44
using namespace std;
@@ -10,7 +10,7 @@ int main()
1010
constexpr uint16_t PORT = 8888;
1111

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

1616
// Send String:

examples/udp-server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#include "../async-sockets/include/udpserver.hpp"
1+
#include "udpserver.hpp"
22
#include <iostream>
33

44
using namespace std;
55

66
int main()
77
{
88
// Initialize server socket..
9-
UDPServer udpServer;
9+
UDPServer<> udpServer;
1010

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

0 commit comments

Comments
 (0)