/* * * Created on: Mar 1, 2021 * Author: astrisw */ #pragma once #include #include /* * class UDPProtocol * Implementation of a UDP socket * @member field fd_sock: socket file descriptor * @member field sockaddr_in: struct of the client address. */ namespace inaf::oasbo::ConnectionProtocols { class UDPProtocol: public BaseProtocol { protected: int srv_sock; int cli_sock; struct sockaddr_in cliaddr; struct sockaddr_in srvaddr; void split_ip_port(const std::string &ip_port, std::string &ip, std::string &port); int receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, int packetSize); void resetPacket(PacketLib::BasePacket&, int bytes); UDPProtocol(std::string ip, int prt); UDPProtocol(); public: std::string ip { }; int port; ~UDPProtocol(); 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; friend class Builder; class Builder { protected: UDPProtocol *protocol; public: std::string ip_key { "ip" }; std::string port_key { "port" }; Builder(); Builder(std::string ip, int port); ~Builder(); void reset(); Builder* configFrom(Configurators::BaseConfigurator *conf); Builder* setIp(std::string ip); Builder* setPort(int port); UDPProtocol* getProtocol(); }; }; }