1
This commit is contained in:
parent
75d8a8b38d
commit
6674f3e8f8
@ -13,14 +13,14 @@ import (
|
||||
|
||||
type WSPListener_ struct {
|
||||
listener net.Listener
|
||||
ch chan *q5.MsgHdr
|
||||
ch chan *f5.MsgHdr
|
||||
msgList q5.ListHead
|
||||
}
|
||||
|
||||
var WSPListener = new (WSPListener_)
|
||||
|
||||
func (this *WSPListener_) Init() {
|
||||
this.ch = make(chan *q5.MsgHdr)
|
||||
this.ch = make(chan *f5.MsgHdr)
|
||||
listener, err := net.Listen("tcp", "0.0.0.0:8888")
|
||||
if err != nil {
|
||||
|
||||
@ -45,18 +45,6 @@ func (this *WSPListener_) accept() {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
struct WSProxyPackHead_C
|
||||
{
|
||||
unsigned short packlen;
|
||||
unsigned short msgid;
|
||||
unsigned int seqid;
|
||||
unsigned short magic_code;
|
||||
|
||||
unsigned short socket_handle;
|
||||
unsigned long ip_saddr;
|
||||
};
|
||||
*/
|
||||
func (this *WSPListener_) socketRead(conn net.Conn) {
|
||||
buf := make([]byte, 1024 * 64)
|
||||
recvBufLen := 0
|
||||
@ -84,7 +72,7 @@ func (this *WSPListener_) socketRead(conn net.Conn) {
|
||||
prevOffset := 0
|
||||
for {
|
||||
prevOffset = offset
|
||||
this.decodePacket(recvBuf, &offset, recvBufLen)
|
||||
this.decodePacket(conn, recvBuf, &offset, recvBufLen)
|
||||
if prevOffset >= offset || offset >= recvBufLen {
|
||||
break
|
||||
}
|
||||
@ -108,19 +96,22 @@ func (this *WSPListener_) socketRead(conn net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *WSPListener_) decodePacket(buf []byte, offset *int, bufLen int) {
|
||||
func (this *WSPListener_) decodePacket(conn net.Conn, buf []byte, offset *int, bufLen int) {
|
||||
warning := false
|
||||
for bufLen - *offset >= 20 {
|
||||
packLen := int(buf[*offset + 0]) + int(buf[*offset + 1] << 16)
|
||||
if buf[*offset + 8] == 'K' && buf[*offset + 9] == 'S' {
|
||||
if bufLen - *offset < 12 + packLen {
|
||||
for bufLen - *offset >= f5.WSPROXYPACKHEAD_C_SIZE {
|
||||
msgHead := f5.WSProxyPackHead_C{}
|
||||
msgHead.Read(buf, *offset)
|
||||
if msgHead.MagicCode == f5.NET_MSG_MAGIC_CODE {
|
||||
if bufLen - *offset < int(f5.WSPROXYPACKHEAD_C_SIZE) + int(msgHead.PackLen) {
|
||||
continue
|
||||
}
|
||||
hdr := new(q5.MsgHdr)
|
||||
hdr.MsgId = int(buf[*offset + 2]) + int(buf[*offset + 3] << 16)
|
||||
//hdr.SeqId = int(recvBuf[4]) + int(recvBuf[5] )
|
||||
hdr := new(f5.MsgHdr)
|
||||
hdr.Conn = conn
|
||||
hdr.MsgId = msgHead.MsgId
|
||||
hdr.SocketHandle = msgHead.SocketHandle
|
||||
hdr.SeqId = msgHead.SeqId
|
||||
this.ch <- hdr
|
||||
*offset += 20 + packLen
|
||||
*offset += 20 + int(msgHead.PackLen)
|
||||
} else {
|
||||
warning = true
|
||||
*offset++
|
||||
@ -138,39 +129,31 @@ func (this *WSPListener_) parseNetPkt() {
|
||||
if hdr, ok := <-this.ch; ok {
|
||||
hdr.Msg = cs.ParsePb(hdr.MsgId, hdr.Data)
|
||||
if hdr.Msg != nil {
|
||||
|
||||
hdr.Entry.Init(hdr)
|
||||
App.addNetMsg(hdr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
struct WSProxyPackHead_S
|
||||
{
|
||||
unsigned short packlen;
|
||||
unsigned short msgid;
|
||||
unsigned int seqid;
|
||||
unsigned short magic_code;
|
||||
unsigned short rpc_error_code;
|
||||
|
||||
unsigned short socket_handle;
|
||||
unsigned short ext_len;
|
||||
};
|
||||
*/
|
||||
func (this *WSPListener_) sendProxyMsg(conn net.Conn, socketHandle uint16, msg proto.Message) {
|
||||
netMsg := msg.(q5.NetMsg)
|
||||
netMsg := msg.(f5.NetMsg)
|
||||
msgId := netMsg.GetNetMsgId()
|
||||
msgData, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
buff := make([]byte, len(msgData) + 12)
|
||||
buff := make([]byte, len(msgData) + 16)
|
||||
buff[0] = byte(len(msgData) & 0xFF)
|
||||
buff[1] = byte(len(msgData) & 0xFFFF >> 16)
|
||||
buff[1] = byte((len(msgData) & 0xFFFF) >> 8)
|
||||
buff[2] = byte(msgId & 0xFF)
|
||||
buff[3] = byte(msgId >> 16)
|
||||
buff[9] = byte(socketHandle & 0xFF)
|
||||
buff[10] = byte(socketHandle >> 16)
|
||||
copy(buff[12:], msgData[:])
|
||||
buff[3] = byte((msgId & 0xFFFF) >> 8)
|
||||
buff[8] = byte('K')
|
||||
buff[9] = byte('S')
|
||||
|
||||
buff[12] = byte(socketHandle & 0xFF)
|
||||
buff[13] = byte((socketHandle & 0xFFFF) >> 8)
|
||||
|
||||
copy(buff[16:], msgData[:])
|
||||
conn.Write(buff)
|
||||
}
|
||||
|
@ -25,11 +25,13 @@ func (this *App_) Init() {
|
||||
this.App_.Init(this.Update)
|
||||
this.msgList.Init(nil)
|
||||
this.workList.Init(nil)
|
||||
HandlerMgr.Init()
|
||||
WSPListener.Init()
|
||||
go this.goReportServerState();
|
||||
}
|
||||
|
||||
func (this *App_) UnInit() {
|
||||
HandlerMgr.UnInit()
|
||||
WSPListener.UnInit()
|
||||
this.App_.UnInit()
|
||||
}
|
||||
@ -44,7 +46,7 @@ func (this *App_) Update() {
|
||||
}
|
||||
for !this.workList.Empty() {
|
||||
next := this.workList.Next()
|
||||
hdr, ok := next.GetData().(*q5.MsgHdr)
|
||||
hdr, ok := next.GetData().(*f5.MsgHdr)
|
||||
if ok {
|
||||
handler := cs.GetNetMsgHandler(hdr.MsgId)
|
||||
if handler != nil && handler.HandlerId== PLAYER_MGR_HANDLER_ID {
|
||||
@ -76,7 +78,7 @@ func (this *App_) goReportServerState() {
|
||||
}
|
||||
|
||||
|
||||
func (this *App_) addNetMsg(hdr *q5.MsgHdr) {
|
||||
func (this *App_) addNetMsg(hdr *f5.MsgHdr) {
|
||||
{
|
||||
this.msgMutex.Lock()
|
||||
defer this.msgMutex.Unlock()
|
||||
|
@ -1,14 +1,14 @@
|
||||
package cs
|
||||
|
||||
import (
|
||||
"q5"
|
||||
"f5"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
type MsgHandler interface {
|
||||
CMPing(*q5.MsgHdr, *CMPing)
|
||||
CMLogin(*q5.MsgHdr, *CMLogin)
|
||||
CMReconnect(*q5.MsgHdr, *CMReconnect)
|
||||
CMPing(*f5.MsgHdr, *CMPing)
|
||||
CMLogin(*f5.MsgHdr, *CMLogin)
|
||||
CMReconnect(*f5.MsgHdr, *CMReconnect)
|
||||
}
|
||||
|
||||
type MsgHandlerImpl struct {
|
||||
@ -18,26 +18,26 @@ type NetMsgHandler struct {
|
||||
MsgId int
|
||||
HandlerId int
|
||||
parseCb func([]byte) interface{}
|
||||
cb func(*q5.MsgHdr, MsgHandler)
|
||||
cb func(*f5.MsgHdr, MsgHandler)
|
||||
}
|
||||
|
||||
var handlers [2000]*NetMsgHandler
|
||||
|
||||
func (this *MsgHandlerImpl) CMPing(hdr *q5.MsgHdr, msg *CMPing) {
|
||||
func (this *MsgHandlerImpl) CMPing(hdr *f5.MsgHdr, msg *CMPing) {
|
||||
}
|
||||
|
||||
func (this *MsgHandlerImpl) CMLogin(hdr *q5.MsgHdr, msg *CMLogin) {
|
||||
func (this *MsgHandlerImpl) CMLogin(hdr *f5.MsgHdr, msg *CMLogin) {
|
||||
}
|
||||
|
||||
func (this *MsgHandlerImpl) CMReconnect(hdr *q5.MsgHdr, msg *CMReconnect) {
|
||||
func (this *MsgHandlerImpl) CMReconnect(hdr *f5.MsgHdr, msg *CMReconnect) {
|
||||
}
|
||||
|
||||
func GetNetMsgHandler(msgId int) *NetMsgHandler {
|
||||
func GetNetMsgHandler(msgId uint16) *NetMsgHandler {
|
||||
handler := handlers[msgId]
|
||||
return handler
|
||||
}
|
||||
|
||||
func DispatchMsg(handler *NetMsgHandler, hdr *q5.MsgHdr, msgHandler MsgHandler) {
|
||||
func DispatchMsg(handler *NetMsgHandler, hdr *f5.MsgHdr, msgHandler MsgHandler) {
|
||||
handler.cb(hdr, msgHandler)
|
||||
}
|
||||
|
||||
@ -46,25 +46,43 @@ func RegHandlerId(msgId int, handlerId int) {
|
||||
handler.HandlerId = handlerId
|
||||
}
|
||||
|
||||
func ParsePb(msgId int, data []byte) interface{} {
|
||||
func ParsePb(msgId uint16, data []byte) interface{} {
|
||||
handler := handlers[msgId]
|
||||
if handler == nil {
|
||||
return nil
|
||||
}
|
||||
return handler.parseCb(data)
|
||||
}
|
||||
|
||||
func init() {
|
||||
handlers[10] = &NetMsgHandler{
|
||||
handlers[int(CMMessageIdE__CMPing)] = &NetMsgHandler{
|
||||
MsgId: int(CMMessageIdE__CMPing),
|
||||
parseCb: func (data []byte) interface{} {
|
||||
msg := &CMLogin{}
|
||||
proto.Unmarshal(data, msg)
|
||||
return msg
|
||||
},
|
||||
cb: func (hdr *q5.MsgHdr, handler MsgHandler) {
|
||||
cb: func (hdr *f5.MsgHdr, handler MsgHandler) {
|
||||
handler.CMPing(hdr, hdr.Msg.(*CMPing))
|
||||
},
|
||||
};
|
||||
}
|
||||
handlers[int(CMMessageIdE__CMLogin)] = &NetMsgHandler{
|
||||
MsgId: int(CMMessageIdE__CMLogin),
|
||||
parseCb: func (data []byte) interface{} {
|
||||
msg := &CMLogin{}
|
||||
proto.Unmarshal(data, msg)
|
||||
return msg
|
||||
},
|
||||
cb: func (hdr *f5.MsgHdr, handler MsgHandler) {
|
||||
handler.CMLogin(hdr, hdr.Msg.(*CMLogin))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (x *CMLogin) GetNetMsgId() uint16 {
|
||||
return uint16(CMMessageIdE__CMLogin);
|
||||
}
|
||||
|
||||
func (x *SMLogin) GetNetMsgId() uint16 {
|
||||
return uint16(SMMessageIdE__SMLogin);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"cs"
|
||||
"q5"
|
||||
"f5"
|
||||
)
|
||||
|
||||
type PlayerMgr_ struct {
|
||||
@ -11,7 +11,9 @@ type PlayerMgr_ struct {
|
||||
|
||||
var PlayerMgr = new (PlayerMgr_)
|
||||
|
||||
func (this *PlayerMgr_) CMLogin(hdr *q5.MsgHdr, msg *cs.CMLogin) {
|
||||
func (this *PlayerMgr_) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
|
||||
serverInfo := "192.168.100.39:1000"
|
||||
rspMsg := &cs.SMLogin{}
|
||||
rspMsg.ServerInfo = &serverInfo
|
||||
WSPListener.sendProxyMsg(hdr.Conn, 0, rspMsg)
|
||||
}
|
||||
|
@ -82,6 +82,7 @@ class ClientNet {
|
||||
|
||||
async #onParsePacket() {
|
||||
let offset = 0;
|
||||
console.log('onParsepacket');
|
||||
while (this.recvBuf.length > offset + 12) {
|
||||
const msgSize = this.recvBuf.readUInt32LE(offset + 0);
|
||||
const msgId = this.recvBuf.readUInt16LE(offset + 4);
|
||||
|
Loading…
x
Reference in New Issue
Block a user