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.

master
Aadhavan Srinivasan 8 months ago
parent 6d1565a020
commit c6bbe82d25

@ -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();

@ -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 */

Loading…
Cancel
Save