From 1415b143afdfde1713956866d4215a3ec0f8b40c Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 18 Nov 2014 15:38:36 +0100 Subject: [PATCH] documentation for class TCPSocket, TCPClient and TCPServer added. --- src/ebusctl/ebusctl.cpp | 5 +-- src/ebusd/ebusd.cpp | 2 + src/lib/ebus/port.h | 2 +- src/lib/utils/appl.h | 2 +- src/lib/utils/tcpsocket.h | 86 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index a1c15ca3..0bf1596c 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -41,7 +41,7 @@ void define_args() A.addText(" 'help' show server commands\n\n" " 'feed' sends a dump file to a local serial device (pts)\n" " (hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)\n\n" - " Options:\n"); + "Options:\n"); A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, "virtual serial device (/dev/ttyUSB60)"); @@ -124,10 +124,9 @@ int main(int argc, char* argv[]) delete socket; } - else { + else std::cout << "error connecting to " << A.getOptVal("server") << ":" << A.getOptVal("port") << std::endl; - } delete client; } diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 7020a50e..88e69330 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -41,6 +41,8 @@ void define_args() A.setVersion(""PACKAGE_STRING""); + A.addText("Options:\n"); + A.addOption("address", "a", OptVal("FF"), dt_string, ot_mandatory, "\tebus device address (FF)"); diff --git a/src/lib/ebus/port.h b/src/lib/ebus/port.h index 83badc92..b8097af0 100644 --- a/src/lib/ebus/port.h +++ b/src/lib/ebus/port.h @@ -74,7 +74,7 @@ public: bool isOpen(); /** - * @brief sendBytes write bytes into opened file descriptor. + * @brief sendBytes write bytes to opened file descriptor. * @param buffer data to send. * @param nbytes number of bytes to send. * @return number of written bytes or -1 if an error has occured. diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index 80894c9f..b9b79478 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -113,7 +113,7 @@ public: /** * @brief returns the value of the interested option - * @param the interested option + * @param name the interested option * @return casted value */ template diff --git a/src/lib/utils/tcpsocket.h b/src/lib/utils/tcpsocket.h index 8f86f8bb..cf9d974b 100644 --- a/src/lib/utils/tcpsocket.h +++ b/src/lib/utils/tcpsocket.h @@ -23,61 +23,141 @@ #include #include +/** + * @brief class for low level tcp socket operations. (open, close, send, receive) + */ class TCPSocket { public: + /** grant access for friend classes */ friend class TCPClient; friend class TCPServer; + /** + * @brief destructor. + */ ~TCPSocket() { close(m_sfd); } - ssize_t recv(char* buffer, size_t len) { return read(m_sfd, buffer, len); } + /** + * @brief write bytes to opened file descriptor. + * @param buffer data to send. + * @param len number of bytes to send. + * @return number of written bytes or -1 if an error has occured. + */ ssize_t send(const char* buffer, size_t len) { return write(m_sfd, buffer, len); } + /** + * @brief read bytes from opened file descriptor. + * @param buffer for received bytes. + * @param len size of the receive buffer. + * @return number of read bytes or -1 if an error has occured. + */ + ssize_t recv(char* buffer, size_t len) { return read(m_sfd, buffer, len); } + + /** + * @brief returns the tcp port. + * @return the tcp port. + */ int getPort() const { return m_port; } + + /** + * @brief returns the ip address. + * @return the ip address. + */ std::string getIP() const { return m_ip; } + /** + * @brief returns the file descriptor. + * @return the file descriptor. + */ int getFD() const { return m_sfd; } + + /** + * @brief returns status of file descriptor. + * @return true if file descriptor is valid. + */ bool isValid(); private: + /** file descriptor from tcp socket */ int m_sfd; + /** port of tcp socket */ int m_port; + /** ip address of tcp socket */ std::string m_ip; + /** + * @brief private constructor, limited access only for friend classes. + * @param sfd the file desctriptor of tcp socket. + * @param address struct which holds the ip address. + */ TCPSocket(int sfd, struct sockaddr_in* address); }; +/** + * @brief class to initiate a tcp socket connection to a listening server. + */ class TCPClient { public: + /** + * @brief initiate a tcp socket connection to a listening server. + * @param server the server name or ip address to connect. + * @param port the tcp port. + * @return pointer to an opened tcp socket. + */ TCPSocket* connect(const std::string& server, const int& port); -private: - }; +/** + * @brief class for a tcp based network server. + */ class TCPServer { public: + /** + * @brief creates a new instance of a listening tcp server. + * @param port the tcp port + * @param address the ip address + */ TCPServer(const int port, const std::string address) : m_lfd(0), m_port(port), m_address(address), m_listening(false) {} + /** + * @brief destructor. + */ ~TCPServer() { if (m_lfd > 0) {close(m_lfd);} } + /** + * @brief start listening of tcp socket. + */ int start(); + + /** + * @brief accept an incomming tcp connection and create a local tcp socket for communication. + * @return pointer to an opened tcp socket. + */ TCPSocket* newSocket(); + /** + * @brief returns the file descriptor. + * @return the file descriptor. + */ int getFD() const { return m_lfd; } private: + /** file descriptor from listening tcp socket */ int m_lfd; + /** listening tcp port */ int m_port; + /** listening tcp socket ip address */ std::string m_address; + /** true if object is already listening */ bool m_listening; };