1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00

Use std::byte to manage network data

This commit is contained in:
Ivan Savenko 2024-02-02 01:27:19 +02:00
parent c12558bf8a
commit 29c0989849
19 changed files with 72 additions and 100 deletions

View File

@ -875,7 +875,7 @@ public:
}
};
void CServerHandler::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<uint8_t> & message)
void CServerHandler::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message)
{
CPack * pack = c->retrievePack(message);
if(state == EClientState::DISCONNECTING)
@ -891,7 +891,7 @@ void CServerHandler::onPacketReceived(const std::shared_ptr<INetworkConnection>
}
}
void CServerHandler::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
void CServerHandler::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
assert(networkConnection == connection);
networkConnection.reset();

View File

@ -114,10 +114,10 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
void onServerFinished();
void sendLobbyPack(const CPackForLobby & pack) const override;
void onPacketReceived(const NetworkConnectionPtr &, const std::vector<uint8_t> & message) override;
void onPacketReceived(const NetworkConnectionPtr &, const std::vector<std::byte> & message) override;
void onConnectionFailed(const std::string & errorMessage) override;
void onConnectionEstablished(const NetworkConnectionPtr &) override;
void onDisconnected(const NetworkConnectionPtr &) override;
void onDisconnected(const NetworkConnectionPtr &, const std::string & errorMessage) override;
void onTimer() override;
void applyPackOnLobbyScreen(CPackForLobby & pack);

View File

@ -36,7 +36,7 @@ static std::string getCurrentTimeFormatted(int timeOffsetSeconds = 0)
return TextOperations::getFormattedTimeLocal(std::chrono::system_clock::to_time_t(timeNowChrono));
}
void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<uint8_t> & message)
void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message)
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
@ -247,7 +247,7 @@ void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
loginWindowPtr->onConnectionFailed(errorMessage);
}
void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
assert(connection == networkConnection);
networkConnection.reset();
@ -258,15 +258,7 @@ void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection>
void GlobalLobbyClient::sendMessage(const JsonNode & data)
{
std::string payloadString = data.toJson(true);
// FIXME: find better approach
uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
uint8_t * payloadEnd = payloadBegin + payloadString.size();
std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
networkConnection->sendPacket(payloadBuffer);
networkConnection->sendPacket(data.toBytes(true));
}
void GlobalLobbyClient::sendOpenPublicRoom()
@ -355,13 +347,5 @@ void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & ne
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
toSend["gameRoomID"] = settings["lobby"]["roomID"];
std::string payloadString = toSend.toJson(true);
// FIXME: find better approach
uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
uint8_t * payloadEnd = payloadBegin + payloadString.size();
std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
netConnection->sendPacket(payloadBuffer);
netConnection->sendPacket(toSend.toBytes(true));
}

View File

@ -30,10 +30,10 @@ class GlobalLobbyClient final : public INetworkClientListener, boost::noncopyabl
std::weak_ptr<GlobalLobbyWindow> lobbyWindow;
std::shared_ptr<GlobalLobbyWindow> lobbyWindowLock; // helper strong reference to prevent window destruction on closing
void onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<uint8_t> & message) override;
void onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message) override;
void onConnectionFailed(const std::string & errorMessage) override;
void onConnectionEstablished(const std::shared_ptr<INetworkConnection> &) override;
void onDisconnected(const std::shared_ptr<INetworkConnection> &) override;
void onDisconnected(const std::shared_ptr<INetworkConnection> &, const std::string & errorMessage) override;
void sendClientRegister();
void sendClientLogin();

View File

@ -76,6 +76,10 @@ JsonNode::JsonNode(const uint8_t *data, size_t datasize)
:JsonNode(reinterpret_cast<const char*>(data), datasize)
{}
JsonNode::JsonNode(const std::byte *data, size_t datasize)
:JsonNode(reinterpret_cast<const char*>(data), datasize)
{}
JsonNode::JsonNode(const char *data, size_t datasize)
{
JsonParser parser(data, datasize);
@ -421,6 +425,15 @@ JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer)
return ::resolvePointer(*this, jsonPointer);
}
std::vector<std::byte> JsonNode::toBytes(bool compact) const
{
std::string jsonString = toJson(compact);
auto dataBegin = reinterpret_cast<const std::byte*>(jsonString.data());
auto dataEnd = dataBegin + jsonString.size();
std::vector<std::byte> result(dataBegin, dataEnd);
return result;
}
std::string JsonNode::toJson(bool compact) const
{
std::ostringstream out;

View File

@ -52,6 +52,7 @@ public:
//Create tree from Json-formatted input
explicit JsonNode(const char * data, size_t datasize);
explicit JsonNode(const uint8_t * data, size_t datasize);
explicit JsonNode(const std::byte * data, size_t datasize);
//Create tree from JSON file
explicit JsonNode(const JsonPath & fileURI);
explicit JsonNode(const std::string & modName, const JsonPath & fileURI);
@ -117,6 +118,7 @@ public:
const JsonNode & operator[](size_t child) const;
std::string toJson(bool compact = false) const;
std::vector<std::byte> toBytes(bool compact = false) const;
template <typename Handler> void serialize(Handler &h)
{

View File

@ -33,7 +33,7 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ec)
{
if (ec)
{
listener.onDisconnected(shared_from_this());
listener.onDisconnected(shared_from_this(), ec.message());
return;
}
@ -50,14 +50,11 @@ uint32_t NetworkConnection::readPacketSize()
if (readBuffer.size() < messageHeaderSize)
throw std::runtime_error("Failed to read header!");
std::istream istream(&readBuffer);
uint32_t messageSize;
istream.read(reinterpret_cast<char *>(&messageSize), messageHeaderSize);
readBuffer.sgetn(reinterpret_cast<char *>(&messageSize), sizeof(messageSize));
if (messageSize > messageMaxSize)
{
throw std::runtime_error("Invalid packet size!");
}
return messageSize;
}
@ -66,7 +63,7 @@ void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, u
{
if (ec)
{
listener.onDisconnected(shared_from_this());
listener.onDisconnected(shared_from_this(), ec.message());
return;
}
@ -75,28 +72,21 @@ void NetworkConnection::onPacketReceived(const boost::system::error_code & ec, u
throw std::runtime_error("Failed to read header!");
}
std::vector<uint8_t> message;
message.resize(expectedPacketSize);
std::istream istream(&readBuffer);
istream.read(reinterpret_cast<char *>(message.data()), expectedPacketSize);
std::vector<std::byte> message(expectedPacketSize);
readBuffer.sgetn(reinterpret_cast<char *>(message.data()), expectedPacketSize);
listener.onPacketReceived(shared_from_this(), message);
start();
}
void NetworkConnection::sendPacket(const std::vector<uint8_t> & message)
void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
{
NetworkBuffer writeBuffer;
std::ostream ostream(&writeBuffer);
uint32_t messageSize = message.size();
ostream.write(reinterpret_cast<const char *>(&messageSize), messageHeaderSize);
ostream.write(reinterpret_cast<const char *>(message.data()), message.size());
boost::system::error_code ec;
boost::asio::write(*socket, writeBuffer, ec );
std::array<uint32_t, 1> messageSize{static_cast<uint32_t>(message.size())};
boost::asio::write(*socket, boost::asio::buffer(messageSize), ec );
boost::asio::write(*socket, boost::asio::buffer(message), ec );
// FIXME: handle error?
}

View File

@ -31,7 +31,7 @@ public:
NetworkConnection(INetworkConnectionListener & listener, const std::shared_ptr<NetworkSocket> & socket);
void start();
void sendPacket(const std::vector<uint8_t> & message) override;
void sendPacket(const std::vector<std::byte> & message) override;
};
VCMI_LIB_NAMESPACE_END

View File

@ -16,7 +16,7 @@ class DLL_LINKAGE INetworkConnection : boost::noncopyable
{
public:
virtual ~INetworkConnection() = default;
virtual void sendPacket(const std::vector<uint8_t> & message) = 0;
virtual void sendPacket(const std::vector<std::byte> & message) = 0;
};
using NetworkConnectionPtr = std::shared_ptr<INetworkConnection>;
@ -29,7 +29,7 @@ public:
virtual ~INetworkClient() = default;
virtual bool isConnected() const = 0;
virtual void sendPacket(const std::vector<uint8_t> & message) = 0;
virtual void sendPacket(const std::vector<std::byte> & message) = 0;
};
/// Base class for incoming connections support
@ -38,7 +38,7 @@ class DLL_LINKAGE INetworkServer : boost::noncopyable
public:
virtual ~INetworkServer() = default;
virtual void sendPacket(const std::shared_ptr<INetworkConnection> &, const std::vector<uint8_t> & message) = 0;
virtual void sendPacket(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message) = 0;
virtual void closeConnection(const std::shared_ptr<INetworkConnection> &) = 0;
virtual void start(uint16_t port) = 0;
};
@ -47,8 +47,8 @@ public:
class DLL_LINKAGE INetworkConnectionListener
{
public:
virtual void onDisconnected(const std::shared_ptr<INetworkConnection> & connection) = 0;
virtual void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message) = 0;
virtual void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) = 0;
virtual void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) = 0;
virtual ~INetworkConnectionListener() = default;
};

View File

@ -46,7 +46,7 @@ void NetworkServer::connectionAccepted(std::shared_ptr<NetworkSocket> upcomingCo
startAsyncAccept();
}
void NetworkServer::sendPacket(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
void NetworkServer::sendPacket(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
{
connection->sendPacket(message);
}
@ -58,18 +58,18 @@ void NetworkServer::closeConnection(const std::shared_ptr<INetworkConnection> &
connections.erase(connection);
}
void NetworkServer::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
void NetworkServer::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
logNetwork->info("Connection lost!");
logNetwork->info("Connection lost! Reason: %s", errorMessage);
assert(connections.count(connection));
if (connections.count(connection)) // how? Connection was explicitly closed before?
{
connections.erase(connection);
listener.onDisconnected(connection);
listener.onDisconnected(connection, errorMessage);
}
}
void NetworkServer::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
void NetworkServer::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
{
listener.onPacketReceived(connection, message);
}

View File

@ -24,12 +24,12 @@ class NetworkServer : public INetworkConnectionListener, public INetworkServer
void connectionAccepted(std::shared_ptr<NetworkSocket>, const boost::system::error_code & ec);
void startAsyncAccept();
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection) override;
void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message) override;
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) override;
public:
NetworkServer(INetworkServerListener & listener, const std::shared_ptr<NetworkContext> & context);
void sendPacket(const std::shared_ptr<INetworkConnection> &, const std::vector<uint8_t> & message) override;
void sendPacket(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message) override;
void closeConnection(const std::shared_ptr<INetworkConnection> &) override;
void start(uint16_t port) override;

View File

@ -22,7 +22,7 @@ VCMI_LIB_NAMESPACE_BEGIN
class DLL_LINKAGE ConnectionPackWriter final : public IBinaryWriter
{
public:
std::vector<uint8_t> buffer;
std::vector<std::byte> buffer;
int write(const void * data, unsigned size) final;
};
@ -30,7 +30,7 @@ public:
class DLL_LINKAGE ConnectionPackReader final : public IBinaryReader
{
public:
const std::vector<uint8_t> * buffer;
const std::vector<std::byte> * buffer;
size_t position;
int read(void * data, unsigned size) final;
@ -38,8 +38,8 @@ public:
int ConnectionPackWriter::write(const void * data, unsigned size)
{
const uint8_t * begin_ptr = static_cast<const uint8_t *>(data);
const uint8_t * end_ptr = begin_ptr + size;
const std::byte * begin_ptr = static_cast<const std::byte *>(data);
const std::byte * end_ptr = begin_ptr + size;
buffer.insert(buffer.end(), begin_ptr, end_ptr);
return size;
}
@ -49,7 +49,7 @@ int ConnectionPackReader::read(void * data, unsigned size)
if (position + size > buffer->size())
throw std::runtime_error("End of file reached when reading received network pack!");
uint8_t * begin_ptr = static_cast<uint8_t *>(data);
std::byte * begin_ptr = static_cast<std::byte *>(data);
std::copy_n(buffer->begin() + position, size, begin_ptr);
position += size;
@ -88,7 +88,7 @@ void CConnection::sendPack(const CPack * pack)
packWriter->buffer.clear();
}
CPack * CConnection::retrievePack(const std::vector<uint8_t> & data)
CPack * CConnection::retrievePack(const std::vector<std::byte> & data)
{
CPack * result;

View File

@ -50,7 +50,7 @@ public:
~CConnection();
void sendPack(const CPack * pack);
CPack * retrievePack(const std::vector<uint8_t> & data);
CPack * retrievePack(const std::vector<std::byte> & data);
void enterLobbyConnectionMode();
void setCallback(IGameCallback * cb);

View File

@ -60,16 +60,7 @@ NetworkConnectionPtr LobbyServer::findGameRoom(const std::string & gameRoomID) c
void LobbyServer::sendMessage(const NetworkConnectionPtr & target, const JsonNode & json)
{
//NOTE: copy-paste from LobbyClient::sendMessage
std::string payloadString = json.toJson(true);
// TODO: find better approach
const uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
const uint8_t * payloadEnd = payloadBegin + payloadString.size();
std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
networkServer->sendPacket(target, payloadBuffer);
networkServer->sendPacket(target, json.toBytes(true));
}
void LobbyServer::sendAccountCreated(const NetworkConnectionPtr & target, const std::string & accountID, const std::string & accountCookie)
@ -212,7 +203,7 @@ void LobbyServer::onNewConnection(const NetworkConnectionPtr & connection)
// no-op - waiting for incoming data
}
void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection)
void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection, const std::string & errorMessage)
{
if (activeAccounts.count(connection))
database->setAccountOnline(activeAccounts.at(connection), false);
@ -230,7 +221,7 @@ void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection)
broadcastActiveGameRooms();
}
void LobbyServer::onPacketReceived(const NetworkConnectionPtr & connection, const std::vector<uint8_t> & message)
void LobbyServer::onPacketReceived(const NetworkConnectionPtr & connection, const std::vector<std::byte> & message)
{
// proxy connection - no processing, only redirect
if(activeProxies.count(connection))

View File

@ -51,8 +51,8 @@ class LobbyServer final : public INetworkServerListener
NetworkConnectionPtr findGameRoom(const std::string & gameRoomID) const;
void onNewConnection(const NetworkConnectionPtr & connection) override;
void onDisconnected(const NetworkConnectionPtr & connection) override;
void onPacketReceived(const NetworkConnectionPtr & connection, const std::vector<uint8_t> & message) override;
void onDisconnected(const NetworkConnectionPtr & connection, const std::string & errorMessage) override;
void onPacketReceived(const NetworkConnectionPtr & connection, const std::vector<std::byte> & message) override;
void sendMessage(const NetworkConnectionPtr & target, const JsonNode & json);

View File

@ -164,7 +164,7 @@ void CVCMIServer::onNewConnection(const std::shared_ptr<INetworkConnection> & co
}
}
void CVCMIServer::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
void CVCMIServer::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
{
std::shared_ptr<CConnection> c = findConnection(connection);
auto pack = c->retrievePack(message);
@ -325,7 +325,7 @@ void CVCMIServer::startGameImmediately()
onTimer();
}
void CVCMIServer::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
void CVCMIServer::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
logNetwork->error("Network error receiving a pack. Connection has been closed");

View File

@ -77,8 +77,8 @@ private:
public:
// INetworkListener impl
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection) override;
void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message) override;
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) override;
void onNewConnection(const std::shared_ptr<INetworkConnection> &) override;
void onTimer() override;

View File

@ -27,12 +27,12 @@ void GlobalLobbyProcessor::establishNewConnection()
owner.networkHandler->connectToRemote(*this, hostname, port);
}
void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
throw std::runtime_error("Lost connection to a lobby server!");
}
void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message)
void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
{
if (connection == controlConnection)
{
@ -122,13 +122,5 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & target, const JsonNode & data)
{
std::string payloadString = data.toJson(true);
// FIXME: find better approach
uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
uint8_t * payloadEnd = payloadBegin + payloadString.size();
std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
target->sendPacket(payloadBuffer);
target->sendPacket(data.toBytes(true));
}

View File

@ -24,8 +24,8 @@ class GlobalLobbyProcessor : public INetworkClientListener
NetworkConnectionPtr controlConnection;
std::map<std::string, NetworkConnectionPtr> proxyConnections;
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection) override;
void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<uint8_t> & message) override;
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) override;
void onConnectionFailed(const std::string & errorMessage) override;
void onConnectionEstablished(const std::shared_ptr<INetworkConnection> &) override;