mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-25 00:21:05 +00:00
shardServer: simplified peer map logic
This commit is contained in:
parent
1da82ac750
commit
dfc00bcb52
@ -28,6 +28,7 @@ type CNPeer struct {
|
|||||||
E_key []byte
|
E_key []byte
|
||||||
FE_key []byte
|
FE_key []byte
|
||||||
AccountID int
|
AccountID int
|
||||||
|
PlayerID int32
|
||||||
whichKey int
|
whichKey int
|
||||||
alive bool
|
alive bool
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,10 @@ func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Pack
|
|||||||
peer.FE_key = loginData.FEKey
|
peer.FE_key = loginData.FEKey
|
||||||
peer.SetActiveKey(protocol.USE_FE)
|
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)
|
log.Printf("Player %d (AccountID %d) entered\n", resp.IID, loginData.AccountID)
|
||||||
return peer.Send(protocol.P_FE2CL_REP_PC_ENTER_SUCC, resp)
|
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
|
var loadComplete protocol.SP_CL2FE_REQ_PC_LOADING_COMPLETE
|
||||||
pkt.Decode(&loadComplete)
|
pkt.Decode(&loadComplete)
|
||||||
|
|
||||||
plr := server.LoadPlayer(peer)
|
plr, err := server.LoadPlayer(peer)
|
||||||
if plr == nil {
|
if err != nil {
|
||||||
return fmt.Errorf("peer has no player attached!")
|
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)})
|
return peer.Send(protocol.P_FE2CL_REP_PC_LOADING_COMPLETE_SUCC, protocol.SP_FE2CL_REP_PC_LOADING_COMPLETE_SUCC{IPC_ID: int32(plr.PlayerID)})
|
||||||
|
@ -23,9 +23,8 @@ type ShardServer struct {
|
|||||||
dbHndlr *db.DBHandler
|
dbHndlr *db.DBHandler
|
||||||
redisHndlr *redis.RedisHandler
|
redisHndlr *redis.RedisHandler
|
||||||
packetHandlers map[uint32]PacketHandler
|
packetHandlers map[uint32]PacketHandler
|
||||||
loginMetadataQueue sync.Map // [int64]*LoginMetadata w/ int64 = serialKey
|
|
||||||
peersLock sync.Mutex
|
peersLock sync.Mutex
|
||||||
peers sync.Map // [*protocol.CNPeer]core.Player
|
peers map[*protocol.CNPeer]*core.Player
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
|
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) {
|
func (server *ShardServer) Disconnect(peer *protocol.CNPeer) {
|
||||||
log.Printf("Peer %p disconnected from SHARD\n", peer)
|
log.Printf("Peer %p disconnected from SHARD\n", peer)
|
||||||
server.peers.Delete(peer)
|
delete(server.peers, peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *ShardServer) Connect(peer *protocol.CNPeer) {
|
func (server *ShardServer) Connect(peer *protocol.CNPeer) {
|
||||||
log.Printf("New peer %p connected to SHARD\n", peer)
|
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 {
|
// Returns a copy of the player
|
||||||
val, ok := server.peers.Load(peer)
|
func (server *ShardServer) LoadPlayer(peer *protocol.CNPeer) (core.Player, error) {
|
||||||
|
plr, ok := server.peers[peer]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return core.Player{}, fmt.Errorf("Player not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
plr, ok := val.(*core.Player)
|
return *plr, nil
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return plr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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.
|
// 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. This function should NOT be called recursively.
|
||||||
// The pointers new and old may be the same if you are just updating struct fields.
|
|
||||||
func (server *ShardServer) UpdatePlayer(peer *protocol.CNPeer, f func(old *core.Player) (new *core.Player, err error)) error {
|
func (server *ShardServer) UpdatePlayer(peer *protocol.CNPeer, f func(old *core.Player) (new *core.Player, err error)) error {
|
||||||
server.peersLock.Lock()
|
server.peersLock.Lock()
|
||||||
defer server.peersLock.Unlock()
|
defer server.peersLock.Unlock()
|
||||||
|
|
||||||
// on fail, the player should not be stored
|
// on fail, the player should not be stored
|
||||||
new, err := f(server.LoadPlayer(peer))
|
new, err := f(server.peers[peer])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
server.storePlayer(peer, new)
|
server.peers[peer] = new
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *ShardServer) storePlayer(peer *protocol.CNPeer, player *core.Player) {
|
// If f returns false the iteration is stopped.
|
||||||
server.peers.Store(peer, player)
|
func (server *ShardServer) RangePeers(f func(peer *protocol.CNPeer) bool) {
|
||||||
}
|
for peer := range server.peers {
|
||||||
|
if f(peer) {
|
||||||
// Simple wrapper for server.peers.Range, if f returns false the iteration is stopped.
|
return
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user