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 @@ ...@@ -9,6 +9,7 @@
#include <game/game.hpp> #include <game/game.hpp>
#include <game/occupant.hpp> #include <game/occupant.hpp>
#include <network/client_base.hpp> #include <network/client_base.hpp>
#include <network/tcp_socket.hpp>
#include <gui/screen/screen.hpp> #include <gui/screen/screen.hpp>
#include <game/selection.hpp> #include <game/selection.hpp>
#include <game/turn_handler.hpp> #include <game/turn_handler.hpp>
...@@ -20,7 +21,7 @@ ...@@ -20,7 +21,7 @@
class Task; class Task;
class GameClient: public Game, public ClientBase class GameClient: public Game, public ClientBase<TCPSocket>
{ {
public: public:
explicit GameClient(const std::shared_ptr<Screen>& screen); explicit GameClient(const std::shared_ptr<Screen>& screen);
......
...@@ -16,12 +16,13 @@ ...@@ -16,12 +16,13 @@
#include "batadb.hpp" #include "batadb.hpp"
#include <network/remote_client_base.hpp> #include <network/remote_client_base.hpp>
#include <network/tcp_socket.hpp>
class MasterToClientServer; class MasterToClientServer;
class Message; class Message;
class TransferSender; class TransferSender;
class RemoteClient: public RemoteClientBase class RemoteClient: public RemoteClientBase<TCPSocket>
{ {
public: public:
RemoteClient(); RemoteClient();
......
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
#define REMOTE_SLAVE_CLIENT_HPP_INCLUDED #define REMOTE_SLAVE_CLIENT_HPP_INCLUDED
#include <network/remote_client_base.hpp> #include <network/remote_client_base.hpp>
#include <network/tcp_socket.hpp>
class MasterToSlaveServer; class MasterToSlaveServer;
class RemoteSlaveClient: public RemoteClientBase class RemoteSlaveClient: public RemoteClientBase<TCPSocket>
{ {
public: public:
RemoteSlaveClient() = default; RemoteSlaveClient() = default;
......
...@@ -28,7 +28,8 @@ ...@@ -28,7 +28,8 @@
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
class ClientBase: public MessageHandler, public TimedEventHandler, template <typename SocketType>
class ClientBase: public MessageHandler<SocketType>, public TimedEventHandler,
public PingHandler public PingHandler
{ {
public: public:
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <network/transfer_sender.hpp> #include <network/transfer_sender.hpp>
#include <network/base_socket.hpp>
#include <network/message.hpp> #include <network/message.hpp>
#include <utils/scopeguard.hpp> #include <utils/scopeguard.hpp>
...@@ -27,7 +26,8 @@ ...@@ -27,7 +26,8 @@
typedef std::function<void(Message*)> t_read_callback; typedef std::function<void(Message*)> t_read_callback;
class MessageHandler: public BaseSocket template <typename SocketType>
class MessageHandler: public SocketType
{ {
public: public:
MessageHandler(): MessageHandler():
......
#include <network/remote_client_base.hpp> #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 @@ ...@@ -33,13 +33,16 @@
// In seconds // In seconds
static constexpr int ping_interval = 10; 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: public:
RemoteClientBase(): RemoteClientBase():
id(RemoteClientBase::clients_number++) id(RemoteClientBase<SocketType>::clients_number++)
{ } { }
virtual ~RemoteClientBase() = default; virtual ~RemoteClientBase() = default;
/** /**
* starts the client (install the read handler, etc) * starts the client (install the read handler, etc)
*/ */
...@@ -83,8 +86,6 @@ public: ...@@ -83,8 +86,6 @@ public:
*/ */
uint32_t get_id() const { return this->id; } uint32_t get_id() const { return this->id; }
virtual void on_connection_closed() = 0;
boost::asio::ip::tcp::endpoint& get_endpoint() boost::asio::ip::tcp::endpoint& get_endpoint()
{ {
return this->endpoint; return this->endpoint;
...@@ -100,8 +101,9 @@ protected: ...@@ -100,8 +101,9 @@ protected:
* It is executed whenever that message is received. * It is executed whenever that message is received.
* See MessageHandler for details * See MessageHandler for details
*/ */
virtual void install_callbacks() = 0;
private: 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 * A endpoint containing the information of the remote peer. It is set by
* asio, when async_accept succeeds, just before the accept handler is * asio, when async_accept succeeds, just before the accept handler is
......
...@@ -28,12 +28,12 @@ ...@@ -28,12 +28,12 @@
#include <boost/chrono.hpp> #include <boost/chrono.hpp>
#include <logging/logging.hpp> #include <logging/logging.hpp>
#include <network/base_socket.hpp> #include <network/tcp_socket.hpp>
#include <network/message.hpp> #include <network/message.hpp>
#include <utils/time.hpp> #include <utils/time.hpp>
template <class T> template <class T>
class Server: public BaseSocket class Server: public TCPSocket
{ {
public: public:
/** /**
...@@ -41,7 +41,7 @@ public: ...@@ -41,7 +41,7 @@ public:
* @param port The port on which the servers accepts new connections. * @param port The port on which the servers accepts new connections.
*/ */
Server(short port): Server(short port):
BaseSocket(), TCPSocket(),
port(port), port(port),
timeout(IoService::get()), timeout(IoService::get()),
acceptor(IoService::get(), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)), 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 * 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 #ifndef TCP_SOCKET
# define BASE_SOCKET # define TCP_SOCKET
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <network/ioservice.hpp> #include <network/ioservice.hpp>
class BaseSocket class TCPSocket
{ {
public: public:
explicit BaseSocket(): explicit TCPSocket():
socket(IoService::get()) socket(IoService::get())
{ {
} }
~BaseSocket() = default; ~TCPSocket() = default;
const boost::asio::ip::tcp::socket& get_socket() const const boost::asio::ip::tcp::socket& get_socket() const
{ {
return this->socket; return this->socket;
...@@ -29,10 +29,10 @@ public: ...@@ -29,10 +29,10 @@ public:
private: private:
boost::asio::ip::tcp::socket socket; boost::asio::ip::tcp::socket socket;
BaseSocket(const BaseSocket&) = delete; TCPSocket(const TCPSocket&) = delete;
BaseSocket(BaseSocket&&) = delete; TCPSocket(TCPSocket&&) = delete;
BaseSocket& operator=(const BaseSocket&) = delete; TCPSocket& operator=(const TCPSocket&) = delete;
BaseSocket& operator=(BaseSocket&&) = 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