mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-21 23:10:06 +00:00
server: refactoring; start support for variadic packets
This commit is contained in:
parent
d60638d555
commit
00fa94162c
@ -35,7 +35,7 @@ func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int3
|
|||||||
UiSvrTime: uint64(time.Now().Unix()),
|
UiSvrTime: uint64(time.Now().Unix()),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := peer.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC); err != nil {
|
if err := peer.Send(protocol.P_LS2CL_REP_LOGIN_SUCC, resp); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int3
|
|||||||
|
|
||||||
// send characters (if any)
|
// send characters (if any)
|
||||||
for i := 0; i < len(data); i++ {
|
for i := 0; i < len(data); i++ {
|
||||||
if err := peer.Send(&data[i], protocol.P_LS2CL_REP_CHAR_INFO); err != nil {
|
if err := peer.Send(protocol.P_LS2CL_REP_CHAR_INFO, &data[i]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,10 +66,10 @@ func (server *LoginServer) Login(peer *Peer, pkt protocol.Packet) error {
|
|||||||
pkt.Decode(&loginPkt)
|
pkt.Decode(&loginPkt)
|
||||||
|
|
||||||
SendError := func(e int32) {
|
SendError := func(e int32) {
|
||||||
peer.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{
|
peer.Send(protocol.P_LS2CL_REP_LOGIN_FAIL, &protocol.SP_LS2CL_REP_LOGIN_FAIL{
|
||||||
IErrorCode: e,
|
IErrorCode: e,
|
||||||
SzID: loginPkt.SzID,
|
SzID: loginPkt.SzID,
|
||||||
}, protocol.P_LS2CL_REP_LOGIN_FAIL)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// client is resending a login packet??
|
// client is resending a login packet??
|
||||||
@ -137,10 +137,10 @@ func (server *LoginServer) CheckCharacterName(peer *Peer, pkt protocol.Packet) e
|
|||||||
pkt.Decode(&charPkt)
|
pkt.Decode(&charPkt)
|
||||||
|
|
||||||
// just auto accept, client resends this data later
|
// just auto accept, client resends this data later
|
||||||
return peer.Send(&protocol.SP_LS2CL_REP_CHECK_CHAR_NAME_SUCC{
|
return peer.Send(protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC, &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)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *LoginServer) SaveCharacterName(peer *Peer, pkt protocol.Packet) error {
|
func (server *LoginServer) SaveCharacterName(peer *Peer, pkt protocol.Packet) error {
|
||||||
@ -148,24 +148,24 @@ func (server *LoginServer) SaveCharacterName(peer *Peer, pkt protocol.Packet) er
|
|||||||
pkt.Decode(&charPkt)
|
pkt.Decode(&charPkt)
|
||||||
|
|
||||||
if peer.AccountID == -1 {
|
if peer.AccountID == -1 {
|
||||||
peer.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL)
|
peer.Send(protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL, &protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{})
|
||||||
return fmt.Errorf("Out of order P_LS2CL_REP_SAVE_CHAR_NAME_FAIL!")
|
return fmt.Errorf("Out of order P_LS2CL_REP_SAVE_CHAR_NAME_FAIL!")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: sanity check SzFirstName && SzLastName
|
// TODO: sanity check SzFirstName && SzLastName
|
||||||
PlayerID, err := db.DefaultDB.NewPlayer(peer.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 {
|
||||||
peer.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL)
|
peer.Send(protocol.P_LS2CL_REP_SAVE_CHAR_NAME_FAIL, &protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_FAIL{})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return peer.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC{
|
return peer.Send(protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC, &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,
|
||||||
SzFirstName: charPkt.SzFirstName,
|
SzFirstName: charPkt.SzFirstName,
|
||||||
SzLastName: charPkt.SzLastName,
|
SzLastName: charPkt.SzLastName,
|
||||||
}, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) bool {
|
func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) bool {
|
||||||
@ -194,33 +194,40 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SendFail(peer *Peer) error {
|
||||||
|
if err := peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_FAIL, &protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{
|
||||||
|
IErrorCode: 2,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (server *LoginServer) CharacterCreate(peer *Peer, pkt protocol.Packet) error {
|
func (server *LoginServer) CharacterCreate(peer *Peer, pkt protocol.Packet) error {
|
||||||
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) {
|
||||||
peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
|
return SendFail(peer)
|
||||||
return fmt.Errorf("invalid SP_CL2LS_REQ_CHAR_CREATE!")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.DefaultDB.FinishPlayer(&charPkt, peer.AccountID); err != nil {
|
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)
|
return SendFail(peer)
|
||||||
return 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 {
|
||||||
peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
|
return SendFail(peer)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PCStyle, PCStyle2 := util.Player2PCStyle(plr)
|
PCStyle, PCStyle2 := util.Player2PCStyle(plr)
|
||||||
return peer.Send(&protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC{
|
return peer.Send(protocol.P_LS2CL_REP_CHAR_CREATE_SUCC, &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,
|
||||||
SOn_Item: charPkt.SOn_Item, // if items were faked, we don't really care since the db only stores the sanitized fields
|
SOn_Item: charPkt.SOn_Item, // if items were faked, we don't really care since the db only stores the sanitized fields
|
||||||
}, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *LoginServer) CharacterDelete(peer *Peer, pkt protocol.Packet) error {
|
func (server *LoginServer) CharacterDelete(peer *Peer, pkt protocol.Packet) error {
|
||||||
@ -229,13 +236,12 @@ func (server *LoginServer) CharacterDelete(peer *Peer, pkt protocol.Packet) erro
|
|||||||
|
|
||||||
slot, err := db.DefaultDB.DeletePlayer(int(charPkt.IPC_UID), peer.AccountID)
|
slot, err := db.DefaultDB.DeletePlayer(int(charPkt.IPC_UID), peer.AccountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
|
return SendFail(peer)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return peer.Send(&protocol.SP_LS2CL_REP_CHAR_DELETE_SUCC{
|
return peer.Send(protocol.P_LS2CL_REP_CHAR_DELETE_SUCC, &protocol.SP_LS2CL_REP_CHAR_DELETE_SUCC{
|
||||||
ISlotNum: int8(slot),
|
ISlotNum: int8(slot),
|
||||||
}, protocol.P_LS2CL_REP_CHAR_DELETE_SUCC)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *LoginServer) FinishTutorial(peer *Peer, pkt protocol.Packet) error {
|
func (server *LoginServer) FinishTutorial(peer *Peer, pkt protocol.Packet) error {
|
||||||
@ -243,8 +249,7 @@ func (server *LoginServer) FinishTutorial(peer *Peer, pkt protocol.Packet) error
|
|||||||
pkt.Decode(&charPkt)
|
pkt.Decode(&charPkt)
|
||||||
|
|
||||||
if err := db.DefaultDB.FinishTutorial(int(charPkt.IPC_UID), peer.AccountID); err != nil {
|
if err := db.DefaultDB.FinishTutorial(int(charPkt.IPC_UID), peer.AccountID); err != nil {
|
||||||
peer.Send(&protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{IErrorCode: 2}, protocol.P_LS2CL_REP_SHARD_SELECT_FAIL)
|
return SendFail(peer)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// no response
|
// no response
|
||||||
|
@ -63,7 +63,7 @@ func (server *LoginServer) Start() {
|
|||||||
|
|
||||||
client := NewPeer(server, conn)
|
client := NewPeer(server, conn)
|
||||||
server.Connect(client)
|
server.Connect(client)
|
||||||
go client.ClientHandler()
|
go client.Handler()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,56 +49,64 @@ func NewPeer(handler PeerHandler, conn net.Conn) *Peer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Peer) Send(data interface{}, typeID uint32) error {
|
func (peer *Peer) Send(typeID uint32, data ...interface{}) error {
|
||||||
buf := pool.Get()
|
buf := pool.Get()
|
||||||
defer func() { // always return the buffer to the pool
|
defer pool.Put(buf) // always return the buffer to the pool
|
||||||
pool.Put(buf)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// encode
|
// body start
|
||||||
pkt := protocol.NewPacket(buf)
|
pkt := protocol.NewPacket(buf)
|
||||||
|
|
||||||
// write the typeID and packet body
|
// encode type id
|
||||||
pkt.Encode(uint32(typeID))
|
if err := pkt.Encode(uint32(typeID)); err != nil {
|
||||||
pkt.Encode(data)
|
return err
|
||||||
|
|
||||||
// write the packet size
|
|
||||||
binary.Write(client.conn, binary.LittleEndian, uint32(buf.Len()))
|
|
||||||
|
|
||||||
// encrypt typeID & body
|
|
||||||
switch client.whichKey {
|
|
||||||
case USE_E:
|
|
||||||
protocol.EncryptData(buf.Bytes(), client.E_key)
|
|
||||||
case USE_FE:
|
|
||||||
protocol.EncryptData(buf.Bytes(), client.FE_key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write packet type && packet body
|
// encode data
|
||||||
|
for _, trailer := range data {
|
||||||
|
if err := pkt.Encode(trailer); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// encrypt body
|
||||||
|
switch peer.whichKey {
|
||||||
|
case USE_E:
|
||||||
|
protocol.EncryptData(buf.Bytes(), peer.E_key)
|
||||||
|
case USE_FE:
|
||||||
|
protocol.EncryptData(buf.Bytes(), peer.FE_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// write packet size
|
||||||
|
if err := binary.Write(peer.conn, binary.LittleEndian, uint32(buf.Len())); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// write packet body
|
||||||
log.Printf("Sending %#v, sizeof: %d", data, buf.Len())
|
log.Printf("Sending %#v, sizeof: %d", data, buf.Len())
|
||||||
if _, err := client.conn.Write(buf.Bytes()); err != nil {
|
if _, err := peer.conn.Write(buf.Bytes()); err != nil {
|
||||||
return fmt.Errorf("[FATAL] failed to write packet body! %v", err)
|
return fmt.Errorf("[FATAL] failed to write packet body! %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Peer) Kill() {
|
func (peer *Peer) Kill() {
|
||||||
if !client.alive {
|
if !peer.alive {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.alive = false
|
peer.alive = false
|
||||||
client.conn.Close()
|
peer.conn.Close()
|
||||||
client.handler.Disconnect(client)
|
peer.handler.Disconnect(peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Peer) ClientHandler() {
|
func (peer *Peer) Handler() {
|
||||||
defer client.Kill()
|
defer peer.Kill()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// read packet size
|
// read packet size, the goroutine spends most of it's time parked here
|
||||||
var sz uint32
|
var sz uint32
|
||||||
if err := binary.Read(client.conn, binary.LittleEndian, &sz); err != nil {
|
if err := binary.Read(peer.conn, binary.LittleEndian, &sz); err != nil {
|
||||||
log.Printf("[FATAL] failed to read packet size! %v\n", err)
|
log.Printf("[FATAL] failed to read packet size! %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -111,17 +119,17 @@ func (client *Peer) ClientHandler() {
|
|||||||
|
|
||||||
// read packet body
|
// read packet body
|
||||||
buf := pool.Get()
|
buf := pool.Get()
|
||||||
if _, err := buf.ReadFrom(io.LimitReader(client.conn, int64(sz))); err != nil {
|
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
|
||||||
log.Printf("[FATAL] failed to read packet body! %v", err)
|
log.Printf("[FATAL] failed to read packet body! %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// decrypt
|
// decrypt
|
||||||
protocol.DecryptData(buf.Bytes(), client.E_key)
|
protocol.DecryptData(buf.Bytes(), peer.E_key)
|
||||||
|
pkt := protocol.NewPacket(buf)
|
||||||
|
|
||||||
// create packet && read typeID
|
// create packet && read typeID
|
||||||
var typeID uint32
|
var typeID uint32
|
||||||
pkt := protocol.NewPacket(buf)
|
|
||||||
if err := pkt.Decode(&typeID); err != nil {
|
if err := pkt.Decode(&typeID); err != nil {
|
||||||
log.Printf("[FATAL] failed to read packet type! %v", err)
|
log.Printf("[FATAL] failed to read packet type! %v", err)
|
||||||
return
|
return
|
||||||
@ -129,7 +137,7 @@ func (client *Peer) ClientHandler() {
|
|||||||
|
|
||||||
// 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", typeID, sz)
|
||||||
if err := client.handler.HandlePacket(client, typeID, pkt); err != nil {
|
if err := peer.handler.HandlePacket(peer, typeID, pkt); err != nil {
|
||||||
log.Printf("[FATAL] %v", err)
|
log.Printf("[FATAL] %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user