diff --git a/deps/Base-DAQ b/deps/Base-DAQ index 8a0ea2d0e699863df5fe1c91caf2d7b0855957be..a00f9a27afbf5f75dab7db2368b9b9b6fcb395e1 160000 --- a/deps/Base-DAQ +++ b/deps/Base-DAQ @@ -1 +1 @@ -Subproject commit 8a0ea2d0e699863df5fe1c91caf2d7b0855957be +Subproject commit a00f9a27afbf5f75dab7db2368b9b9b6fcb395e1 diff --git a/include/TCP_Protocol.h b/include/TCP_Protocol.h index 2996157edd5314c98a1eff726469693618f67720..3bb961ef6359d0b5de908903e6e34fa1554f34cf 100755 --- a/include/TCP_Protocol.h +++ b/include/TCP_Protocol.h @@ -1,62 +1,181 @@ - #pragma once #include #include +/** + * @brief The namespace inaf::oasbo::Receivers contains classes related to receivers in the ASTRI DAQ system. + */ namespace inaf::oasbo::Receivers { +/** + * @brief The TCPProtocol class is a derived class of BaseReceiver and represents a TCP protocol receiver. + * check the Base_Receiver.h file for more information. + */ class TCPProtocol: public BaseReceiver { protected: + /** + * @brief Constructs a TCPProtocol object with the specified IP address and port number. + * @param ip The IP address to connect to. + * @param prt The port number to connect to. + */ TCPProtocol(std::string ip, int prt); + + /** + * @brief Constructs a TCPProtocol object with default values for IP address and port number. + */ TCPProtocol(); - std::string ip; - int port; - int srv_sock; - int cli_sock; - struct sockaddr_in servaddr, cliaddr; - bool checkConnFlag = true; + std::string ip; /**< The IP address to connect to. */ + int port; /**< The port number to connect to. */ + int srv_sock; /**< The server socket. */ + int cli_sock; /**< The client socket. */ + struct sockaddr_in servaddr; /**< The server address. */ + struct sockaddr_in cliaddr; /**< The client address. */ + + bool checkConnFlag = true; /**< Flag that tells if it has to check for incoming connection. Set to false when calling closeConnectionToClient */ + + /** + * @brief Helper function. Connects to the client with the specified IP address and port number. + * @param ip The IP address to connect to. + * @param port The port number to connect to. + * @return The result of the connection attempt. + */ int m_connectToCli(std::string ip, int port); - void resetPacket(inaf::oasbo::PacketLib::BasePacket &pack, int bytes); - bool split_ip_port(const std::string &ip_port, std::string &ip, - int &port); + /** + * @brief Resets the first bytes of the packet to 0. + * @param pack The packet to reset. + * @param bytes The number of bytes to reset. + */ + void resetPacket(inaf::oasbo::Packets::BasePacket &pack, int bytes); + + /** + * @brief Splits the IP address and port number from the specified string. + * @param ip_port The string containing the IP address and port number. + * @param ip The extracted IP address. + * @param port The extracted port number. + * @return True if the split was successful, false otherwise. + */ + bool split_ip_port(const std::string &ip_port, std::string &ip, int &port); public: + /** + * @brief Destroys the TCPProtocol object. + */ ~TCPProtocol(); + /** + * @brief Gets the host name. + * @return The host name. + */ std::string getHost() override; + + /** + * @brief Sets the host name. + * @param host The host name to set. + */ void setHost(std::string host) override; + /** + * @brief Sets the IP address. + * @param ip The IP address to set. + */ void setIp(std::string); - std::string getIp(){return this->ip;} + + /** + * @brief Gets the IP address. + * @return The IP address. + */ + std::string getIp() { + return this->ip; + } + + /** + * @brief Sets the port number. + * @param port The port number to set. + */ void setPort(int); - int getPort(){return this->port;} + + /** + * @brief Gets the port number. + * @return The port number. + */ + int getPort() { + return this->port; + } int connectToClient() override; + int closeConnectionToClient() override; + bool isConnectedToClient() const override; - int receiveFromClient(PacketLib::BasePacket&) override; + + int receiveFromClient(Packets::BasePacket&) override; friend class TCPProtocolBuilder; }; + +/** + * @brief The TCPProtocolBuilder class is a builder class for TCPProtocol objects. + */ class TCPProtocolBuilder { protected: - TCPProtocol *protocol; + TCPProtocol *protocol; /**< The TCPProtocol object being built. */ public: - std::string config_target { "tcpreceiver" }; - std::string ip_key { "ip" }; - std::string port_key { "port" }; + std::string config_target { "tcpreceiver" }; /**< The configuration target. */ + std::string ip_key { "ip" }; /**< The IP configuration key. */ + std::string port_key { "port" }; /**< The port configuration key. */ + /** + * @brief Constructs a TCPProtocolBuilder object. + */ TCPProtocolBuilder(); + + /** + * @brief Constructs a TCPProtocolBuilder object with the specified IP address and port number. + * @param ip The IP address to set. + * @param port The port number to set. + */ TCPProtocolBuilder(std::string ip, int port); + + /** + * @brief Destroys the TCPProtocolBuilder object. + */ ~TCPProtocolBuilder(); + + /** + * @brief Resets the builder to its initial state. + */ void reset(); + + /** + * @brief Configures the builder from the specified configurator. + * @param conf The configurator to use. + * @return The configured TCPProtocolBuilder object. + */ TCPProtocolBuilder* configFrom(Configurators::BaseConfigurator &conf); + + /** + * @brief Sets the IP address. + * @param ip The IP address to set. + * @return The TCPProtocolBuilder object. + */ TCPProtocolBuilder* setIp(std::string ip); + + /** + * @brief Sets the port number. + * @param port The port number to set. + * @return The TCPProtocolBuilder object. + */ TCPProtocolBuilder* setPort(int port); + + /** + * @brief Gets the built TCPProtocol object. + * @return The built TCPProtocol object. + */ TCPProtocol* getReceiver(); }; -} + +} // namespace inaf::oasbo::Receivers diff --git a/src/TCP_Protocol.cpp b/src/TCP_Protocol.cpp index 088dd61c36c6af7e95126830104217050ca0d991..7942ff568c2047c42098054864fa33182df999e9 100755 --- a/src/TCP_Protocol.cpp +++ b/src/TCP_Protocol.cpp @@ -27,12 +27,12 @@ TCPProtocol::TCPProtocol(std::string ip, int prt) : TCPProtocol::~TCPProtocol() { closeConnectionToClient(); - if(srv_sock != -1){ + if (srv_sock != -1) { ::close(srv_sock); } } -int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { +int TCPProtocol::receiveFromClient(Packets::BasePacket &pack) { ssize_t headerSize = pack.getHeaderSize(); uint8_t *buff = new uint8_t[pack.getPacketStructureByteSize()]; ssize_t rec = 0; @@ -56,7 +56,7 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { to_be_rcv -= rec; } - if(!this->checkConnFlag){ // the connection has been terminated + if (!this->checkConnFlag) { // the connection has been terminated closeConnectionToClient(); delete[] buff; return 0; @@ -64,7 +64,7 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { // entire header received... pack.copyToMemory(buff, headerSize); - if (!pack.hasRecognizedHeader()) { + if (!pack.hasRecognizedHeader()) { // header not recognized resetPacket(pack, headerSize); delete[] buff; return -1; @@ -73,7 +73,7 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { // receive the rest of the packet size_t payload_and_tail_size = pack.getPayloadSize() + pack.getTailSize(); to_be_rcv = payload_and_tail_size; - while (this->checkConnFlag && to_be_rcv > 0) { // go ahead until the resto of the packet received or connection terminates + while (this->checkConnFlag && to_be_rcv > 0) { // go ahead until the rest of the packet received or connection terminates rec = ::recv(this->cli_sock, &buff[headerSize], to_be_rcv, MSG_DONTWAIT); if (rec == 0) { // connection closed @@ -91,14 +91,13 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) { to_be_rcv -= rec; } - if(!this->checkConnFlag){ // the connection has been terminated + if (!this->checkConnFlag) { // the connection has been terminated closeConnectionToClient(); delete[] buff; return 0; } - - pack.copyToMemory(&buff[headerSize],payload_and_tail_size, headerSize); + pack.copyToMemory(&buff[headerSize], payload_and_tail_size, headerSize); delete[] buff; return headerSize + payload_and_tail_size; @@ -197,7 +196,7 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) { return cli_sock; } -void TCPProtocol::resetPacket(PacketLib::BasePacket &pack, int bytes) { +void TCPProtocol::resetPacket(Packets::BasePacket &pack, int bytes) { uint8_t *buff = new uint8_t[bytes]; std::memset(buff, 0, bytes); int toBeReset = std::min(