documentation for class TCPSocket, TCPClient and TCPServer added.

This commit is contained in:
Roland Jax
2014-11-18 15:38:36 +01:00
parent 48fcf39d53
commit 1415b143af
5 changed files with 89 additions and 8 deletions
+2 -3
View File
@@ -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<const char*>("server")
<< ":" << A.getOptVal<int>("port") << std::endl;
}
delete client;
}
+2
View File
@@ -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)");
+1 -1
View File
@@ -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.
+1 -1
View File
@@ -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 <typename T>
+83 -3
View File
@@ -23,61 +23,141 @@
#include <unistd.h>
#include <string>
/**
* @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;
};