mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-14 03:50:05 +00:00
refactoring; moved client to protocol package
This commit is contained in:
parent
4c7696dc92
commit
1fff485f93
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
@ -6,8 +6,6 @@ import (
|
||||
"log"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/CPunch/GopenFusion/protocol"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -15,26 +13,31 @@ const (
|
||||
USE_FE
|
||||
)
|
||||
|
||||
type ClientHandler interface {
|
||||
HandlePacket(client *Client, typeID uint32, pkt *Packet)
|
||||
Disconnect(client *Client)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
server Server
|
||||
handler ClientHandler
|
||||
conn net.Conn
|
||||
e_key []byte
|
||||
fe_key []byte
|
||||
whichKey int
|
||||
}
|
||||
|
||||
func newClient(server Server, conn net.Conn) *Client {
|
||||
func NewClient(handler ClientHandler, conn net.Conn) *Client {
|
||||
return &Client{
|
||||
server: server,
|
||||
handler: handler,
|
||||
conn: conn,
|
||||
e_key: []byte(protocol.DEFAULT_KEY),
|
||||
e_key: []byte(DEFAULT_KEY),
|
||||
whichKey: USE_E,
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) Send(data interface{}, typeID uint32) {
|
||||
// encode
|
||||
pkt := protocol.NewPacket(make([]byte, 0))
|
||||
pkt := NewPacket(make([]byte, 0))
|
||||
pkt.Encode(data)
|
||||
log.Printf("Sending %#v, sizeof: %d", data, len(pkt.Buf))
|
||||
|
||||
@ -52,9 +55,9 @@ func (client *Client) Send(data interface{}, typeID uint32) {
|
||||
// encrypt typeID & body
|
||||
switch client.whichKey {
|
||||
case USE_E:
|
||||
protocol.EncryptData(tmp, client.e_key)
|
||||
EncryptData(tmp, client.e_key)
|
||||
case USE_FE:
|
||||
protocol.EncryptData(tmp, client.fe_key)
|
||||
EncryptData(tmp, client.fe_key)
|
||||
}
|
||||
|
||||
// write packet body
|
||||
@ -63,8 +66,8 @@ func (client *Client) Send(data interface{}, typeID uint32) {
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) {
|
||||
resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{
|
||||
func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, data []SP_LS2CL_REP_CHAR_INFO) {
|
||||
resp := &SP_LS2CL_REP_LOGIN_SUCC{
|
||||
SzID: SZID,
|
||||
ICharCount: int8(len(data)),
|
||||
ISlotNum: ISlotNum,
|
||||
@ -73,21 +76,21 @@ func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8,
|
||||
UiSvrTime: uint64(time.Now().Unix()),
|
||||
}
|
||||
|
||||
client.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC)
|
||||
client.e_key = protocol.CreateNewKey(
|
||||
client.Send(resp, P_LS2CL_REP_LOGIN_SUCC)
|
||||
client.e_key = CreateNewKey(
|
||||
resp.UiSvrTime,
|
||||
uint64(resp.ICharCount+1),
|
||||
uint64(resp.ISlotNum+1),
|
||||
)
|
||||
client.fe_key = protocol.CreateNewKey(
|
||||
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
|
||||
client.fe_key = CreateNewKey(
|
||||
binary.LittleEndian.Uint64([]byte(DEFAULT_KEY)),
|
||||
uint64(IClientVerC),
|
||||
1,
|
||||
)
|
||||
|
||||
// send characters (if any)
|
||||
for i := 0; i < len(data); i++ {
|
||||
client.Send(data[i], protocol.P_LS2CL_REP_CHAR_INFO)
|
||||
client.Send(data[i], P_LS2CL_REP_CHAR_INFO)
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,10 +100,10 @@ func (client *Client) ClientHandler() {
|
||||
log.Printf("Client %p panic'd! %v", client, err)
|
||||
}
|
||||
client.conn.Close()
|
||||
client.server.Disconnect(client)
|
||||
client.handler.Disconnect(client)
|
||||
}()
|
||||
|
||||
tmp := make([]byte, 4, protocol.CN_PACKET_BUFFER_SIZE)
|
||||
tmp := make([]byte, 4, CN_PACKET_BUFFER_SIZE)
|
||||
for {
|
||||
// read packet size
|
||||
if _, err := client.conn.Read(tmp); err != nil {
|
||||
@ -109,7 +112,7 @@ func (client *Client) ClientHandler() {
|
||||
sz := int(binary.LittleEndian.Uint32(tmp))
|
||||
|
||||
// client should never send a packet size outside of this range
|
||||
if sz > protocol.CN_PACKET_BUFFER_SIZE || sz < 4 {
|
||||
if sz > CN_PACKET_BUFFER_SIZE || sz < 4 {
|
||||
panic(fmt.Errorf("[FATAL] malicious packet size received! %d", sz))
|
||||
}
|
||||
|
||||
@ -119,13 +122,13 @@ func (client *Client) ClientHandler() {
|
||||
}
|
||||
|
||||
// decrypt && grab typeID
|
||||
protocol.DecryptData(tmp[:sz], client.e_key)
|
||||
DecryptData(tmp[:sz], client.e_key)
|
||||
typeID := uint32(binary.LittleEndian.Uint32(tmp[:4]))
|
||||
|
||||
// dispatch packet
|
||||
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz)
|
||||
pkt := protocol.NewPacket(tmp[4:sz])
|
||||
client.server.HandlePacket(client, typeID, pkt)
|
||||
pkt := NewPacket(tmp[4:sz])
|
||||
client.handler.HandlePacket(client, typeID, pkt)
|
||||
|
||||
// reset tmp
|
||||
tmp = tmp[:4]
|
@ -10,8 +10,8 @@ import (
|
||||
|
||||
type LoginServer struct {
|
||||
listener net.Listener
|
||||
clients map[*Client]bool
|
||||
unregister chan *Client
|
||||
clients map[*protocol.Client]bool
|
||||
unregister chan *protocol.Client
|
||||
}
|
||||
|
||||
func NewLoginServer() *LoginServer {
|
||||
@ -22,8 +22,8 @@ func NewLoginServer() *LoginServer {
|
||||
|
||||
return &LoginServer{
|
||||
listener: listener,
|
||||
clients: make(map[*Client]bool),
|
||||
unregister: make(chan *Client),
|
||||
clients: make(map[*protocol.Client]bool),
|
||||
unregister: make(chan *protocol.Client),
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,15 +42,15 @@ func (server *LoginServer) Start() {
|
||||
return
|
||||
}
|
||||
|
||||
client := newClient(server, conn)
|
||||
client := protocol.NewClient(server, conn)
|
||||
server.clients[client] = true
|
||||
go client.ClientHandler()
|
||||
fmt.Printf("Client %p connected\n", client)
|
||||
go client.ClientHandler()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (server *LoginServer) HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet) {
|
||||
func (server *LoginServer) HandlePacket(client *protocol.Client, typeID uint32, pkt *protocol.Packet) {
|
||||
switch typeID {
|
||||
case protocol.P_CL2LS_REQ_LOGIN:
|
||||
var loginPkt protocol.SP_CL2LS_REQ_LOGIN
|
||||
@ -82,6 +82,6 @@ func (server *LoginServer) HandlePacket(client *Client, typeID uint32, pkt *prot
|
||||
}
|
||||
}
|
||||
|
||||
func (server *LoginServer) Disconnect(client *Client) {
|
||||
func (server *LoginServer) Disconnect(client *protocol.Client) {
|
||||
server.unregister <- client
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/CPunch/GopenFusion/protocol"
|
||||
)
|
||||
|
||||
type Server interface {
|
||||
HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet)
|
||||
Disconnect(client *Client)
|
||||
}
|
@ -376,4 +376,3 @@ if __name__ == '__main__':
|
||||
os.remove("tmp.c")
|
||||
os.remove("tmp.o")
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user