Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bye Boost Lib 🙌 #252

Merged
merged 9 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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: 1 addition & 1 deletion src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void Game::resetGameStates()
g_map.resetAwareRange();
}

void Game::processConnectionError(const boost::system::error_code& ec)
void Game::processConnectionError(const std::error_code& ec)
{
// connection errors only have meaning if we still have a protocol
if (m_protocolGame) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Game
void resetGameStates();

protected:
void processConnectionError(const boost::system::error_code& ec);
void processConnectionError(const std::error_code& ec);
void processDisconnect();
void processPing();
void processPingBack();
Expand Down
2 changes: 1 addition & 1 deletion src/client/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void ProtocolGame::onRecv(const InputMessagePtr& inputMessage)
recv();
}

void ProtocolGame::onError(const boost::system::error_code& error)
void ProtocolGame::onError(const std::error_code& error)
{
g_game.processConnectionError(error);
disconnect();
Expand Down
2 changes: 1 addition & 1 deletion src/client/protocolgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class ProtocolGame : public Protocol
protected:
void onConnect() override;
void onRecv(const InputMessagePtr& inputMessage) override;
void onError(const boost::system::error_code& error) override;
void onError(const std::error_code& error) override;

friend class Game;

Expand Down
11 changes: 11 additions & 0 deletions src/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/util/point.h
${CMAKE_CURRENT_LIST_DIR}/util/rect.h
${CMAKE_CURRENT_LIST_DIR}/util/size.h
${CMAKE_CURRENT_LIST_DIR}/util/uuid.h

# stdext
${CMAKE_CURRENT_LIST_DIR}/stdext/cast.h
Expand Down Expand Up @@ -219,6 +220,16 @@ find_package(Protobuf REQUIRED)
find_package(LibLZMA REQUIRED)
find_package(nlohmann_json REQUIRED)

find_path(ASIO_INCLUDE_PATH asio.hpp HINTS
"/usr/include"
"/usr/local/include"
"/opt/local/include"
)

if(ASIO_INCLUDE_PATH)
message(STATUS "Found existing ASIO install")
endif(ASIO_INCLUDE_PATH)

set(framework_LIBRARIES ${framework_LIBRARIES}
${Boost_LIBRARIES}
${LUA_LIBRARY}
Expand Down
35 changes: 17 additions & 18 deletions src/framework/net/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <framework/core/eventdispatcher.h>

#include <utility>
#include <boost/asio.hpp>

asio::io_service g_ioService;
std::list<std::shared_ptr<asio::streambuf>> Connection::m_outputStreams;
Expand Down Expand Up @@ -84,7 +83,7 @@ void Connection::close()
m_delayedWriteTimer.cancel();

if (m_socket.is_open()) {
boost::system::error_code ec;
std::error_code ec;
m_socket.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
m_socket.close();
}
Expand All @@ -105,7 +104,7 @@ void Connection::connect(const std::string& host, uint16 port, const std::functi
});

m_readTimer.cancel();
m_readTimer.expires_from_now(boost::posix_time::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.expires_from_now(asio::chrono::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.async_wait([capture0 = asConnection()](auto&& PH1)
{
capture0->onTimeout(std::forward<decltype(PH1)>(PH1));
Expand All @@ -120,7 +119,7 @@ void Connection::internal_connect(const asio::ip::basic_resolver<asio::ip::tcp>:
});

m_readTimer.cancel();
m_readTimer.expires_from_now(boost::posix_time::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.expires_from_now(asio::chrono::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.async_wait([capture0 = asConnection()](auto&& PH1)
{
capture0->onTimeout(std::forward<decltype(PH1)>(PH1));
Expand All @@ -141,7 +140,7 @@ void Connection::write(uint8* buffer, size_t size)
m_outputStream = std::make_shared<asio::streambuf>();

m_delayedWriteTimer.cancel();
m_delayedWriteTimer.expires_from_now(boost::posix_time::milliseconds(0));
m_delayedWriteTimer.expires_from_now(asio::chrono::milliseconds(0));
m_delayedWriteTimer.async_wait([capture0 = asConnection()](auto&& PH1)
{
capture0->onCanWrite(std::forward<decltype(PH1)>(PH1));
Expand Down Expand Up @@ -169,7 +168,7 @@ void Connection::internal_write()
});

m_writeTimer.cancel();
m_writeTimer.expires_from_now(boost::posix_time::seconds(static_cast<uint32>(WRITE_TIMEOUT)));
m_writeTimer.expires_from_now(asio::chrono::seconds(static_cast<uint32>(WRITE_TIMEOUT)));
m_writeTimer.async_wait([capture0 = asConnection()](auto&& PH1)
{
capture0->onTimeout(std::forward<decltype(PH1)>(PH1));
Expand All @@ -191,7 +190,7 @@ void Connection::read(uint16 bytes, const RecvCallback& callback)
});

m_readTimer.cancel();
m_readTimer.expires_from_now(boost::posix_time::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.expires_from_now(asio::chrono::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.async_wait([capture0 = asConnection()](auto&& PH1)
{
capture0->onTimeout(std::forward<decltype(PH1)>(PH1));
Expand All @@ -214,7 +213,7 @@ void Connection::read_until(const std::string& what, const RecvCallback& callbac
});

m_readTimer.cancel();
m_readTimer.expires_from_now(boost::posix_time::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.expires_from_now(asio::chrono::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.async_wait([capture0 = asConnection()](auto&& PH1)
{
capture0->onTimeout(std::forward<decltype(PH1)>(PH1));
Expand All @@ -235,14 +234,14 @@ void Connection::read_some(const RecvCallback& callback)
});

m_readTimer.cancel();
m_readTimer.expires_from_now(boost::posix_time::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.expires_from_now(asio::chrono::seconds(static_cast<uint32>(READ_TIMEOUT)));
m_readTimer.async_wait([capture0 = asConnection()](auto&& PH1)
{
capture0->onTimeout(std::forward<decltype(PH1)>(PH1));
});
}

void Connection::onResolve(const boost::system::error_code& error, asio::ip::basic_resolver<asio::ip::tcp>::iterator endpointIterator)
void Connection::onResolve(const std::error_code& error, asio::ip::basic_resolver<asio::ip::tcp>::iterator endpointIterator)
{
m_readTimer.cancel();

Expand All @@ -255,7 +254,7 @@ void Connection::onResolve(const boost::system::error_code& error, asio::ip::bas
handleError(error);
}

void Connection::onConnect(const boost::system::error_code& error)
void Connection::onConnect(const std::error_code& error)
{
m_readTimer.cancel();
m_activityTimer.restart();
Expand All @@ -278,7 +277,7 @@ void Connection::onConnect(const boost::system::error_code& error)
m_connecting = false;
}

void Connection::onCanWrite(const boost::system::error_code& error)
void Connection::onCanWrite(const std::error_code& error)
{
m_delayedWriteTimer.cancel();

Expand All @@ -289,7 +288,7 @@ void Connection::onCanWrite(const boost::system::error_code& error)
internal_write();
}

void Connection::onWrite(const boost::system::error_code& error, size_t, const std::shared_ptr<asio::streambuf>&
void Connection::onWrite(const std::error_code& error, size_t, const std::shared_ptr<asio::streambuf>&
outputStream)
{
m_writeTimer.cancel();
Expand All @@ -305,7 +304,7 @@ void Connection::onWrite(const boost::system::error_code& error, size_t, const s
handleError(error);
}

void Connection::onRecv(const boost::system::error_code& error, size_t recvSize)
void Connection::onRecv(const std::error_code& error, size_t recvSize)
{
m_readTimer.cancel();
m_activityTimer.restart();
Expand All @@ -316,7 +315,7 @@ void Connection::onRecv(const boost::system::error_code& error, size_t recvSize)
if (m_connected) {
if (!error) {
if (m_recvCallback) {
auto header = boost::asio::buffer_cast<const char*>(m_inputStream.data());
auto header = asio::buffer_cast<const char*>(m_inputStream.data());
m_recvCallback((uint8*)header, recvSize);
}
} else
Expand All @@ -327,15 +326,15 @@ void Connection::onRecv(const boost::system::error_code& error, size_t recvSize)
m_inputStream.consume(recvSize);
}

void Connection::onTimeout(const boost::system::error_code& error)
void Connection::onTimeout(const std::error_code& error)
{
if (error == asio::error::operation_aborted)
return;

handleError(asio::error::timed_out);
}

void Connection::handleError(const boost::system::error_code& error)
void Connection::handleError(const std::error_code& error)
{
if (error == asio::error::operation_aborted)
return;
Expand All @@ -349,7 +348,7 @@ void Connection::handleError(const boost::system::error_code& error)

int Connection::getIp()
{
boost::system::error_code error;
std::error_code error;
const asio::ip::tcp::endpoint ip = m_socket.remote_endpoint(error);
if (!error)
return asio::detail::socket_ops::host_to_network_long(ip.address().to_v4().to_ulong());
Expand Down
27 changes: 14 additions & 13 deletions src/framework/net/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@

#include <framework/luaengine/luaobject.h>
#include "declarations.h"
#include <asio.hpp>

class Connection : public LuaObject
{
using ErrorCallback = std::function<void(const boost::system::error_code&)>;
using ErrorCallback = std::function<void(const std::error_code&)>;
using RecvCallback = std::function<void(uint8*, uint16)>;

enum
Expand Down Expand Up @@ -57,7 +58,7 @@ class Connection : public LuaObject
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }

int getIp();
boost::system::error_code getError() { return m_error; }
std::error_code getError() { return m_error; }
bool isConnecting() { return m_connecting; }
bool isConnected() { return m_connected; }
ticks_t getElapsedTicksSinceLastRead() { return m_connected ? m_activityTimer.elapsed_millis() : -1; }
Expand All @@ -67,22 +68,22 @@ class Connection : public LuaObject
protected:
void internal_connect(const asio::ip::basic_resolver<asio::ip::tcp>::iterator& endpointIterator);
void internal_write();
void onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator);
void onConnect(const boost::system::error_code& error);
void onCanWrite(const boost::system::error_code& error);
void onWrite(const boost::system::error_code& error, size_t writeSize, const std::shared_ptr<asio::streambuf>&
void onResolve(const std::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator);
void onConnect(const std::error_code& error);
void onCanWrite(const std::error_code& error);
void onWrite(const std::error_code& error, size_t writeSize, const std::shared_ptr<asio::streambuf>&
outputStream);
void onRecv(const boost::system::error_code& error, size_t recvSize);
void onTimeout(const boost::system::error_code& error);
void handleError(const boost::system::error_code& error);
void onRecv(const std::error_code& error, size_t recvSize);
void onTimeout(const std::error_code& error);
void handleError(const std::error_code& error);

std::function<void()> m_connectCallback;
ErrorCallback m_errorCallback;
RecvCallback m_recvCallback;

asio::deadline_timer m_readTimer;
asio::deadline_timer m_writeTimer;
asio::deadline_timer m_delayedWriteTimer;
asio::basic_waitable_timer<std::chrono::high_resolution_clock> m_readTimer;
asio::basic_waitable_timer<std::chrono::high_resolution_clock> m_writeTimer;
asio::basic_waitable_timer<std::chrono::high_resolution_clock> m_delayedWriteTimer;
asio::ip::tcp::resolver m_resolver;
asio::ip::tcp::socket m_socket;

Expand All @@ -91,7 +92,7 @@ class Connection : public LuaObject
asio::streambuf m_inputStream;
bool m_connected;
bool m_connecting;
boost::system::error_code m_error;
std::error_code m_error;
stdext::timer m_activityTimer;

friend class Server;
Expand Down
4 changes: 1 addition & 3 deletions src/framework/net/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
#ifndef FRAMEWORK_NET_DECLARATIONS_H
#define FRAMEWORK_NET_DECLARATIONS_H

#include <boost/asio.hpp>
#include <framework/global.h>

namespace asio = boost::asio;
#include <asio/ip/tcp.hpp>

class InputMessage;
class OutputMessage;
Expand Down
2 changes: 1 addition & 1 deletion src/framework/net/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void Protocol::onRecv(const InputMessagePtr& inputMessage)
callLuaField("onRecv", inputMessage);
}

void Protocol::onError(const boost::system::error_code& err)
void Protocol::onError(const std::error_code& err)
{
callLuaField("onError", err.message(), err.value());
disconnect();
Expand Down
2 changes: 1 addition & 1 deletion src/framework/net/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Protocol : public LuaObject
protected:
virtual void onConnect();
virtual void onRecv(const InputMessagePtr& inputMessage);
virtual void onError(const boost::system::error_code& err);
virtual void onError(const std::error_code& err);

std::array<uint32, 4> m_xteaKey;

Expand Down
2 changes: 1 addition & 1 deletion src/framework/net/protocolhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void ProtocolHttp::onRecv(uint8* buffer, uint16 size)
callLuaField("onRecv", string);
}

void ProtocolHttp::onError(const boost::system::error_code& err)
void ProtocolHttp::onError(const std::error_code& err)
{
callLuaField("onError", err.message(), err.value());
disconnect();
Expand Down
2 changes: 1 addition & 1 deletion src/framework/net/protocolhttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ProtocolHttp : public LuaObject
protected:
void onConnect();
void onRecv(uint8* buffer, uint16 size);
void onError(const boost::system::error_code& err);
void onError(const std::error_code& err);

private:
ConnectionPtr m_connection;
Expand Down
2 changes: 1 addition & 1 deletion src/framework/net/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void Server::acceptNext()
const auto connection = ConnectionPtr(new Connection);
connection->m_connecting = true;
const auto self = static_self_cast<Server>();
m_acceptor.async_accept(connection->m_socket, [=](const boost::system::error_code& error) {
m_acceptor.async_accept(connection->m_socket, [=](const std::error_code& error) {
if (!error) {
connection->m_connected = true;
connection->m_connecting = false;
Expand Down
5 changes: 2 additions & 3 deletions src/framework/otml/otmlparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/

#include "otmlparser.h"
#include <boost/tokenizer.hpp>
#include "otmldocument.h"
#include "otmlexception.h"

Expand Down Expand Up @@ -188,8 +187,8 @@ void OTMLParser::parseNode(const std::string& data)
node->setNull(true);
else {
if (value.starts_with("[") && value.ends_with("]")) {
std::string tmp = value.substr(1, value.length() - 2);
boost::tokenizer<boost::escaped_list_separator<char>> tokens(tmp);
const std::string tmp = value.substr(1, value.length() - 2);
const std::vector tokens = stdext::split(tmp, ",");
for (std::string v : tokens) {
stdext::trim(v);
node->writeIn(v);
Expand Down
12 changes: 6 additions & 6 deletions src/framework/stdext/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
*/

#include "net.h"
#include <boost/asio/ip/address_v4.hpp>
#include <asio.hpp>

namespace stdext
{
std::string ip_to_string(uint32 ip)
{
ip = boost::asio::detail::socket_ops::network_to_host_long(ip);
const auto address_v4 = boost::asio::ip::address_v4(ip);
ip = asio::detail::socket_ops::network_to_host_long(ip);
const auto address_v4 = asio::ip::address_v4(ip);
return address_v4.to_string();
}

uint32 string_to_ip(const std::string& string)
{
const boost::asio::ip::address_v4 address_v4 = boost::asio::ip::address_v4::from_string(string);
return boost::asio::detail::socket_ops::host_to_network_long(address_v4.to_ulong());
const asio::ip::address_v4 address_v4 = asio::ip::address_v4::from_string(string);
return asio::detail::socket_ops::host_to_network_long(address_v4.to_ulong());
}

std::vector<uint32> listSubnetAddresses(uint32 address, uint8 mask)
Expand All @@ -44,7 +44,7 @@ namespace stdext
if (mask < 32) {
const uint32 bitmask = (0xFFFFFFFF >> mask);
for (uint32 i = 0; i <= bitmask; i++) {
uint32 ip = boost::asio::detail::socket_ops::host_to_network_long((boost::asio::detail::socket_ops::network_to_host_long(address) & (~bitmask)) | i);
uint32 ip = asio::detail::socket_ops::host_to_network_long((asio::detail::socket_ops::network_to_host_long(address) & (~bitmask)) | i);
if ((ip >> 24) != 0 && (ip >> 24) != 0xFF)
list.push_back(ip);
}
Expand Down
Loading