#1 - quicr module
This commit is contained in:
@@ -22,45 +22,68 @@ tl::expected<void, tw::chat::ChatClientError> from_error_code(mmo::chat::ChatErr
|
||||
|
||||
namespace tw::chat {
|
||||
|
||||
namespace {
|
||||
|
||||
std::unique_ptr<tw::msg::MessageEndpoint> create_endpoint() {
|
||||
auto endpoint_r = tw::msg::MessageEndpoint::create();
|
||||
if (!endpoint_r) {
|
||||
throw std::runtime_error("Chat client failed to create an endpoint: " +
|
||||
endpoint_r.error().message());
|
||||
}
|
||||
|
||||
return std::move(endpoint_r.value());
|
||||
}
|
||||
|
||||
tw::msg::MessageConnection* connect_to_server(tw::msg::MessageEndpoint* endpoint,
|
||||
const std::string& server_address,
|
||||
int16_t port) {
|
||||
auto server_r = endpoint->connect(server_address, port);
|
||||
if (!server_r) {
|
||||
throw std::runtime_error("Chat client failed to connect: " + server_r.error().message());
|
||||
}
|
||||
|
||||
return server_r.value();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ChatClient::ChatClient(const std::string& server_address, int16_t port)
|
||||
: m_session(net::Address(server_address, port))
|
||||
: m_endpoint(create_endpoint())
|
||||
, m_server(connect_to_server(m_endpoint.get(), server_address, port))
|
||||
, m_messages(m_endpoint.get())
|
||||
{
|
||||
m_session.set_handler(CHAT_MESSAGE_BROADCAST_REQUEST, [this](std::span<const std::byte> data) {
|
||||
if (!m_on_message) return;
|
||||
mmo::chat::ChatMessageBroadcastRequest proto;
|
||||
if (!proto.ParseFromArray(data.data(), static_cast<int>(data.size()))) return;
|
||||
ChatMessage msg;
|
||||
msg.channel_id = proto.channel_id();
|
||||
msg.client_id = proto.sender_id();
|
||||
msg.message = proto.message();
|
||||
msg.timestamp = ChatMessage::Clock::now();
|
||||
m_on_message(std::move(msg));
|
||||
});
|
||||
m_messages.set_handler<mmo::chat::ChatMessageBroadcastRequest>(
|
||||
[this](msg::PeerId, const mmo::chat::ChatMessageBroadcastRequest& proto) {
|
||||
if (!m_on_message) return;
|
||||
ChatMessage msg;
|
||||
msg.channel_id = proto.channel_id();
|
||||
msg.client_id = proto.sender_id();
|
||||
msg.message = proto.message();
|
||||
msg.timestamp = ChatMessage::Clock::now();
|
||||
m_on_message(std::move(msg));
|
||||
});
|
||||
|
||||
m_session.set_handler(CHAT_SEND_MESSAGE_RESPONSE, [this](std::span<const std::byte> data) {
|
||||
if (!m_on_send_response) return;
|
||||
mmo::chat::SendChatMessageResponse proto;
|
||||
if (!proto.ParseFromArray(data.data(), static_cast<int>(data.size()))) return;
|
||||
m_on_send_response(from_error_code(proto.error()));
|
||||
});
|
||||
m_messages.set_handler<mmo::chat::SendChatMessageResponse>(
|
||||
[this](msg::PeerId, const mmo::chat::SendChatMessageResponse& proto) {
|
||||
if (!m_on_send_response) return;
|
||||
m_on_send_response(from_error_code(proto.error()));
|
||||
});
|
||||
|
||||
m_session.set_handler(CHAT_JOIN_CHANNEL_RESPONSE, [this](std::span<const std::byte> data) {
|
||||
if (!m_on_join_response) return;
|
||||
mmo::chat::JoinChannelResponse proto;
|
||||
if (!proto.ParseFromArray(data.data(), static_cast<int>(data.size()))) return;
|
||||
m_on_join_response(static_cast<uint32_t>(proto.channel_id()), from_error_code(proto.error()));
|
||||
});
|
||||
m_messages.set_handler<mmo::chat::JoinChannelResponse>(
|
||||
[this](msg::PeerId, const mmo::chat::JoinChannelResponse& proto) {
|
||||
if (!m_on_join_response) return;
|
||||
m_on_join_response(static_cast<uint32_t>(proto.channel_id()), from_error_code(proto.error()));
|
||||
});
|
||||
|
||||
m_session.set_handler(CHAT_LEAVE_CHANNEL_RESPONSE, [this](std::span<const std::byte> data) {
|
||||
if (!m_on_leave_response) return;
|
||||
mmo::chat::LeaveChannelResponse proto;
|
||||
if (!proto.ParseFromArray(data.data(), static_cast<int>(data.size()))) return;
|
||||
m_on_leave_response(static_cast<uint32_t>(proto.channel_id()), from_error_code(proto.error()));
|
||||
});
|
||||
m_messages.set_handler<mmo::chat::LeaveChannelResponse>(
|
||||
[this](msg::PeerId, const mmo::chat::LeaveChannelResponse& proto) {
|
||||
if (!m_on_leave_response) return;
|
||||
m_on_leave_response(static_cast<uint32_t>(proto.channel_id()), from_error_code(proto.error()));
|
||||
});
|
||||
}
|
||||
|
||||
void ChatClient::update() {
|
||||
m_session.update();
|
||||
m_endpoint->update();
|
||||
}
|
||||
|
||||
tl::expected<void, ChatClientError> ChatClient::send_mesg(SendChatMessage message) {
|
||||
@@ -68,11 +91,7 @@ tl::expected<void, ChatClientError> ChatClient::send_mesg(SendChatMessage messag
|
||||
mesg.set_channel_id(message.channel_id);
|
||||
mesg.set_message(message.message);
|
||||
|
||||
std::vector<std::byte> buf(mesg.ByteSizeLong());
|
||||
(void)mesg.SerializeToArray(buf.data(), static_cast<int>(buf.size()));
|
||||
|
||||
auto send_r = m_session.send(Message<mmo::chat::SendChatMessageRequest>::value,
|
||||
std::span(buf), true);
|
||||
auto send_r = m_messages.send(m_server, mesg, true);
|
||||
if (!send_r)
|
||||
return tl::make_unexpected(ChatClientError::PermissionDenied);
|
||||
return {};
|
||||
|
||||
Reference in New Issue
Block a user