Message has COnnection as parameter. tcpserver_main.cpp works again.

master
Denes Matetelki 13 years ago
parent e4b28fcdbe
commit a59674ba9d

@ -26,7 +26,7 @@ public:
, m_host()
, m_port()
, m_status(CLOSED)
, m_message(msgParam)
, m_message(this, msgParam)
, m_buffer(0)
, m_bufferLength(bufferLength)
@ -45,7 +45,7 @@ public:
, m_host(host)
, m_port(port)
, m_status(CLOSED)
, m_message(msgParam)
, m_message(this, msgParam)
, m_buffer(0)
, m_bufferLength(bufferLength)
{
@ -73,6 +73,12 @@ public:
return m_socket.bindToHost(m_host, m_port);
}
bool listen( const int maxPendingQueueLen = 64 )
{
TRACE;
return m_socket.listen( maxPendingQueueLen );
}
void closeConnection()
{
TRACE;
@ -118,6 +124,12 @@ public:
return m_socket.getSocket();
}
std::string getHost() const
{
TRACE;
return m_host;
}
private:

@ -1,6 +1,8 @@
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
#include "Connection.hpp"
#include <string>
#include <stddef.h> // size_t
@ -9,13 +11,17 @@
* getExpectedLength().
*/
template <typename T>
class Message
{
public:
Message( void * msgParam = 0 )
: m_buffer()
, m_param(msgParam) {};
Message( Connection<T> *connection,
void *msgParam = 0 )
: m_connection(connection)
, m_param(msgParam)
, m_buffer()
{};
virtual ~Message() {};
@ -27,10 +33,12 @@ protected:
virtual size_t getExpectedLength() = 0;
/// @todo shall i use dinamic array?
std::string m_buffer;
void *m_param;
Connection<T> *m_connection;
void *m_param;
/// @todo shall i use dinamic array?
std::string m_buffer;
};

@ -15,10 +15,8 @@ class Poll
{
public:
Poll( Connection<T> &connection,
const nfds_t maxClient = 10,
void *MessageParam )
)
Poll( Connection<T> *connection,
const nfds_t maxClient = 10 )
: m_connection(connection)
, m_polling(false)
, m_connectionPool()
@ -28,7 +26,7 @@ public:
{
TRACE;
m_fds = new pollfd[m_maxclients];
addFd( m_connection.getSocket(), POLLIN | POLLPRI );
addFd( m_connection->getSocket(), POLLIN | POLLPRI );
}
virtual ~Poll()
@ -60,7 +58,7 @@ public:
for ( nfds_t i = 0; i < m_num_of_fds; ++i )
if ( m_fds[i].revents != 0 )
m_fds[i].fd == m_connection.getSocket() ?
m_fds[i].fd == m_connection->getSocket() ?
acceptClient(m_fds[i].fd) :
handleClient(m_fds[i].fd);
@ -82,7 +80,7 @@ protected:
sockaddr clientAddr;
socklen_t clientAddrLen;
int client_socket = accept( m_connection.getSocket(),
int client_socket = accept( m_connection->getSocket(),
&clientAddr, &clientAddrLen ) ;
if ( client_socket == -1 ) {
@ -165,7 +163,7 @@ private:
typedef typename std::map< int, Connection<T>* > ConnectionPool;
Connection<T> &m_connection;
Connection<T> *m_connection;
bool m_polling;
ConnectionPool m_connectionPool;

@ -11,21 +11,23 @@ class Socket
{
public:
Socket(const int domain,
const int type,
const int protocol = 0);
Socket( const int domain,
const int type,
const int protocol = 0 );
Socket(const int socket );
Socket( const int socket );
virtual ~Socket();
bool createSocket();
void closeSocket();
bool connectToHost(const std::string host,
const std::string port);
bool bindToHost(const std::string host,
const std::string port );
bool connectToHost( const std::string host,
const std::string port );
bool bindToHost( const std::string host,
const std::string port );
bool listen( const int maxPendingQueueLen = 64 );
void getPeerName(std::string &host,
std::string &port);

@ -23,7 +23,7 @@ private:
{
public:
PollerThread( TcpClient<U> &data )
: Poll<U>(data.m_connection)
: Poll<U>( &(data.m_connection) )
, m_tcpClient(data)
{
TRACE;

@ -1,39 +1,67 @@
#ifndef TCP_SERVER_HPP
#define TCP_SERVER_HPP
#include "Socket.hpp"
#include "Logger.hpp"
#include "Connection.hpp"
#include "Poll.hpp"
#include <string>
class TcpServer : public Socket
, public Poll
template <typename T>
class TcpServer
{
public:
TcpServer ( const std::string host,
const std::string port,
const int maxClients = 5 );
const int maxClients = 5,
const int maxPendingQueueLen = 10 )
: m_connection(host, port)
, m_poll( &m_connection, maxClients)
, m_maxPendingQueueLen(maxPendingQueueLen)
{
TRACE;
}
virtual ~TcpServer()
{
TRACE;
}
bool start()
{
TRACE;
if ( !m_connection.bindToHost() )
return false;
virtual ~TcpServer();
if ( m_connection.listen( m_maxPendingQueueLen ) == -1 ) {
return false;
}
bool start();
void stop();
m_poll.startPolling();
return true;
}
// implements Poll::receive
bool receive( const int fd );
void stop()
{
TRACE;
m_poll.stopPolling();
m_connection.closeConnection();
}
virtual void msgArrived(const int clientSocket,
const unsigned char* msg,
const int msgLen ) = 0;
private:
TcpServer(const TcpServer&);
TcpServer& operator=(const TcpServer&);
std::string m_host;
std::string m_port;
Connection<T> m_connection;
Poll<T> m_poll;
const int m_maxPendingQueueLen;
};
#endif // TCP_SERVER_HPP

@ -3,17 +3,21 @@
#include "Logger.hpp"
#include "TcpClient.hpp"
#include "Connection.hpp"
#include "Message.hpp"
#include <iostream>
#include <string>
class SimpleMessage : public Message
class SimpleMessage : public Message<SimpleMessage>
{
public:
SimpleMessage(void* msgParam = 0)
: Message(msgParam)
SimpleMessage( Connection<SimpleMessage> *connection,
void *msgParam = 0)
: Message<SimpleMessage>(connection, msgParam)
{
TRACE;
}

@ -1,43 +1,56 @@
// gpp tcpServer_main.cpp -o client -I../include ../src/Logger.cpp ../src/TcpClient.cpp
#include "TcpServer.hpp"
#include "Logger.hpp"
#include "Common.hpp"
#include "TcpServer.hpp"
#include "Message.hpp"
#include <iostream>
#include <string>
class EchoTcpServer : public TcpServer
class EchoMessage : public Message<EchoMessage>
{
public:
EchoTcpServer ( const std::string host,
const std::string port,
const int maxClients = 5 )
: TcpServer(host, port, maxClients)
EchoMessage( Connection<EchoMessage> *connection,
void *msgParam = 0)
: Message<EchoMessage>(connection, msgParam)
{
TRACE;
}
void msgArrived(const int clientSocket,
const unsigned char*msg,
const int msgLen )
bool buildMessage( const void *msgPart,
const size_t msgLen )
{
TRACE;
m_buffer = std::string( (const char*) msgPart, msgLen );
onMessageReady();
return true;
}
std::string message((char*)msg, msgLen);
LOG( Logger::DEBUG, std::string("Got msg: ").append(message).c_str() );
void onMessageReady()
{
TRACE;
std::string reply("Got your msg, buddy: \"");
reply.append(message).append("\" see you!");
LOG( Logger::INFO, std::string("Got message: \"").
append(m_buffer).append("\" from: ").
append(m_connection->getHost()).c_str() );
ssize_t n = write(clientSocket,message.c_str(), message.length());
if (n == -1) {
LOG( Logger::ERR, errnoToString("ERROR writing to socket. ").c_str() );
}
std::string reply("Got your message, ");
reply.append(m_connection->getHost()).
append(" \"").append(m_buffer).append("\"");
m_connection->send( reply.c_str(), reply.length() );
}
protected:
size_t getExpectedLength()
{
TRACE;
return 0;
}
};
@ -47,7 +60,7 @@ int main( int argc, char * argv[] )
Logger::init(std::cout);
Logger::setLogLevel(Logger::FINEST);
EchoTcpServer tcpServer("localhost", "4455");
TcpServer<EchoMessage> tcpServer("localhost", "4455");
tcpServer.start();
@ -55,7 +68,6 @@ int main( int argc, char * argv[] )
sleep(1);
tcpServer.stop();
Logger::destroy();
return 0;
}

@ -106,6 +106,18 @@ bool Socket::bindToHost( const std::string host,
}
bool Socket::listen ( const int maxPendingQueueLen )
{
TRACE;
if ( ::listen(m_socket, maxPendingQueueLen) == -1 ) {
LOG( Logger::ERR, errnoToString("ERROR listening. ").c_str() );
return false;
}
return true;
}
bool Socket::send ( const void *message, const int length )
{
TRACE;

Loading…
Cancel
Save