Made the sendAll and recvAll functions in sock.cpp use variables set by the children, rather than having the functions pass parameters

This commit is contained in:
2024-02-21 22:25:52 -05:00
parent 350b51e28b
commit 9954a18171
3 changed files with 58 additions and 26 deletions

View File

@@ -4,6 +4,7 @@
#include "includes/server.hpp"
#include "includes/exception_consts.hpp"
#include "includes/easysock.hpp"
#include "includes/connect_code.hpp"
/* Destructor - closes any open sockets */
Server::~Server() {
@@ -16,22 +17,37 @@ base method, because a different socket must be used. In the server's case, the
'peer' socket i.e. the socket returned after calling 'accept', must be used. */
void Server::sendAll(std::string to_send) {
Sock::sendAll(to_send, this->other_socket);
Sock::sendAll(to_send);
}
/* Receives data from socket, and returns it. This function works differently
based on the Transport layer protocol used. For TCP, it calls the parent method
with the peer socket. As mentioned above, this is the socket returned after
calling the accept function. With UDP, there is no concept of a peer socket, and
so the regular server socket (the one created in create_socket() ) is used
instead. */
based on the Transport layer protocol used. For TCP, it sets the 'other_socket' variable (used
by the parent function) to the peer socket. As mentioned above, this is the
socket returned after calling the accept function. With UDP, there is no concept
of a peer socket, and so the regular server socket (the one created in create_socket())
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() {
if (this->protocol == 'T') {
return Sock::recvAll(this->other_socket);
} else {
return Sock::recvAll(this->sock_fd);
if (this->protocol == ES_UDP) {
this->other_socket = this->sock_fd;
}
/* Call receive method of parent */
std::string 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;
}
/* FOR TCP ONLY - Waits for a peer to connect to the server socket. It stores
@@ -40,8 +56,8 @@ nothing if your server socket is a UDP socket. If the accept function fails, the
is thrown as an exception. */
void Server::wait_for_peer() {
if (this->protocol == 'T') {
this->other_socket = accept(this->sock_fd, NULL, NULL);
if (this->protocol == ES_TCP) {
this->other_socket = accept(this->sock_fd, dest, &addrlen);
if (this->other_socket < 0) {
throw errno;
}
@@ -61,9 +77,17 @@ void Server::create_socket() {
if (this->sock_fd < 0) {
throw (this->sock_fd * -1);
}
if (protocol == 'T') {
if (protocol == ES_TCP) {
listen(sock_fd, 10);
}
}
/* Returns the address of the peer socket as a string, can be used for debugging */
std::string Server::get_peer_addr() {
return this->peer_addr;
}
/* Returns the type of socket based on the global constants set in sock.hpp */
int Server::get_type() {
return SOCK_SERVER;
}