Commit 0c402d20 authored by louiz’'s avatar louiz’

Use a steady_timer for the server poll timeout

And clean some time-related stuf
parent a836ecaf
......@@ -40,7 +40,7 @@ option(BUILD_CLIENT "build the server" ${BUILD_CLIENT})
# Libraries look up
##########################
find_package(Boost 1.48.0 COMPONENTS filesystem system date_time iostreams locale serialization REQUIRED)
find_package(Boost 1.48.0 COMPONENTS filesystem system iostreams locale serialization chrono REQUIRED)
find_package(Cryptopp REQUIRED)
find_package(YamlCpp REQUIRED)
find_package(Protobuf REQUIRED)
......
......@@ -60,7 +60,7 @@ void GameClient::run()
}
// recv/send from the network
this->poll(0);
this->poll();
// Draw the screen. Limit to ~60 fps.
if (fps_clock.getElapsedTime().asMicroseconds() > 10000)
......
......@@ -2,18 +2,9 @@
#include <network/client_base.hpp>
#include <boost/algorithm/string.hpp>
/**
* Does nothing, it is just used to exit the io_service.run_one() after
* a timeout.
*/
static void poll_timeout_handler(const boost::system::error_code&)
{
}
ClientBase::ClientBase():
BaseIoservice(),
MessageHandler(io_service),
timeout(io_service)
MessageHandler(io_service)
{
}
......@@ -71,24 +62,10 @@ void ClientBase::ping_callback(Message*)
this->send(message);
}
void ClientBase::poll(long timeout)
void ClientBase::poll()
{
if (timeout == 0)
{
this->io_service.poll();
return ;
}
if (this->timeout.expires_from_now(boost::posix_time::milliseconds(timeout)) == 0)
// The last run_one() call returned because the timeout expired, so
// we reinstall it. If that's not the case
// (something actually happened on the socket)
// we just need to reset the time of expiration, but not reinstall it.
this->timeout.async_wait(&poll_timeout_handler);
// Wait for one event to happen (either a timeout or something
// on the socket).
this->io_service.run_one();
while (this->io_service.poll() != 0)
; // Execute all other available handlers, if any.
;
}
......@@ -24,6 +24,8 @@
#include <network/message.hpp>
#include <network/timed_event.hpp>
#include <utils/time.hpp>
class ClientBase: public BaseIoservice, public MessageHandler,
public TimedEventHandler, public PingHandler
{
......@@ -45,10 +47,8 @@ public:
virtual void install_callbacks() = 0;
/**
* Checks for network or timed events readiness.
* The timeout argument makes this call block for that amount
* of milliseconds.
*/
void poll(long timeout = 0);
void poll();
virtual void on_connection_closed() = 0;
......@@ -60,8 +60,6 @@ private:
* Called when the server sends us a PING request. Sends a PONG back.
*/
void ping_callback(Message*);
boost::asio::deadline_timer timeout;
};
#endif /*__CLIENT_HPP__ */
......
......@@ -23,11 +23,13 @@
#include <signal.h>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <logging/logging.hpp>
#include <network/base_ioservice.hpp>
#include <network/base_socket.hpp>
#include <network/message.hpp>
#include <utils/time.hpp>
template <class T>
class Server: public BaseIoservice, public BaseSocket
......@@ -83,14 +85,16 @@ public:
* The timeout argument makes this call block for that amount
* of milliseconds.
*/
void poll(long timeout = 0)
void poll(long t)
{
if (timeout == 0)
if (t == 0)
{
this->io_service.poll();
while (this->io_service.poll())
;
return ;
}
if (this->timeout.expires_from_now(boost::posix_time::milliseconds(timeout)) == 0)
if (this->timeout.expires_from_now(boost::chrono::milliseconds(t)) == 0)
// The last run_one() call returned because the timeout expired, so
// we reinstall it. If that's not the case
// (something actually happened on the socket)
......@@ -239,7 +243,7 @@ private:
std::vector<T*> clients;
const short port;
boost::asio::deadline_timer timeout;
boost::asio::steady_timer timeout;
boost::asio::ip::tcp::acceptor acceptor;
boost::asio::signal_set stop_signal_set;
bool started;
......
......@@ -4,7 +4,7 @@
TimedEvent::TimedEvent(TimedEventHandler* handler,
boost::asio::deadline_timer* timer,
const t_timed_callback callback):
const timed_callback_t callback):
handler(handler),
timer(timer),
callback(callback)
......
......@@ -14,20 +14,19 @@
#include <functional>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class TimedEventHandler;
typedef std::function< void(void) > t_timed_callback;
typedef std::function< void(void) > timed_callback_t;
class TimedEventHandler;
class TimedEvent
{
public:
TimedEvent(TimedEventHandler*,
boost::asio::deadline_timer*,
const t_timed_callback);
TimedEvent(TimedEventHandler* handler,
boost::asio::deadline_timer* timer,
const timed_callback_t callback);
~TimedEvent();
void cancel();
void on_expires(const boost::system::error_code&);
......@@ -38,7 +37,7 @@ private:
TimedEventHandler* handler;
boost::asio::deadline_timer* timer;
const t_timed_callback callback;
const timed_callback_t callback;
};
#endif // __TIMED_EVENT_HPP__
......@@ -17,7 +17,7 @@ TimedEventHandler::~TimedEventHandler()
}
void TimedEventHandler::install_timed_event(boost::asio::io_service& io_service,
const t_timed_callback callback,
const timed_callback_t callback,
const int delay)
{
log_debug("installing timed_event");
......
......@@ -23,7 +23,7 @@ public:
/**
* Install a timed event to be executed in the futur at the specified date.
*/
void install_timed_event(boost::asio::io_service&, const t_timed_callback, const int);
void install_timed_event(boost::asio::io_service&, const timed_callback_t, const int);
/**
* Remove the event from the list. It is deleted and canceled (if it was
* not already called, it'll never be).
......
......@@ -3,6 +3,8 @@
#include <chrono>
using namespace std::chrono_literals;
namespace utils
{
using namespace std::chrono_literals;
......
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