Commit 878c353a authored by louiz’'s avatar louiz’

Use templates to choose the type of socket a (remote)client class uses

Only TCPSocket are available at the moment, but this will be useful to use a
TCPSocket easily
parent c64b323c
......@@ -9,6 +9,7 @@
#include <game/game.hpp>
#include <game/occupant.hpp>
#include <network/client_base.hpp>
#include <network/tcp_socket.hpp>
#include <gui/screen/screen.hpp>
#include <game/selection.hpp>
#include <game/turn_handler.hpp>
......@@ -20,7 +21,7 @@
class Task;
class GameClient: public Game, public ClientBase
class GameClient: public Game, public ClientBase<TCPSocket>
{
public:
explicit GameClient(const std::shared_ptr<Screen>& screen);
......
......@@ -16,12 +16,13 @@
#include "batadb.hpp"
#include <network/remote_client_base.hpp>
#include <network/tcp_socket.hpp>
class MasterToClientServer;
class Message;
class TransferSender;
class RemoteClient: public RemoteClientBase
class RemoteClient: public RemoteClientBase<TCPSocket>
{
public:
RemoteClient();
......
......@@ -2,10 +2,11 @@
#define REMOTE_SLAVE_CLIENT_HPP_INCLUDED
#include <network/remote_client_base.hpp>
#include <network/tcp_socket.hpp>
class MasterToSlaveServer;
class RemoteSlaveClient: public RemoteClientBase
class RemoteSlaveClient: public RemoteClientBase<TCPSocket>
{
public:
RemoteSlaveClient() = default;
......
......@@ -28,7 +28,8 @@
#include <string>
#include <cstdlib>
class ClientBase: public MessageHandler, public TimedEventHandler,
template <typename SocketType>
class ClientBase: public MessageHandler<SocketType>, public TimedEventHandler,
public PingHandler
{
public:
......
......@@ -19,7 +19,6 @@
#include <boost/asio.hpp>
#include <network/transfer_sender.hpp>
#include <network/base_socket.hpp>
#include <network/message.hpp>
#include <utils/scopeguard.hpp>
......@@ -27,7 +26,8 @@
typedef std::function<void(Message*)> t_read_callback;
class MessageHandler: public BaseSocket
template <typename SocketType>
class MessageHandler: public SocketType
{
public:
MessageHandler():
......
#include <network/remote_client_base.hpp>
#include <network/tcp_socket.hpp>
unsigned long int RemoteClientBase::clients_number = 0;
template <>
unsigned long int RemoteClientBase<TCPSocket>::clients_number = 0;
......@@ -33,13 +33,16 @@
// In seconds
static constexpr int ping_interval = 10;
class RemoteClientBase: public MessageHandler, public TimedEventHandler, public PingHandler
template <typename SocketType>
class RemoteClientBase: public MessageHandler<SocketType>, public TimedEventHandler, public PingHandler
{
public:
RemoteClientBase():
id(RemoteClientBase::clients_number++)
id(RemoteClientBase<SocketType>::clients_number++)
{ }
virtual ~RemoteClientBase() = default;
/**
* starts the client (install the read handler, etc)
*/
......@@ -83,8 +86,6 @@ public:
*/
uint32_t get_id() const { return this->id; }
virtual void on_connection_closed() = 0;
boost::asio::ip::tcp::endpoint& get_endpoint()
{
return this->endpoint;
......@@ -100,8 +101,9 @@ protected:
* It is executed whenever that message is received.
* See MessageHandler for details
*/
virtual void install_callbacks() = 0;
private:
virtual void on_connection_closed() = 0;
virtual void install_callbacks() = 0;
/**
* A endpoint containing the information of the remote peer. It is set by
* asio, when async_accept succeeds, just before the accept handler is
......
......@@ -28,12 +28,12 @@
#include <boost/chrono.hpp>
#include <logging/logging.hpp>
#include <network/base_socket.hpp>
#include <network/tcp_socket.hpp>
#include <network/message.hpp>
#include <utils/time.hpp>
template <class T>
class Server: public BaseSocket
class Server: public TCPSocket
{
public:
/**
......@@ -41,7 +41,7 @@ public:
* @param port The port on which the servers accepts new connections.
*/
Server(short port):
BaseSocket(),
TCPSocket(),
port(port),
timeout(IoService::get()),
acceptor(IoService::get(), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
......
/**
* A very simple base class that must be inherited by all network object
* that needs a socket.
* that needs a simple TCP socket
*/
#ifndef BASE_SOCKET
# define BASE_SOCKET
#ifndef TCP_SOCKET
# define TCP_SOCKET
#include <boost/asio.hpp>
#include <network/ioservice.hpp>
class BaseSocket
class TCPSocket
{
public:
explicit BaseSocket():
explicit TCPSocket():
socket(IoService::get())
{
}
~BaseSocket() = default;
~TCPSocket() = default;
const boost::asio::ip::tcp::socket& get_socket() const
{
return this->socket;
......@@ -29,10 +29,10 @@ public:
private:
boost::asio::ip::tcp::socket socket;
BaseSocket(const BaseSocket&) = delete;
BaseSocket(BaseSocket&&) = delete;
BaseSocket& operator=(const BaseSocket&) = delete;
BaseSocket& operator=(BaseSocket&&) = delete;
TCPSocket(const TCPSocket&) = delete;
TCPSocket(TCPSocket&&) = delete;
TCPSocket& operator=(const TCPSocket&) = delete;
TCPSocket& operator=(TCPSocket&&) = delete;
};
#endif // BASE_SOCKET
#endif // TCP_SOCKET
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment