mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 23:24:45 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
776b234022 | ||
|
|
a4f425bd69 |
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duanhf2012/origin/network"
|
"github.com/duanhf2012/origin/network"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"github.com/duanhf2012/origin/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageJsonInfo struct {
|
type MessageJsonInfo struct {
|
||||||
@@ -44,18 +45,18 @@ func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// must goroutine safe
|
// must goroutine safe
|
||||||
func (jsonProcessor *JsonProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
func (jsonProcessor *JsonProcessor ) MsgRoute(clientId uint64,msg interface{}) error{
|
||||||
pPackInfo := msg.(*JsonPackInfo)
|
pPackInfo := msg.(*JsonPackInfo)
|
||||||
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
v,ok := jsonProcessor.mapMsg[pPackInfo.typ]
|
||||||
if ok == false {
|
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)
|
v.msgHandler(clientId,pPackInfo.msg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) Unmarshal(data []byte) (interface{}, error) {
|
func (jsonProcessor *JsonProcessor) Unmarshal(clientId uint64,data []byte) (interface{}, error) {
|
||||||
typeStruct := struct {Type int `json:"typ"`}{}
|
typeStruct := struct {Type int `json:"typ"`}{}
|
||||||
defer jsonProcessor.ReleaseByteSlice(data)
|
defer jsonProcessor.ReleaseByteSlice(data)
|
||||||
err := json.Unmarshal(data, &typeStruct)
|
err := json.Unmarshal(data, &typeStruct)
|
||||||
@@ -78,7 +79,7 @@ func (jsonProcessor *JsonProcessor) Unmarshal(data []byte) (interface{}, error)
|
|||||||
return &JsonPackInfo{typ:msgType,msg:msgData},nil
|
return &JsonPackInfo{typ:msgType,msg:msgData},nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) Marshal(msg interface{}) ([]byte, error) {
|
func (jsonProcessor *JsonProcessor) Marshal(clientId uint64,msg interface{}) ([]byte, error) {
|
||||||
rawMsg,err := json.Marshal(msg)
|
rawMsg,err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil,err
|
return nil,err
|
||||||
@@ -103,16 +104,26 @@ func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonP
|
|||||||
return &JsonPackInfo{typ:msgType,rawMsg:msg}
|
return &JsonPackInfo{typ:msgType,rawMsg:msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId uint64,msg interface{}){
|
||||||
jsonProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
if jsonProcessor.unknownMessageHandler==nil {
|
||||||
|
log.SDebug("Unknown message received from ",clientId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonProcessor.unknownMessageHandler(clientId,msg.([]byte))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) ConnectedRoute(userData interface{}){
|
func (jsonProcessor *JsonProcessor) ConnectedRoute(clientId uint64){
|
||||||
jsonProcessor.connectHandler(userData.(uint64))
|
if jsonProcessor.connectHandler != nil {
|
||||||
|
jsonProcessor.connectHandler(clientId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) DisConnectedRoute(userData interface{}){
|
func (jsonProcessor *JsonProcessor) DisConnectedRoute(clientId uint64){
|
||||||
jsonProcessor.disconnectHandler(userData.(uint64))
|
if jsonProcessor.disconnectHandler != nil {
|
||||||
|
jsonProcessor.disconnectHandler(clientId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){
|
func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type WSClient struct {
|
|||||||
cons WebsocketConnSet
|
cons WebsocketConnSet
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
closeFlag bool
|
closeFlag bool
|
||||||
|
messageType int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *WSClient) Start() {
|
func (client *WSClient) Start() {
|
||||||
@@ -62,7 +63,7 @@ func (client *WSClient) init() {
|
|||||||
if client.cons != nil {
|
if client.cons != nil {
|
||||||
log.SFatal("client is running")
|
log.SFatal("client is running")
|
||||||
}
|
}
|
||||||
|
client.messageType = websocket.TextMessage
|
||||||
client.cons = make(WebsocketConnSet)
|
client.cons = make(WebsocketConnSet)
|
||||||
client.closeFlag = false
|
client.closeFlag = false
|
||||||
client.dialer = websocket.Dialer{
|
client.dialer = websocket.Dialer{
|
||||||
@@ -83,6 +84,9 @@ func (client *WSClient) dial() *websocket.Conn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *WSClient) SetMessageType(messageType int){
|
||||||
|
client.messageType = messageType
|
||||||
|
}
|
||||||
func (client *WSClient) connect() {
|
func (client *WSClient) connect() {
|
||||||
defer client.wg.Done()
|
defer client.wg.Done()
|
||||||
|
|
||||||
@@ -102,7 +106,7 @@ reconnect:
|
|||||||
client.cons[conn] = struct{}{}
|
client.cons[conn] = struct{}{}
|
||||||
client.Unlock()
|
client.Unlock()
|
||||||
|
|
||||||
wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen)
|
wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen,client.messageType)
|
||||||
agent := client.NewAgent(wsConn)
|
agent := client.NewAgent(wsConn)
|
||||||
agent.Run()
|
agent.Run()
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type WSConn struct {
|
|||||||
closeFlag bool
|
closeFlag bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWSConn(conn *websocket.Conn, pendingWriteNum int, maxMsgLen uint32) *WSConn {
|
func newWSConn(conn *websocket.Conn, pendingWriteNum int, maxMsgLen uint32,messageType int) *WSConn {
|
||||||
wsConn := new(WSConn)
|
wsConn := new(WSConn)
|
||||||
wsConn.conn = conn
|
wsConn.conn = conn
|
||||||
wsConn.writeChan = make(chan []byte, pendingWriteNum)
|
wsConn.writeChan = make(chan []byte, pendingWriteNum)
|
||||||
@@ -30,7 +30,7 @@ func newWSConn(conn *websocket.Conn, pendingWriteNum int, maxMsgLen uint32) *WSC
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
err := conn.WriteMessage(websocket.BinaryMessage, b)
|
err := conn.WriteMessage(messageType, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type WSServer struct {
|
|||||||
NewAgent func(*WSConn) Agent
|
NewAgent func(*WSConn) Agent
|
||||||
ln net.Listener
|
ln net.Listener
|
||||||
handler *WSHandler
|
handler *WSHandler
|
||||||
|
messageType int
|
||||||
}
|
}
|
||||||
|
|
||||||
type WSHandler struct {
|
type WSHandler struct {
|
||||||
@@ -32,6 +33,11 @@ type WSHandler struct {
|
|||||||
conns WebsocketConnSet
|
conns WebsocketConnSet
|
||||||
mutexConns sync.Mutex
|
mutexConns sync.Mutex
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
messageType int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (handler *WSHandler) SetMessageType(messageType int){
|
||||||
|
handler.messageType = messageType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -45,6 +51,7 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.SetReadLimit(int64(handler.maxMsgLen))
|
conn.SetReadLimit(int64(handler.maxMsgLen))
|
||||||
|
handler.messageType = websocket.TextMessage
|
||||||
|
|
||||||
handler.wg.Add(1)
|
handler.wg.Add(1)
|
||||||
defer handler.wg.Done()
|
defer handler.wg.Done()
|
||||||
@@ -64,7 +71,7 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
handler.conns[conn] = struct{}{}
|
handler.conns[conn] = struct{}{}
|
||||||
handler.mutexConns.Unlock()
|
handler.mutexConns.Unlock()
|
||||||
|
|
||||||
wsConn := newWSConn(conn, handler.pendingWriteNum, handler.maxMsgLen)
|
wsConn := newWSConn(conn, handler.pendingWriteNum, handler.maxMsgLen,handler.messageType)
|
||||||
agent := handler.newAgent(wsConn)
|
agent := handler.newAgent(wsConn)
|
||||||
agent.Run()
|
agent.Run()
|
||||||
|
|
||||||
@@ -76,6 +83,13 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
agent.OnClose()
|
agent.OnClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (server *WSServer) SetMessageType(messageType int){
|
||||||
|
server.messageType = messageType
|
||||||
|
if server.handler!= nil {
|
||||||
|
server.handler.SetMessageType(messageType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (server *WSServer) Start() {
|
func (server *WSServer) Start() {
|
||||||
ln, err := net.Listen("tcp", server.Addr)
|
ln, err := net.Listen("tcp", server.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/duanhf2012/origin/network/processor"
|
"github.com/duanhf2012/origin/network/processor"
|
||||||
"github.com/duanhf2012/origin/node"
|
"github.com/duanhf2012/origin/node"
|
||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
|
"sync/atomic"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -42,12 +43,12 @@ const Default_ReadDeadline = 180 //30s
|
|||||||
const Default_WriteDeadline = 180 //30s
|
const Default_WriteDeadline = 180 //30s
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MaxNodeId = 1<<10 - 1 //Uint10
|
MaxNodeId = 1<<14 - 1 //最大值 16383
|
||||||
MaxSeed = 1<<22 - 1 //MaxUint24
|
MaxSeed = 1<<19 - 1 //最大值 524287
|
||||||
|
MaxTime = 1<<31 - 1 //最大值 2147483647
|
||||||
)
|
)
|
||||||
|
|
||||||
var seed uint32
|
var seed uint32
|
||||||
var seedLocker sync.Mutex
|
|
||||||
|
|
||||||
type TcpPack struct {
|
type TcpPack struct {
|
||||||
Type TcpPackType //0表示连接 1表示断开 2表示数据
|
Type TcpPackType //0表示连接 1表示断开 2表示数据
|
||||||
@@ -66,14 +67,12 @@ func (tcpService *TcpService) genId() uint64 {
|
|||||||
panic("nodeId exceeds the maximum!")
|
panic("nodeId exceeds the maximum!")
|
||||||
}
|
}
|
||||||
|
|
||||||
seedLocker.Lock()
|
newSeed := atomic.AddUint32(&seed,1) % MaxSeed
|
||||||
seed = (seed+1)%MaxSeed
|
nowTime := uint64(time.Now().Unix())%MaxTime
|
||||||
seedLocker.Unlock()
|
return (uint64(node.GetNodeId())<<50)|(nowTime<<19)|uint64(newSeed)
|
||||||
|
|
||||||
nowTime := uint64(time.Now().Second())
|
|
||||||
return (uint64(node.GetNodeId())<<54)|(nowTime<<22)|uint64(seed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func GetNodeId(agentId uint64) int {
|
func GetNodeId(agentId uint64) int {
|
||||||
return int(agentId>>54)
|
return int(agentId>>54)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,19 +7,27 @@ import (
|
|||||||
"github.com/duanhf2012/origin/network"
|
"github.com/duanhf2012/origin/network"
|
||||||
"github.com/duanhf2012/origin/network/processor"
|
"github.com/duanhf2012/origin/network/processor"
|
||||||
"github.com/duanhf2012/origin/service"
|
"github.com/duanhf2012/origin/service"
|
||||||
|
"github.com/duanhf2012/origin/node"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type WSService struct {
|
type WSService struct {
|
||||||
service.Service
|
service.Service
|
||||||
wsServer network.WSServer
|
wsServer network.WSServer
|
||||||
|
|
||||||
mapClientLocker sync.RWMutex
|
mapClientLocker sync.RWMutex
|
||||||
mapClient map[uint64] *WSClient
|
mapClient map[uint64] *WSClient
|
||||||
initClientId uint64
|
|
||||||
process processor.IProcessor
|
process processor.IProcessor
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var seed uint32
|
||||||
|
|
||||||
type WSPackType int8
|
type WSPackType int8
|
||||||
const(
|
const(
|
||||||
WPT_Connected WSPackType = 0
|
WPT_Connected WSPackType = 0
|
||||||
@@ -32,6 +40,12 @@ const Default_WS_MaxConnNum = 3000
|
|||||||
const Default_WS_PendingWriteNum = 10000
|
const Default_WS_PendingWriteNum = 10000
|
||||||
const Default_WS_MaxMsgLen = 65535
|
const Default_WS_MaxMsgLen = 65535
|
||||||
|
|
||||||
|
const (
|
||||||
|
MaxNodeId = 1<<14 - 1 //最大值 16383
|
||||||
|
MaxSeed = 1<<19 - 1 //最大值 524287
|
||||||
|
MaxTime = 1<<31 - 1 //最大值 2147483647
|
||||||
|
)
|
||||||
|
|
||||||
type WSClient struct {
|
type WSClient struct {
|
||||||
id uint64
|
id uint64
|
||||||
wsConn *network.WSConn
|
wsConn *network.WSConn
|
||||||
@@ -46,6 +60,7 @@ type WSPack struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WSService) OnInit() error{
|
func (ws *WSService) OnInit() error{
|
||||||
|
|
||||||
iConfig := ws.GetServiceCfg()
|
iConfig := ws.GetServiceCfg()
|
||||||
if iConfig == nil {
|
if iConfig == nil {
|
||||||
return fmt.Errorf("%s service config is error!", ws.GetName())
|
return fmt.Errorf("%s service config is error!", ws.GetName())
|
||||||
@@ -80,6 +95,10 @@ func (ws *WSService) OnInit() error{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ws *WSService) SetMessageType(messageType int){
|
||||||
|
ws.wsServer.SetMessageType(messageType)
|
||||||
|
}
|
||||||
|
|
||||||
func (ws *WSService) WSEventHandler(ev event.IEvent) {
|
func (ws *WSService) WSEventHandler(ev event.IEvent) {
|
||||||
pack := ev.(*event.Event).Data.(*WSPack)
|
pack := ev.(*event.Event).Data.(*WSPack)
|
||||||
switch pack.Type {
|
switch pack.Type {
|
||||||
@@ -88,9 +107,9 @@ func (ws *WSService) WSEventHandler(ev event.IEvent) {
|
|||||||
case WPT_DisConnected:
|
case WPT_DisConnected:
|
||||||
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
|
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
|
||||||
case WPT_UnknownPack:
|
case WPT_UnknownPack:
|
||||||
pack.MsgProcessor.UnknownMsgRoute(pack.Data,pack.ClientId)
|
pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data)
|
||||||
case WPT_Pack:
|
case WPT_Pack:
|
||||||
pack.MsgProcessor.MsgRoute(pack.Data, pack.ClientId)
|
pack.MsgProcessor.MsgRoute(pack.ClientId,pack.Data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,20 +118,30 @@ func (ws *WSService) SetProcessor(process processor.IProcessor,handler event.IEv
|
|||||||
ws.RegEventReceiverFunc(event.Sys_Event_WebSocket,handler, ws.WSEventHandler)
|
ws.RegEventReceiverFunc(event.Sys_Event_WebSocket,handler, ws.WSEventHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ws *WSService) genId() uint64 {
|
||||||
|
if node.GetNodeId()>MaxNodeId{
|
||||||
|
panic("nodeId exceeds the maximum!")
|
||||||
|
}
|
||||||
|
|
||||||
|
newSeed := atomic.AddUint32(&seed,1) % MaxSeed
|
||||||
|
nowTime := uint64(time.Now().Unix())%MaxTime
|
||||||
|
return (uint64(node.GetNodeId())<<50)|(nowTime<<19)|uint64(newSeed)
|
||||||
|
}
|
||||||
|
|
||||||
func (ws *WSService) NewWSClient(conn *network.WSConn) network.Agent {
|
func (ws *WSService) NewWSClient(conn *network.WSConn) network.Agent {
|
||||||
ws.mapClientLocker.Lock()
|
ws.mapClientLocker.Lock()
|
||||||
defer ws.mapClientLocker.Unlock()
|
defer ws.mapClientLocker.Unlock()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
ws.initClientId+=1
|
clientId := ws.genId()
|
||||||
_,ok := ws.mapClient[ws.initClientId]
|
_,ok := ws.mapClient[clientId]
|
||||||
if ok == true {
|
if ok == true {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pClient := &WSClient{wsConn:conn, id: ws.initClientId}
|
pClient := &WSClient{wsConn:conn, id: clientId}
|
||||||
pClient.wsService = ws
|
pClient.wsService = ws
|
||||||
ws.mapClient[ws.initClientId] = pClient
|
ws.mapClient[clientId] = pClient
|
||||||
return pClient
|
return pClient
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +160,7 @@ func (slf *WSClient) Run() {
|
|||||||
log.Debug("read client id %d is error:%+v",slf.id,err)
|
log.Debug("read client id %d is error:%+v",slf.id,err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
data,err:=slf.wsService.process.Unmarshal(bytes)
|
data,err:=slf.wsService.process.Unmarshal(slf.id,bytes)
|
||||||
if err != nil {
|
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
|
continue
|
||||||
@@ -156,7 +185,7 @@ func (ws *WSService) SendMsg(clientid uint64,msg interface{}) error{
|
|||||||
}
|
}
|
||||||
|
|
||||||
ws.mapClientLocker.Unlock()
|
ws.mapClientLocker.Unlock()
|
||||||
bytes,err := ws.process.Marshal(msg)
|
bytes,err := ws.process.Marshal(clientid,msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user