Core/Misc: Minor code modernization - kill std::bind

(cherry picked from commit 78bcc3f52a)
This commit is contained in:
Shauren
2023-12-08 20:27:41 +01:00
committed by funjoker
parent c8e87e59e0
commit a18e3ef20a
31 changed files with 342 additions and 230 deletions

View File

@@ -34,7 +34,7 @@ public:
AsyncAcceptor(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port) :
_acceptor(ioContext), _endpoint(Trinity::Net::make_address(bindIp), port),
_socket(ioContext), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this))
_socket(ioContext), _closed(false), _socketFactory([this] { return DefeaultSocketFactory(); })
{
}
@@ -114,7 +114,7 @@ public:
_acceptor.close(err);
}
void SetSocketFactory(std::function<std::pair<boost::asio::ip::tcp::socket*, uint32>()> func) { _socketFactory = func; }
void SetSocketFactory(std::function<std::pair<boost::asio::ip::tcp::socket*, uint32>()> func) { _socketFactory = std::move(func); }
private:
std::pair<boost::asio::ip::tcp::socket*, uint32> DefeaultSocketFactory() { return std::make_pair(&_socket, 0); }

View File

@@ -112,10 +112,13 @@ public:
_readBuffer.Normalize();
_readBuffer.EnsureFreeSpace();
_socket.async_read_some(boost::asio::buffer(_readBuffer.GetWritePointer(), _readBuffer.GetRemainingSpace()),
std::bind(&Socket<T, Stream>::ReadHandlerInternal, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
[self = this->shared_from_this()](boost::system::error_code const& error, size_t transferredBytes)
{
self->ReadHandlerInternal(error, transferredBytes);
});
}
void AsyncReadWithCallback(void (T::*callback)(boost::system::error_code, std::size_t))
void AsyncReadWithCallback(void (T::*callback)(boost::system::error_code const&, std::size_t))
{
if (!IsOpen())
return;
@@ -123,7 +126,10 @@ public:
_readBuffer.Normalize();
_readBuffer.EnsureFreeSpace();
_socket.async_read_some(boost::asio::buffer(_readBuffer.GetWritePointer(), _readBuffer.GetRemainingSpace()),
std::bind(callback, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
[self = this->shared_from_this(), callback](boost::system::error_code const& error, size_t transferredBytes)
{
(self.get()->*callback)(error, transferredBytes);
});
}
void QueuePacket(MessageBuffer&& buffer)
@@ -170,11 +176,17 @@ protected:
#ifdef TC_SOCKET_USE_IOCP
MessageBuffer& buffer = _writeQueue.front();
_socket.async_write_some(boost::asio::buffer(buffer.GetReadPointer(), buffer.GetActiveSize()), std::bind(&Socket<T, Stream>::WriteHandler,
this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
_socket.async_write_some(boost::asio::buffer(buffer.GetReadPointer(), buffer.GetActiveSize()),
[self = this->shared_from_this()](boost::system::error_code const& error, std::size_t transferedBytes)
{
self->WriteHandler(error, transferedBytes);
});
#else
_socket.async_write_some(boost::asio::null_buffers(), std::bind(&Socket<T, Stream>::WriteHandlerWrapper,
this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
_socket.async_write_some(boost::asio::null_buffers(),
[self = this->shared_from_this()](boost::system::error_code const& error, std::size_t transferedBytes)
{
self->WriteHandlerWrapper(error, transferedBytes);
});
#endif
return false;
@@ -195,7 +207,7 @@ protected:
}
private:
void ReadHandlerInternal(boost::system::error_code error, size_t transferredBytes)
void ReadHandlerInternal(boost::system::error_code const& error, size_t transferredBytes)
{
if (error)
{
@@ -209,7 +221,7 @@ private:
#ifdef TC_SOCKET_USE_IOCP
void WriteHandler(boost::system::error_code error, std::size_t transferedBytes)
void WriteHandler(boost::system::error_code const& error, std::size_t transferedBytes)
{
if (!error)
{
@@ -229,7 +241,7 @@ private:
#else
void WriteHandlerWrapper(boost::system::error_code /*error*/, std::size_t /*transferedBytes*/)
void WriteHandlerWrapper(boost::system::error_code const& /*error*/, std::size_t /*transferedBytes*/)
{
_isWritingAsync = false;
HandleQueue();

View File

@@ -53,7 +53,7 @@ void RealmList::Initialize(Trinity::Asio::IoContext& ioContext, uint32 updateInt
LoadBuildInfo();
// Get the content of the realmlist table in the database
UpdateRealms(boost::system::error_code());
UpdateRealms();
}
void RealmList::Close()
@@ -113,11 +113,8 @@ void RealmList::UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint
realm.Port = port;
}
void RealmList::UpdateRealms(boost::system::error_code const& error)
void RealmList::UpdateRealms()
{
if (error)
return;
TC_LOG_DEBUG("realmlist", "Updating Realm List...");
LoginDatabasePreparedStatement *stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_REALMLIST);
@@ -207,7 +204,13 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)
if (_updateInterval)
{
_updateTimer->expires_from_now(boost::posix_time::seconds(_updateInterval));
_updateTimer->async_wait(std::bind(&RealmList::UpdateRealms, this, std::placeholders::_1));
_updateTimer->async_wait([this](boost::system::error_code const& error)
{
if (error)
return;
UpdateRealms();
});
}
}

View File

@@ -39,35 +39,15 @@ struct RealmBuildInfo
std::array<uint8, 16> Mac64AuthSeed;
};
namespace boost
namespace bgs::protocol::game_utilities::v1
{
namespace system
{
class error_code;
}
class ClientResponse;
class GetAllValuesForAttributeResponse;
}
namespace bgs
namespace JSON::RealmList
{
namespace protocol
{
namespace game_utilities
{
namespace v1
{
class ClientResponse;
class GetAllValuesForAttributeResponse;
}
}
}
}
namespace JSON
{
namespace RealmList
{
class RealmListUpdates;
}
class RealmListUpdates;
}
/// Storage object for the list of realms on the server
@@ -99,7 +79,7 @@ private:
RealmList();
void LoadBuildInfo();
void UpdateRealms(boost::system::error_code const& error);
void UpdateRealms();
void UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint32 build, std::string const& name,
boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr,
uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population);
@@ -114,4 +94,5 @@ private:
};
#define sRealmList RealmList::Instance()
#endif