Skip to content
UDP_Protocol.h 1.41 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed
/*
 *
 *  Created on: Mar 1, 2021
 *      Author: astrisw
 */
Valerio Pastore's avatar
Valerio Pastore committed
#ifndef UDP_PROTOCOL_H_
#define UDP_PROTOCOL_H_
Valerio Pastore's avatar
Valerio Pastore committed

#include <Base_Protocol.h>

/*
Valerio Pastore's avatar
Valerio Pastore committed
 * class UDPProtocol
Valerio Pastore's avatar
Valerio Pastore committed
 * Implementation of a UDP socket
 * @member field fd_sock: socket file descriptor
 * @member field sockaddr_in: struct of the client address.
 */
Valerio Pastore's avatar
Valerio Pastore committed
namespace inaf::oasbo::ConnectionProtocols{
Valerio Pastore's avatar
Valerio Pastore committed

class UDPProtocol : public BaseProtocol{
protected:
	int srv_sock;
	int cli_sock;
	std::string ip{};
	int port;
Valerio Pastore's avatar
Valerio Pastore committed
	struct sockaddr_in cliaddr;
	struct sockaddr_in srvaddr;
	void split_ip_port(const std::string& ip_port, std::string& ip, std::string& port);
	bool isHeaderWellFormed(PacketLib::BasePacket &);
	int receiveHeader(PacketLib::BasePacket &);
	int receivePayloadAndTail(PacketLib::BasePacket &, int offset);
	void resetPacket(PacketLib::BasePacket &, int bytes);

Valerio Pastore's avatar
Valerio Pastore committed

public:
	UDPProtocol(std::string ip, int prt);
	UDPProtocol();
	~UDPProtocol();

	std::string getIp(){return ip;}
	int getPort(){ return port;}
	void setIp(std::string ip){ this->ip = ip;}
	void setPort(int port){this->port = port;}

	int connectToServer() override;
	int connectToClient() override;
	int closeConnectionToServer() override;
	int closeConnectionToClient() override;
	bool isConnectedToClient() const override;
	bool isConnectedToServer() const override;
	int receiveFromClient(PacketLib::BasePacket &) override;
	int sendToServer(PacketLib::BasePacket &) override;
Valerio Pastore's avatar
Valerio Pastore committed
};
}
#endif /* UDP_PROTOCOL_H_ */