mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-20 03:24:43 +08:00
新增TcpGateWay服务,支持通过配置进行路由转发
This commit is contained in:
77
sysservice/tcpgateway/GateProxyModule.go
Normal file
77
sysservice/tcpgateway/GateProxyModule.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package tcpgateway
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/origin/sysservice/tcpservice"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
type GateProxyModule struct{
|
||||
service.Module
|
||||
defaultGateRpc string
|
||||
}
|
||||
|
||||
func NewGateProxyModule() *GateProxyModule{
|
||||
return &GateProxyModule{defaultGateRpc:"TcpGateService.RPC_Dispatch"}
|
||||
}
|
||||
|
||||
func (slf *GateProxyModule) Send(clientId interface{},msgType uint16,msg proto.Message) error {
|
||||
//对agentId进行分组
|
||||
mapNodeClientId := map[int][]uint64{}
|
||||
switch clientId.(type) {
|
||||
case uint64:
|
||||
id := clientId.(uint64)
|
||||
nodeId := tcpservice.GetNodeId(id)
|
||||
mapNodeClientId[nodeId] = append(mapNodeClientId[nodeId],id)
|
||||
|
||||
case []uint64:
|
||||
idList := clientId.([]uint64)
|
||||
for _,id := range idList{
|
||||
nodeId := tcpservice.GetNodeId(id)
|
||||
mapNodeClientId[nodeId] = append(mapNodeClientId[nodeId],id)
|
||||
}
|
||||
}
|
||||
|
||||
bData,err := proto.Marshal(msg)
|
||||
if err!=nil {
|
||||
return err
|
||||
}
|
||||
var replyMsg ReplyMessage
|
||||
replyMsg.MsgType = proto.Uint32(uint32(msgType))
|
||||
replyMsg.Msg = bData
|
||||
|
||||
|
||||
for nodeId,clientIdList := range mapNodeClientId {
|
||||
if nodeId <0 || nodeId>tcpservice.MaxNodeId {
|
||||
fmt.Errorf("nodeid is error %d",nodeId)
|
||||
continue
|
||||
}
|
||||
|
||||
replyMsg.ClientList = clientIdList
|
||||
slf.GetService().GetRpcHandler().GoNode(nodeId,slf.defaultGateRpc,&replyMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (slf *GateProxyModule) SetDefaultGateRpcMethodName(rpcMethodName string){
|
||||
slf.defaultGateRpc = rpcMethodName
|
||||
}
|
||||
|
||||
|
||||
func (slf *GateProxyModule) send(clientId uint64,msgType uint16,msg []byte) error {
|
||||
nodeId := tcpservice.GetNodeId(clientId)
|
||||
if nodeId <0 || nodeId>tcpservice.MaxNodeId {
|
||||
return fmt.Errorf("nodeid is error %d",nodeId)
|
||||
}
|
||||
|
||||
var replyMsg ReplyMessage
|
||||
replyMsg.MsgType = proto.Uint32(uint32(msgType))
|
||||
replyMsg.Msg = msg
|
||||
replyMsg.ClientList = append(replyMsg.ClientList ,clientId)
|
||||
|
||||
return slf.GetService().GetRpcHandler().GoNode(nodeId,slf.defaultGateRpc,&replyMsg)
|
||||
}
|
||||
Reference in New Issue
Block a user