Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/asio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8.7)

project(asio)

LIST(APPEND CMAKE_CXX_FLAGS -std=c++11)

add_subdirectory(client)
add_subdirectory(server)

45 changes: 43 additions & 2 deletions lib/asio/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ namespace crete
Server::Server(Port port) :
acceptor_(io_service_, tcp::endpoint(tcp::v6(), port)),
socket_(io_service_),
port_(port)
port_(port),
deadline_timer_(io_service_)
{
deadline_timer_.expires_at(boost::posix_time::pos_infin);
}

Server::Server() :
acceptor_(io_service_),
socket_(io_service_),
port_(0)
port_(0),
deadline_timer_(io_service_)
{
use_available_port();

deadline_timer_.expires_at(boost::posix_time::pos_infin);
}

Server::~Server()
Expand All @@ -51,6 +56,42 @@ void Server::open_connection_wait()
acceptor_.accept(socket_);
}

/**
* @brief Server::open_connection_wait
* @param timeout Amount of time to wait for connection before aborting.
* @return true if successful connection made within timeout limit, otherwise false.
*/
bool Server::open_connection_wait(const boost::posix_time::time_duration& timeout)
{
deadline_timer_.expires_from_now(timeout);
deadline_timer_.async_wait([this](const boost::system::error_code& e)
{
if(e == boost::system::errc::success)
{
acceptor_.cancel();
}
});

boost::system::error_code ec;
acceptor_.async_accept(socket_, [this, &ec](const boost::system::error_code& e)
{
if(e != boost::asio::error::operation_aborted)
{
deadline_timer_.cancel();
}

ec = e;
});

io_service_.reset();
io_service_.run();

deadline_timer_.expires_at(boost::posix_time::pos_infin);

return ec == boost::system::errc::success
&& socket_.is_open();
}

bool Server::is_socket_open()
{
return socket_.is_open();
Expand Down
4 changes: 3 additions & 1 deletion lib/include/crete/asio/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <crete/asio/common.h>

Expand Down Expand Up @@ -42,7 +43,7 @@ class Server
template <typename Handler>
void open_connection_async(Handler handler);
void open_connection_wait();
void open_connection_wait(boost::posix_time::time_duration timeout);
bool open_connection_wait(const boost::posix_time::time_duration& timeout);
bool is_socket_open(); // Will return true even if connection has been closed.

void update_directory(const boost::filesystem::path& from, const boost::filesystem::path& to);
Expand All @@ -59,6 +60,7 @@ class Server
boost::asio::ip::tcp::acceptor acceptor_;
boost::asio::ip::tcp::socket socket_;
Port port_;
boost::asio::deadline_timer deadline_timer_;
};

template <typename Handler>
Expand Down