Skip to content

Commit 19789cb

Browse files
add WebSocket adaptor, ConnectionManager integration
1 parent 367929c commit 19789cb

9 files changed

Lines changed: 456 additions & 159 deletions

File tree

include/network/ConnectionManager.hpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
#include <algorithm>
44
#include <atomic>
55
#include <chrono>
6-
#include <unordered_set>
7-
#include <shared_mutex>
8-
#include <mutex>
6+
#include <functional>
97
#include <memory>
8+
#include <mutex>
9+
#include <shared_mutex>
1010
#include <string>
11-
#include <vector>
12-
#include <functional>
1311
#include <set>
12+
#include <vector>
13+
#include <unordered_set>
1414
#include <unordered_map>
1515

16-
#include "logging/Logger.hpp"
17-
#include "network/GameSession.hpp"
1816
#include "nlohmann/json.hpp"
1917

18+
#include "logging/Logger.hpp"
19+
#include "network/IConnection.hpp"
20+
2021
class ConnectionManager {
2122
public:
2223
// Delete copy constructor and assignment operator
@@ -29,21 +30,21 @@ class ConnectionManager {
2930
// Get shared_ptr to singleton (useful for passing to other components)
3031
static std::shared_ptr<ConnectionManager> GetInstancePtr();
3132

32-
void Start(std::shared_ptr<GameSession> session);
33-
void Stop(std::shared_ptr<GameSession> session);
33+
void Start(std::shared_ptr<IConnection> session);
34+
void Stop(std::shared_ptr<IConnection> session);
3435
void StopAll();
3536

3637
size_t GetConnectionCount() const;
37-
std::shared_ptr<GameSession> GetSession(uint64_t sessionId) const;
38-
std::vector<std::shared_ptr<GameSession>> GetAllSessions() const;
38+
std::shared_ptr<IConnection> GetSession(uint64_t sessionId) const;
39+
std::vector<std::shared_ptr<IConnection>> GetAllSessions() const;
3940

4041
// Broadcast methods
4142
void Broadcast(const nlohmann::json& message);
4243
void BroadcastToGroup(const std::string& groupId, const nlohmann::json& message);
4344

4445
// New broadcast methods
4546
void BroadcastWithFilter(const nlohmann::json& message,
46-
std::function<bool(std::shared_ptr<GameSession>)> filter);
47+
std::function<bool(std::shared_ptr<IConnection>)> filter);
4748
void BroadcastExcept(uint64_t excludeSessionId, const nlohmann::json& message);
4849
void BroadcastToAuthenticated(const nlohmann::json& message);
4950
void BroadcastToUnauthenticated(const nlohmann::json& message);
@@ -54,9 +55,9 @@ class ConnectionManager {
5455
void RemoveFromAllGroups(uint64_t sessionId);
5556

5657
// Session query methods
57-
std::vector<std::shared_ptr<GameSession>> GetSessionsByPlayerId(int64_t playerId) const;
58+
std::vector<std::shared_ptr<IConnection>> GetSessionsByPlayerId(int64_t playerId) const;
5859
std::vector<uint64_t> GetSessionIdsInGroup(const std::string& groupId) const;
59-
std::vector<std::shared_ptr<GameSession>> GetSessionsInGroup(const std::string& groupId) const;
60+
std::vector<std::shared_ptr<IConnection>> GetSessionsInGroup(const std::string& groupId) const;
6061
std::set<std::string> GetGroupsForSession(uint64_t sessionId) const;
6162
bool IsSessionInGroup(uint64_t sessionId, const std::string& groupId) const;
6263

@@ -106,7 +107,7 @@ class ConnectionManager {
106107
void DisconnectAllInGroup(const std::string& groupId);
107108

108109
// Load balancing
109-
std::vector<std::shared_ptr<GameSession>> GetSessionsByWorkerId(int workerId) const;
110+
std::vector<std::shared_ptr<IConnection>> GetSessionsByWorkerId(int workerId) const;
110111
void RedistributeSessions(const std::vector<int>& workerIds);
111112

112113
// Event system
@@ -118,7 +119,7 @@ class ConnectionManager {
118119
void EnforceGlobalRateLimit(int maxMessagesPerSecond);
119120

120121
// Session migration
121-
bool MigrateSession(uint64_t sessionId, std::shared_ptr<GameSession> newSession);
122+
bool MigrateSession(uint64_t sessionId, std::shared_ptr<IConnection> newSession);
122123

123124
// Monitoring
124125
void MonitorConnections();
@@ -138,7 +139,7 @@ class ConnectionManager {
138139

139140
// Session storage
140141
mutable std::shared_mutex sessionsMutex_;
141-
std::unordered_map<uint64_t, std::shared_ptr<GameSession>> sessions_;
142+
std::unordered_map<uint64_t, std::shared_ptr<IConnection>> sessions_;
142143

143144
// Group management
144145
mutable std::shared_mutex groupsMutex_;

include/network/GameServer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "network/ConnectionManager.hpp"
1616
#include "network/GameSession.hpp"
1717
#include "network/WebSocketProtocol.hpp"
18+
#include "network/WebSocketSession.hpp"
1819

1920
class GameServer {
2021
public:
@@ -57,4 +58,4 @@ class GameServer {
5758

5859
SessionFactory sessionFactory_;
5960
WebSocketFactory webSocketFactory_;
60-
};
61+
};

0 commit comments

Comments
 (0)