mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化网络层
This commit is contained in:
@@ -19,6 +19,7 @@ type KcpModule struct {
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[string]*Client
|
||||
process processor.IRawProcessor
|
||||
newClientIdHandler func() string
|
||||
|
||||
kcpServer network.KCPServer
|
||||
kcpCfg *network.KcpCfg
|
||||
@@ -56,7 +57,11 @@ func (km *KcpModule) OnInit() error {
|
||||
km.process.SetByteOrder(km.kcpCfg.LittleEndian)
|
||||
km.kcpServer.Init(km.kcpCfg)
|
||||
km.kcpServer.NewAgent = km.NewAgent
|
||||
|
||||
if km.newClientIdHandler == nil {
|
||||
km.newClientIdHandler = func()string{
|
||||
return primitive.NewObjectID().Hex()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -65,6 +70,10 @@ func (km *KcpModule) Init(kcpCfg *network.KcpCfg, process processor.IRawProcesso
|
||||
km.process = process
|
||||
}
|
||||
|
||||
func (km *KcpModule) SetNewClientIdHandler(newClientIdHandler func() string){
|
||||
km.newClientIdHandler = newClientIdHandler
|
||||
}
|
||||
|
||||
func (km *KcpModule) Start() error {
|
||||
return km.kcpServer.Start()
|
||||
}
|
||||
@@ -77,9 +86,9 @@ func (km *KcpModule) kcpEventHandler(ev event.IEvent) {
|
||||
case KPTDisConnected:
|
||||
km.process.DisConnectedRoute(e.StringExt[0])
|
||||
case KPTUnknownPack:
|
||||
km.process.UnknownMsgRoute(e.StringExt[0], e.Data, e.AnyExt[0].(func(data []byte)))
|
||||
km.process.UnknownMsgRoute(e.StringExt[0], e.Data)
|
||||
case KPTPack:
|
||||
km.process.MsgRoute(e.StringExt[0], e.Data, e.AnyExt[0].(func(data []byte)))
|
||||
km.process.MsgRoute(e.StringExt[0], e.Data)
|
||||
}
|
||||
|
||||
event.DeleteEvent(ev)
|
||||
@@ -111,7 +120,7 @@ func (km *KcpModule) newClient(conn network.Conn) *Client {
|
||||
km.mapClientLocker.Lock()
|
||||
defer km.mapClientLocker.Unlock()
|
||||
|
||||
pClient := &Client{kcpConn: conn.(*network.NetConn), id: primitive.NewObjectID().Hex()}
|
||||
pClient := &Client{kcpConn: conn.(*network.NetConn), id: km.newClientIdHandler()}
|
||||
pClient.kcpModule = km
|
||||
km.mapClient[pClient.id] = pClient
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ type TcpModule struct {
|
||||
mapClient map[string]*Client
|
||||
process processor.IRawProcessor
|
||||
tcpCfg *TcpCfg
|
||||
newClientIdHandler func() string
|
||||
}
|
||||
|
||||
type TcpPackType int8
|
||||
@@ -35,6 +36,7 @@ type TcpPack struct {
|
||||
Type TcpPackType //0表示连接 1表示断开 2表示数据
|
||||
ClientId string
|
||||
Data interface{}
|
||||
rawData []byte
|
||||
RecyclerReaderBytes func(data []byte)
|
||||
}
|
||||
|
||||
@@ -51,7 +53,8 @@ type TcpCfg struct {
|
||||
LittleEndian bool //是否小端序
|
||||
LenMsgLen int //消息头占用byte数量,只能是1byte,2byte,4byte。如果是4byte,意味着消息最大可以是math.MaxUint32(4GB)
|
||||
MinMsgLen uint32 //最小消息长度
|
||||
MaxMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
|
||||
MaxReadMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
|
||||
MaxWriteMsgLen uint32 // 最大写消息长度
|
||||
ReadDeadlineSecond time.Duration //读超时
|
||||
WriteDeadlineSecond time.Duration //写超时
|
||||
}
|
||||
@@ -68,11 +71,17 @@ func (tm *TcpModule) OnInit() error {
|
||||
tm.tcpServer.LittleEndian = tm.tcpCfg.LittleEndian
|
||||
tm.tcpServer.LenMsgLen = tm.tcpCfg.LenMsgLen
|
||||
tm.tcpServer.MinMsgLen = tm.tcpCfg.MinMsgLen
|
||||
tm.tcpServer.MaxMsgLen = tm.tcpCfg.MaxMsgLen
|
||||
tm.tcpServer.MaxReadMsgLen = tm.tcpCfg.MaxReadMsgLen
|
||||
tm.tcpServer.MaxWriteMsgLen = tm.tcpCfg.MaxWriteMsgLen
|
||||
tm.tcpServer.ReadDeadline = tm.tcpCfg.ReadDeadlineSecond * time.Second
|
||||
tm.tcpServer.WriteDeadline = tm.tcpCfg.WriteDeadlineSecond * time.Second
|
||||
tm.mapClient = make(map[string]*Client, tm.tcpServer.MaxConnNum)
|
||||
tm.tcpServer.NewAgent = tm.NewClient
|
||||
if tm.newClientIdHandler == nil {
|
||||
tm.newClientIdHandler = func()string{
|
||||
return primitive.NewObjectID().Hex()
|
||||
}
|
||||
}
|
||||
|
||||
//3.设置解析处理器
|
||||
tm.process.SetByteOrder(tm.tcpCfg.LittleEndian)
|
||||
@@ -87,6 +96,10 @@ func (tm *TcpModule) Init(tcpCfg *TcpCfg, process processor.IRawProcessor) {
|
||||
tm.process = process
|
||||
}
|
||||
|
||||
func (tm *TcpModule) SetNewClientIdHandler(newClientIdHandler func() string){
|
||||
tm.newClientIdHandler = newClientIdHandler
|
||||
}
|
||||
|
||||
func (tm *TcpModule) Start() error {
|
||||
return tm.tcpServer.Start()
|
||||
}
|
||||
@@ -99,9 +112,11 @@ func (tm *TcpModule) tcpEventHandler(ev event.IEvent) {
|
||||
case TPTDisConnected:
|
||||
tm.process.DisConnectedRoute(pack.ClientId)
|
||||
case TPTUnknownPack:
|
||||
tm.process.UnknownMsgRoute(pack.ClientId, pack.Data, pack.RecyclerReaderBytes)
|
||||
tm.process.UnknownMsgRoute(pack.ClientId, pack.Data)
|
||||
pack.RecyclerReaderBytes(pack.rawData)
|
||||
case TPTPack:
|
||||
tm.process.MsgRoute(pack.ClientId, pack.Data, pack.RecyclerReaderBytes)
|
||||
tm.process.MsgRoute(pack.ClientId, pack.Data)
|
||||
pack.RecyclerReaderBytes(pack.rawData)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +124,7 @@ func (tm *TcpModule) NewClient(conn network.Conn) network.Agent {
|
||||
tm.mapClientLocker.Lock()
|
||||
defer tm.mapClientLocker.Unlock()
|
||||
|
||||
clientId := primitive.NewObjectID().Hex()
|
||||
clientId := tm.newClientIdHandler()
|
||||
pClient := &Client{tcpConn: conn.(*network.NetConn), id: clientId}
|
||||
pClient.tcpModule = tm
|
||||
tm.mapClient[clientId] = pClient
|
||||
@@ -138,10 +153,10 @@ func (slf *Client) Run() {
|
||||
}
|
||||
data, err := slf.tcpModule.process.Unmarshal(slf.id, bytes)
|
||||
if err != nil {
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTUnknownPack, Data: bytes, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTUnknownPack, Data: data,rawData: bytes,RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
|
||||
continue
|
||||
}
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTPack, Data: data, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
|
||||
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTPack, Data: data,rawData: bytes, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ type WSModule struct {
|
||||
mapClient map[string]*WSClient
|
||||
process processor.IRawProcessor
|
||||
wsCfg *WSCfg
|
||||
newClientIdHandler func() string
|
||||
}
|
||||
|
||||
type WSClient struct {
|
||||
@@ -74,7 +75,11 @@ func (ws *WSModule) OnInit() error {
|
||||
ws.WSServer.HandshakeTimeout = ws.wsCfg.HandshakeTimeoutSecond*time.Second
|
||||
ws.WSServer.ReadTimeout = ws.wsCfg.ReadTimeoutSecond*time.Second
|
||||
ws.WSServer.WriteTimeout = ws.wsCfg.WriteTimeoutSecond*time.Second
|
||||
|
||||
if ws.newClientIdHandler == nil {
|
||||
ws.newClientIdHandler = func()string{
|
||||
return primitive.NewObjectID().Hex()
|
||||
}
|
||||
}
|
||||
if ws.wsCfg.KeyFile != "" && ws.wsCfg.CertFile != "" {
|
||||
ws.WSServer.KeyFile = ws.wsCfg.KeyFile
|
||||
ws.WSServer.CertFile = ws.wsCfg.CertFile
|
||||
@@ -97,6 +102,10 @@ func (ws *WSModule) Init(wsCfg *WSCfg, process processor.IRawProcessor) {
|
||||
ws.process = process
|
||||
}
|
||||
|
||||
func (ws *WSModule) SetNewClientIdHandler(newClientIdHandler func() string){
|
||||
ws.newClientIdHandler = newClientIdHandler
|
||||
}
|
||||
|
||||
func (ws *WSModule) Start() error {
|
||||
return ws.WSServer.Start()
|
||||
}
|
||||
@@ -109,9 +118,9 @@ func (ws *WSModule) wsEventHandler(ev event.IEvent) {
|
||||
case WPTDisConnected:
|
||||
ws.process.DisConnectedRoute(pack.ClientId)
|
||||
case WPTUnknownPack:
|
||||
ws.process.UnknownMsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
ws.process.UnknownMsgRoute(pack.ClientId, pack.Data)
|
||||
case WPTPack:
|
||||
ws.process.MsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
ws.process.MsgRoute(pack.ClientId, pack.Data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +130,7 @@ func (ws *WSModule) recyclerReaderBytes([]byte) {
|
||||
func (ws *WSModule) NewWSClient(conn *network.WSConn) network.Agent {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
pClient := &WSClient{wsConn: conn, id: primitive.NewObjectID().Hex()}
|
||||
pClient := &WSClient{wsConn: conn, id: ws.newClientIdHandler()}
|
||||
pClient.wsModule = ws
|
||||
ws.mapClient[pClient.id] = pClient
|
||||
|
||||
@@ -142,7 +151,7 @@ func (wc *WSClient) Run() {
|
||||
}
|
||||
data, err := wc.wsModule.process.Unmarshal(wc.id, bytes)
|
||||
if err != nil {
|
||||
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTUnknownPack, Data: bytes}})
|
||||
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTUnknownPack, Data: data}})
|
||||
continue
|
||||
}
|
||||
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTPack, Data: data}})
|
||||
|
||||
Reference in New Issue
Block a user