Started work on the server, continuing client to be able to test

This commit is contained in:
2026-02-03 17:04:20 +01:00
parent 7fc64a03ad
commit a0d9d9c6de
15 changed files with 112 additions and 1 deletions

View File

@@ -8,6 +8,11 @@ add_executable(chat_server
${SOURCES} ${SOURCES}
) )
target_include_directories(chat_server
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
target_link_libraries(chat_server target_link_libraries(chat_server
PRIVATE PRIVATE
proto_static proto_static

0
server/inc/.gitempty Normal file
View File

View File

View File

@@ -0,0 +1,33 @@
#pragma once
#include "messages.pb.h"
#include <absl/base/thread_annotations.h>
#include <absl/synchronization/mutex.h>
#include <grpcpp/support/server_callback.h>
#include <vector>
class Service;
class ChatReactor
: public grpc::ServerBidiReactor<chat::chatMsg, chat::chatMsg> {
public:
ChatReactor(Service *service, absl::Mutex *mu,
std::vector<chat::chatMsg> *msgs);
void OnReadDone(bool ok) override;
void OnWriteDone(bool /*ok*/) override { StartRead(&m_msg); }
void OnDone() override {
std::cout << "RPC Completed";
delete this;
}
void OnCancel() override { std::cout << "RPC Cancelled"; }
private:
Service *m_service;
chat::chatMsg m_msg;
absl::Mutex *m_mu;
std::vector<chat::chatMsg> *m_messages ABSL_GUARDED_BY(m_mu);
};

View File

@@ -0,0 +1,20 @@
#pragma once
#include "logic/reactor.h"
#include "messages.pb.h"
#include "services.grpc.pb.h"
#include <grpcpp/server_context.h>
#include <grpcpp/support/server_callback.h>
class Service : public chat::Chat::CallbackService {
public:
~Service();
grpc::ServerBidiReactor<chat::chatMsg, chat::chatMsg> *
sendMsg(grpc::CallbackServerContext *context) override;
void sendToAll(const chat::chatMsg &msg);
private:
std::vector<ChatReactor *> m_clients;
absl::Mutex m_mu;
std::vector<chat::chatMsg> m_messages ABSL_GUARDED_BY(m_mu);
};

View File

0
server/inc/ui/.gitempty Normal file
View File

0
server/lib/.gitempty Normal file
View File

View File

@@ -1,2 +1,14 @@
#include "logic/service.h"
#include <grpcpp/grpcpp.h>
int main(int argc, char *argv[]) { return 0; } int main(int argc, char *argv[]) {
Service service;
std::string server_address = "0.0.0.0:50051";
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
server->Wait();
return 0;
}

0
server/src/.gitempty Normal file
View File

View File

View 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);
}
}

View 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);
}
}

View File

0
server/src/ui/.gitempty Normal file
View File