Skip to content

Commit

Permalink
Bye Boost Lib 🙌 (#252)
Browse files Browse the repository at this point in the history
Co-authored-by: Nekiro <[email protected]>
  • Loading branch information
mehah and nekiro authored Apr 6, 2022
1 parent 5a1403a commit 877a163
Show file tree
Hide file tree
Showing 20 changed files with 1,028 additions and 89 deletions.
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
23 changes: 11 additions & 12 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 @@ -189,16 +190,6 @@ add_definitions(-D"BUILD_COMMIT=\\\"${BUILD_COMMIT}\\\"")
message(STATUS "Build revision: ${BUILD_REVISION}")
add_definitions(-D"BUILD_REVISION=\\\"${BUILD_REVISION}\\\"")

# find boost
set(REQUIRED_BOOST_COMPONENTS system thread filesystem)
if(WIN32)
set(Boost_THREADAPI win32)
set(framework_DEFINITIONS ${framework_DEFINITIONS} -DBOOST_THREAD_USE_LIB) # fix boost thread linkage
endif()
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_LIBS ${USE_STATIC_LIBS})
find_package(Boost 1.48.0 COMPONENTS ${REQUIRED_BOOST_COMPONENTS} REQUIRED)

#find lua
if(LUAJIT)
find_package(LuaJIT REQUIRED)
Expand All @@ -219,8 +210,17 @@ 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}
${PHYSFS_LIBRARY}
${ZLIB_LIBRARY}
Expand All @@ -230,7 +230,6 @@ set(framework_LIBRARIES ${framework_LIBRARIES}
)

set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${LUA_INCLUDE_DIR}
${PHYSFS_INCLUDE_DIR}
${framework_INCLUDE_DIRS}
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
Loading

0 comments on commit 877a163

Please sign in to comment.