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

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

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

@ -11,21 +11,23 @@ class Socket
{ {
public: public:
Socket(const int domain, Socket( const int domain,
const int type, const int type,
const int protocol = 0); const int protocol = 0 );
Socket(const int socket ); Socket( const int socket );
virtual ~Socket(); virtual ~Socket();
bool createSocket(); bool createSocket();
void closeSocket(); void closeSocket();
bool connectToHost(const std::string host, bool connectToHost( const std::string host,
const std::string port);
bool bindToHost(const std::string host,
const std::string port ); 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, void getPeerName(std::string &host,
std::string &port); std::string &port);

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

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

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

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

Loading…
Cancel
Save