shardServer: simplified peer map logic

This commit is contained in:
unknown 2023-06-24 22:36:04 -05:00
parent 1da82ac750
commit dfc00bcb52
3 changed files with 31 additions and 44 deletions

View File

@ -28,6 +28,7 @@ type CNPeer struct {
E_key []byte
FE_key []byte
AccountID int
PlayerID int32
whichKey int
alive bool
}

View File

@ -49,6 +49,10 @@ func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Pack
peer.FE_key = loginData.FEKey
peer.SetActiveKey(protocol.USE_FE)
// setup peer
peer.PlayerID = loginData.PlayerID
peer.AccountID = loginData.AccountID
log.Printf("Player %d (AccountID %d) entered\n", resp.IID, loginData.AccountID)
return peer.Send(protocol.P_FE2CL_REP_PC_ENTER_SUCC, resp)
}
@ -57,9 +61,9 @@ func (server *ShardServer) LoadingComplete(peer *protocol.CNPeer, pkt protocol.P
var loadComplete protocol.SP_CL2FE_REQ_PC_LOADING_COMPLETE
pkt.Decode(&loadComplete)
plr := server.LoadPlayer(peer)
if plr == nil {
return fmt.Errorf("peer has no player attached!")
plr, err := server.LoadPlayer(peer)
if err != nil {
return err
}
return peer.Send(protocol.P_FE2CL_REP_PC_LOADING_COMPLETE_SUCC, protocol.SP_FE2CL_REP_PC_LOADING_COMPLETE_SUCC{IPC_ID: int32(plr.PlayerID)})

View File

@ -18,14 +18,13 @@ type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
func stubbedPacket(_ *protocol.CNPeer, _ protocol.Packet) error { /* stubbed */ return nil }
type ShardServer struct {
listener net.Listener
port int
dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler
packetHandlers map[uint32]PacketHandler
loginMetadataQueue sync.Map // [int64]*LoginMetadata w/ int64 = serialKey
peersLock sync.Mutex
peers sync.Map // [*protocol.CNPeer]core.Player
listener net.Listener
port int
dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler
packetHandlers map[uint32]PacketHandler
peersLock sync.Mutex
peers map[*protocol.CNPeer]*core.Player
}
func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
@ -93,63 +92,46 @@ func (server *ShardServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pk
func (server *ShardServer) Disconnect(peer *protocol.CNPeer) {
log.Printf("Peer %p disconnected from SHARD\n", peer)
server.peers.Delete(peer)
delete(server.peers, peer)
}
func (server *ShardServer) Connect(peer *protocol.CNPeer) {
log.Printf("New peer %p connected to SHARD\n", peer)
server.peers.Store(peer, nil)
server.peers[peer] = nil
}
func (server *ShardServer) LoadPlayer(peer *protocol.CNPeer) *core.Player {
val, ok := server.peers.Load(peer)
// Returns a copy of the player
func (server *ShardServer) LoadPlayer(peer *protocol.CNPeer) (core.Player, error) {
plr, ok := server.peers[peer]
if !ok {
return nil
return core.Player{}, fmt.Errorf("Player not found")
}
plr, ok := val.(*core.Player)
if !ok {
return nil
}
return plr
return *plr, nil
}
// UpdatePlayer locks the peers map, and calls the provided callback. The returned new pointer will be stored, however if an error returns it will be passed back.
// Since it is UNSAFE to write to the returned pointer from LoadPlayer, this wrapper is for the cases that state in the player struct needs to be updated.
// TODO: maybe LoadPlayer should return a player by value instead?
// The pointers new and old may be the same if you are just updating struct fields.
// The pointers new and old may be the same if you are just updating struct fields. This function should NOT be called recursively.
func (server *ShardServer) UpdatePlayer(peer *protocol.CNPeer, f func(old *core.Player) (new *core.Player, err error)) error {
server.peersLock.Lock()
defer server.peersLock.Unlock()
// on fail, the player should not be stored
new, err := f(server.LoadPlayer(peer))
new, err := f(server.peers[peer])
if err != nil {
return err
}
server.storePlayer(peer, new)
server.peers[peer] = new
return nil
}
func (server *ShardServer) storePlayer(peer *protocol.CNPeer, player *core.Player) {
server.peers.Store(peer, player)
}
// Simple wrapper for server.peers.Range, if f returns false the iteration is stopped.
func (server *ShardServer) RangePeers(f func(peer *protocol.CNPeer, player *core.Player) bool) {
server.peers.Range(func(key, value any) bool { // simple wrapper to cast the datatypes
peer, ok := key.(*protocol.CNPeer)
if !ok { // this should never happen
panic(fmt.Errorf("ShardServer.peers has an invalid key: peers[%#v] = %#v", key, value))
// If f returns false the iteration is stopped.
func (server *ShardServer) RangePeers(f func(peer *protocol.CNPeer) bool) {
for peer := range server.peers {
if f(peer) {
return
}
player, ok := value.(*core.Player)
if !ok { // this should also never happen
panic(fmt.Errorf("ShardServer.peers has an invalid value: peers[%#v] = %#v", key, value))
}
return f(peer, player)
})
}
}