moved internal/protocol/cnpeer to cnpeer

also started a util package
This commit is contained in:
CPunch 2023-12-01 15:29:19 -06:00
parent 0ed19ad6c5
commit e355af19ab
12 changed files with 93 additions and 77 deletions

View File

@ -1,4 +1,4 @@
package protocol
package cnpeer
import (
"bytes"
@ -8,7 +8,8 @@ import (
"io"
"net"
"sync/atomic"
"time"
"github.com/CPunch/gopenfusion/internal/protocol"
)
const (
@ -37,10 +38,6 @@ type CNPeer struct {
FE_key []byte
}
func GetTime() uint64 {
return uint64(time.Now().UnixMilli())
}
func NewCNPeer(ctx context.Context, conn net.Conn) *CNPeer {
p := &CNPeer{
conn: conn,
@ -48,7 +45,7 @@ func NewCNPeer(ctx context.Context, conn net.Conn) *CNPeer {
whichKey: USE_E,
alive: &atomic.Bool{},
E_key: []byte(DEFAULT_KEY),
E_key: []byte(protocol.DEFAULT_KEY),
FE_key: nil,
}
@ -65,14 +62,14 @@ func (peer *CNPeer) UserData() interface{} {
func (peer *CNPeer) Send(typeID uint32, data ...interface{}) error {
// grab buffer from pool
buf := GetBuffer()
defer PutBuffer(buf)
buf := protocol.GetBuffer()
defer protocol.PutBuffer(buf)
// allocate space for packet size
buf.Write(make([]byte, 4))
// body start
pkt := NewPacket(buf)
pkt := protocol.NewPacket(buf)
// encode type id
if err := pkt.Encode(typeID); err != nil {
@ -97,7 +94,7 @@ func (peer *CNPeer) Send(typeID uint32, data ...interface{}) error {
case USE_FE:
key = peer.FE_key
}
EncryptData(buf.Bytes()[4:], key)
protocol.EncryptData(buf.Bytes()[4:], key)
// send full packet
// log.Printf("Sending %#v, sizeof: %d, buffer: %v", data, buf.Len(), buf.Bytes())
@ -140,19 +137,19 @@ func (peer *CNPeer) Handler(eRecv chan<- *PacketEvent) error {
}
// client should never send a packet size outside of this range
if sz > CN_PACKET_BUFFER_SIZE || sz < 4 {
if sz > protocol.CN_PACKET_BUFFER_SIZE || sz < 4 {
return fmt.Errorf("invalid packet size: %d", sz)
}
// grab buffer && read packet body
buf := GetBuffer()
buf := protocol.GetBuffer()
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
return fmt.Errorf("failed to read packet body: %v", err)
}
// decrypt
DecryptData(buf.Bytes(), peer.E_key)
pkt := NewPacket(buf)
protocol.DecryptData(buf.Bytes(), peer.E_key)
pkt := protocol.NewPacket(buf)
// create packet && read pktID
var pktID uint32

View File

@ -1,6 +1,6 @@
package entity
import "github.com/CPunch/gopenfusion/internal/protocol"
import "github.com/CPunch/gopenfusion/cnpeer"
type EntityKind int
@ -20,6 +20,6 @@ type Entity interface {
SetPosition(x, y, z int)
SetAngle(angle int)
DisappearFromViewOf(peer *protocol.CNPeer)
EnterIntoViewOf(peer *protocol.CNPeer)
DisappearFromViewOf(peer *cnpeer.CNPeer)
EnterIntoViewOf(peer *cnpeer.CNPeer)
}

View File

@ -3,6 +3,7 @@ package entity
import (
"sync/atomic"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/internal/protocol"
)
@ -62,13 +63,13 @@ func (npc *NPC) SetAngle(angle int) {
npc.Angle = angle
}
func (npc *NPC) DisappearFromViewOf(peer *protocol.CNPeer) {
func (npc *NPC) DisappearFromViewOf(peer *cnpeer.CNPeer) {
peer.Send(protocol.P_FE2CL_NPC_EXIT, protocol.SP_FE2CL_NPC_EXIT{
INPC_ID: int32(npc.ID),
})
}
func (npc *NPC) EnterIntoViewOf(peer *protocol.CNPeer) {
func (npc *NPC) EnterIntoViewOf(peer *cnpeer.CNPeer) {
peer.Send(protocol.P_FE2CL_NPC_NEW, protocol.SP_FE2CL_NPC_NEW{
NPCAppearanceData: npc.GetAppearanceData(),
})

View File

@ -1,17 +1,18 @@
package entity
import (
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/protocol"
)
type Player struct {
db.Player
Peer *protocol.CNPeer
Peer *cnpeer.CNPeer
Chunk ChunkPosition
}
func NewPlayer(peer *protocol.CNPeer, player *db.Player) *Player {
func NewPlayer(peer *cnpeer.CNPeer, player *db.Player) *Player {
return &Player{
Player: *player,
Peer: peer,
@ -51,13 +52,13 @@ func (plr *Player) SetAngle(angle int) {
plr.Angle = angle
}
func (plr *Player) DisappearFromViewOf(peer *protocol.CNPeer) {
func (plr *Player) DisappearFromViewOf(peer *cnpeer.CNPeer) {
peer.Send(protocol.P_FE2CL_PC_EXIT, protocol.SP_FE2CL_PC_EXIT{
IID: int32(plr.PlayerID),
})
}
func (plr *Player) EnterIntoViewOf(peer *protocol.CNPeer) {
func (plr *Player) EnterIntoViewOf(peer *cnpeer.CNPeer) {
peer.Send(protocol.P_FE2CL_PC_NEW, protocol.SP_FE2CL_PC_NEW{
PCAppearanceData: plr.GetAppearanceData(),
})

View File

@ -10,13 +10,14 @@ import (
"strconv"
"sync"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/internal/protocol"
)
type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
type PacketHandler func(peer *cnpeer.CNPeer, pkt protocol.Packet) error
func StubbedPacket(_ *protocol.CNPeer, _ protocol.Packet) error {
func StubbedPacket(_ *cnpeer.CNPeer, _ protocol.Packet) error {
return nil
}
@ -28,18 +29,18 @@ type Service struct {
started chan struct{}
stopped chan struct{}
packetHandlers map[uint32]PacketHandler
peers map[chan *protocol.PacketEvent]*protocol.CNPeer
peers map[chan *cnpeer.PacketEvent]*cnpeer.CNPeer
stateLock sync.Mutex
// OnDisconnect is called when a peer disconnects from the service.
// uData is the stored value of the key/value pair in the peer map.
// It may not be set while the service is running. (eg. srvc.Start() has been called)
OnDisconnect func(peer *protocol.CNPeer)
OnDisconnect func(peer *cnpeer.CNPeer)
// OnConnect is called when a peer connects to the service.
// return value is used as the value in the peer map.
// It may not be set while the service is running. (eg. srvc.Start() has been called)
OnConnect func(peer *protocol.CNPeer)
OnConnect func(peer *cnpeer.CNPeer)
}
func RandomPort() (int, error) {
@ -69,7 +70,7 @@ func NewService(ctx context.Context, name string, port int) *Service {
func (srvc *Service) Reset(ctx context.Context) {
srvc.ctx = ctx
srvc.packetHandlers = make(map[uint32]PacketHandler)
srvc.peers = make(map[chan *protocol.PacketEvent]*protocol.CNPeer)
srvc.peers = make(map[chan *cnpeer.PacketEvent]*cnpeer.CNPeer)
srvc.started = make(chan struct{})
srvc.stopped = make(chan struct{})
}
@ -80,8 +81,8 @@ func (srvc *Service) AddPacketHandler(pktID uint32, handler PacketHandler) {
}
type newPeerConnection struct {
peer *protocol.CNPeer
channel chan *protocol.PacketEvent
peer *cnpeer.CNPeer
channel chan *cnpeer.PacketEvent
}
func (srvc *Service) Start() error {
@ -112,22 +113,22 @@ func (srvc *Service) Start() error {
}
// create a new peer and pass it to the event loop
peer := protocol.NewCNPeer(srvc.ctx, conn)
eRecv := make(chan *protocol.PacketEvent)
peer := cnpeer.NewCNPeer(srvc.ctx, conn)
eRecv := make(chan *cnpeer.PacketEvent)
peerConnections <- newPeerConnection{channel: eRecv, peer: peer}
go peer.Handler(eRecv)
}
}
func (srvc *Service) getPeer(channel chan *protocol.PacketEvent) *protocol.CNPeer {
func (srvc *Service) getPeer(channel chan *cnpeer.PacketEvent) *cnpeer.CNPeer {
return srvc.peers[channel]
}
func (srvc *Service) setPeer(channel chan *protocol.PacketEvent, peer *protocol.CNPeer) {
func (srvc *Service) setPeer(channel chan *cnpeer.PacketEvent, peer *cnpeer.CNPeer) {
srvc.peers[channel] = peer
}
func (srvc *Service) removePeer(channel chan *protocol.PacketEvent) {
func (srvc *Service) removePeer(channel chan *cnpeer.PacketEvent) {
delete(srvc.peers, channel)
}
@ -147,7 +148,7 @@ func (srvc *Service) Stopped() <-chan struct{} {
// if f returns false, the iteration is stopped.
// NOTE: the peer map is not locked while iterating, if you're calling this
// outside of the service's event loop, you'll need to lock the peer map yourself.
func (srvc *Service) RangePeers(f func(peer *protocol.CNPeer) bool) {
func (srvc *Service) RangePeers(f func(peer *cnpeer.CNPeer) bool) {
for _, peer := range srvc.peers {
if !f(peer) {
break
@ -167,7 +168,7 @@ func (srvc *Service) Unlock() {
func (srvc *Service) stop() {
// OnDisconnect handler might need to do something important
srvc.RangePeers(func(peer *protocol.CNPeer) bool {
srvc.RangePeers(func(peer *cnpeer.CNPeer) bool {
peer.Kill()
if srvc.OnDisconnect != nil {
srvc.OnDisconnect(peer)
@ -196,7 +197,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) {
Chan: reflect.ValueOf(peerPipe),
})
addPoll := func(channel chan *protocol.PacketEvent) {
addPoll := func(channel chan *cnpeer.PacketEvent) {
poll = append(poll, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(channel),
@ -221,7 +222,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) {
addPoll(evnt.channel)
srvc.connect(evnt.channel, evnt.peer)
default: // peer event
channel := poll[chosen].Chan.Interface().(chan *protocol.PacketEvent)
channel := poll[chosen].Chan.Interface().(chan *cnpeer.PacketEvent)
peer := srvc.getPeer(channel)
if peer == nil {
log.Printf("Unknown peer event: %v", value)
@ -229,7 +230,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) {
continue
}
evnt, ok := value.Interface().(*protocol.PacketEvent)
evnt, ok := value.Interface().(*cnpeer.PacketEvent)
if !recvOK || !ok || evnt == nil {
// peer disconnected, remove it from our poll queue
removePoll(chosen)
@ -250,7 +251,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) {
}
}
func (srvc *Service) handlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error {
func (srvc *Service) handlePacket(peer *cnpeer.CNPeer, typeID uint32, pkt protocol.Packet) error {
if hndlr, ok := srvc.packetHandlers[typeID]; ok {
// fmt.Printf("Handling packet %x\n", typeID)
if err := hndlr(peer, pkt); err != nil {
@ -263,7 +264,7 @@ func (srvc *Service) handlePacket(peer *protocol.CNPeer, typeID uint32, pkt prot
return nil
}
func (srvc *Service) disconnect(channel chan *protocol.PacketEvent, peer *protocol.CNPeer) {
func (srvc *Service) disconnect(channel chan *cnpeer.PacketEvent, peer *cnpeer.CNPeer) {
log.Printf("Peer %p disconnected from %s\n", peer, srvc.Name)
if srvc.OnDisconnect != nil {
srvc.OnDisconnect(peer)
@ -272,7 +273,7 @@ func (srvc *Service) disconnect(channel chan *protocol.PacketEvent, peer *protoc
srvc.removePeer(channel)
}
func (srvc *Service) connect(channel chan *protocol.PacketEvent, peer *protocol.CNPeer) {
func (srvc *Service) connect(channel chan *cnpeer.PacketEvent, peer *cnpeer.CNPeer) {
log.Printf("New peer %p connected to %s\n", peer, srvc.Name)
if srvc.OnConnect != nil {
srvc.OnConnect(peer)

View File

@ -10,6 +10,7 @@ import (
"testing"
"time"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/service"
"github.com/matryer/is"
@ -67,7 +68,7 @@ func TestService(t *testing.T) {
// our dummy packet handler
wg.Add(maxDummyPeers)
srvc.AddPacketHandler(0x1234, func(peer *protocol.CNPeer, pkt protocol.Packet) error {
srvc.AddPacketHandler(0x1234, func(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
log.Printf("Received packet %#v", pkt)
wg.Done()
return nil
@ -75,12 +76,12 @@ func TestService(t *testing.T) {
// wait for all dummy peers to connect and disconnect
wg.Add(maxDummyPeers)
srvc.OnConnect = func(peer *protocol.CNPeer) {
srvc.OnConnect = func(peer *cnpeer.CNPeer) {
wg.Done()
}
wg.Add(maxDummyPeers)
srvc.OnDisconnect = func(peer *protocol.CNPeer) {
srvc.OnDisconnect = func(peer *cnpeer.CNPeer) {
wg.Done()
}
@ -95,7 +96,7 @@ func TestService(t *testing.T) {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", srvcPort))
is.NoErr(err) // net.Dial error
peer := protocol.NewCNPeer(ctx, conn)
peer := cnpeer.NewCNPeer(ctx, conn)
go func() {
defer peer.Kill()
@ -106,7 +107,7 @@ func TestService(t *testing.T) {
}()
// we wait until Handler gracefully exits (peer was killed)
peer.Handler(make(chan *protocol.PacketEvent))
peer.Handler(make(chan *cnpeer.PacketEvent))
wg.Done()
}()
}

View File

@ -7,10 +7,12 @@ import (
"log"
"math/rand"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/util"
)
const (
@ -25,14 +27,14 @@ const (
LOGIN_UPDATED_EUALA_REQUIRED = 9
)
func (server *LoginServer) AcceptLogin(peer *protocol.CNPeer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) error {
func (server *LoginServer) AcceptLogin(peer *cnpeer.CNPeer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) error {
resp := protocol.SP_LS2CL_REP_LOGIN_SUCC{
SzID: SzID,
ICharCount: int8(len(data)),
ISlotNum: ISlotNum,
IPaymentFlag: 1,
IOpenBetaFlag: 0,
UiSvrTime: protocol.GetTime(),
UiSvrTime: util.GetTime(),
}
if err := peer.Send(protocol.P_LS2CL_REP_LOGIN_SUCC, resp); err != nil {
@ -61,7 +63,7 @@ func (server *LoginServer) AcceptLogin(peer *protocol.CNPeer, SzID string, IClie
return nil
}
func (server *LoginServer) Login(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *LoginServer) Login(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var loginPkt protocol.SP_CL2LS_REQ_LOGIN
pkt.Decode(&loginPkt)
@ -137,7 +139,7 @@ func (server *LoginServer) Login(peer *protocol.CNPeer, pkt protocol.Packet) err
return server.AcceptLogin(peer, loginPkt.SzID, loginPkt.IClientVerC, 1, charInfo[:len(plrs)])
}
func (server *LoginServer) CheckCharacterName(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *LoginServer) CheckCharacterName(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME
pkt.Decode(&charPkt)
@ -148,7 +150,7 @@ func (server *LoginServer) CheckCharacterName(peer *protocol.CNPeer, pkt protoco
})
}
func (server *LoginServer) SaveCharacterName(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *LoginServer) SaveCharacterName(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME
pkt.Decode(&charPkt)
@ -201,7 +203,7 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo
return true
}
func SendFail(peer *protocol.CNPeer) error {
func SendFail(peer *cnpeer.CNPeer) error {
if err := peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_FAIL, protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{
IErrorCode: 2,
}); err != nil {
@ -211,7 +213,7 @@ func SendFail(peer *protocol.CNPeer) error {
return nil
}
func (server *LoginServer) CharacterCreate(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *LoginServer) CharacterCreate(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var charPkt protocol.SP_CL2LS_REQ_CHAR_CREATE
pkt.Decode(&charPkt)
@ -244,7 +246,7 @@ func (server *LoginServer) CharacterCreate(peer *protocol.CNPeer, pkt protocol.P
})
}
func (server *LoginServer) CharacterDelete(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *LoginServer) CharacterDelete(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var charPkt protocol.SP_CL2LS_REQ_CHAR_DELETE
pkt.Decode(&charPkt)
@ -263,7 +265,7 @@ func (server *LoginServer) CharacterDelete(peer *protocol.CNPeer, pkt protocol.P
})
}
func (server *LoginServer) ShardSelect(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *LoginServer) ShardSelect(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var selection protocol.SP_CL2LS_REQ_CHAR_SELECT
pkt.Decode(&selection)
@ -319,7 +321,7 @@ func (server *LoginServer) ShardSelect(peer *protocol.CNPeer, pkt protocol.Packe
return peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_SUCC, resp)
}
func (server *LoginServer) FinishTutorial(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *LoginServer) FinishTutorial(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_TUTOR
pkt.Decode(&charPkt)

View File

@ -3,11 +3,12 @@ package shard
import (
"fmt"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/internal/entity"
"github.com/CPunch/gopenfusion/internal/protocol"
)
func (server *ShardServer) freeChat(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) freeChat(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var chat protocol.SP_CL2FE_REQ_SEND_FREECHAT_MESSAGE
pkt.Decode(&chat)
@ -24,7 +25,7 @@ func (server *ShardServer) freeChat(peer *protocol.CNPeer, pkt protocol.Packet)
})
}
func (server *ShardServer) menuChat(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) menuChat(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var chat protocol.SP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE
pkt.Decode(&chat)
@ -41,7 +42,7 @@ func (server *ShardServer) menuChat(peer *protocol.CNPeer, pkt protocol.Packet)
})
}
func (server *ShardServer) emoteChat(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) emoteChat(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var chat protocol.SP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT
pkt.Decode(&chat)

View File

@ -4,12 +4,14 @@ import (
"fmt"
"log"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/internal/entity"
"github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/util"
)
func (server *ShardServer) attachPlayer(peer *protocol.CNPeer, meta redis.LoginMetadata) (*entity.Player, error) {
func (server *ShardServer) attachPlayer(peer *cnpeer.CNPeer, meta redis.LoginMetadata) (*entity.Player, error) {
dbPlr, err := server.dbHndlr.GetPlayer(int(meta.PlayerID))
if err != nil {
return nil, err
@ -24,7 +26,7 @@ func (server *ShardServer) attachPlayer(peer *protocol.CNPeer, meta redis.LoginM
return plr, nil
}
func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) RequestEnter(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var enter protocol.SP_CL2FE_REQ_PC_ENTER
pkt.Decode(&enter)
@ -49,13 +51,13 @@ func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Pack
resp := &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
IID: int32(plr.PlayerID),
PCLoadData2CL: plr.ToPCLoadData2CL(),
UiSvrTime: protocol.GetTime(),
UiSvrTime: util.GetTime(),
}
// setup peer
peer.E_key = protocol.CreateNewKey(resp.UiSvrTime, uint64(resp.IID+1), uint64(resp.PCLoadData2CL.IFusionMatter+1))
peer.FE_key = loginData.FEKey
peer.SetActiveKey(protocol.USE_FE)
peer.SetActiveKey(cnpeer.USE_FE)
log.Printf("Player %d (AccountID %d) entered\n", resp.IID, loginData.AccountID)
if err := peer.Send(protocol.P_FE2CL_REP_PC_ENTER_SUCC, resp); err != nil {
@ -65,7 +67,7 @@ func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Pack
return nil
}
func (server *ShardServer) LoadingComplete(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) LoadingComplete(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var loadComplete protocol.SP_CL2FE_REQ_PC_LOADING_COMPLETE
pkt.Decode(&loadComplete)

View File

@ -3,8 +3,10 @@ package shard
import (
"fmt"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/internal/entity"
"github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/util"
)
func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Angle int) {
@ -15,7 +17,7 @@ func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Ang
server.updateEntityChunk(plr, plr.GetChunkPos(), entity.MakeChunkPosition(X, Y))
}
func (server *ShardServer) playerMove(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) playerMove(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var move protocol.SP_CL2FE_REQ_PC_MOVE
pkt.Decode(&move)
@ -39,11 +41,11 @@ func (server *ShardServer) playerMove(peer *protocol.CNPeer, pkt protocol.Packet
CKeyValue: move.CKeyValue,
ISpeed: move.ISpeed,
IID: int32(plr.PlayerID),
ISvrTime: protocol.GetTime(),
ISvrTime: util.GetTime(),
})
}
func (server *ShardServer) playerStop(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) playerStop(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var stop protocol.SP_CL2FE_REQ_PC_STOP
pkt.Decode(&stop)
@ -61,11 +63,11 @@ func (server *ShardServer) playerStop(peer *protocol.CNPeer, pkt protocol.Packet
IY: stop.IY,
IZ: stop.IZ,
IID: int32(plr.PlayerID),
ISvrTime: protocol.GetTime(),
ISvrTime: util.GetTime(),
})
}
func (server *ShardServer) playerJump(peer *protocol.CNPeer, pkt protocol.Packet) error {
func (server *ShardServer) playerJump(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
var jump protocol.SP_CL2FE_REQ_PC_JUMP
pkt.Decode(&jump)
@ -89,6 +91,6 @@ func (server *ShardServer) playerJump(peer *protocol.CNPeer, pkt protocol.Packet
CKeyValue: jump.CKeyValue,
ISpeed: jump.ISpeed,
IID: int32(plr.PlayerID),
ISvrTime: protocol.GetTime(),
ISvrTime: util.GetTime(),
})
}

View File

@ -3,6 +3,7 @@ package shard
import (
"context"
"github.com/CPunch/gopenfusion/cnpeer"
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/entity"
@ -11,7 +12,7 @@ import (
"github.com/CPunch/gopenfusion/internal/service"
)
type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
type PacketHandler func(peer *cnpeer.CNPeer, pkt protocol.Packet) error
type ShardServer struct {
service *service.Service
@ -55,7 +56,7 @@ func (server *ShardServer) Start() {
server.service.Start()
}
func (server *ShardServer) onDisconnect(peer *protocol.CNPeer) {
func (server *ShardServer) onDisconnect(peer *cnpeer.CNPeer) {
// remove from chunks
plr, ok := peer.UserData().(*entity.Player)
if ok && plr != nil {
@ -63,6 +64,6 @@ func (server *ShardServer) onDisconnect(peer *protocol.CNPeer) {
}
}
func (server *ShardServer) onConnect(peer *protocol.CNPeer) {
func (server *ShardServer) onConnect(peer *cnpeer.CNPeer) {
}

7
util/util.go Normal file
View File

@ -0,0 +1,7 @@
package util
import "time"
func GetTime() uint64 {
return uint64(time.Now().UnixMilli())
}