-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.h
53 lines (43 loc) · 1.45 KB
/
server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include <string>
#include <memory>
#include <asio.hpp>
#include <iostream>
#include <random>
#include <thread>
#include <mutex>
#include <unordered_map>
#include <vector>
#include <imgui.h>
using namespace std;
#define SERVER_PORT 60000 // Listening port
#define BUF_SIZE 4096
#define MAX_CLNT 256 // Maximum number of connections
// Forward declare ChatWindow
class ChatWindow;
class Server {
public:
Server(ChatWindow& chatWindow); // Constructor
~Server(); // Destructor
int run(); // Starts the server
void stop(); // Stops the server
asio::io_context context;
private:
// Core server functions
void accept_connections();
void handle_client(std::shared_ptr<asio::ip::tcp::socket> clnt_sock);
void broadcast_message(const std::string& msg);
// Client management
void add_client(const std::string& name, std::shared_ptr<asio::ip::tcp::socket> sock);
void remove_client(const std::string& name);
void broadcast_client_list();
// Utility functions
void append_to_chat_log(const std::string& message);
// Members
std::unordered_map<std::string, std::shared_ptr<asio::ip::tcp::socket>> clients;
std::vector<std::thread> client_threads;
std::mutex mtx;
std::vector<std::string> connected_clients;
asio::ip::tcp::acceptor acceptor;
ChatWindow& chatWindowRef;
};