Started work on the server, continuing client to be able to test
This commit is contained in:
0
server/src/.gitempty
Normal file
0
server/src/.gitempty
Normal file
0
server/src/logic/.gitempty
Normal file
0
server/src/logic/.gitempty
Normal file
20
server/src/logic/reactor.cpp
Normal file
20
server/src/logic/reactor.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "logic/reactor.h"
|
||||
#include "logic/service.h"
|
||||
|
||||
ChatReactor::ChatReactor(Service *service, absl::Mutex *mu,
|
||||
std::vector<chat::chatMsg> *msgs)
|
||||
: m_service(service), m_mu(mu), m_messages(msgs) {
|
||||
StartRead(&m_msg);
|
||||
}
|
||||
|
||||
void ChatReactor::OnReadDone(bool ok) {
|
||||
if (ok) {
|
||||
m_mu->lock();
|
||||
m_messages->push_back(m_msg);
|
||||
m_mu->unlock();
|
||||
m_service->sendToAll(m_msg);
|
||||
StartRead(&m_msg);
|
||||
} else {
|
||||
Finish(grpc::Status::OK);
|
||||
}
|
||||
}
|
||||
21
server/src/logic/service.cpp
Normal file
21
server/src/logic/service.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "logic/service.h"
|
||||
#include "logic/reactor.h"
|
||||
|
||||
Service::~Service() {
|
||||
for (auto *client : m_clients) {
|
||||
delete client;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return newClient;
|
||||
}
|
||||
|
||||
void Service::sendToAll(const chat::chatMsg &msg) {
|
||||
for (auto *client : m_clients) {
|
||||
client->StartWrite(&msg);
|
||||
}
|
||||
}
|
||||
0
server/src/models/.gitempty
Normal file
0
server/src/models/.gitempty
Normal file
0
server/src/ui/.gitempty
Normal file
0
server/src/ui/.gitempty
Normal file
Reference in New Issue
Block a user