171 lines
3.7 KiB
Go
171 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
|
|
"cs"
|
|
"f5"
|
|
"mt"
|
|
"q5"
|
|
"ss"
|
|
|
|
proto "github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
type WSPListener struct {
|
|
ss.MsgHandlerImpl
|
|
listener net.Listener
|
|
ch chan *f5.MsgHdr
|
|
msgList q5.ListHead
|
|
}
|
|
|
|
func (this *WSPListener) init() {
|
|
this.ch = make(chan *f5.MsgHdr)
|
|
listener, err := net.Listen("tcp", "0.0.0.0:"+
|
|
q5.ToString(mt.Table.IMCluster.GetListenPort()))
|
|
if err != nil {
|
|
|
|
} else {
|
|
this.listener = listener
|
|
go this.parseNetPkt()
|
|
go this.accept()
|
|
}
|
|
}
|
|
|
|
func (this *WSPListener) unInit() {
|
|
}
|
|
|
|
func (this *WSPListener) SS_ping(hdr *f5.MsgHdr, msg *ss.SS_Ping) {
|
|
}
|
|
|
|
func (this *WSPListener) SS_WSP_SocketDisconnect(hdr *f5.MsgHdr, msg *ss.SS_WSP_SocketDisconnect) {
|
|
}
|
|
|
|
func (this *WSPListener) accept() {
|
|
fmt.Println("accpet")
|
|
for {
|
|
conn, err := this.listener.Accept()
|
|
if err == nil {
|
|
fmt.Println("connected")
|
|
go this.socketRead(conn)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *WSPListener) socketRead(conn net.Conn) {
|
|
buf := make([]byte, 1024*64)
|
|
recvBufLen := 0
|
|
recvBuf := make([]byte, 1024*64*2)
|
|
for {
|
|
bufLen, err := conn.Read(buf)
|
|
if err == nil && bufLen > 0 {
|
|
alreadyReadBytes := 0
|
|
for {
|
|
{
|
|
readBytes := bufLen - alreadyReadBytes
|
|
if readBytes > MAX_PACKET_LEN-recvBufLen {
|
|
readBytes = MAX_PACKET_LEN - recvBufLen
|
|
}
|
|
|
|
if readBytes > 0 {
|
|
copy(recvBuf[recvBufLen:],
|
|
buf[alreadyReadBytes:alreadyReadBytes+readBytes])
|
|
recvBufLen += readBytes
|
|
alreadyReadBytes += readBytes
|
|
}
|
|
}
|
|
|
|
offset := 0
|
|
prevOffset := 0
|
|
for {
|
|
prevOffset = offset
|
|
this.decodePacket(conn, recvBuf, &offset, recvBufLen)
|
|
if prevOffset >= offset || offset >= recvBufLen {
|
|
break
|
|
}
|
|
}
|
|
|
|
if offset > 0 && offset < recvBufLen {
|
|
copy(recvBuf[:], recvBuf[offset:])
|
|
}
|
|
recvBufLen -= offset
|
|
if recvBufLen >= MAX_PACKET_LEN {
|
|
panic("recv max packet")
|
|
}
|
|
|
|
if alreadyReadBytes >= bufLen {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *WSPListener) decodePacket(conn net.Conn, buf []byte, offset *int, bufLen int) {
|
|
warning := false
|
|
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(f5.MsgHdr)
|
|
hdr.Conn = conn
|
|
hdr.MsgId = msgHead.MsgId
|
|
hdr.SocketHandle = msgHead.SocketHandle
|
|
hdr.SeqId = msgHead.SeqId
|
|
hdr.Data = buf[int(*offset)+f5.WSPROXYPACKHEAD_C_SIZE : int(*offset)+f5.WSPROXYPACKHEAD_C_SIZE+int(msgHead.PackLen)]
|
|
this.ch <- hdr
|
|
*offset += f5.WSPROXYPACKHEAD_C_SIZE + int(msgHead.PackLen)
|
|
} else {
|
|
warning = true
|
|
*offset++
|
|
continue
|
|
}
|
|
}
|
|
|
|
if warning {
|
|
f5.GetSysLog().Warning("收到client非法数据包")
|
|
}
|
|
}
|
|
|
|
func (this *WSPListener) parseNetPkt() {
|
|
for {
|
|
if hdr, ok := <-this.ch; ok {
|
|
if hdr.MsgId < f5.WSP_SS_MAX_MSGID {
|
|
hdr.Msg = ss.ParsePb(hdr.MsgId, hdr.Data)
|
|
if hdr.Msg != nil {
|
|
hdr.Entry.Init(hdr)
|
|
app.addNetMsg(hdr)
|
|
}
|
|
} else {
|
|
hdr.Msg = cs.ParsePb(hdr.MsgId, hdr.Data)
|
|
if hdr.Msg != nil {
|
|
hdr.Entry.Init(hdr)
|
|
app.addNetMsg(hdr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *WSPListener) sendProxyMsg(conn net.Conn, socketHandle uint16, msg proto.Message) {
|
|
netMsg := msg.(f5.NetMsg)
|
|
msgData, err := proto.Marshal(msg)
|
|
if err != nil {
|
|
return
|
|
}
|
|
msgHead := f5.WSProxyPackHead_S{}
|
|
msgHead.PackLen = uint16(len(msgData))
|
|
msgHead.MsgId = netMsg.GetNetMsgId()
|
|
msgHead.MagicCode = f5.NET_MSG_MAGIC_CODE
|
|
msgHead.SocketHandle = socketHandle
|
|
|
|
buff := make([]byte, len(msgData)+f5.WSPROXYPACKHEAD_S_SIZE)
|
|
msgHead.Write(buff, 0)
|
|
copy(buff[f5.WSPROXYPACKHEAD_S_SIZE:], msgData[:])
|
|
conn.Write(buff)
|
|
}
|