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.
This commit is contained in:
@@ -32,7 +32,10 @@ public:
|
|||||||
void sendAll(std::string to_send);
|
void sendAll(std::string to_send);
|
||||||
|
|
||||||
/* Receive data from peer socket */
|
/* Receive data from peer socket */
|
||||||
std::string recvAll();
|
char* recvAll();
|
||||||
|
|
||||||
|
/* Non-blocking receive */
|
||||||
|
char* recvAllNB();
|
||||||
|
|
||||||
/* Return the address of the peer */
|
/* Return the address of the peer */
|
||||||
std::string get_peer_addr();
|
std::string get_peer_addr();
|
||||||
|
36
server.cpp
36
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
|
is used instead. This function also sets the 'peer_addr' string to the address of the
|
||||||
peer socket, handling both TCP and UDP. */
|
peer socket, handling both TCP and UDP. */
|
||||||
|
|
||||||
std::string Server::recvAll() {
|
char* Server::recvAll() {
|
||||||
if (this->protocol == ES_UDP) {
|
if (this->protocol == ES_UDP) {
|
||||||
this->other_socket = this->sock_fd;
|
this->other_socket = this->sock_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Call receive method of parent */
|
/* 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 */
|
/* Set the peer address of the socket */
|
||||||
if (this->ip_ver == 4) {
|
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
|
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
|
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
|
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() {
|
void Server::create_socket() {
|
||||||
Sock::create_socket();
|
Sock::create_socket();
|
||||||
@@ -80,6 +104,12 @@ void Server::create_socket() {
|
|||||||
if (protocol == ES_TCP) {
|
if (protocol == ES_TCP) {
|
||||||
listen(sock_fd, 10);
|
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 */
|
/* Returns the address of the peer socket as a string, can be used for debugging */
|
||||||
|
Reference in New Issue
Block a user