small code cleanups, Connection hides Socket

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

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

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

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

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

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

@ -70,11 +70,22 @@ int main(int argc, char* argv[] )
TcpClient<SimpleMessage> tcpclient(argv[1], argv[2], &finished);
tcpclient.connect();
sleep(1); // wait for thread creation
if ( !tcpclient.connect() ) {
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]);
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
struct timespec tm = {0,1000};
@ -82,7 +93,6 @@ int main(int argc, char* argv[] )
nanosleep(&tm, &tm) ;
tcpclient.disconnect();
Logger::destroy();
return 0;
}

@ -66,7 +66,11 @@ int main()
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
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
{
TRACE;

Loading…
Cancel
Save