god forgive me for this commit

This commit is contained in:
unknown 2023-06-25 01:51:21 -05:00
parent dfc00bcb52
commit f0b9bc6ed6
13 changed files with 327 additions and 96 deletions

View File

@ -33,6 +33,7 @@ var (
SHARD_PORT = 23001 SHARD_PORT = 23001
LOGIN_TIMEOUT = time.Second * 30 LOGIN_TIMEOUT = time.Second * 30
VIEW_DISTANCE = 25600
) )
func getEnv(key string, fallback string) string { func getEnv(key string, fallback string) string {

View File

@ -4,7 +4,7 @@ import (
"database/sql" "database/sql"
"github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/core" "github.com/CPunch/gopenfusion/core/entity"
"github.com/CPunch/gopenfusion/core/protocol" "github.com/CPunch/gopenfusion/core/protocol"
"github.com/blockloop/scan" "github.com/blockloop/scan"
) )
@ -121,8 +121,8 @@ const (
INNER JOIN Accounts as acc ON p.AccountID = acc.AccountID ` INNER JOIN Accounts as acc ON p.AccountID = acc.AccountID `
) )
func (db *DBHandler) readPlayer(rows *sql.Rows) (*core.Player, error) { func (db *DBHandler) readPlayer(rows *sql.Rows) (*entity.Player, error) {
plr := core.Player{ActiveNanoSlotNum: -1} plr := entity.Player{ActiveNanoSlotNum: -1}
if err := rows.Scan( if err := rows.Scan(
&plr.PlayerID, &plr.AccountID, &plr.Slot, &plr.PCStyle.SzFirstName, &plr.PCStyle.SzLastName, &plr.PlayerID, &plr.AccountID, &plr.Slot, &plr.PCStyle.SzFirstName, &plr.PCStyle.SzLastName,
@ -162,13 +162,13 @@ func (db *DBHandler) readPlayer(rows *sql.Rows) (*core.Player, error) {
return &plr, nil return &plr, nil
} }
func (db *DBHandler) GetPlayer(PlayerID int) (*core.Player, error) { func (db *DBHandler) GetPlayer(PlayerID int) (*entity.Player, error) {
rows, err := db.Query(QUERY_PLAYERS+"WHERE p.PlayerID = $1", PlayerID) rows, err := db.Query(QUERY_PLAYERS+"WHERE p.PlayerID = $1", PlayerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var plr *core.Player var plr *entity.Player
for rows.Next() { for rows.Next() {
plr, err = db.readPlayer(rows) plr, err = db.readPlayer(rows)
if err != nil { if err != nil {
@ -179,13 +179,13 @@ func (db *DBHandler) GetPlayer(PlayerID int) (*core.Player, error) {
return plr, nil return plr, nil
} }
func (db *DBHandler) GetPlayers(AccountID int) ([]core.Player, error) { func (db *DBHandler) GetPlayers(AccountID int) ([]entity.Player, error) {
rows, err := db.Query(QUERY_PLAYERS+"WHERE p.AccountID = $1", AccountID) rows, err := db.Query(QUERY_PLAYERS+"WHERE p.AccountID = $1", AccountID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var plrs []core.Player var plrs []entity.Player
for rows.Next() { for rows.Next() {
plr, err := db.readPlayer(rows) plr, err := db.readPlayer(rows)
if err != nil { if err != nil {

79
core/entity/chunk.go Normal file
View File

@ -0,0 +1,79 @@
package entity
import (
"log"
"sync"
"github.com/CPunch/gopenfusion/config"
)
type ChunkPosition struct {
X int
Y int
}
func makeChunkPosition(x, y int) ChunkPosition {
return ChunkPosition{
X: x / (config.VIEW_DISTANCE / 3),
Y: y / (config.VIEW_DISTANCE / 3),
}
}
type Chunk struct {
Position ChunkPosition
entities map[Entity]struct{}
lock sync.Mutex
}
func NewChunk(position ChunkPosition) *Chunk {
return &Chunk{
Position: position,
entities: make(map[Entity]struct{}),
}
}
func (c *Chunk) AddEntity(entity Entity) {
entity.SetChunk(c)
c.entities[entity] = struct{}{}
}
func (c *Chunk) RemoveEntity(entity Entity) {
entity.SetChunk(nil)
delete(c.entities, entity)
}
// send packet to all peers in this chunk and kill each peer if error
func (c *Chunk) SendPacket(typeID uint32, pkt ...interface{}) {
c.lock.Lock()
defer c.lock.Unlock()
for entity := range c.entities {
if entity.GetKind() != ENTITY_KIND_PLAYER {
continue
}
plr, ok := entity.(*Player)
if !ok {
log.Panic("Chunk.SendPacket: entity kind was player, but is not a *Player")
}
peer := plr.Peer
if err := peer.Send(typeID, pkt...); err != nil {
peer.Kill()
}
}
}
func (c *Chunk) GetAdjacentPositions() []ChunkPosition {
return []ChunkPosition{
{c.Position.X - 1, c.Position.Y - 1},
{c.Position.X - 1, c.Position.Y},
{c.Position.X - 1, c.Position.Y + 1},
{c.Position.X, c.Position.Y - 1},
{c.Position.X, c.Position.Y},
{c.Position.X, c.Position.Y + 1},
{c.Position.X + 1, c.Position.Y - 1},
{c.Position.X + 1, c.Position.Y},
{c.Position.X + 1, c.Position.Y + 1},
}
}

20
core/entity/entity.go Normal file
View File

@ -0,0 +1,20 @@
package entity
type EntityKind int
const (
ENTITY_KIND_PLAYER EntityKind = iota
ENTITY_KIND_NPC
)
type Entity interface {
GetKind() EntityKind
GetChunk() *Chunk
GetPosition() (x int, y int, z int)
GetAngle() int
SetChunk(chunk *Chunk)
SetPosition(x, y, z int)
SetAngle(angle int)
}

View File

@ -1,4 +1,4 @@
package core package entity
import ( import (
"github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/config"
@ -6,6 +6,8 @@ import (
) )
type Player struct { type Player struct {
Peer *protocol.CNPeer
CurrentChunk *Chunk
PlayerID int PlayerID int
AccountID int AccountID int
AccountLevel int AccountLevel int
@ -35,6 +37,38 @@ type Player struct {
CurrentMissionID int CurrentMissionID int
} }
// ==================== Entity interface ====================
func (plr *Player) GetKind() EntityKind {
return ENTITY_KIND_PLAYER
}
func (plr *Player) GetChunk() *Chunk {
return plr.CurrentChunk
}
func (plr *Player) GetPosition() (x int, y int, z int) {
return plr.X, plr.Y, plr.Z
}
func (plr *Player) GetAngle() int {
return plr.Angle
}
func (plr *Player) SetPosition(x, y, z int) {
plr.X = x
plr.Y = y
plr.Z = z
}
func (plr *Player) SetAngle(angle int) {
plr.Angle = angle
}
func (plr *Player) SetChunk(chunk *Chunk) {
plr.CurrentChunk = chunk
}
func (plr *Player) ToPCLoadData2CL() protocol.SPCLoadData2CL { func (plr *Player) ToPCLoadData2CL() protocol.SPCLoadData2CL {
return protocol.SPCLoadData2CL{ return protocol.SPCLoadData2CL{
IUserLevel: int16(plr.AccountLevel), IUserLevel: int16(plr.AccountLevel),

View File

@ -15,15 +15,10 @@ const (
USE_FE USE_FE
) )
type PeerHandler interface {
HandlePacket(peer *CNPeer, typeID uint32, pkt Packet) error
Disconnect(peer *CNPeer)
}
// CNPeer is a simple wrapper for net.Conn connections to send/recv packets over the Fusionfall packet protocol. // CNPeer is a simple wrapper for net.Conn connections to send/recv packets over the Fusionfall packet protocol.
type CNPeer struct { type CNPeer struct {
conn net.Conn conn net.Conn
handler PeerHandler eRecv chan *Event
SzID string SzID string
E_key []byte E_key []byte
FE_key []byte FE_key []byte
@ -33,10 +28,10 @@ type CNPeer struct {
alive bool alive bool
} }
func NewCNPeer(handler PeerHandler, conn net.Conn) *CNPeer { func NewCNPeer(eRecv chan *Event, conn net.Conn) *CNPeer {
return &CNPeer{ return &CNPeer{
conn: conn, conn: conn,
handler: handler, eRecv: eRecv,
SzID: "", SzID: "",
E_key: []byte(DEFAULT_KEY), E_key: []byte(DEFAULT_KEY),
FE_key: nil, FE_key: nil,
@ -93,13 +88,14 @@ func (peer *CNPeer) SetActiveKey(whichKey int) {
} }
func (peer *CNPeer) Kill() { func (peer *CNPeer) Kill() {
log.Printf("Killing peer %p", peer)
if !peer.alive { if !peer.alive {
return return
} }
peer.alive = false peer.alive = false
peer.conn.Close() peer.conn.Close()
peer.handler.Disconnect(peer) peer.eRecv <- &Event{Type: EVENT_CLIENT_DISCONNECT, Peer: peer}
} }
// meant to be invoked as a goroutine // meant to be invoked as a goroutine
@ -123,7 +119,6 @@ func (peer *CNPeer) Handler() {
// grab buffer && read packet body // grab buffer && read packet body
if err := func() error { if err := func() error {
buf := pool.Get() buf := pool.Get()
defer pool.Put(buf)
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil { if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
return fmt.Errorf("failed to read packet body! %v", err) return fmt.Errorf("failed to read packet body! %v", err)
} }
@ -132,18 +127,15 @@ func (peer *CNPeer) Handler() {
DecryptData(buf.Bytes(), peer.E_key) DecryptData(buf.Bytes(), peer.E_key)
pkt := NewPacket(buf) pkt := NewPacket(buf)
// create packet && read typeID // create packet && read pktID
var typeID uint32 var pktID uint32
if err := pkt.Decode(&typeID); err != nil { if err := pkt.Decode(&pktID); err != nil {
return fmt.Errorf("failed to read packet type! %v", err) return fmt.Errorf("failed to read packet type! %v", err)
} }
// dispatch packet // dispatch packet
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz) log.Printf("Got packet ID: %x, with a sizeof: %d\n", pktID, sz)
if err := peer.handler.HandlePacket(peer, typeID, pkt); err != nil { peer.eRecv <- &Event{Type: EVENT_CLIENT_PACKET, Peer: peer, Pkt: buf, PktID: pktID}
return err
}
return nil return nil
}(); err != nil { }(); err != nil {
log.Printf("[FATAL] %v", err) log.Printf("[FATAL] %v", err)

15
core/protocol/event.go Normal file
View File

@ -0,0 +1,15 @@
package protocol
import "bytes"
const (
EVENT_CLIENT_DISCONNECT = iota
EVENT_CLIENT_PACKET
)
type Event struct {
Type int
Peer *CNPeer
Pkt *bytes.Buffer
PktID uint32
}

View File

@ -217,6 +217,7 @@ func (server *LoginServer) CharacterCreate(peer *protocol.CNPeer, pkt protocol.P
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
if !validateCharacterCreation(&charPkt) { if !validateCharacterCreation(&charPkt) {
log.Printf("Invalid character creation packet: %+v", charPkt)
return SendFail(peer) return SendFail(peer)
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/core/db" "github.com/CPunch/gopenfusion/core/db"
"github.com/CPunch/gopenfusion/core/protocol" "github.com/CPunch/gopenfusion/core/protocol"
"github.com/CPunch/gopenfusion/core/protocol/pool"
"github.com/CPunch/gopenfusion/core/redis" "github.com/CPunch/gopenfusion/core/redis"
) )
@ -21,9 +22,10 @@ type LoginServer struct {
port int port int
dbHndlr *db.DBHandler dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler redisHndlr *redis.RedisHandler
packetHandlers map[uint32]PacketHandler eRecv chan *protocol.Event
peers map[*protocol.CNPeer]bool peers map[*protocol.CNPeer]bool
peersLock sync.Mutex packetHandlers map[uint32]PacketHandler
peerLock sync.Mutex
} }
func NewLoginServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*LoginServer, error) { func NewLoginServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*LoginServer, error) {
@ -38,6 +40,7 @@ func NewLoginServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port
dbHndlr: dbHndlr, dbHndlr: dbHndlr,
redisHndlr: redisHndlr, redisHndlr: redisHndlr,
peers: make(map[*protocol.CNPeer]bool), peers: make(map[*protocol.CNPeer]bool),
eRecv: make(chan *protocol.Event),
} }
server.packetHandlers = map[uint32]PacketHandler{ server.packetHandlers = map[uint32]PacketHandler{
@ -63,6 +66,7 @@ func NewLoginServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port
func (server *LoginServer) Start() { func (server *LoginServer) Start() {
log.Printf("Login service hosted on %s:%d\n", config.GetAnnounceIP(), server.port) log.Printf("Login service hosted on %s:%d\n", config.GetAnnounceIP(), server.port)
go server.handleEvents()
for { for {
conn, err := server.listener.Accept() conn, err := server.listener.Accept()
if err != nil { if err != nil {
@ -70,13 +74,32 @@ func (server *LoginServer) Start() {
return return
} }
client := protocol.NewCNPeer(server, conn) client := protocol.NewCNPeer(server.eRecv, conn)
server.Connect(client) server.connect(client)
go client.Handler() go client.Handler()
} }
} }
func (server *LoginServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error { func (server *LoginServer) handleEvents() {
for {
select {
case event := <-server.eRecv:
switch event.Type {
case protocol.EVENT_CLIENT_DISCONNECT:
server.disconnect(event.Peer)
case protocol.EVENT_CLIENT_PACKET:
defer pool.Put(event.Pkt)
if err := server.handlePacket(event.Peer, event.PktID, protocol.NewPacket(event.Pkt)); err != nil {
log.Printf("Error handling packet: %v", err)
event.Peer.Kill()
}
}
}
}
}
func (server *LoginServer) handlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error {
if hndlr, ok := server.packetHandlers[typeID]; ok { if hndlr, ok := server.packetHandlers[typeID]; ok {
if err := hndlr(peer, pkt); err != nil { if err := hndlr(peer, pkt); err != nil {
return err return err
@ -88,16 +111,18 @@ func (server *LoginServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pk
return nil return nil
} }
func (server *LoginServer) Disconnect(peer *protocol.CNPeer) { func (server *LoginServer) disconnect(peer *protocol.CNPeer) {
server.peersLock.Lock() server.peerLock.Lock()
defer server.peerLock.Unlock()
log.Printf("Peer %p disconnected from LOGIN\n", peer) log.Printf("Peer %p disconnected from LOGIN\n", peer)
delete(server.peers, peer) delete(server.peers, peer)
server.peersLock.Unlock()
} }
func (server *LoginServer) Connect(peer *protocol.CNPeer) { func (server *LoginServer) connect(peer *protocol.CNPeer) {
server.peersLock.Lock() server.peerLock.Lock()
defer server.peerLock.Unlock()
log.Printf("New peer %p connected to LOGIN\n", peer) log.Printf("New peer %p connected to LOGIN\n", peer)
server.peers[peer] = true server.peers[peer] = true
server.peersLock.Unlock()
} }

23
shard/chunks.go Normal file
View File

@ -0,0 +1,23 @@
package shard
import "github.com/CPunch/gopenfusion/core/entity"
func (server *ShardServer) getChunk(pos entity.ChunkPosition) *entity.Chunk {
chunk, ok := server.chunks[pos]
if !ok {
chunk = entity.NewChunk(pos)
server.chunks[pos] = chunk
}
return chunk
}
func (server *ShardServer) getViewableChunks(plr *entity.Player) []*entity.Chunk {
chunks := make([]*entity.Chunk, 0, 9)
for _, pos := range plr.GetChunk().GetAdjacentPositions() {
chunks = append(chunks, server.getChunk(pos))
}
return chunks
}

View File

@ -5,10 +5,28 @@ import (
"log" "log"
"time" "time"
"github.com/CPunch/gopenfusion/core" "github.com/CPunch/gopenfusion/core/entity"
"github.com/CPunch/gopenfusion/core/protocol" "github.com/CPunch/gopenfusion/core/protocol"
"github.com/CPunch/gopenfusion/core/redis"
) )
func (server *ShardServer) attachPlayer(peer *protocol.CNPeer, meta redis.LoginMetadata) (*entity.Player, error) {
// resending a shard enter packet?
old, err := server.getPlayer(peer)
if old != nil {
return nil, fmt.Errorf("resent enter packet!")
}
// attach player
plr, err := server.dbHndlr.GetPlayer(int(meta.PlayerID))
if err != nil {
return nil, err
}
server.setPlayer(peer, plr)
return plr, err
}
func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Packet) error { func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Packet) error {
var enter protocol.SP_CL2FE_REQ_PC_ENTER var enter protocol.SP_CL2FE_REQ_PC_ENTER
pkt.Decode(&enter) pkt.Decode(&enter)
@ -20,38 +38,23 @@ func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Pack
return err return err
} }
// attach player plr, err := server.attachPlayer(peer, loginData)
var resp *protocol.SP_FE2CL_REP_PC_ENTER_SUCC if err != nil {
if err := server.UpdatePlayer(peer, func(old *core.Player) (*core.Player, error) {
if old != nil { // resending a shard enter packet?
return nil, fmt.Errorf("resent enter packet!")
}
plr, err := server.dbHndlr.GetPlayer(int(loginData.PlayerID))
if err != nil {
return nil, err
}
resp = &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
IID: int32(plr.PlayerID),
PCLoadData2CL: plr.ToPCLoadData2CL(),
UiSvrTime: uint64(time.Now().Unix()),
}
return plr, nil
}); err != nil {
peer.Send(protocol.P_FE2CL_REP_PC_ENTER_FAIL, protocol.SP_FE2CL_REP_PC_ENTER_FAIL{})
return err return err
} }
// setup key resp := &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
peer.E_key = protocol.CreateNewKey(resp.UiSvrTime, uint64(resp.IID+1), uint64(resp.PCLoadData2CL.IFusionMatter+1)) IID: int32(plr.PlayerID),
peer.FE_key = loginData.FEKey PCLoadData2CL: plr.ToPCLoadData2CL(),
peer.SetActiveKey(protocol.USE_FE) UiSvrTime: uint64(time.Now().Unix()),
}
// setup peer // setup peer
peer.E_key = protocol.CreateNewKey(resp.UiSvrTime, uint64(resp.IID+1), uint64(resp.PCLoadData2CL.IFusionMatter+1))
peer.FE_key = loginData.FEKey
peer.PlayerID = loginData.PlayerID peer.PlayerID = loginData.PlayerID
peer.AccountID = loginData.AccountID peer.AccountID = loginData.AccountID
peer.SetActiveKey(protocol.USE_FE)
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)
@ -61,7 +64,7 @@ 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, err := server.LoadPlayer(peer) plr, err := server.getPlayer(peer)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1 +1,26 @@
package shard package shard
import (
"github.com/CPunch/gopenfusion/core/protocol"
)
func (server *ShardServer) updatePlayerPosition(peer *protocol.CNPeer, X, Y, Z, Angle int) error {
plr, err := server.getPlayer(peer)
if err != nil {
return err
}
plr.X = X
plr.Y = Y
plr.Z = Z
plr.Angle = Angle
return nil
}
func (server *ShardServer) playerMove(peer *protocol.CNPeer, pkt protocol.Packet) error {
var move protocol.SP_CL2FE_REQ_PC_MOVE
pkt.Decode(&move)
return server.updatePlayerPosition(peer, int(move.IX), int(move.IY), int(move.IZ), int(move.IAngle))
}

View File

@ -7,9 +7,10 @@ import (
"sync" "sync"
"github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/core"
"github.com/CPunch/gopenfusion/core/db" "github.com/CPunch/gopenfusion/core/db"
"github.com/CPunch/gopenfusion/core/entity"
"github.com/CPunch/gopenfusion/core/protocol" "github.com/CPunch/gopenfusion/core/protocol"
"github.com/CPunch/gopenfusion/core/protocol/pool"
"github.com/CPunch/gopenfusion/core/redis" "github.com/CPunch/gopenfusion/core/redis"
) )
@ -22,9 +23,11 @@ type ShardServer struct {
port int port int
dbHndlr *db.DBHandler dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler redisHndlr *redis.RedisHandler
eRecv chan *protocol.Event
packetHandlers map[uint32]PacketHandler packetHandlers map[uint32]PacketHandler
peersLock sync.Mutex peers map[*protocol.CNPeer]*entity.Player
peers map[*protocol.CNPeer]*core.Player chunks map[entity.ChunkPosition]*entity.Chunk
peerLock sync.Mutex
} }
func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) { func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
@ -39,6 +42,9 @@ func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port
dbHndlr: dbHndlr, dbHndlr: dbHndlr,
redisHndlr: redisHndlr, redisHndlr: redisHndlr,
packetHandlers: make(map[uint32]PacketHandler), packetHandlers: make(map[uint32]PacketHandler),
peers: make(map[*protocol.CNPeer]*entity.Player),
chunks: make(map[entity.ChunkPosition]*entity.Chunk),
eRecv: make(chan *protocol.Event),
} }
server.packetHandlers = map[uint32]PacketHandler{ server.packetHandlers = map[uint32]PacketHandler{
@ -54,13 +60,28 @@ func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port
return server, nil return server, nil
} }
func (server *ShardServer) RegisterPacketHandler(typeID uint32, hndlr PacketHandler) { func (server *ShardServer) handleEvents() {
server.packetHandlers[typeID] = hndlr for {
select {
case event := <-server.eRecv:
switch event.Type {
case protocol.EVENT_CLIENT_DISCONNECT:
server.disconnect(event.Peer)
case protocol.EVENT_CLIENT_PACKET:
defer pool.Put(event.Pkt)
log.Printf("Received packet %x from %p\n", event.PktID, event.Peer)
if err := server.handlePacket(event.Peer, event.PktID, protocol.NewPacket(event.Pkt)); err != nil {
event.Peer.Kill()
}
}
}
}
} }
func (server *ShardServer) Start() { func (server *ShardServer) Start() {
log.Printf("Shard service hosted on %s:%d\n", config.GetAnnounceIP(), server.port) log.Printf("Shard service hosted on %s:%d\n", config.GetAnnounceIP(), server.port)
go server.handleEvents()
for { for {
conn, err := server.listener.Accept() conn, err := server.listener.Accept()
if err != nil { if err != nil {
@ -68,8 +89,8 @@ func (server *ShardServer) Start() {
return return
} }
client := protocol.NewCNPeer(server, conn) client := protocol.NewCNPeer(server.eRecv, conn)
server.Connect(client) server.connect(client)
go client.Handler() go client.Handler()
} }
} }
@ -78,7 +99,7 @@ func (server *ShardServer) GetPort() int {
return server.port return server.port
} }
func (server *ShardServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error { func (server *ShardServer) handlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error {
if hndlr, ok := server.packetHandlers[typeID]; ok { if hndlr, ok := server.packetHandlers[typeID]; ok {
if err := hndlr(peer, pkt); err != nil { if err := hndlr(peer, pkt); err != nil {
return err return err
@ -90,47 +111,39 @@ func (server *ShardServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pk
return nil return nil
} }
func (server *ShardServer) Disconnect(peer *protocol.CNPeer) { func (server *ShardServer) disconnect(peer *protocol.CNPeer) {
server.peerLock.Lock()
defer server.peerLock.Unlock()
log.Printf("Peer %p disconnected from SHARD\n", peer) log.Printf("Peer %p disconnected from SHARD\n", peer)
delete(server.peers, peer) delete(server.peers, peer)
} }
func (server *ShardServer) Connect(peer *protocol.CNPeer) { func (server *ShardServer) connect(peer *protocol.CNPeer) {
server.peerLock.Lock()
defer server.peerLock.Unlock()
log.Printf("New peer %p connected to SHARD\n", peer) log.Printf("New peer %p connected to SHARD\n", peer)
server.peers[peer] = nil server.peers[peer] = nil
} }
// Returns a copy of the player func (server *ShardServer) getPlayer(peer *protocol.CNPeer) (*entity.Player, error) {
func (server *ShardServer) LoadPlayer(peer *protocol.CNPeer) (core.Player, error) {
plr, ok := server.peers[peer] plr, ok := server.peers[peer]
if !ok { if !ok {
return core.Player{}, fmt.Errorf("Player not found") return nil, fmt.Errorf("Player not found")
} }
return *plr, nil 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. func (server *ShardServer) setPlayer(peer *protocol.CNPeer, plr *entity.Player) {
// 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. server.peers[peer] = plr
// 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.peers[peer])
if err != nil {
return err
}
server.peers[peer] = new
return nil
} }
// If f returns false the iteration is stopped. // If f returns false the iteration is stopped.
func (server *ShardServer) RangePeers(f func(peer *protocol.CNPeer) bool) { func (server *ShardServer) rangePeers(f func(peer *protocol.CNPeer, plr *entity.Player) bool) {
for peer := range server.peers { for peer, plr := range server.peers {
if f(peer) { if f(peer, plr) {
return return
} }
} }