diff --git a/async-sockets/include/tcpserver.hpp b/async-sockets/include/tcpserver.hpp index a91888b..e7beb59 100644 --- a/async-sockets/include/tcpserver.hpp +++ b/async-sockets/include/tcpserver.hpp @@ -3,11 +3,12 @@ #include "tcpsocket.hpp" #include +template class TCPServer : public BaseSocket { public: // Event Listeners: - std::function onNewConnection = [](TCPSocket* sock){FDR_UNUSED(sock)}; + std::function*)> onNewConnection = [](TCPSocket* sock){FDR_UNUSED(sock)}; explicit TCPServer(FDR_ON_ERROR): BaseSocket(onError, SocketType::TCP) { @@ -57,7 +58,7 @@ class TCPServer : public BaseSocket } private: - static void Accept(TCPServer* server, FDR_ON_ERROR) + static void Accept(TCPServer* server, FDR_ON_ERROR) { sockaddr_in newSocketInfo; socklen_t newSocketInfoLength = sizeof(newSocketInfo); @@ -75,7 +76,7 @@ class TCPServer : public BaseSocket return; } - TCPSocket* newSocket = new TCPSocket(onError, newSocketFileDescriptor); + TCPSocket* newSocket = new TCPSocket(onError, newSocketFileDescriptor); newSocket->deleteAfterClosed = true; newSocket->setAddressStruct(newSocketInfo); diff --git a/async-sockets/include/tcpsocket.hpp b/async-sockets/include/tcpsocket.hpp index 1b498a0..2f31dea 100644 --- a/async-sockets/include/tcpsocket.hpp +++ b/async-sockets/include/tcpsocket.hpp @@ -6,6 +6,7 @@ #include #include +template class TCPSocket : public BaseSocket { public: @@ -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 void Connect(uint32_t ipv4, uint16_t port, std::function onConnected = [](){}, FDR_ON_ERROR) { this->address.sin_family = AF_INET; @@ -46,10 +46,9 @@ class TCPSocket : public BaseSocket onConnected(); // Start listening from server: - this->Listen(); + this->Listen(); } // Connect to a TCP Server with `const char* host` & `uint16_t port` values - template void Connect(const char* host, uint16_t port, std::function onConnected = [](){}, FDR_ON_ERROR) { struct addrinfo hints, *res, *it; @@ -74,20 +73,18 @@ class TCPSocket : public BaseSocket freeaddrinfo(res); - this->Connect((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 void Connect(const std::string& host, uint16_t port, std::function onConnected = [](){}, FDR_ON_ERROR) { - this->Connect(host.c_str(), port, onConnected, onError); + this->Connect(host.c_str(), port, onConnected, onError); } // Start another thread to listen the socket - template void Listen() { - std::thread t(TCPSocket::Receive, this); + std::thread t(TCPSocket::Receive, this); t.detach(); } @@ -97,7 +94,6 @@ class TCPSocket : public BaseSocket bool deleteAfterClosed = false; private: - template static void Receive(TCPSocket* socket) { char tempBuffer[BUFFER_SIZE]; diff --git a/async-sockets/include/udpserver.hpp b/async-sockets/include/udpserver.hpp index df938d8..65ba250 100644 --- a/async-sockets/include/udpserver.hpp +++ b/async-sockets/include/udpserver.hpp @@ -3,7 +3,8 @@ #include "udpsocket.hpp" #include -class UDPServer : public UDPSocket +template +class UDPServer : public UDPSocket { public: // Bind the custom address & port of the server. diff --git a/async-sockets/include/udpsocket.hpp b/async-sockets/include/udpsocket.hpp index 41eb96d..3610358 100644 --- a/async-sockets/include/udpsocket.hpp +++ b/async-sockets/include/udpsocket.hpp @@ -4,24 +4,23 @@ #include #include +template class UDPSocket : public BaseSocket { public: std::function onMessageReceived; std::function onRawMessageReceived; - template explicit UDPSocket(bool useConnect = false, FDR_ON_ERROR, int socketId = -1): BaseSocket(onError, SocketType::UDP, socketId) { if (useConnect) { - - std::thread t(Receive, this); // usage with Connect() + std::thread t(Receive, this); // usage with Connect() t.detach(); } else { - std::thread t(ReceiveFrom, this); + std::thread t(ReceiveFrom, this); t.detach(); } } @@ -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 static void Receive(UDPSocket* udpSocket) { char tempBuffer[BUFFER_SIZE]; @@ -151,7 +149,6 @@ class UDPSocket : public BaseSocket } } - template static void ReceiveFrom(UDPSocket* udpSocket) { sockaddr_in hostAddr; diff --git a/examples/tcp-client.cpp b/examples/tcp-client.cpp index 13dea87..3e82b5e 100644 --- a/examples/tcp-client.cpp +++ b/examples/tcp-client.cpp @@ -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; }); @@ -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: diff --git a/examples/tcp-server.cpp b/examples/tcp-server.cpp index 5b44d40..7c642b8 100644 --- a/examples/tcp-server.cpp +++ b/examples/tcp-server.cpp @@ -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; diff --git a/examples/udp-client.cpp b/examples/udp-client.cpp index b689c41..d9e63aa 100644 --- a/examples/udp-client.cpp +++ b/examples/udp-client.cpp @@ -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: diff --git a/examples/udp-server.cpp b/examples/udp-server.cpp index 4201c55..990bb2d 100644 --- a/examples/udp-server.cpp +++ b/examples/udp-server.cpp @@ -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) {