-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAJOR: Transform to Header-only library.
- Loading branch information
Showing
30 changed files
with
569 additions
and
870 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#pragma once | ||
|
||
#if defined(__linux__) || defined(__APPLE__) | ||
#include <arpa/inet.h> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include <netdb.h> | ||
#elif _WIN32 | ||
#include <winsock32.h> | ||
#endif | ||
|
||
#include <string> | ||
#include <functional> | ||
#include <cerrno> | ||
|
||
#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)} | ||
|
||
class BaseSocket | ||
{ | ||
// Definitions | ||
public: | ||
enum SocketType | ||
{ | ||
TCP = SOCK_STREAM, | ||
UDP = SOCK_DGRAM | ||
}; | ||
const uint16_t BUFFER_SIZE = 0xFFFF; | ||
sockaddr_in address; | ||
bool isClosed = false; | ||
|
||
protected: | ||
int sock = 0; | ||
static std::string ipToString(sockaddr_in addr) | ||
{ | ||
char ip[INET_ADDRSTRLEN]; | ||
inet_ntop(AF_INET, &(addr.sin_addr), ip, INET_ADDRSTRLEN); | ||
|
||
return std::string(ip); | ||
} | ||
|
||
BaseSocket(FDR_ON_ERROR, SocketType sockType = TCP, int socketId = -1) | ||
{ | ||
if (socketId < 0) | ||
{ | ||
if ((this->sock = socket(AF_INET, sockType, 0)) < 0) | ||
{ | ||
onError(errno, "Socket creating error."); | ||
} | ||
} | ||
else | ||
{ | ||
this->sock = socketId; | ||
} | ||
} | ||
|
||
// Methods | ||
public: | ||
virtual void Close() { | ||
if(isClosed) return; | ||
|
||
isClosed = true; | ||
close(this->sock); | ||
} | ||
|
||
std::string remoteAddress() {return ipToString(this->address);} | ||
int remotePort() {return ntohs(this->address.sin_port);} | ||
int fileDescriptor() const { return this->sock; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#pragma once | ||
|
||
#include "tcpsocket.hpp" | ||
#include <thread> | ||
|
||
class TCPServer : public BaseSocket | ||
{ | ||
public: | ||
// Event Listeners: | ||
std::function<void(TCPSocket *)> onNewConnection = [](TCPSocket* sock){FDR_UNUSED(sock)}; | ||
|
||
explicit TCPServer(FDR_ON_ERROR): BaseSocket(onError, SocketType::TCP) | ||
{ | ||
int opt = 1; | ||
setsockopt(this->sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(int)); | ||
setsockopt(this->sock,SOL_SOCKET,SO_REUSEPORT,&opt,sizeof(int)); | ||
} | ||
|
||
// Binding the server. | ||
void Bind(const char *address, uint16_t port, FDR_ON_ERROR) | ||
{ | ||
if (inet_pton(AF_INET, address, &this->address.sin_addr) <= 0) | ||
{ | ||
onError(errno, "Invalid address. Address type not supported."); | ||
return; | ||
} | ||
|
||
this->address.sin_family = AF_INET; | ||
this->address.sin_port = htons(port); | ||
|
||
if (bind(this->sock, (const sockaddr *)&this->address, sizeof(this->address)) < 0) | ||
{ | ||
onError(errno, "Cannot bind the socket."); | ||
return; | ||
} | ||
} | ||
void Bind(int port, FDR_ON_ERROR) { this->Bind("0.0.0.0", port, onError); } | ||
|
||
// Start listening the server. | ||
void Listen(FDR_ON_ERROR) | ||
{ | ||
if (listen(this->sock, 20) < 0) | ||
{ | ||
onError(errno, "Error: Server can't listen the socket."); | ||
return; | ||
} | ||
|
||
std::thread t(Accept, this, onError); | ||
t.detach(); | ||
} | ||
|
||
// Overriding Close to add shutdown(): | ||
void Close() | ||
{ | ||
shutdown(this->sock, SHUT_RDWR); | ||
|
||
BaseSocket::Close(); | ||
} | ||
|
||
private: | ||
static void Accept(TCPServer *server, FDR_ON_ERROR) | ||
{ | ||
sockaddr_in newSocketInfo; | ||
socklen_t newSocketInfoLength = sizeof(newSocketInfo); | ||
|
||
int newSock; | ||
while (!server->isClosed) | ||
{ | ||
while ((newSock = accept(server->sock, (sockaddr *)&newSocketInfo, &newSocketInfoLength)) < 0) | ||
{ | ||
if (errno == EBADF || errno == EINVAL) return; | ||
|
||
onError(errno, "Error while accepting a new connection."); | ||
return; | ||
} | ||
|
||
if (!server->isClosed && newSock >= 0) | ||
{ | ||
TCPSocket *newSocket = new TCPSocket(onError, newSock); | ||
newSocket->setAddressStruct(newSocketInfo); | ||
|
||
server->onNewConnection(newSocket); | ||
newSocket->Listen(); | ||
} | ||
} | ||
} | ||
}; |
Oops, something went wrong.