gopenfusion/shard/shardServer.go

151 lines
3.8 KiB
Go
Raw Normal View History

2023-03-27 22:02:13 +00:00
package shard
import (
"fmt"
"log"
"net"
"sync"
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/core/db"
2023-06-25 06:51:21 +00:00
"github.com/CPunch/gopenfusion/core/entity"
"github.com/CPunch/gopenfusion/core/protocol"
2023-06-25 06:51:21 +00:00
"github.com/CPunch/gopenfusion/core/protocol/pool"
"github.com/CPunch/gopenfusion/core/redis"
)
2023-03-27 22:02:13 +00:00
type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
func stubbedPacket(_ *protocol.CNPeer, _ protocol.Packet) error { /* stubbed */ return nil }
type ShardServer struct {
2023-06-25 03:36:04 +00:00
listener net.Listener
port int
dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler
2023-06-25 06:51:21 +00:00
eRecv chan *protocol.Event
2023-06-25 03:36:04 +00:00
packetHandlers map[uint32]PacketHandler
2023-06-25 06:51:21 +00:00
peers map[*protocol.CNPeer]*entity.Player
chunks map[entity.ChunkPosition]*entity.Chunk
peerLock sync.Mutex
}
func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
server := &ShardServer{
listener: listener,
port: port,
dbHndlr: dbHndlr,
redisHndlr: redisHndlr,
packetHandlers: make(map[uint32]PacketHandler),
2023-06-25 06:51:21 +00:00
peers: make(map[*protocol.CNPeer]*entity.Player),
chunks: make(map[entity.ChunkPosition]*entity.Chunk),
eRecv: make(chan *protocol.Event),
}
server.packetHandlers = map[uint32]PacketHandler{
protocol.P_CL2FE_REQ_PC_ENTER: server.RequestEnter,
protocol.P_CL2FE_REQ_PC_LOADING_COMPLETE: server.LoadingComplete,
}
redisHndlr.RegisterShard(redis.ShardMetadata{
IP: config.GetAnnounceIP(),
Port: port,
})
return server, nil
}
2023-06-25 06:51:21 +00:00
func (server *ShardServer) 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)
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() {
log.Printf("Shard service hosted on %s:%d\n", config.GetAnnounceIP(), server.port)
2023-06-25 06:51:21 +00:00
go server.handleEvents()
for {
conn, err := server.listener.Accept()
if err != nil {
log.Println("Connection error: ", err)
return
}
2023-06-25 06:51:21 +00:00
client := protocol.NewCNPeer(server.eRecv, conn)
server.connect(client)
go client.Handler()
}
}
2023-03-27 22:02:13 +00:00
func (server *ShardServer) GetPort() int {
return server.port
}
2023-06-25 06:51:21 +00:00
func (server *ShardServer) handlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error {
if hndlr, ok := server.packetHandlers[typeID]; ok {
if err := hndlr(peer, pkt); err != nil {
return err
}
} else {
log.Printf("[WARN] invalid packet ID: %x\n", typeID)
}
return nil
}
2023-06-25 06:51:21 +00:00
func (server *ShardServer) disconnect(peer *protocol.CNPeer) {
server.peerLock.Lock()
defer server.peerLock.Unlock()
log.Printf("Peer %p disconnected from SHARD\n", peer)
2023-06-25 03:36:04 +00:00
delete(server.peers, peer)
}
2023-06-25 06:51:21 +00:00
func (server *ShardServer) connect(peer *protocol.CNPeer) {
server.peerLock.Lock()
defer server.peerLock.Unlock()
log.Printf("New peer %p connected to SHARD\n", peer)
2023-06-25 03:36:04 +00:00
server.peers[peer] = nil
}
2023-06-25 06:51:21 +00:00
func (server *ShardServer) getPlayer(peer *protocol.CNPeer) (*entity.Player, error) {
2023-06-25 03:36:04 +00:00
plr, ok := server.peers[peer]
if !ok {
2023-06-25 06:51:21 +00:00
return nil, fmt.Errorf("Player not found")
}
2023-06-25 06:51:21 +00:00
return plr, nil
}
2023-06-25 06:51:21 +00:00
func (server *ShardServer) setPlayer(peer *protocol.CNPeer, plr *entity.Player) {
server.peers[peer] = plr
}
2023-06-25 03:36:04 +00:00
// If f returns false the iteration is stopped.
2023-06-25 06:51:21 +00:00
func (server *ShardServer) rangePeers(f func(peer *protocol.CNPeer, plr *entity.Player) bool) {
for peer, plr := range server.peers {
if f(peer, plr) {
2023-06-25 03:36:04 +00:00
return
}
2023-06-25 03:36:04 +00:00
}
}