From 8ffb7c10d50d9e9cb522b0aaf7a73398321e38c9 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Fri, 24 Jan 2025 10:49:08 +0100 Subject: [PATCH] change typedef --- server/src/server.cpp | 31 ++++++++++++++++--------------- shared/inc/TcpSocket.hpp | 5 +---- shared/src/TcpSocket.cpp | 3 +-- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/server/src/server.cpp b/server/src/server.cpp index 03b0f9f..94411a5 100644 --- a/server/src/server.cpp +++ b/server/src/server.cpp @@ -5,25 +5,26 @@ using namespace std; // use pthread rw_lock to lock db so you can safy clone .db file +// When a new client connected: +void call(int sock, sockaddr_in newSocketInfo) +{ + std::cout << "new User" << std::endl; + char tempBuffer[AS_DEFAULT_BUFFER_SIZE + 1]; + ssize_t messageLength; + while ((messageLength = TcpSocket::recvt(sock, tempBuffer, AS_DEFAULT_BUFFER_SIZE)) > 0) + { + tempBuffer[messageLength] = '\0'; + TcpSocket::sendt(sock, tempBuffer, messageLength); + } + std::cout << "del USER" << std::endl; + TcpSocket::closet(sock); +} + int main() { - // When a new client connected: - TcpSocket::OnNewConnectionCallBack onNewConnection = [](int sock, sockaddr_in newSocketInfo) - { - std::cout << "new User" << std::endl; - char tempBuffer[AS_DEFAULT_BUFFER_SIZE + 1]; - ssize_t messageLength; - while ((messageLength = TcpSocket::recvt(sock, tempBuffer, AS_DEFAULT_BUFFER_SIZE)) > 0) - { - tempBuffer[messageLength] = '\0'; - TcpSocket::sendt(sock, tempBuffer, messageLength); - } - std::cout << "del USER" << std::endl; - TcpSocket::closet(sock); - }; // Bind the server to a port. - int err = TcpSocket::listent("0.0.0.0", 8888, onNewConnection); + int err = TcpSocket::listent("0.0.0.0", 8888, call); if (err < 0) { printf("ERROR %d", err); diff --git a/shared/inc/TcpSocket.hpp b/shared/inc/TcpSocket.hpp index bf2bd8a..8f21f88 100644 --- a/shared/inc/TcpSocket.hpp +++ b/shared/inc/TcpSocket.hpp @@ -1,6 +1,3 @@ - - -#include #include #include @@ -22,7 +19,7 @@ namespace TcpSocket int remotePort(sockaddr_in &address); int connectt(const char *host, uint16_t port); - typedef std::function OnNewConnectionCallBack; + typedef void(OnNewConnectionCallBack)(int sock, sockaddr_in newSocketInfo); int listent(const char *host, uint16_t port, OnNewConnectionCallBack callback); } // namespace TcpSocket diff --git a/shared/src/TcpSocket.cpp b/shared/src/TcpSocket.cpp index f0c115f..613360a 100644 --- a/shared/src/TcpSocket.cpp +++ b/shared/src/TcpSocket.cpp @@ -1,3 +1,4 @@ +#include "TcpSocket.hpp" #include #include @@ -95,8 +96,6 @@ namespace TcpSocket return sock; } - typedef std::function OnNewConnectionCallBack; - int listent(const char *host, uint16_t port, OnNewConnectionCallBack callback) { sockaddr_in address;