small code cleanups, Connection hides Socket

master
Denes Matetelki 13 years ago
parent 60d6757975
commit 76dd8a2ca6

@ -8,6 +8,8 @@
#include <string> #include <string>
/** @todo make connection an iface and this class shall be a TcpConnection,
* inherited from connection */
template <typename T> template <typename T>
class Connection class Connection
@ -95,27 +97,23 @@ public:
{ {
TRACE; TRACE;
// LOG ( Logger::DEBUG, std::string("receving on socket: "). ssize_t length;
// append(TToStr(m_socket.getSocket())).c_str() ); if ( !m_socket.receive(m_buffer, m_bufferLength, &length) ) {
if (length == -1) {
ssize_t len = recv(m_socket.getSocket(), m_buffer, m_bufferLength, 0); LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() );
}
LOG ( Logger::DEBUG, std::string("Received: "). else if (length == 0) {
append(TToStr(len)).append(" bytes from: "). LOG( Logger::INFO, std::string("Connection closed by ").
append(m_host).append(":").append(m_port).c_str() ); append(m_host).append(":").append(m_port).c_str() );
}
if (len == -1) { return false;
LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() );
return false;
} }
if (len == 0) { LOG ( Logger::DEBUG, std::string("Received: ").
LOG( Logger::INFO, std::string("Connection closed by "). append(TToStr(length)).append(" bytes from: ").
append(m_host).append(":").append(m_port).c_str() ); append(m_host).append(":").append(m_port).c_str() );
return false;
}
return m_message.buildMessage( (void*)m_buffer, (size_t)len); return m_message.buildMessage( (void*)m_buffer, (size_t)length);
} }

@ -36,8 +36,6 @@ protected:
Connection<T> *m_connection; Connection<T> *m_connection;
void *m_param; void *m_param;
/// @todo shall i use dinamic array?
std::string m_buffer; std::string m_buffer;
private: private:

@ -25,7 +25,7 @@ public:
, m_num_of_fds(0) , m_num_of_fds(0)
{ {
TRACE; TRACE;
m_fds = new pollfd[m_maxclients]; m_fds = new pollfd[m_maxclients+1]; // plus the server socket
addFd( m_connection->getSocket(), POLLIN | POLLPRI ); addFd( m_connection->getSocket(), POLLIN | POLLPRI );
} }
@ -74,6 +74,7 @@ public:
protected: protected:
// can be overriden: behaviour alters in server/client
virtual void acceptClient() virtual void acceptClient()
{ {
TRACE; TRACE;
@ -98,6 +99,8 @@ protected:
addFd( client_socket, POLLIN | POLLPRI ); addFd( client_socket, POLLIN | POLLPRI );
} }
// can be overriden: behaviour alters in server/client
virtual void handleClient( const int socket ) virtual void handleClient( const int socket )
{ {
TRACE; TRACE;

@ -33,6 +33,8 @@ public:
std::string &port); std::string &port);
bool send( const void *message, const int lenght ); bool send( const void *message, const int lenght );
bool receive ( void* buffer, const int bufferLen, ssize_t *msgLen );
int getSocket() const; int getSocket() const;
static bool convertNameInfo( sockaddr* addr, static bool convertNameInfo( sockaddr* addr,

@ -38,7 +38,7 @@ private:
protected: protected:
/// @todo this is unclear and nasty hack // overridig poll's behaviour
virtual void acceptClient() virtual void acceptClient()
{ {
TRACE; TRACE;
@ -47,7 +47,7 @@ private:
stopPolling(); stopPolling();
} }
/// @todo this is unclear and nasty hack // overridig poll's behaviour
virtual void handleClient( const int ) virtual void handleClient( const int )
{ {
TRACE; TRACE;
@ -120,11 +120,6 @@ private:
TcpClient(const TcpClient& ); TcpClient(const TcpClient& );
TcpClient& operator=(const TcpClient& ); TcpClient& operator=(const TcpClient& );
Connection<T>& getConnection()
{
TRACE;
return m_connection;
}
Connection<T> m_connection; Connection<T> m_connection;
PollerThread<T> m_watcher; PollerThread<T> m_watcher;
@ -132,5 +127,3 @@ private:
}; };
#endif // TCP_CLIENT_HPP #endif // TCP_CLIENT_HPP

@ -70,11 +70,22 @@ int main(int argc, char* argv[] )
TcpClient<SimpleMessage> tcpclient(argv[1], argv[2], &finished); TcpClient<SimpleMessage> tcpclient(argv[1], argv[2], &finished);
tcpclient.connect(); if ( !tcpclient.connect() ) {
sleep(1); // wait for thread creation LOG( Logger::ERR, "Couldn't connect to server, exiting..." );
Logger::destroy();
return 1;
}
// wait for thread creation
sleep(1);
// send message to server
std::string msg1(argv[3]); std::string msg1(argv[3]);
tcpclient.send( msg1.c_str(), msg1.length()); if ( !tcpclient.send( msg1.c_str(), msg1.length()) ) {
LOG( Logger::ERR, "Couldn't send message to server, exiting..." );
Logger::destroy();
return 1;
}
// wait for the complate &handled reply // wait for the complate &handled reply
struct timespec tm = {0,1000}; struct timespec tm = {0,1000};
@ -82,7 +93,6 @@ int main(int argc, char* argv[] )
nanosleep(&tm, &tm) ; nanosleep(&tm, &tm) ;
tcpclient.disconnect(); tcpclient.disconnect();
Logger::destroy(); Logger::destroy();
return 0; return 0;
} }

@ -66,7 +66,11 @@ int main()
TcpServer<EchoMessage> tcpServer("localhost", "4455"); TcpServer<EchoMessage> tcpServer("localhost", "4455");
tcpServer.start(); if ( !tcpServer.start() ) {
LOG( Logger::ERR, "Failed to start TCP server, exiting...");
Logger::destroy();
return 1;
}
// never reached // never reached
sleep(1); sleep(1);

@ -131,6 +131,16 @@ bool Socket::send ( const void *message, const int length )
} }
bool Socket::receive( void *buffer, const int bufferLen, ssize_t *msgLen )
{
TRACE;
*msgLen = recv(m_socket, buffer, bufferLen, 0);
return (*msgLen > 0);
}
int Socket::getSocket() const int Socket::getSocket() const
{ {
TRACE; TRACE;

Loading…
Cancel
Save