mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化工程结构
This commit is contained in:
@@ -7,10 +7,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var Default_MaxHeaderBytes int = 1<<20
|
||||
var DefaultMaxHeaderBytes int = 1<<20
|
||||
|
||||
type CAFile struct {
|
||||
Certfile string
|
||||
CertFile string
|
||||
Keyfile string
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ func (slf *HttpServer) startListen() error {
|
||||
var tlsCaList []tls.Certificate
|
||||
var tlsConfig *tls.Config
|
||||
for _, caFile := range slf.caFileList {
|
||||
cer, err := tls.LoadX509KeyPair(caFile.Certfile, caFile.Keyfile)
|
||||
cer, err := tls.LoadX509KeyPair(caFile.CertFile, caFile.Keyfile)
|
||||
if err != nil {
|
||||
log.Fatal("Load CA [%s]-[%s] file is fail:%s", caFile.Certfile, caFile.Keyfile, err.Error())
|
||||
log.Fatal("Load CA [%s]-[%s] file is fail:%s", caFile.CertFile, caFile.Keyfile, err.Error())
|
||||
return err
|
||||
}
|
||||
tlsCaList = append(tlsCaList, cer)
|
||||
@@ -57,7 +57,7 @@ func (slf *HttpServer) startListen() error {
|
||||
Handler: slf.handler,
|
||||
ReadTimeout: slf.readTimeout,
|
||||
WriteTimeout: slf.writeTimeout,
|
||||
MaxHeaderBytes: Default_MaxHeaderBytes,
|
||||
MaxHeaderBytes: DefaultMaxHeaderBytes,
|
||||
TLSConfig: tlsConfig,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package network
|
||||
|
||||
type inetserver interface {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -11,13 +11,11 @@ type MessageJsonInfo struct {
|
||||
msgHandler MessageJsonHandler
|
||||
}
|
||||
|
||||
type MessageJsonHandler func(clientid uint64,msg interface{})
|
||||
type ConnectJsonHandler func(clientid uint64)
|
||||
type UnknownMessageJsonHandler func(clientid uint64,msg []byte)
|
||||
type MessageJsonHandler func(clientId uint64,msg interface{})
|
||||
type ConnectJsonHandler func(clientId uint64)
|
||||
type UnknownMessageJsonHandler func(clientId uint64,msg []byte)
|
||||
|
||||
type JsonProcessor struct {
|
||||
//SetByteOrder(littleEndian bool)
|
||||
//SetMsgLen(lenMsgLen int, minMsgLen uint32, maxMsgLen uint32)
|
||||
mapMsg map[uint16]MessageJsonInfo
|
||||
LittleEndian bool
|
||||
|
||||
@@ -26,33 +24,25 @@ type JsonProcessor struct {
|
||||
disconnectHandler ConnectJsonHandler
|
||||
}
|
||||
|
||||
func NewJsonProcessor() *JsonProcessor {
|
||||
processor := &JsonProcessor{mapMsg:map[uint16]MessageJsonInfo{}}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (p *JsonProcessor) SetByteOrder(littleEndian bool) {
|
||||
p.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
type JsonPackInfo struct {
|
||||
typ uint16
|
||||
msg interface{}
|
||||
rawMsg []byte
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
func NewJsonProcessor() *JsonProcessor {
|
||||
processor := &JsonProcessor{mapMsg:map[uint16]MessageJsonInfo{}}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetMsg() interface{} {
|
||||
return slf.msg
|
||||
func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
|
||||
jsonProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *JsonProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
func (jsonProcessor *JsonProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
pPackInfo := msg.(*JsonPackInfo)
|
||||
v,ok := slf.mapMsg[pPackInfo.typ]
|
||||
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ)
|
||||
}
|
||||
@@ -61,18 +51,19 @@ func (slf *JsonProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
|
||||
typeStrcut := struct {Type int `json:"typ"`}{}
|
||||
err := json.Unmarshal(data, &typeStrcut)
|
||||
func (jsonProcessor *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
|
||||
typeStruct := struct {Type int `json:"typ"`}{}
|
||||
err := json.Unmarshal(data, &typeStruct)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msgType := uint16(typeStrcut.Type)
|
||||
|
||||
info,ok := slf.mapMsg[msgType]
|
||||
msgType := uint16(typeStruct.Type)
|
||||
info,ok := jsonProcessor.mapMsg[msgType]
|
||||
if ok == false {
|
||||
return nil,fmt.Errorf("cannot find register %d msgtype!",msgType)
|
||||
return nil,fmt.Errorf("Cannot find register %d msgType!",msgType)
|
||||
}
|
||||
|
||||
msg := reflect.New(info.msgType.Elem()).Interface()
|
||||
err = json.Unmarshal(data, msg)
|
||||
if err != nil {
|
||||
@@ -82,8 +73,7 @@ func (slf *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
|
||||
return &JsonPackInfo{typ:msgType,msg:msg},nil
|
||||
}
|
||||
|
||||
|
||||
func (slf *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {
|
||||
func (jsonProcessor *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {
|
||||
rawMsg,err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
@@ -92,43 +82,50 @@ func (slf *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {
|
||||
return rawMsg,nil
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) Register(msgtype uint16,msg interface{},handle MessageJsonHandler) {
|
||||
func (jsonProcessor *JsonProcessor) Register(msgtype uint16,msg interface{},handle MessageJsonHandler) {
|
||||
var info MessageJsonInfo
|
||||
|
||||
info.msgType = reflect.TypeOf(msg)
|
||||
info.msgHandler = handle
|
||||
slf.mapMsg[msgtype] = info
|
||||
jsonProcessor.mapMsg[msgtype] = info
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) MakeMsg(msgType uint16,msg interface{}) *JsonPackInfo {
|
||||
func (jsonProcessor *JsonProcessor) MakeMsg(msgType uint16,msg interface{}) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ:msgType,msg:msg}
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonPackInfo {
|
||||
func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonPackInfo {
|
||||
return &JsonPackInfo{typ:msgType,rawMsg:msg}
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
slf.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
jsonProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (slf *JsonProcessor) ConnectedRoute(userData interface{}){
|
||||
slf.connectHandler(userData.(uint64))
|
||||
func (jsonProcessor *JsonProcessor) ConnectedRoute(userData interface{}){
|
||||
jsonProcessor.connectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) DisConnectedRoute(userData interface{}){
|
||||
slf.disconnectHandler(userData.(uint64))
|
||||
func (jsonProcessor *JsonProcessor) DisConnectedRoute(userData interface{}){
|
||||
jsonProcessor.disconnectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){
|
||||
slf.unknownMessageHandler = unknownMessageHandler
|
||||
func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){
|
||||
jsonProcessor.unknownMessageHandler = unknownMessageHandler
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler){
|
||||
slf.connectHandler = connectHandler
|
||||
func (jsonProcessor *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler){
|
||||
jsonProcessor.connectHandler = connectHandler
|
||||
}
|
||||
|
||||
func (slf *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler){
|
||||
slf.disconnectHandler = disconnectHandler
|
||||
}
|
||||
func (jsonProcessor *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler){
|
||||
jsonProcessor.disconnectHandler = disconnectHandler
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
}
|
||||
|
||||
func (slf *JsonPackInfo) GetMsg() interface{} {
|
||||
return slf.msg
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ type MessageInfo struct {
|
||||
msgHandler MessageHandler
|
||||
}
|
||||
|
||||
type MessageHandler func(clientid uint64,msg proto.Message)
|
||||
type ConnectHandler func(clientid uint64)
|
||||
type UnknownMessageHandler func(clientid uint64,msg []byte)
|
||||
type MessageHandler func(clientId uint64,msg proto.Message)
|
||||
type ConnectHandler func(clientId uint64)
|
||||
type UnknownMessageHandler func(clientId uint64,msg []byte)
|
||||
|
||||
const MsgTypeSize = 2
|
||||
type PBProcessor struct {
|
||||
@@ -26,20 +26,19 @@ type PBProcessor struct {
|
||||
disconnectHandler ConnectHandler
|
||||
}
|
||||
|
||||
type PBPackInfo struct {
|
||||
typ uint16
|
||||
msg proto.Message
|
||||
rawMsg []byte
|
||||
}
|
||||
|
||||
func NewPBProcessor() *PBProcessor {
|
||||
processor := &PBProcessor{mapMsg:map[uint16]MessageInfo{}}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (p *PBProcessor) SetByteOrder(littleEndian bool) {
|
||||
p.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
|
||||
type PBPackInfo struct {
|
||||
typ uint16
|
||||
msg proto.Message
|
||||
rawMsg []byte
|
||||
func (pbProcessor *PBProcessor) SetByteOrder(littleEndian bool) {
|
||||
pbProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
func (slf *PBPackInfo) GetPackType() uint16 {
|
||||
@@ -51,28 +50,27 @@ func (slf *PBPackInfo) GetMsg() proto.Message {
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
func (pbProcessor *PBProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
pPackInfo := msg.(*PBPackInfo)
|
||||
v,ok := slf.mapMsg[pPackInfo.typ]
|
||||
v,ok := pbProcessor.mapMsg[pPackInfo.typ]
|
||||
if ok == false {
|
||||
return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ)
|
||||
return fmt.Errorf("Cannot find msgtype %d is register!",pPackInfo.typ)
|
||||
}
|
||||
|
||||
|
||||
v.msgHandler(userdata.(uint64),pPackInfo.msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
func (pbProcessor *PBProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
var msgType uint16
|
||||
if slf.LittleEndian == true {
|
||||
if pbProcessor.LittleEndian == true {
|
||||
msgType = binary.LittleEndian.Uint16(data[:2])
|
||||
}else{
|
||||
msgType = binary.BigEndian.Uint16(data[:2])
|
||||
}
|
||||
|
||||
info,ok := slf.mapMsg[msgType]
|
||||
info,ok := pbProcessor.mapMsg[msgType]
|
||||
if ok == false {
|
||||
return nil,fmt.Errorf("cannot find register %d msgtype!",msgType)
|
||||
}
|
||||
@@ -87,7 +85,7 @@ func (slf *PBProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
func (pbProcessor *PBProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
pMsg := msg.(*PBPackInfo)
|
||||
|
||||
var err error
|
||||
@@ -99,7 +97,7 @@ func (slf *PBProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
}
|
||||
|
||||
buff := make([]byte, 2, len(pMsg.rawMsg)+MsgTypeSize)
|
||||
if slf.LittleEndian == true {
|
||||
if pbProcessor.LittleEndian == true {
|
||||
binary.LittleEndian.PutUint16(buff[:2],pMsg.typ)
|
||||
}else{
|
||||
binary.BigEndian.PutUint16(buff[:2],pMsg.typ)
|
||||
@@ -109,45 +107,45 @@ func (slf *PBProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
return buff,nil
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) Register(msgtype uint16,msg proto.Message,handle MessageHandler) {
|
||||
func (pbProcessor *PBProcessor) Register(msgtype uint16,msg proto.Message,handle MessageHandler) {
|
||||
var info MessageInfo
|
||||
|
||||
info.msgType = reflect.TypeOf(msg.(proto.Message))
|
||||
info.msgHandler = handle
|
||||
slf.mapMsg[msgtype] = info
|
||||
pbProcessor.mapMsg[msgtype] = info
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) MakeMsg(msgType uint16,protoMsg proto.Message) *PBPackInfo {
|
||||
func (pbProcessor *PBProcessor) MakeMsg(msgType uint16,protoMsg proto.Message) *PBPackInfo {
|
||||
return &PBPackInfo{typ:msgType,msg:protoMsg}
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBPackInfo {
|
||||
func (pbProcessor *PBProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBPackInfo {
|
||||
return &PBPackInfo{typ:msgType,rawMsg:msg}
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
slf.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
func (pbProcessor *PBProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
pbProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (slf *PBProcessor) ConnectedRoute(userData interface{}){
|
||||
slf.connectHandler(userData.(uint64))
|
||||
func (pbProcessor *PBProcessor) ConnectedRoute(userData interface{}){
|
||||
pbProcessor.connectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) DisConnectedRoute(userData interface{}){
|
||||
slf.disconnectHandler(userData.(uint64))
|
||||
func (pbProcessor *PBProcessor) DisConnectedRoute(userData interface{}){
|
||||
pbProcessor.disconnectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageHandler){
|
||||
slf.unknownMessageHandler = unknownMessageHandler
|
||||
func (pbProcessor *PBProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageHandler){
|
||||
pbProcessor.unknownMessageHandler = unknownMessageHandler
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) RegisterConnected(connectHandler ConnectHandler){
|
||||
slf.connectHandler = connectHandler
|
||||
func (pbProcessor *PBProcessor) RegisterConnected(connectHandler ConnectHandler){
|
||||
pbProcessor.connectHandler = connectHandler
|
||||
}
|
||||
|
||||
func (slf *PBProcessor) RegisterDisConnected(disconnectHandler ConnectHandler){
|
||||
slf.disconnectHandler = disconnectHandler
|
||||
func (pbProcessor *PBProcessor) RegisterDisConnected(disconnectHandler ConnectHandler){
|
||||
pbProcessor.disconnectHandler = disconnectHandler
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,45 +18,36 @@ const RawMsgTypeSize = 2
|
||||
type PBRawProcessor struct {
|
||||
msgHandler RawMessageHandler
|
||||
LittleEndian bool
|
||||
|
||||
unknownMessageHandler UnknownRawMessageHandler
|
||||
connectHandler RawConnectHandler
|
||||
disconnectHandler RawConnectHandler
|
||||
}
|
||||
|
||||
func NewPBRawProcessor() *PBRawProcessor {
|
||||
processor := &PBRawProcessor{}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (p *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
||||
p.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
type PBRawPackInfo struct {
|
||||
typ uint16
|
||||
rawMsg []byte
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
func NewPBRawProcessor() *PBRawProcessor {
|
||||
processor := &PBRawProcessor{}
|
||||
return processor
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetMsg() []byte {
|
||||
return slf.rawMsg
|
||||
func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
||||
pbRawProcessor.LittleEndian = littleEndian
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBRawProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
func (pbRawProcessor *PBRawProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
pPackInfo := msg.(*PBRawPackInfo)
|
||||
slf.msgHandler(userdata.(uint64),pPackInfo.typ,pPackInfo.rawMsg)
|
||||
pbRawProcessor.msgHandler(userdata.(uint64),pPackInfo.typ,pPackInfo.rawMsg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBRawProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
func (pbRawProcessor *PBRawProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
var msgType uint16
|
||||
if slf.LittleEndian == true {
|
||||
if pbRawProcessor.LittleEndian == true {
|
||||
msgType = binary.LittleEndian.Uint16(data[:2])
|
||||
}else{
|
||||
msgType = binary.BigEndian.Uint16(data[:2])
|
||||
@@ -66,11 +57,11 @@ func (slf *PBRawProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (slf *PBRawProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
func (pbRawProcessor *PBRawProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
pMsg := msg.(*PBRawPackInfo)
|
||||
|
||||
buff := make([]byte, 2, len(pMsg.rawMsg)+RawMsgTypeSize)
|
||||
if slf.LittleEndian == true {
|
||||
if pbRawProcessor.LittleEndian == true {
|
||||
binary.LittleEndian.PutUint16(buff[:2],pMsg.typ)
|
||||
}else{
|
||||
binary.BigEndian.PutUint16(buff[:2],pMsg.typ)
|
||||
@@ -80,40 +71,46 @@ func (slf *PBRawProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
return buff,nil
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetRawMsgHandler(handle RawMessageHandler) {
|
||||
slf.msgHandler = handle
|
||||
func (pbRawProcessor *PBRawProcessor) SetRawMsgHandler(handle RawMessageHandler) {
|
||||
pbRawProcessor.msgHandler = handle
|
||||
}
|
||||
|
||||
|
||||
func (slf *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBRawPackInfo {
|
||||
func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte) *PBRawPackInfo {
|
||||
return &PBRawPackInfo{typ:msgType,rawMsg:msg}
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
if slf.unknownMessageHandler == nil {
|
||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
if pbRawProcessor.unknownMessageHandler == nil {
|
||||
return
|
||||
}
|
||||
slf.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
pbRawProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (slf *PBRawProcessor) ConnectedRoute(userData interface{}){
|
||||
slf.connectHandler(userData.(uint64))
|
||||
func (pbRawProcessor *PBRawProcessor) ConnectedRoute(userData interface{}){
|
||||
pbRawProcessor.connectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) DisConnectedRoute(userData interface{}){
|
||||
slf.disconnectHandler(userData.(uint64))
|
||||
func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(userData interface{}){
|
||||
pbRawProcessor.disconnectHandler(userData.(uint64))
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler){
|
||||
slf.unknownMessageHandler = unknownMessageHandler
|
||||
func (pbRawProcessor *PBRawProcessor) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler){
|
||||
pbRawProcessor.unknownMessageHandler = unknownMessageHandler
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetConnectedHandler(connectHandler RawConnectHandler){
|
||||
slf.connectHandler = connectHandler
|
||||
func (pbRawProcessor *PBRawProcessor) SetConnectedHandler(connectHandler RawConnectHandler){
|
||||
pbRawProcessor.connectHandler = connectHandler
|
||||
}
|
||||
|
||||
func (slf *PBRawProcessor) SetDisConnectedHandler(disconnectHandler RawConnectHandler){
|
||||
slf.disconnectHandler = disconnectHandler
|
||||
func (pbRawProcessor *PBRawProcessor) SetDisConnectedHandler(disconnectHandler RawConnectHandler){
|
||||
pbRawProcessor.disconnectHandler = disconnectHandler
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetPackType() uint16 {
|
||||
return slf.typ
|
||||
}
|
||||
|
||||
func (slf *PBRawPackInfo) GetMsg() []byte {
|
||||
return slf.rawMsg
|
||||
}
|
||||
@@ -57,12 +57,8 @@ func (areaPool *memAreaPool) releaseByteSlice(byteBuff []byte) bool{
|
||||
panic("assert!")
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
areaPool.pool[pos].Put(byteBuff)
|
||||
|
||||
|
||||
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ type TCPClient struct {
|
||||
PendingWriteNum int
|
||||
AutoReconnect bool
|
||||
NewAgent func(*TCPConn) Agent
|
||||
conns ConnSet
|
||||
cons ConnSet
|
||||
wg sync.WaitGroup
|
||||
closeFlag bool
|
||||
|
||||
@@ -55,11 +55,11 @@ func (client *TCPClient) init() {
|
||||
if client.NewAgent == nil {
|
||||
log.Fatal("NewAgent must not be nil")
|
||||
}
|
||||
if client.conns != nil {
|
||||
if client.cons != nil {
|
||||
log.Fatal("client is running")
|
||||
}
|
||||
|
||||
client.conns = make(ConnSet)
|
||||
client.cons = make(ConnSet)
|
||||
client.closeFlag = false
|
||||
|
||||
// msg parser
|
||||
@@ -97,7 +97,7 @@ reconnect:
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
client.conns[conn] = struct{}{}
|
||||
client.cons[conn] = struct{}{}
|
||||
client.Unlock()
|
||||
|
||||
tcpConn := newTCPConn(conn, client.PendingWriteNum, client.msgParser)
|
||||
@@ -107,7 +107,7 @@ reconnect:
|
||||
// cleanup
|
||||
tcpConn.Close()
|
||||
client.Lock()
|
||||
delete(client.conns, conn)
|
||||
delete(client.cons, conn)
|
||||
client.Unlock()
|
||||
agent.OnClose()
|
||||
|
||||
@@ -120,10 +120,10 @@ reconnect:
|
||||
func (client *TCPClient) Close(waitDone bool) {
|
||||
client.Lock()
|
||||
client.closeFlag = true
|
||||
for conn := range client.conns {
|
||||
for conn := range client.cons {
|
||||
conn.Close()
|
||||
}
|
||||
client.conns = nil
|
||||
client.cons = nil
|
||||
client.Unlock()
|
||||
|
||||
if waitDone == true{
|
||||
|
||||
@@ -17,7 +17,6 @@ type TCPConn struct {
|
||||
msgParser *MsgParser
|
||||
}
|
||||
|
||||
|
||||
func freeChannel(conn *TCPConn){
|
||||
for;len(conn.writeChan)>0;{
|
||||
byteBuff := <- conn.writeChan
|
||||
@@ -139,4 +138,4 @@ func (tcpConn *TCPConn) WriteMsg(args ...[]byte) error {
|
||||
|
||||
func (tcpConn *TCPConn) IsConnected() bool {
|
||||
return tcpConn.closeFlag == false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,6 @@ func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// check len
|
||||
if msgLen > p.maxMsgLen {
|
||||
return nil, errors.New("message too long")
|
||||
@@ -123,8 +122,7 @@ func (p *MsgParser) Write(conn *TCPConn, args ...[]byte) error {
|
||||
} else if msgLen < p.minMsgLen {
|
||||
return errors.New("message too short")
|
||||
}
|
||||
|
||||
//msgLen -= 2
|
||||
|
||||
//msg := make([]byte, uint32(p.lenMsgLen)+msgLen)
|
||||
msg := makeByteSlice(p.lenMsgLen+int(msgLen))
|
||||
// write len
|
||||
|
||||
@@ -18,7 +18,7 @@ type WSClient struct {
|
||||
AutoReconnect bool
|
||||
NewAgent func(*WSConn) Agent
|
||||
dialer websocket.Dialer
|
||||
conns WebsocketConnSet
|
||||
cons WebsocketConnSet
|
||||
wg sync.WaitGroup
|
||||
closeFlag bool
|
||||
}
|
||||
@@ -59,11 +59,11 @@ func (client *WSClient) init() {
|
||||
if client.NewAgent == nil {
|
||||
log.Fatal("NewAgent must not be nil")
|
||||
}
|
||||
if client.conns != nil {
|
||||
if client.cons != nil {
|
||||
log.Fatal("client is running")
|
||||
}
|
||||
|
||||
client.conns = make(WebsocketConnSet)
|
||||
client.cons = make(WebsocketConnSet)
|
||||
client.closeFlag = false
|
||||
client.dialer = websocket.Dialer{
|
||||
HandshakeTimeout: client.HandshakeTimeout,
|
||||
@@ -99,7 +99,7 @@ reconnect:
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
client.conns[conn] = struct{}{}
|
||||
client.cons[conn] = struct{}{}
|
||||
client.Unlock()
|
||||
|
||||
wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen)
|
||||
@@ -109,7 +109,7 @@ reconnect:
|
||||
// cleanup
|
||||
wsConn.Close()
|
||||
client.Lock()
|
||||
delete(client.conns, conn)
|
||||
delete(client.cons, conn)
|
||||
client.Unlock()
|
||||
agent.OnClose()
|
||||
|
||||
@@ -122,10 +122,10 @@ reconnect:
|
||||
func (client *WSClient) Close() {
|
||||
client.Lock()
|
||||
client.closeFlag = true
|
||||
for conn := range client.conns {
|
||||
for conn := range client.cons {
|
||||
conn.Close()
|
||||
}
|
||||
client.conns = nil
|
||||
client.cons = nil
|
||||
client.Unlock()
|
||||
|
||||
client.wg.Wait()
|
||||
|
||||
@@ -133,6 +133,5 @@ func (wsConn *WSConn) WriteMsg(args ...[]byte) error {
|
||||
}
|
||||
|
||||
wsConn.doWrite(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user