parent
a12d07d909
commit
6ff4599d25
@ -0,0 +1,42 @@
|
||||
#ifndef CONNECTION_HPP
|
||||
#define CONNECTION_HPP
|
||||
|
||||
|
||||
#include "string"
|
||||
|
||||
class Connection
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~Connection();
|
||||
virtual Connection* clone(const int socket) = 0;
|
||||
|
||||
virtual bool bind() = 0;
|
||||
|
||||
virtual bool send( const void* message, const size_t length ) = 0;
|
||||
virtual bool receive() = 0;
|
||||
|
||||
std::string getHost() const;
|
||||
int getPort() const;
|
||||
|
||||
void setHost(const std::string host);
|
||||
void setPort(const int port);
|
||||
|
||||
virtual int getSocket() const = 0;
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
Connection(std::string host = std::string("invalid"), int port = -1);
|
||||
|
||||
std::string m_host;
|
||||
int m_port;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Connection(const Connection &);
|
||||
Connection& operator= (const Connection &);
|
||||
};
|
||||
|
||||
#endif // CONNECTION_HPP
|
@ -1,56 +0,0 @@
|
||||
#ifndef SOCKET_CONNECTION_HPP
|
||||
#define SOCKET_CONNECTION_HPP
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "Message.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
class SocketConnection
|
||||
{
|
||||
public:
|
||||
|
||||
SocketConnection ( const int socket,
|
||||
Message *message,
|
||||
const size_t bufferLength = 1024 );
|
||||
|
||||
SocketConnection ( const std::string host,
|
||||
const std::string port,
|
||||
Message *message,
|
||||
const size_t bufferLength = 1024 );
|
||||
|
||||
virtual ~SocketConnection();
|
||||
|
||||
virtual SocketConnection* clone(const int socket) = 0;
|
||||
virtual bool connectToHost() = 0;
|
||||
virtual bool bindToHost() = 0;
|
||||
virtual bool listen( const int maxPendingQueueLen = 64 ) = 0;
|
||||
virtual void closeConnection() = 0;
|
||||
|
||||
virtual bool send( const void* message, const size_t length ) = 0;
|
||||
virtual bool receive() = 0;
|
||||
|
||||
int getSocket() const;
|
||||
std::string getHost() const;
|
||||
std::string getPort() const;
|
||||
|
||||
protected:
|
||||
|
||||
Socket m_socket;
|
||||
std::string m_host;
|
||||
std::string m_port;
|
||||
Message *m_message;
|
||||
|
||||
unsigned char *m_buffer;
|
||||
size_t m_bufferLength;
|
||||
|
||||
private:
|
||||
|
||||
SocketConnection(const SocketConnection&);
|
||||
SocketConnection& operator=(const SocketConnection&);
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // SOCKET_CONNECTION_HPP
|
@ -0,0 +1,38 @@
|
||||
#ifndef STREAM_CONNECTION_HPP
|
||||
#define STREAM_CONNECTION_HPP
|
||||
|
||||
|
||||
#include "Connection.hpp"
|
||||
|
||||
#include "string"
|
||||
|
||||
class StreamConnection : public Connection
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~StreamConnection() {};
|
||||
virtual Connection* clone(const int socket) = 0;
|
||||
|
||||
virtual bool connect() = 0;
|
||||
virtual bool disconnect() = 0;
|
||||
|
||||
virtual bool listen( const int maxPendingQueueLen = 64 ) = 0;
|
||||
|
||||
/// @todo move accept and poll here
|
||||
// virtual bool accept() = 0;
|
||||
// virtual bool poll() = 0;
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
StreamConnection(std::string host = std::string("invalid"), int port = -1)
|
||||
: Connection(host, port) {};
|
||||
|
||||
|
||||
private:
|
||||
|
||||
StreamConnection(const StreamConnection &);
|
||||
StreamConnection& operator= (const StreamConnection &);
|
||||
};
|
||||
|
||||
#endif // STREAM_CONNECTION_HPP
|
@ -0,0 +1,96 @@
|
||||
// gpp sslserver_main.cpp -o sslserver -I../include ../src/Logger.cpp ../src/Socket.cpp -ggdb ../src/SocketServer.cpp ../src/Connection.cpp ../src/Poll.cpp ../src/TcpConnection.cpp ../src/SslConnection.cpp -lssl -lcrypto
|
||||
|
||||
#include "Logger.hpp"
|
||||
#include "Common.hpp"
|
||||
|
||||
#include "Message.hpp"
|
||||
#include "SslConnection.hpp"
|
||||
#include "SocketServer.hpp"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class EchoMessage : public Message
|
||||
{
|
||||
public:
|
||||
|
||||
EchoMessage( void *msgParam = 0)
|
||||
: Message(msgParam)
|
||||
{
|
||||
TRACE;
|
||||
}
|
||||
|
||||
bool buildMessage( const void *msgPart,
|
||||
const size_t msgLen )
|
||||
{
|
||||
TRACE;
|
||||
m_buffer = std::string( (const char*) msgPart, msgLen );
|
||||
onMessageReady();
|
||||
return true;
|
||||
}
|
||||
|
||||
void onMessageReady()
|
||||
{
|
||||
TRACE;
|
||||
|
||||
LOG( Logger::INFO, std::string("Got message: \"").
|
||||
append(m_buffer).append("\" from: ").
|
||||
append(m_connection->getHost().append(":").
|
||||
append(TToStr(m_connection->getPort())) ).c_str() );
|
||||
|
||||
std::string reply("Got your message, ");
|
||||
reply.append(m_connection->getHost()).append(":").
|
||||
append(TToStr(m_connection->getPort())).
|
||||
append(" \"").append(m_buffer).append("\"");
|
||||
|
||||
m_connection->send( reply.c_str(), reply.length() );
|
||||
}
|
||||
|
||||
Message* clone()
|
||||
{
|
||||
TRACE;
|
||||
return new EchoMessage(m_param);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
size_t getExpectedLength()
|
||||
{
|
||||
TRACE;
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char* argv[] )
|
||||
{
|
||||
if ( argc != 3 ) {
|
||||
std::cerr << "Usage: " << argv[0] << " <HOST> <PORT>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Logger::createInstance();
|
||||
Logger::init(std::cout);
|
||||
Logger::setLogLevel(Logger::FINEST);
|
||||
// Logger::setNoPrefix();
|
||||
SslConnection::init();
|
||||
|
||||
EchoMessage msg;
|
||||
SslConnection conn(argv[1], StrToT<int>(argv[2]), &msg);
|
||||
SocketServer socketServer(&conn);
|
||||
|
||||
if ( !socketServer.start() ) {
|
||||
LOG( Logger::ERR, "Failed to start TCP server, exiting...");
|
||||
Logger::destroy();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// never reached
|
||||
sleep(1);
|
||||
|
||||
socketServer.stop();
|
||||
SslConnection::destroy();
|
||||
Logger::destroy();
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
#include "Connection.hpp"
|
||||
|
||||
#include "Logger.hpp"
|
||||
|
||||
|
||||
Connection::Connection(std::string host, int port)
|
||||
: m_host(host)
|
||||
, m_port(port)
|
||||
{
|
||||
TRACE;
|
||||
}
|
||||
|
||||
|
||||
Connection::~Connection()
|
||||
{
|
||||
TRACE;
|
||||
}
|
||||
|
||||
|
||||
std::string Connection::getHost() const
|
||||
{
|
||||
TRACE;
|
||||
return m_host;
|
||||
}
|
||||
|
||||
|
||||
int Connection::getPort() const
|
||||
{
|
||||
TRACE;
|
||||
return m_port;
|
||||
}
|
||||
|
||||
|
||||
void Connection::setHost(const std::string host)
|
||||
{
|
||||
TRACE;
|
||||
m_host = host;
|
||||
}
|
||||
|
||||
|
||||
void Connection::setPort(const int port)
|
||||
{
|
||||
TRACE;
|
||||
m_port = port;
|
||||
}
|
||||
|
||||
|
@ -1,69 +0,0 @@
|
||||
#include "SocketConnection.hpp"
|
||||
|
||||
#include "Logger.hpp"
|
||||
#include "Common.hpp"
|
||||
|
||||
|
||||
SocketConnection::SocketConnection ( const int socket,
|
||||
Message *message,
|
||||
const size_t bufferLength )
|
||||
: m_socket(socket)
|
||||
, m_host()
|
||||
, m_port()
|
||||
, m_message(message)
|
||||
, m_buffer(0)
|
||||
, m_bufferLength(bufferLength)
|
||||
{
|
||||
TRACE;
|
||||
|
||||
m_socket.getPeerName(m_host, m_port);
|
||||
m_buffer = new unsigned char[m_bufferLength];
|
||||
m_message->setConnection(this);
|
||||
}
|
||||
|
||||
|
||||
SocketConnection::SocketConnection ( const std::string host,
|
||||
const std::string port,
|
||||
Message *message,
|
||||
const size_t bufferLength )
|
||||
: m_socket(AF_INET, SOCK_STREAM)
|
||||
, m_host(host)
|
||||
, m_port(port)
|
||||
, m_message(message)
|
||||
, m_buffer(0)
|
||||
, m_bufferLength(bufferLength)
|
||||
{
|
||||
TRACE;
|
||||
m_socket.createSocket();
|
||||
m_buffer = new unsigned char[m_bufferLength];
|
||||
m_message->setConnection(this);
|
||||
}
|
||||
|
||||
|
||||
SocketConnection::~SocketConnection()
|
||||
{
|
||||
TRACE;
|
||||
m_socket.closeSocket();
|
||||
delete[] m_buffer;
|
||||
}
|
||||
|
||||
|
||||
int SocketConnection::getSocket() const
|
||||
{
|
||||
TRACE;
|
||||
return m_socket.getSocket();
|
||||
}
|
||||
|
||||
|
||||
std::string SocketConnection::getHost() const
|
||||
{
|
||||
TRACE;
|
||||
return m_host;
|
||||
}
|
||||
|
||||
|
||||
std::string SocketConnection::getPort() const
|
||||
{
|
||||
TRACE;
|
||||
return m_port;
|
||||
}
|
Loading…
Reference in new issue