server: Client -> Peer

This commit is contained in:
CPunch 2023-03-09 16:42:13 -06:00
parent 294e83d533
commit 8c52df6554
4 changed files with 49 additions and 54 deletions

View File

@ -10,13 +10,14 @@ import (
) )
/* /*
this file handles serializing (and deserializing) structs to alignment-strict c structures this file handles serializing (and deserializing) structs to alignment-strict c structures generated via `tools/genstructs.py`.
see script for details on usage!
*/ */
type Packet struct { type Packet struct {
ByteOrder binary.ByteOrder ByteOrder binary.ByteOrder
Buf []byte Buf []byte
cursor int // to keep track of things like member alignment cursor int // to keep track of things like member alignment for easier debugging
} }
const PACK_ALIGN = 4 const PACK_ALIGN = 4
@ -34,12 +35,6 @@ func (pkt *Packet) ResetCursor() {
pkt.cursor = 0 pkt.cursor = 0
} }
func (pkt *Packet) CompletePacket(typeID uint32) {
tmp := make([]byte, 4)
pkt.ByteOrder.PutUint32(tmp, typeID)
pkt.Buf = append(tmp, pkt.Buf...)
}
func (pkt *Packet) writeRaw(data []byte) { func (pkt *Packet) writeRaw(data []byte) {
pkt.Buf = append(pkt.Buf, data...) pkt.Buf = append(pkt.Buf, data...)
pkt.cursor += len(data) pkt.cursor += len(data)

View File

@ -23,8 +23,8 @@ const (
LOGIN_UPDATED_EUALA_REQUIRED = 9 LOGIN_UPDATED_EUALA_REQUIRED = 9
) )
func (server *LoginServer) AcceptLogin(client *Client, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) { func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) {
client.SzID = SzID peer.SzID = SzID
resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{ resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{
SzID: SzID, SzID: SzID,
@ -35,13 +35,13 @@ func (server *LoginServer) AcceptLogin(client *Client, SzID string, IClientVerC
UiSvrTime: uint64(time.Now().Unix()), UiSvrTime: uint64(time.Now().Unix()),
} }
client.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC) peer.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC)
client.E_key = protocol.CreateNewKey( peer.E_key = protocol.CreateNewKey(
resp.UiSvrTime, resp.UiSvrTime,
uint64(resp.ICharCount+1), uint64(resp.ICharCount+1),
uint64(resp.ISlotNum+1), uint64(resp.ISlotNum+1),
) )
client.FE_key = protocol.CreateNewKey( peer.FE_key = protocol.CreateNewKey(
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)), binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
uint64(IClientVerC), uint64(IClientVerC),
1, 1,
@ -49,23 +49,23 @@ func (server *LoginServer) AcceptLogin(client *Client, SzID string, IClientVerC
// send characters (if any) // send characters (if any)
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
client.Send(&data[i], protocol.P_LS2CL_REP_CHAR_INFO) peer.Send(&data[i], protocol.P_LS2CL_REP_CHAR_INFO)
} }
} }
func (server *LoginServer) Login(client *Client, pkt *protocol.Packet) { func (server *LoginServer) Login(peer *Peer, pkt *protocol.Packet) {
var loginPkt protocol.SP_CL2LS_REQ_LOGIN var loginPkt protocol.SP_CL2LS_REQ_LOGIN
pkt.Decode(&loginPkt) pkt.Decode(&loginPkt)
SendError := func(e int32) { SendError := func(e int32) {
client.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{ peer.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{
IErrorCode: e, IErrorCode: e,
SzID: loginPkt.SzID, SzID: loginPkt.SzID,
}, protocol.P_LS2CL_REP_LOGIN_FAIL) }, protocol.P_LS2CL_REP_LOGIN_FAIL)
} }
// client is resending a login packet?? // client is resending a login packet??
if client.AccountID != -1 { if peer.AccountID != -1 {
SendError(LOGIN_ERROR) SendError(LOGIN_ERROR)
panic(fmt.Errorf("Out of order P_CL2LS_REQ_LOGIN!")) panic(fmt.Errorf("Out of order P_CL2LS_REQ_LOGIN!"))
} }
@ -90,7 +90,7 @@ func (server *LoginServer) Login(client *Client, pkt *protocol.Packet) {
} }
// grab player data // grab player data
client.AccountID = account.AccountID peer.AccountID = account.AccountID
plrs, err := db.DefaultDB.GetPlayers(account.AccountID) plrs, err := db.DefaultDB.GetPlayers(account.AccountID)
if err != nil { if err != nil {
SendError(LOGIN_DATABASE_ERROR) SendError(LOGIN_DATABASE_ERROR)
@ -121,25 +121,25 @@ func (server *LoginServer) Login(client *Client, pkt *protocol.Packet) {
charInfo = append(charInfo, info) charInfo = append(charInfo, info)
} }
server.AcceptLogin(client, loginPkt.SzID, loginPkt.IClientVerC, 1, charInfo) server.AcceptLogin(peer, loginPkt.SzID, loginPkt.IClientVerC, 1, charInfo)
} }
func (server *LoginServer) SaveCharacterName(client *Client, pkt *protocol.Packet) { func (server *LoginServer) SaveCharacterName(peer *Peer, pkt *protocol.Packet) {
var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
if client.AccountID == -1 { if peer.AccountID == -1 {
client.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL) peer.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL)
panic(fmt.Errorf("Out of order P_LS2CL_REP_SAVE_CHAR_NAME_FAIL!")) panic(fmt.Errorf("Out of order P_LS2CL_REP_SAVE_CHAR_NAME_FAIL!"))
} }
PlayerID, err := db.DefaultDB.NewPlayer(client.AccountID, charPkt.SzFirstName, charPkt.SzLastName, int(charPkt.ISlotNum)) PlayerID, err := db.DefaultDB.NewPlayer(peer.AccountID, charPkt.SzFirstName, charPkt.SzLastName, int(charPkt.ISlotNum))
if err != nil { if err != nil {
client.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL) peer.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL)
panic(err) panic(err)
} }
client.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC{ peer.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC{
IPC_UID: int64(PlayerID), IPC_UID: int64(PlayerID),
ISlotNum: charPkt.ISlotNum, ISlotNum: charPkt.ISlotNum,
IGender: charPkt.IGender, IGender: charPkt.IGender,
@ -174,28 +174,28 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo
return true return true
} }
func (server *LoginServer) CharacterCreate(client *Client, pkt *protocol.Packet) { func (server *LoginServer) CharacterCreate(peer *Peer, pkt *protocol.Packet) {
var charPkt protocol.SP_CL2LS_REQ_CHAR_CREATE var charPkt protocol.SP_CL2LS_REQ_CHAR_CREATE
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
if !validateCharacterCreation(&charPkt) { if !validateCharacterCreation(&charPkt) {
client.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL) peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
panic(fmt.Errorf("invalid SP_CL2LS_REQ_CHAR_CREATE!")) panic(fmt.Errorf("invalid SP_CL2LS_REQ_CHAR_CREATE!"))
} }
if err := db.DefaultDB.FinishPlayer(&charPkt, client.AccountID); err != nil { if err := db.DefaultDB.FinishPlayer(&charPkt, peer.AccountID); err != nil {
client.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL) peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
panic(err) panic(err)
} }
plr, err := db.DefaultDB.GetPlayer(int(charPkt.PCStyle.IPC_UID)) plr, err := db.DefaultDB.GetPlayer(int(charPkt.PCStyle.IPC_UID))
if err != nil { if err != nil {
client.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL) peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
panic(err) panic(err)
} }
PCStyle, PCStyle2 := util.Player2PCStyle(plr) PCStyle, PCStyle2 := util.Player2PCStyle(plr)
client.Send(&protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC{ peer.Send(&protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC{
ILevel: int16(plr.Level), ILevel: int16(plr.Level),
SPC_Style: PCStyle, SPC_Style: PCStyle,
SPC_Style2: PCStyle2, SPC_Style2: PCStyle2,

View File

@ -10,7 +10,7 @@ import (
type LoginServer struct { type LoginServer struct {
listener net.Listener listener net.Listener
clients map[*Client]bool peers map[*Peer]bool
lock sync.Mutex lock sync.Mutex
} }
@ -22,7 +22,7 @@ func NewLoginServer() *LoginServer {
return &LoginServer{ return &LoginServer{
listener: listener, listener: listener,
clients: make(map[*Client]bool), peers: make(map[*Peer]bool),
} }
} }
@ -36,41 +36,41 @@ func (server *LoginServer) Start() {
return return
} }
client := NewClient(server, conn) client := NewPeer(server, conn)
server.Connect(client) server.Connect(client)
go client.ClientHandler() go client.ClientHandler()
} }
} }
func (server *LoginServer) HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet) { func (server *LoginServer) HandlePacket(peer *Peer, typeID uint32, pkt *protocol.Packet) {
switch typeID { switch typeID {
case protocol.P_CL2LS_REQ_LOGIN: case protocol.P_CL2LS_REQ_LOGIN:
server.Login(client, pkt) server.Login(peer, pkt)
case protocol.P_CL2LS_REQ_CHECK_CHAR_NAME: case protocol.P_CL2LS_REQ_CHECK_CHAR_NAME:
var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
client.Send(&protocol.SP_LS2CL_REP_CHECK_CHAR_NAME_SUCC{ peer.Send(&protocol.SP_LS2CL_REP_CHECK_CHAR_NAME_SUCC{
SzFirstName: charPkt.SzFirstName, SzFirstName: charPkt.SzFirstName,
SzLastName: charPkt.SzLastName, SzLastName: charPkt.SzLastName,
}, protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC) }, protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC)
case protocol.P_CL2LS_REQ_SAVE_CHAR_NAME: case protocol.P_CL2LS_REQ_SAVE_CHAR_NAME:
server.SaveCharacterName(client, pkt) server.SaveCharacterName(peer, pkt)
case protocol.P_CL2LS_REQ_CHAR_CREATE: case protocol.P_CL2LS_REQ_CHAR_CREATE:
server.CharacterCreate(client, pkt) server.CharacterCreate(peer, pkt)
default: default:
log.Printf("[WARN] unsupported packet ID: %x\n", typeID) log.Printf("[WARN] unsupported packet ID: %x\n", typeID)
} }
} }
func (server *LoginServer) Disconnect(client *Client) { func (server *LoginServer) Disconnect(peer *Peer) {
server.lock.Lock() server.lock.Lock()
delete(server.clients, client) delete(server.peers, peer)
server.lock.Unlock() server.lock.Unlock()
} }
func (server *LoginServer) Connect(client *Client) { func (server *LoginServer) Connect(peer *Peer) {
server.lock.Lock() server.lock.Lock()
server.clients[client] = true server.peers[peer] = true
server.lock.Unlock() server.lock.Unlock()
} }

View File

@ -15,26 +15,26 @@ const (
USE_FE USE_FE
) )
type ClientHandler interface { type PeerHandler interface {
HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet) HandlePacket(client *Peer, typeID uint32, pkt *protocol.Packet)
Connect(client *Client) Connect(client *Peer)
Disconnect(client *Client) Disconnect(client *Peer)
} }
type Client struct { type Peer struct {
E_key []byte E_key []byte
FE_key []byte FE_key []byte
SzID string SzID string
AccountID int AccountID int
Player *db.Player Player *db.Player
handler ClientHandler handler PeerHandler
conn net.Conn conn net.Conn
alive bool alive bool
whichKey int whichKey int
} }
func NewClient(handler ClientHandler, conn net.Conn) *Client { func NewPeer(handler PeerHandler, conn net.Conn) *Peer {
return &Client{ return &Peer{
E_key: []byte(protocol.DEFAULT_KEY), E_key: []byte(protocol.DEFAULT_KEY),
FE_key: nil, FE_key: nil,
SzID: "", SzID: "",
@ -47,7 +47,7 @@ func NewClient(handler ClientHandler, conn net.Conn) *Client {
} }
} }
func (client *Client) Send(data interface{}, typeID uint32) { func (client *Peer) Send(data interface{}, typeID uint32) {
// encode // encode
pkt := protocol.NewPacket(make([]byte, 0)) pkt := protocol.NewPacket(make([]byte, 0))
pkt.Encode(data) pkt.Encode(data)
@ -78,7 +78,7 @@ func (client *Client) Send(data interface{}, typeID uint32) {
} }
} }
func (client *Client) Kill() { func (client *Peer) Kill() {
if !client.alive { if !client.alive {
return return
} }
@ -88,7 +88,7 @@ func (client *Client) Kill() {
client.handler.Disconnect(client) client.handler.Disconnect(client)
} }
func (client *Client) ClientHandler() { func (client *Peer) ClientHandler() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Printf("Client %p panic'd! %v", client, err) log.Printf("Client %p panic'd! %v", client, err)