#1 - quicr module
This commit is contained in:
@@ -5,7 +5,8 @@ add_executable(${PROJECT_NAME} ChatMockClient.cpp)
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
tw::protocol
|
||||
tw::messaging
|
||||
tw::message_protocol
|
||||
tw::network
|
||||
tw::quicr
|
||||
spdlog::spdlog
|
||||
)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "Address.hpp"
|
||||
#include "MessageSession.hpp"
|
||||
#include "ProtobufMessages.hpp"
|
||||
#include "MessageRegistry.hpp"
|
||||
#include "Chat.pb.h"
|
||||
#include "message_protocol/MessageEndpoint.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <atomic>
|
||||
@@ -56,30 +57,42 @@ int main(int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
tw::MessageSession session(tw::net::Address{std::string{host}, port});
|
||||
auto endpoint_r = tw::msg::MessageEndpoint::create();
|
||||
if (!endpoint_r) {
|
||||
spdlog::error("Failed to create an endpoint: {}", endpoint_r.error().message());
|
||||
return 1;
|
||||
}
|
||||
auto& endpoint = endpoint_r.value();
|
||||
|
||||
auto server_r = endpoint->connect(host, port);
|
||||
if (!server_r) {
|
||||
spdlog::error("Failed to connect: {}", server_r.error().message());
|
||||
return 1;
|
||||
}
|
||||
auto* server = server_r.value();
|
||||
|
||||
tw::ProtobufMessages messages(endpoint.get());
|
||||
|
||||
spdlog::info("Connecting to {}:{}...", host, port);
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5);
|
||||
while (!session.is_established()) {
|
||||
while (!server->is_established()) {
|
||||
if (std::chrono::steady_clock::now() > deadline) {
|
||||
spdlog::error("Connection timed out");
|
||||
return 1;
|
||||
}
|
||||
session.update();
|
||||
endpoint->update();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
spdlog::info("Connected. Joining channel {}...", channel_id);
|
||||
|
||||
session.set_handler(tw::Message<mmo::chat::ChatMessageBroadcastRequest>::value,
|
||||
[](std::span<const std::byte> data) {
|
||||
mmo::chat::ChatMessageBroadcastRequest bcast;
|
||||
bcast.ParseFromArray(data.data(), static_cast<int>(data.size()));
|
||||
messages.set_handler<mmo::chat::ChatMessageBroadcastRequest>(
|
||||
[](tw::msg::PeerId, const mmo::chat::ChatMessageBroadcastRequest& bcast) {
|
||||
std::println("[ch:{}] <{}> {}", bcast.channel_id(), bcast.sender_id(), bcast.message());
|
||||
});
|
||||
|
||||
mmo::chat::JoinChannelRequest join;
|
||||
join.set_channel_id(channel_id);
|
||||
(void)session.request(
|
||||
(void)server->request(
|
||||
tw::Message<mmo::chat::JoinChannelRequest>::value,
|
||||
serialize(join),
|
||||
[channel_id](std::span<const std::byte> data) {
|
||||
@@ -102,7 +115,7 @@ int main(int argc, char* argv[]) {
|
||||
mmo::chat::SendChatMessageRequest msg;
|
||||
msg.set_channel_id(channel_id);
|
||||
msg.set_message(line);
|
||||
(void)session.request(
|
||||
(void)server->request(
|
||||
tw::Message<mmo::chat::SendChatMessageRequest>::value,
|
||||
serialize(msg),
|
||||
[channel_id](std::span<const std::byte> data) {
|
||||
@@ -113,7 +126,7 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
session.update();
|
||||
endpoint->update();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user