From c6bbe82d2506d4baa23f0ac704324dd86c92a8b8 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 27 Feb 2024 23:59:53 -0500 Subject: [PATCH] Made the 'recvAll' function return a char pointer instead of a std::string, this is better for portability. Also created a non-blocking version of the function. --- includes/server.hpp | 7 +++++-- server.cpp | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/includes/server.hpp b/includes/server.hpp index 0c70665..1d22e5c 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -30,10 +30,13 @@ public: FOR UDP - Send data to the client, from which data was received. FOR UDP, this function MUST be called after recvAll() */ void sendAll(std::string to_send); - + /* Receive data from peer socket */ - std::string recvAll(); + char* recvAll(); + /* Non-blocking receive */ + char* recvAllNB(); + /* Return the address of the peer */ std::string get_peer_addr(); diff --git a/server.cpp b/server.cpp index b0c4e59..fe0ad6b 100644 --- a/server.cpp +++ b/server.cpp @@ -28,13 +28,36 @@ of a peer socket, and so the regular server socket (the one created in create_so is used instead. This function also sets the 'peer_addr' string to the address of the peer socket, handling both TCP and UDP. */ -std::string Server::recvAll() { +char* Server::recvAll() { if (this->protocol == ES_UDP) { this->other_socket = this->sock_fd; } /* Call receive method of parent */ - std::string to_return = Sock::recvAll(); + char* to_return = Sock::recvAll(); + + /* Set the peer address of the socket */ + if (this->ip_ver == 4) { + /* FOR IPv4 */ + struct sockaddr_in* temp_struct = (struct sockaddr_in*)this->dest; + /* Convert the s_addr field of the caseted struct to host network-byte, and convert it to a dotted decimal */ + peer_addr = connect_code::dec_to_dotted_dec(std::to_string(htonl(temp_struct->sin_addr.s_addr))); + } else { + /* FOR IPv6 */ + peer_addr = "IPV6 NOT SUPPORTED YET"; + } + + return to_return; +} + +/* Same as function above, but calls Sock::recvAllNB() instead */ +char* Server::recvAllNB() { + if (this->protocol == ES_UDP) { + this->other_socket = this->sock_fd; + } + + /* Call receive method of parent */ + char* to_return = Sock::recvAllNB(); /* Set the peer address of the socket */ if (this->ip_ver == 4) { @@ -69,7 +92,8 @@ called immediately after the constructor. If the socket is TCP, it also sets the socket to listen for incoming connections. This function throws an exception if the socket could not be created. The excpetion is an integer corresponding to the errno of the failing function, and enables the caller to print a corresponding error message by -'catching' the thrown exception and using strerror(). */ +'catching' the thrown exception and using strerror(). +This function also sets a timeout of 100ms for UDP sockets */ void Server::create_socket() { Sock::create_socket(); @@ -80,6 +104,12 @@ void Server::create_socket() { if (protocol == ES_TCP) { listen(sock_fd, 10); } +// if (protocol == ES_UDP) { +// struct timeval tv; +// tv.tv_sec = 0; +// tv.tv_usec = 10000; +// setsockopt(this->sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); +// } } /* Returns the address of the peer socket as a string, can be used for debugging */