Able to send and receive messages, server panicking and not relaying userids yet
This commit is contained in:
@@ -9,6 +9,7 @@ ChatReactor::ChatReactor(Service *service, absl::Mutex *mu,
|
||||
|
||||
void ChatReactor::OnReadDone(bool ok) {
|
||||
if (ok) {
|
||||
std::cout << "Received message: " << m_msg.message() << std::endl;
|
||||
m_mu->lock();
|
||||
m_messages->push_back(m_msg);
|
||||
m_mu->unlock();
|
||||
|
||||
@@ -1,21 +1,29 @@
|
||||
#include "logic/service.h"
|
||||
#include "logic/reactor.h"
|
||||
#include <cstdlib>
|
||||
#include <format>
|
||||
#include <utility>
|
||||
|
||||
Service::~Service() {
|
||||
for (auto *client : m_clients) {
|
||||
delete client;
|
||||
for (auto client : m_clients) {
|
||||
delete client.second;
|
||||
}
|
||||
}
|
||||
|
||||
grpc::ServerBidiReactor<chat::chatMsg, chat::chatMsg> *
|
||||
Service::sendMsg(grpc::CallbackServerContext *context) {
|
||||
auto newClient = new ChatReactor(this, &m_mu, &m_messages);
|
||||
m_clients.push_back(newClient);
|
||||
std::string newId = std::format("user_{}", std::rand());
|
||||
m_clients.insert(std::make_pair(newId, newClient));
|
||||
std::cout << "New client: " << newId << std::endl;
|
||||
return newClient;
|
||||
}
|
||||
|
||||
void Service::sendToAll(const chat::chatMsg &msg) {
|
||||
for (auto *client : m_clients) {
|
||||
client->StartWrite(&msg);
|
||||
std::cout << "Relay message to: ";
|
||||
for (auto client : m_clients) {
|
||||
std::cout << client.first << " ";
|
||||
client.second->StartWrite(&msg);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user