-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Client connection registers a queue to it and the client uses a…
… network connection allowing to receive the ConnectionMessage
- Loading branch information
1 parent
479523a
commit 2481d67
Showing
8 changed files
with
155 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
#include "NetworkMessageQueue.hh" | ||
#include "MessageParser.hh" | ||
#include "SynchronizedMessageQueue.hh" | ||
|
||
namespace bsgo { | ||
|
||
NetworkMessageQueue::NetworkMessageQueue(IMessageQueuePtr localQueue) | ||
: IMessageQueue() | ||
, utils::CoreObject("message") | ||
, m_localQueue(std::make_unique<SynchronizedMessageQueue>(std::move(localQueue))) | ||
{ | ||
addModule("queue"); | ||
setService("network"); | ||
} | ||
|
||
void NetworkMessageQueue::registerToConnection(net::Connection &connection) | ||
{ | ||
connection.setDataHandler( | ||
[this](const net::ConnectionId connectionId, const std::deque<char> &data) { | ||
return onDataReceived(connectionId, data); | ||
}); | ||
} | ||
|
||
void NetworkMessageQueue::pushMessage(IMessagePtr message) | ||
{ | ||
m_localQueue->pushMessage(std::move(message)); | ||
} | ||
|
||
void NetworkMessageQueue::addListener(IMessageListener *listener) | ||
{ | ||
m_localQueue->addListener(listener); | ||
} | ||
|
||
bool NetworkMessageQueue::empty() | ||
{ | ||
return m_localQueue->empty(); | ||
} | ||
|
||
void NetworkMessageQueue::processMessages(const std::optional<int> &amount) | ||
{ | ||
m_localQueue->processMessages(amount); | ||
} | ||
|
||
auto NetworkMessageQueue::onDataReceived(const net::ConnectionId /*connectionId*/, | ||
const std::deque<char> &data) -> int | ||
{ | ||
bool processedSomeBytes{true}; | ||
auto processedBytes{0}; | ||
std::vector<IMessagePtr> messages{}; | ||
|
||
std::deque<char> workingData(data); | ||
MessageParser parser{}; | ||
|
||
while (processedSomeBytes) | ||
{ | ||
auto result = parser.tryParseMessage(workingData); | ||
if (result.message) | ||
{ | ||
messages.emplace_back(std::move(*result.message)); | ||
} | ||
|
||
processedSomeBytes = (result.bytesProcessed > 0); | ||
processedBytes += result.bytesProcessed; | ||
|
||
workingData.erase(workingData.begin(), workingData.begin() + result.bytesProcessed); | ||
} | ||
|
||
for (auto &message : messages) | ||
{ | ||
m_localQueue->pushMessage(std::move(message)); | ||
} | ||
|
||
return processedBytes; | ||
} | ||
|
||
} // namespace bsgo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
#pragma once | ||
|
||
#include "Connection.hh" | ||
#include "IMessageQueue.hh" | ||
#include <core_utils/CoreObject.hh> | ||
#include <deque> | ||
#include <memory> | ||
|
||
namespace bsgo { | ||
|
||
class NetworkMessageQueue : public IMessageQueue, public utils::CoreObject | ||
{ | ||
public: | ||
NetworkMessageQueue(IMessageQueuePtr localQueue); | ||
~NetworkMessageQueue() override = default; | ||
|
||
void registerToConnection(net::Connection &connection); | ||
|
||
void pushMessage(IMessagePtr message) override; | ||
void addListener(IMessageListener *listener) override; | ||
bool empty() override; | ||
|
||
void processMessages(const std::optional<int> &amount = {}) override; | ||
|
||
private: | ||
IMessageQueuePtr m_localQueue{}; | ||
|
||
auto onDataReceived(const net::ConnectionId connectionId, const std::deque<char> &data) -> int; | ||
}; | ||
|
||
using NetworkMessageQueuePtr = std::unique_ptr<NetworkMessageQueue>; | ||
|
||
} // namespace bsgo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters