mirror of
https://github.com/duanhf2012/origin.git
synced 2026-06-05 04:05:18 +08:00
优化代码规范
This commit is contained in:
@@ -7,9 +7,9 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"sync"
|
||||
"github.com/google/uuid"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type WSService struct {
|
||||
@@ -17,16 +17,17 @@ type WSService struct {
|
||||
wsServer network.WSServer
|
||||
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[string] *WSClient
|
||||
mapClient map[string]*WSClient
|
||||
process processor.IProcessor
|
||||
}
|
||||
|
||||
type WSPackType int8
|
||||
const(
|
||||
WPT_Connected WSPackType = 0
|
||||
|
||||
const (
|
||||
WPT_Connected WSPackType = 0
|
||||
WPT_DisConnected WSPackType = 1
|
||||
WPT_Pack WSPackType = 2
|
||||
WPT_UnknownPack WSPackType = 3
|
||||
WPT_Pack WSPackType = 2
|
||||
WPT_UnknownPack WSPackType = 3
|
||||
)
|
||||
|
||||
const Default_WS_MaxConnNum = 3000
|
||||
@@ -34,8 +35,8 @@ const Default_WS_PendingWriteNum = 10000
|
||||
const Default_WS_MaxMsgLen = 65535
|
||||
|
||||
type WSClient struct {
|
||||
id string
|
||||
wsConn *network.WSConn
|
||||
id string
|
||||
wsConn *network.WSConn
|
||||
wsService *WSService
|
||||
}
|
||||
|
||||
@@ -46,44 +47,44 @@ type WSPack struct {
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
func (ws *WSService) OnInit() error{
|
||||
func (ws *WSService) OnInit() error {
|
||||
|
||||
iConfig := ws.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!", ws.GetName())
|
||||
return fmt.Errorf("%s service config is error", ws.GetName())
|
||||
}
|
||||
wsCfg := iConfig.(map[string]interface{})
|
||||
addr,ok := wsCfg["ListenAddr"]
|
||||
addr, ok := wsCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!", ws.GetName())
|
||||
return fmt.Errorf("%s service config is error", ws.GetName())
|
||||
}
|
||||
|
||||
ws.wsServer.Addr = addr.(string)
|
||||
ws.wsServer.MaxConnNum = Default_WS_MaxConnNum
|
||||
ws.wsServer.PendingWriteNum = Default_WS_PendingWriteNum
|
||||
ws.wsServer.MaxMsgLen = Default_WS_MaxMsgLen
|
||||
MaxConnNum,ok := wsCfg["MaxConnNum"]
|
||||
MaxConnNum, ok := wsCfg["MaxConnNum"]
|
||||
if ok == true {
|
||||
ws.wsServer.MaxConnNum = int(MaxConnNum.(float64))
|
||||
}
|
||||
|
||||
PendingWriteNum,ok := wsCfg["PendingWriteNum"]
|
||||
PendingWriteNum, ok := wsCfg["PendingWriteNum"]
|
||||
if ok == true {
|
||||
ws.wsServer.PendingWriteNum = int(PendingWriteNum.(float64))
|
||||
}
|
||||
|
||||
MaxMsgLen,ok := wsCfg["MaxMsgLen"]
|
||||
MaxMsgLen, ok := wsCfg["MaxMsgLen"]
|
||||
if ok == true {
|
||||
ws.wsServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
||||
}
|
||||
|
||||
ws.mapClient = make( map[string] *WSClient, ws.wsServer.MaxConnNum)
|
||||
ws.mapClient = make(map[string]*WSClient, ws.wsServer.MaxConnNum)
|
||||
ws.wsServer.NewAgent = ws.NewWSClient
|
||||
ws.wsServer.Start()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WSService) SetMessageType(messageType int){
|
||||
func (ws *WSService) SetMessageType(messageType int) {
|
||||
ws.wsServer.SetMessageType(messageType)
|
||||
}
|
||||
|
||||
@@ -95,15 +96,15 @@ func (ws *WSService) WSEventHandler(ev event.IEvent) {
|
||||
case WPT_DisConnected:
|
||||
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
|
||||
case WPT_UnknownPack:
|
||||
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data,ws.recyclerReaderBytes)
|
||||
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
case WPT_Pack:
|
||||
pack.MsgProcessor.MsgRoute(pack.ClientId,pack.Data,ws.recyclerReaderBytes)
|
||||
pack.MsgProcessor.MsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WSService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){
|
||||
func (ws *WSService) SetProcessor(process processor.IProcessor, handler event.IEventHandler) {
|
||||
ws.process = process
|
||||
ws.RegEventReceiverFunc(event.Sys_Event_WebSocket,handler, ws.WSEventHandler)
|
||||
ws.RegEventReceiverFunc(event.Sys_Event_WebSocket, handler, ws.WSEventHandler)
|
||||
}
|
||||
|
||||
func (ws *WSService) NewWSClient(conn *network.WSConn) network.Agent {
|
||||
@@ -125,55 +126,55 @@ func (slf *WSClient) GetId() string {
|
||||
}
|
||||
|
||||
func (slf *WSClient) Run() {
|
||||
slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_Connected,MsgProcessor:slf.wsService.process}})
|
||||
for{
|
||||
bytes,err := slf.wsConn.ReadMsg()
|
||||
slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_Connected, MsgProcessor: slf.wsService.process}})
|
||||
for {
|
||||
bytes, err := slf.wsConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client id %s is error:%+v",slf.id,err)
|
||||
log.Debug("read client id %s is error:%+v", slf.id, err)
|
||||
break
|
||||
}
|
||||
data,err:=slf.wsService.process.Unmarshal(slf.id,bytes)
|
||||
data, err := slf.wsService.process.Unmarshal(slf.id, bytes)
|
||||
if err != nil {
|
||||
slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_UnknownPack,Data:bytes,MsgProcessor:slf.wsService.process}})
|
||||
slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_UnknownPack, Data: bytes, MsgProcessor: slf.wsService.process}})
|
||||
continue
|
||||
}
|
||||
slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_Pack,Data:data,MsgProcessor:slf.wsService.process}})
|
||||
slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_Pack, Data: data, MsgProcessor: slf.wsService.process}})
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *WSClient) OnClose(){
|
||||
slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_DisConnected,MsgProcessor:slf.wsService.process}})
|
||||
func (slf *WSClient) OnClose() {
|
||||
slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_DisConnected, MsgProcessor: slf.wsService.process}})
|
||||
slf.wsService.mapClientLocker.Lock()
|
||||
defer slf.wsService.mapClientLocker.Unlock()
|
||||
delete (slf.wsService.mapClient,slf.GetId())
|
||||
delete(slf.wsService.mapClient, slf.GetId())
|
||||
}
|
||||
|
||||
func (ws *WSService) SendMsg(clientid string,msg interface{}) error{
|
||||
func (ws *WSService) SendMsg(clientId string, msg interface{}) error {
|
||||
ws.mapClientLocker.Lock()
|
||||
client,ok := ws.mapClient[clientid]
|
||||
if ok == false{
|
||||
client, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
ws.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %s is disconnect!",clientid)
|
||||
return fmt.Errorf("client %s is disconnect", clientId)
|
||||
}
|
||||
|
||||
ws.mapClientLocker.Unlock()
|
||||
bytes,err := ws.process.Marshal(clientid,msg)
|
||||
bytes, err := ws.process.Marshal(clientId, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.wsConn.WriteMsg(bytes)
|
||||
}
|
||||
|
||||
func (ws *WSService) Close(clientid string) {
|
||||
func (ws *WSService) Close(clientId string) {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
|
||||
client,ok := ws.mapClient[clientid]
|
||||
if ok == false{
|
||||
client, ok := ws.mapClient[clientId]
|
||||
if ok == false {
|
||||
return
|
||||
}
|
||||
|
||||
if client.wsConn!=nil {
|
||||
if client.wsConn != nil {
|
||||
client.wsConn.Close()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user