mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-24 08:01:06 +00:00
server: Client -> Peer
This commit is contained in:
parent
294e83d533
commit
8c52df6554
@ -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 {
|
||||
ByteOrder binary.ByteOrder
|
||||
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
|
||||
@ -34,12 +35,6 @@ func (pkt *Packet) ResetCursor() {
|
||||
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) {
|
||||
pkt.Buf = append(pkt.Buf, data...)
|
||||
pkt.cursor += len(data)
|
||||
|
@ -23,8 +23,8 @@ const (
|
||||
LOGIN_UPDATED_EUALA_REQUIRED = 9
|
||||
)
|
||||
|
||||
func (server *LoginServer) AcceptLogin(client *Client, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) {
|
||||
client.SzID = SzID
|
||||
func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) {
|
||||
peer.SzID = SzID
|
||||
|
||||
resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{
|
||||
SzID: SzID,
|
||||
@ -35,13 +35,13 @@ func (server *LoginServer) AcceptLogin(client *Client, SzID string, IClientVerC
|
||||
UiSvrTime: uint64(time.Now().Unix()),
|
||||
}
|
||||
|
||||
client.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC)
|
||||
client.E_key = protocol.CreateNewKey(
|
||||
peer.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC)
|
||||
peer.E_key = protocol.CreateNewKey(
|
||||
resp.UiSvrTime,
|
||||
uint64(resp.ICharCount+1),
|
||||
uint64(resp.ISlotNum+1),
|
||||
)
|
||||
client.FE_key = protocol.CreateNewKey(
|
||||
peer.FE_key = protocol.CreateNewKey(
|
||||
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
|
||||
uint64(IClientVerC),
|
||||
1,
|
||||
@ -49,23 +49,23 @@ func (server *LoginServer) AcceptLogin(client *Client, SzID string, IClientVerC
|
||||
|
||||
// send characters (if any)
|
||||
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
|
||||
pkt.Decode(&loginPkt)
|
||||
|
||||
SendError := func(e int32) {
|
||||
client.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{
|
||||
peer.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{
|
||||
IErrorCode: e,
|
||||
SzID: loginPkt.SzID,
|
||||
}, protocol.P_LS2CL_REP_LOGIN_FAIL)
|
||||
}
|
||||
|
||||
// client is resending a login packet??
|
||||
if client.AccountID != -1 {
|
||||
if peer.AccountID != -1 {
|
||||
SendError(LOGIN_ERROR)
|
||||
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
|
||||
client.AccountID = account.AccountID
|
||||
peer.AccountID = account.AccountID
|
||||
plrs, err := db.DefaultDB.GetPlayers(account.AccountID)
|
||||
if err != nil {
|
||||
SendError(LOGIN_DATABASE_ERROR)
|
||||
@ -121,25 +121,25 @@ func (server *LoginServer) Login(client *Client, pkt *protocol.Packet) {
|
||||
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
|
||||
pkt.Decode(&charPkt)
|
||||
|
||||
if client.AccountID == -1 {
|
||||
client.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL)
|
||||
if peer.AccountID == -1 {
|
||||
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!"))
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
client.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC{
|
||||
peer.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC{
|
||||
IPC_UID: int64(PlayerID),
|
||||
ISlotNum: charPkt.ISlotNum,
|
||||
IGender: charPkt.IGender,
|
||||
@ -174,28 +174,28 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo
|
||||
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
|
||||
pkt.Decode(&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!"))
|
||||
}
|
||||
|
||||
if err := db.DefaultDB.FinishPlayer(&charPkt, client.AccountID); err != nil {
|
||||
client.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
|
||||
if err := db.DefaultDB.FinishPlayer(&charPkt, peer.AccountID); err != nil {
|
||||
peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
plr, err := db.DefaultDB.GetPlayer(int(charPkt.PCStyle.IPC_UID))
|
||||
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)
|
||||
}
|
||||
|
||||
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),
|
||||
SPC_Style: PCStyle,
|
||||
SPC_Style2: PCStyle2,
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type LoginServer struct {
|
||||
listener net.Listener
|
||||
clients map[*Client]bool
|
||||
peers map[*Peer]bool
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ func NewLoginServer() *LoginServer {
|
||||
|
||||
return &LoginServer{
|
||||
listener: listener,
|
||||
clients: make(map[*Client]bool),
|
||||
peers: make(map[*Peer]bool),
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,41 +36,41 @@ func (server *LoginServer) Start() {
|
||||
return
|
||||
}
|
||||
|
||||
client := NewClient(server, conn)
|
||||
client := NewPeer(server, conn)
|
||||
server.Connect(client)
|
||||
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 {
|
||||
case protocol.P_CL2LS_REQ_LOGIN:
|
||||
server.Login(client, pkt)
|
||||
server.Login(peer, pkt)
|
||||
case protocol.P_CL2LS_REQ_CHECK_CHAR_NAME:
|
||||
var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME
|
||||
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,
|
||||
SzLastName: charPkt.SzLastName,
|
||||
}, protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC)
|
||||
case protocol.P_CL2LS_REQ_SAVE_CHAR_NAME:
|
||||
server.SaveCharacterName(client, pkt)
|
||||
server.SaveCharacterName(peer, pkt)
|
||||
case protocol.P_CL2LS_REQ_CHAR_CREATE:
|
||||
server.CharacterCreate(client, pkt)
|
||||
server.CharacterCreate(peer, pkt)
|
||||
default:
|
||||
log.Printf("[WARN] unsupported packet ID: %x\n", typeID)
|
||||
}
|
||||
}
|
||||
|
||||
func (server *LoginServer) Disconnect(client *Client) {
|
||||
func (server *LoginServer) Disconnect(peer *Peer) {
|
||||
server.lock.Lock()
|
||||
delete(server.clients, client)
|
||||
delete(server.peers, peer)
|
||||
server.lock.Unlock()
|
||||
}
|
||||
|
||||
func (server *LoginServer) Connect(client *Client) {
|
||||
func (server *LoginServer) Connect(peer *Peer) {
|
||||
server.lock.Lock()
|
||||
server.clients[client] = true
|
||||
server.peers[peer] = true
|
||||
server.lock.Unlock()
|
||||
}
|
||||
|
@ -15,26 +15,26 @@ const (
|
||||
USE_FE
|
||||
)
|
||||
|
||||
type ClientHandler interface {
|
||||
HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet)
|
||||
Connect(client *Client)
|
||||
Disconnect(client *Client)
|
||||
type PeerHandler interface {
|
||||
HandlePacket(client *Peer, typeID uint32, pkt *protocol.Packet)
|
||||
Connect(client *Peer)
|
||||
Disconnect(client *Peer)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
type Peer struct {
|
||||
E_key []byte
|
||||
FE_key []byte
|
||||
SzID string
|
||||
AccountID int
|
||||
Player *db.Player
|
||||
handler ClientHandler
|
||||
handler PeerHandler
|
||||
conn net.Conn
|
||||
alive bool
|
||||
whichKey int
|
||||
}
|
||||
|
||||
func NewClient(handler ClientHandler, conn net.Conn) *Client {
|
||||
return &Client{
|
||||
func NewPeer(handler PeerHandler, conn net.Conn) *Peer {
|
||||
return &Peer{
|
||||
E_key: []byte(protocol.DEFAULT_KEY),
|
||||
FE_key: nil,
|
||||
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
|
||||
pkt := protocol.NewPacket(make([]byte, 0))
|
||||
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 {
|
||||
return
|
||||
}
|
||||
@ -88,7 +88,7 @@ func (client *Client) Kill() {
|
||||
client.handler.Disconnect(client)
|
||||
}
|
||||
|
||||
func (client *Client) ClientHandler() {
|
||||
func (client *Peer) ClientHandler() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("Client %p panic'd! %v", client, err)
|
Loading…
Reference in New Issue
Block a user