refactor IServerSocket

This commit is contained in:
2025-08-06 17:16:50 +02:00
parent fb53ece340
commit 89213e9a97
7 changed files with 39 additions and 30 deletions

View File

@@ -13,7 +13,7 @@ class IServerSocket {
utils::Signal<PlayerID> OnDisconnect;
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
virtual void Send(PlayerID, const protocol::PacketBase&) = 0;
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
IServerSocket() {}
virtual ~IServerSocket() {}
@@ -25,6 +25,7 @@ class IServerSocket {
void OnConnectPeer(PeerID a_PeerId);
void OnDisconnectPeer(PeerID a_PeerId);
void OnReceivePeer(PeerID a_PeerId, const protocol::PacketBase& a_Packet);
virtual void SendPeer(PeerID a_PeerId, const protocol::PacketBase& a_Packet) = 0;
};
} // namespace server

View File

@@ -0,0 +1,20 @@
#pragma once
#include <server/IServerSocket.h>
namespace td {
namespace server {
class FakeSocket : public IServerSocket {
public:
FakeSocket() {}
~FakeSocket() {}
utils::Signal<PeerID, const protocol::PacketBase&> OnSend;
protected:
virtual void SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) override;
};
} // namespace server
} // namespace td

View File

@@ -1,19 +0,0 @@
#pragma once
#include <server/IServerSocket.h>
namespace td {
namespace server {
class TcpSocket : public IServerSocket {
private:
/* data */
public:
TcpSocket(/* args */) {}
~TcpSocket() {}
virtual void Send(PlayerID, const protocol::PacketBase&) {}
};
} // namespace server
} // namespace td

View File

@@ -16,7 +16,7 @@
#include <sp/io/StdIo.h>
#include <server/Server.h>
#include <server/socket/TcpSocket.h>
#include <server/socket/FakeSocket.h>
#include <server/state/LobbyState.h>
class WorldApply : public td::protocol::PacketHandler {
@@ -133,7 +133,7 @@ int main(int argc, char** argv) {
// server
auto socket = std::make_shared<td::server::TcpSocket>();
auto socket = std::make_shared<td::server::FakeSocket>();
td::server::Server server(socket);
server.UpdateState(std::make_shared<td::server::LobbyState>());
server.Update(1.0f);

View File

@@ -17,5 +17,9 @@ void IServerSocket::OnReceivePeer(PeerID a_PeerId, const protocol::PacketBase& a
OnReceive(m_Ids.GetPlayerId(a_PeerId), a_Packet);
}
void IServerSocket::Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet) {
SendPeer(m_Ids.GetPeerId(a_PlayerId), a_Packet);
}
} // namespace server
} // namespace td

View File

@@ -0,0 +1,11 @@
#include <server/socket/FakeSocket.h>
namespace td {
namespace server {
void FakeSocket::SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
OnSend(a_Peer, a_Packet);
}
} // namespace server
} // namespace td

View File

@@ -1,8 +0,0 @@
#include <server/socket/TcpSocket.h>
namespace td {
namespace server {
} // namespace server
} // namespace td