mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-05 21:57:33 +08:00
优化工程结构
This commit is contained in:
@@ -17,11 +17,9 @@ import (
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
var Default_ReadTimeout time.Duration = time.Second*10
|
||||
var Default_WriteTimeout time.Duration = time.Second*10
|
||||
var Default_ProcessTimeout time.Duration = time.Second*10
|
||||
|
||||
var Default_HttpRouter *HttpRouter= &HttpRouter{}
|
||||
var DefaultReadTimeout time.Duration = time.Second*10
|
||||
var DefaultWriteTimeout time.Duration = time.Second*10
|
||||
var DefaultProcessTimeout time.Duration = time.Second*10
|
||||
|
||||
//http redirect
|
||||
type HttpRedirectData struct {
|
||||
@@ -40,13 +38,11 @@ const (
|
||||
)
|
||||
|
||||
type HttpHandle func(session *HttpSession)
|
||||
|
||||
type routerMatchData struct {
|
||||
matchURL string
|
||||
httpHandle HttpHandle
|
||||
}
|
||||
|
||||
|
||||
type routerServeFileData struct {
|
||||
matchUrl string
|
||||
localPath string
|
||||
@@ -54,7 +50,6 @@ type routerServeFileData struct {
|
||||
}
|
||||
|
||||
type IHttpRouter interface {
|
||||
//RegRouter(method HTTP_METHOD, url string, handle HttpHandle) bool
|
||||
GET(url string, handle HttpHandle) bool
|
||||
POST(url string, handle HttpHandle) bool
|
||||
Router(session *HttpSession)
|
||||
@@ -65,11 +60,9 @@ type IHttpRouter interface {
|
||||
AddHttpFiltrate(FiltrateFun HttpFiltrate) bool
|
||||
}
|
||||
|
||||
|
||||
type HttpRouter struct {
|
||||
pathRouter map[HTTP_METHOD] map[string] routerMatchData //url地址,对应本service地址
|
||||
serveFileData map[string] *routerServeFileData
|
||||
//eventReciver event.IEventHandler
|
||||
httpFiltrateList [] HttpFiltrate
|
||||
|
||||
formFileKey string
|
||||
@@ -104,13 +97,18 @@ type HttpService struct {
|
||||
processTimeout time.Duration
|
||||
}
|
||||
|
||||
func (slf *HttpService) AddFiltrate(FiltrateFun HttpFiltrate) bool {
|
||||
return slf.httpRouter.AddHttpFiltrate(FiltrateFun)
|
||||
type HttpFiltrate func(session *HttpSession) bool //true is pass
|
||||
|
||||
type CORSHeader struct {
|
||||
AllowCORSHeader map[string][]string
|
||||
}
|
||||
|
||||
func (httpService *HttpService) AddFiltrate(FiltrateFun HttpFiltrate) bool {
|
||||
return httpService.httpRouter.AddHttpFiltrate(FiltrateFun)
|
||||
}
|
||||
|
||||
func NewHttpHttpRouter() IHttpRouter {
|
||||
httpRouter := &HttpRouter{}
|
||||
//httpRouter.eventReciver = eventHandler
|
||||
httpRouter.pathRouter =map[HTTP_METHOD] map[string] routerMatchData{}
|
||||
httpRouter.serveFileData = map[string] *routerServeFileData{}
|
||||
httpRouter.formFileKey = "file"
|
||||
@@ -118,14 +116,10 @@ func NewHttpHttpRouter() IHttpRouter {
|
||||
httpRouter.pathRouter[i] = map[string] routerMatchData{}
|
||||
}
|
||||
|
||||
|
||||
return httpRouter
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (slf *HttpSession) Query(key string) (string, bool) {
|
||||
|
||||
if slf.mapParam == nil {
|
||||
slf.mapParam = make(map[string]string)
|
||||
|
||||
@@ -211,8 +205,6 @@ func (slf *HttpSession) getMethod(method string) HTTP_METHOD {
|
||||
return METHOD_INVALID
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (slf *HttpRouter) analysisRouterUrl(url string) (string, error) {
|
||||
|
||||
//替换所有空格
|
||||
@@ -237,7 +229,6 @@ func (slf *HttpRouter) GetFormFileKey()string{
|
||||
return slf.formFileKey
|
||||
}
|
||||
|
||||
|
||||
func (slf *HttpRouter) GET(url string, handle HttpHandle) bool {
|
||||
return slf.regRouter(METHOD_GET, url, handle)
|
||||
}
|
||||
@@ -279,7 +270,6 @@ func (slf *HttpRouter) Router(session *HttpSession){
|
||||
}
|
||||
|
||||
v.httpHandle(session)
|
||||
//session.done()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -296,19 +286,15 @@ func (slf *HttpRouter) Router(session *HttpSession){
|
||||
session.Done()
|
||||
}
|
||||
|
||||
|
||||
func (slf *HttpService) HttpEventHandler(ev *event.Event) {
|
||||
func (httpService *HttpService) HttpEventHandler(ev *event.Event) {
|
||||
ev.Data.(*HttpSession).Handle()
|
||||
}
|
||||
|
||||
func (slf *HttpService) SetHttpRouter(httpRouter IHttpRouter,eventHandler event.IEventHandler) {
|
||||
slf.httpRouter = httpRouter
|
||||
slf.RegEventReciverFunc(event.Sys_Event_Http_Event,eventHandler,slf.HttpEventHandler)
|
||||
func (httpService *HttpService) SetHttpRouter(httpRouter IHttpRouter,eventHandler event.IEventHandler) {
|
||||
httpService.httpRouter = httpRouter
|
||||
httpService.RegEventReceiverFunc(event.Sys_Event_Http_Event,eventHandler, httpService.HttpEventHandler)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (slf *HttpRouter) SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error {
|
||||
_, err := os.Stat(dirname)
|
||||
if err != nil {
|
||||
@@ -328,9 +314,6 @@ func (slf *HttpRouter) SetServeFile(method HTTP_METHOD, urlpath string, dirname
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
type HttpFiltrate func(session *HttpSession) bool //true is pass
|
||||
|
||||
func (slf *HttpRouter) AddHttpFiltrate(FiltrateFun HttpFiltrate) bool {
|
||||
slf.httpFiltrateList = append(slf.httpFiltrateList, FiltrateFun)
|
||||
return false
|
||||
@@ -359,20 +342,18 @@ func (slf *HttpSession) redirects() {
|
||||
http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (slf *HttpService) OnInit() error {
|
||||
iConfig := slf.GetServiceCfg()
|
||||
func (httpService *HttpService) OnInit() error {
|
||||
iConfig := httpService.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
return fmt.Errorf("%s service config is error!", httpService.GetName())
|
||||
}
|
||||
tcpCfg := iConfig.(map[string]interface{})
|
||||
addr,ok := tcpCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
return fmt.Errorf("%s service config is error!", httpService.GetName())
|
||||
}
|
||||
var readTimeout time.Duration = Default_ReadTimeout
|
||||
var writeTimeout time.Duration = Default_WriteTimeout
|
||||
var readTimeout time.Duration = DefaultReadTimeout
|
||||
var writeTimeout time.Duration = DefaultWriteTimeout
|
||||
|
||||
if cfgRead,ok := tcpCfg["ReadTimeout"];ok == true {
|
||||
readTimeout = time.Duration(cfgRead.(float64))*time.Millisecond
|
||||
@@ -382,12 +363,12 @@ func (slf *HttpService) OnInit() error {
|
||||
writeTimeout = time.Duration(cfgWrite.(float64))*time.Millisecond
|
||||
}
|
||||
|
||||
slf.processTimeout = Default_ProcessTimeout
|
||||
httpService.processTimeout = DefaultProcessTimeout
|
||||
if cfgProcessTimeout,ok := tcpCfg["ProcessTimeout"];ok == true {
|
||||
slf.processTimeout = time.Duration(cfgProcessTimeout.(float64))*time.Millisecond
|
||||
httpService.processTimeout = time.Duration(cfgProcessTimeout.(float64))*time.Millisecond
|
||||
}
|
||||
|
||||
slf.httpServer.Init(addr.(string), slf, readTimeout, writeTimeout)
|
||||
httpService.httpServer.Init(addr.(string), httpService, readTimeout, writeTimeout)
|
||||
//Set CAFile
|
||||
caFileList,ok := tcpCfg["CAFile"]
|
||||
if ok == false {
|
||||
@@ -408,24 +389,24 @@ func (slf *HttpService) OnInit() error {
|
||||
|
||||
if c.(string)!="" && k.(string)!="" {
|
||||
caFile = append(caFile,network.CAFile{
|
||||
Certfile: c.(string),
|
||||
CertFile: c.(string),
|
||||
Keyfile: k.(string),
|
||||
})
|
||||
}
|
||||
}
|
||||
slf.httpServer.SetCAFile(caFile)
|
||||
slf.httpServer.Start()
|
||||
httpService.httpServer.SetCAFile(caFile)
|
||||
httpService.httpServer.Start()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *HttpService) SetAllowCORS(corsHeader *CORSHeader) {
|
||||
slf.corsHeader = corsHeader
|
||||
func (httpService *HttpService) SetAllowCORS(corsHeader *CORSHeader) {
|
||||
httpService.corsHeader = corsHeader
|
||||
}
|
||||
|
||||
func (slf *HttpService) ProcessFile(session *HttpSession){
|
||||
upath := session.r.URL.Path
|
||||
idx := strings.Index(upath, session.fileData.matchUrl)
|
||||
subPath := strings.Trim(upath[idx+len(session.fileData.matchUrl):], "/")
|
||||
func (httpService *HttpService) ProcessFile(session *HttpSession){
|
||||
uPath := session.r.URL.Path
|
||||
idx := strings.Index(uPath, session.fileData.matchUrl)
|
||||
subPath := strings.Trim(uPath[idx+len(session.fileData.matchUrl):], "/")
|
||||
|
||||
destLocalPath := session.fileData.localPath + "/"+subPath
|
||||
|
||||
@@ -461,26 +442,21 @@ func (slf *HttpService) ProcessFile(session *HttpSession){
|
||||
filePrefixName := uuid.Rand().HexEx()
|
||||
fileName := filePrefixName + "." + imgFormat[len(imgFormat)-1]
|
||||
//创建文件
|
||||
localpath := fmt.Sprintf("%s%s", destLocalPath, fileName)
|
||||
localfd, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
localPath := fmt.Sprintf("%s%s", destLocalPath, fileName)
|
||||
localFd, err := os.OpenFile(localPath, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
session.WriteStatusCode(http.StatusNotFound)
|
||||
session.flush()
|
||||
return
|
||||
}
|
||||
defer localfd.Close()
|
||||
io.Copy(localfd, resourceFile)
|
||||
defer localFd.Close()
|
||||
io.Copy(localFd, resourceFile)
|
||||
session.WriteStatusCode(http.StatusOK)
|
||||
session.Write([]byte(upath+"/"+fileName))
|
||||
session.Write([]byte(uPath+"/"+fileName))
|
||||
session.flush()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type CORSHeader struct {
|
||||
AllowCORSHeader map[string][]string
|
||||
}
|
||||
|
||||
func NewAllowCORSHeader() *CORSHeader{
|
||||
header := &CORSHeader{}
|
||||
header.AllowCORSHeader = map[string][]string{}
|
||||
@@ -503,17 +479,17 @@ func (slf *CORSHeader) copyTo(header http.Header){
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if slf.corsHeader != nil {
|
||||
func (httpService *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if httpService.corsHeader != nil {
|
||||
if origin := r.Header.Get("Origin"); origin != "" {
|
||||
slf.corsHeader.copyTo(w.Header())
|
||||
httpService.corsHeader.copyTo(w.Header())
|
||||
}
|
||||
}
|
||||
if r.Method == "OPTIONS" {
|
||||
return
|
||||
}
|
||||
|
||||
session := &HttpSession{sessionDone:make(chan *HttpSession,1),httpRouter:slf.httpRouter,statusCode:http.StatusOK}
|
||||
session := &HttpSession{sessionDone:make(chan *HttpSession,1),httpRouter:httpService.httpRouter,statusCode:http.StatusOK}
|
||||
session.r = r
|
||||
session.w = w
|
||||
|
||||
@@ -526,8 +502,8 @@ func (slf *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
session.body = body
|
||||
|
||||
slf.GetEventHandler().NotifyEvent(&event.Event{Type:event.Sys_Event_Http_Event,Data:session})
|
||||
ticker := time.NewTicker(slf.processTimeout)
|
||||
httpService.GetEventHandler().NotifyEvent(&event.Event{Type:event.Sys_Event_Http_Event,Data:session})
|
||||
ticker := time.NewTicker(httpService.processTimeout)
|
||||
select {
|
||||
case <-ticker.C:
|
||||
session.WriteStatusCode(http.StatusGatewayTimeout)
|
||||
@@ -535,11 +511,11 @@ func (slf *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
break
|
||||
case <- session.sessionDone:
|
||||
if session.fileData!=nil {
|
||||
slf.ProcessFile(session)
|
||||
httpService.ProcessFile(session)
|
||||
}else if session.redirectData!=nil {
|
||||
session.redirects()
|
||||
}else{
|
||||
session.flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ func NewGateProxyModule() *GateProxyModule{
|
||||
return &GateProxyModule{defaultGateRpc:"TcpGateService.RPC_Dispatch"}
|
||||
}
|
||||
|
||||
func (slf *GateProxyModule) Send(clientId interface{},msgType uint16,msg proto.Message) error {
|
||||
func (gate *GateProxyModule) Send(clientId interface{},msgType uint16,msg proto.Message) error {
|
||||
//对agentId进行分组
|
||||
mapNodeClientId := map[int][]uint64{}
|
||||
switch clientId.(type) {
|
||||
@@ -41,7 +41,6 @@ func (slf *GateProxyModule) Send(clientId interface{},msgType uint16,msg proto.M
|
||||
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)
|
||||
@@ -49,20 +48,17 @@ func (slf *GateProxyModule) Send(clientId interface{},msgType uint16,msg proto.M
|
||||
}
|
||||
|
||||
replyMsg.ClientList = clientIdList
|
||||
slf.GetService().GetRpcHandler().GoNode(nodeId,slf.defaultGateRpc,&replyMsg)
|
||||
gate.GetService().GetRpcHandler().GoNode(nodeId,gate.defaultGateRpc,&replyMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (slf *GateProxyModule) SetDefaultGateRpcMethodName(rpcMethodName string){
|
||||
slf.defaultGateRpc = rpcMethodName
|
||||
func (gate *GateProxyModule) SetDefaultGateRpcMethodName(rpcMethodName string){
|
||||
gate.defaultGateRpc = rpcMethodName
|
||||
}
|
||||
|
||||
|
||||
func (slf *GateProxyModule) send(clientId uint64,msgType uint16,msg []byte) error {
|
||||
func (gate *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)
|
||||
@@ -73,5 +69,5 @@ func (slf *GateProxyModule) send(clientId uint64,msgType uint16,msg []byte) erro
|
||||
replyMsg.Msg = msg
|
||||
replyMsg.ClientList = append(replyMsg.ClientList ,clientId)
|
||||
|
||||
return slf.GetService().GetRpcHandler().GoNode(nodeId,slf.defaultGateRpc,&replyMsg)
|
||||
return gate.GetService().GetRpcHandler().GoNode(nodeId, gate.defaultGateRpc,&replyMsg)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@ package tcpgateway
|
||||
type LoadBalance struct {
|
||||
}
|
||||
|
||||
func (slf *LoadBalance) SelectNode(serviceName string) int {
|
||||
func (balance *LoadBalance) SelectNode(serviceName string) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -46,9 +46,9 @@ func NewRouter(loadBalance ILoadBalance,rpcHandler rpc.IRpcHandler,cfg interface
|
||||
return router
|
||||
}
|
||||
|
||||
func (slf *Router) loadCfg(cfg interface{}){
|
||||
slf.mapMsgRouterInfo = map[uint16]*MsgRouterInfo{}
|
||||
slf.mapEventRouterInfo = map[string]*EventRouterInfo{}
|
||||
func (r *Router) loadCfg(cfg interface{}){
|
||||
r.mapMsgRouterInfo = map[uint16]*MsgRouterInfo{}
|
||||
r.mapEventRouterInfo = map[string]*EventRouterInfo{}
|
||||
|
||||
mapRouter,ok := cfg.(map[string]interface{})
|
||||
if ok == false{
|
||||
@@ -100,7 +100,7 @@ func (slf *Router) loadCfg(cfg interface{}){
|
||||
continue
|
||||
}
|
||||
|
||||
slf.mapMsgRouterInfo[uint16(msgId)] = &MsgRouterInfo{ServiceName:strService[0],Rpc: iRpc.(string),LoadBalanceType: iLoadBalanceType.(string)}
|
||||
r.mapMsgRouterInfo[uint16(msgId)] = &MsgRouterInfo{ServiceName: strService[0],Rpc: iRpc.(string),LoadBalanceType: iLoadBalanceType.(string)}
|
||||
}
|
||||
|
||||
//parse EventRouter
|
||||
@@ -147,12 +147,12 @@ func (slf *Router) loadCfg(cfg interface{}){
|
||||
continue
|
||||
}
|
||||
|
||||
slf.mapEventRouterInfo[strEventType] = &EventRouterInfo{ServiceName:strService[0],Rpc: iRpc.(string),LoadBalanceType: iLoadBalanceType.(string)}
|
||||
r.mapEventRouterInfo[strEventType] = &EventRouterInfo{ServiceName: strService[0],Rpc: iRpc.(string),LoadBalanceType: iLoadBalanceType.(string)}
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *Router) GetMsgRouterService(msgType uint16) *MsgRouterInfo{
|
||||
info,ok := slf.mapMsgRouterInfo[msgType]
|
||||
func (r *Router) GetMsgRouterService(msgType uint16) *MsgRouterInfo{
|
||||
info,ok := r.mapMsgRouterInfo[msgType]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
@@ -160,8 +160,8 @@ func (slf *Router) GetMsgRouterService(msgType uint16) *MsgRouterInfo{
|
||||
return info
|
||||
}
|
||||
|
||||
func (slf *Router) GetEventRouterService(eventType string) *EventRouterInfo{
|
||||
info,ok := slf.mapEventRouterInfo[eventType]
|
||||
func (r *Router) GetEventRouterService(eventType string) *EventRouterInfo{
|
||||
info,ok := r.mapEventRouterInfo[eventType]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
@@ -169,8 +169,8 @@ func (slf *Router) GetEventRouterService(eventType string) *EventRouterInfo{
|
||||
return info
|
||||
}
|
||||
|
||||
func (slf *Router) GetRouterId(clientId uint64,serviceName *string) int {
|
||||
mapServiceRouter,ok := slf.mapClientRouterCache[clientId]
|
||||
func (r *Router) GetRouterId(clientId uint64,serviceName *string) int {
|
||||
mapServiceRouter,ok := r.mapClientRouterCache[clientId]
|
||||
if ok == false{
|
||||
return 0
|
||||
}
|
||||
@@ -183,46 +183,46 @@ func (slf *Router) GetRouterId(clientId uint64,serviceName *string) int {
|
||||
return routerId
|
||||
}
|
||||
|
||||
func (slf *Router) SetRouterId(clientId uint64,serviceName *string,routerId int){
|
||||
slf.mapClientRouterCache[clientId][*serviceName] = routerId
|
||||
func (r *Router) SetRouterId(clientId uint64,serviceName *string,routerId int){
|
||||
r.mapClientRouterCache[clientId][*serviceName] = routerId
|
||||
}
|
||||
|
||||
func (slf *Router) RouterMessage(clientId uint64,msgType uint16,msg []byte) {
|
||||
routerInfo:= slf.GetMsgRouterService(msgType)
|
||||
func (r *Router) RouterMessage(clientId uint64,msgType uint16,msg []byte) {
|
||||
routerInfo:= r.GetMsgRouterService(msgType)
|
||||
if routerInfo==nil {
|
||||
log.Error("The message type is %d with no configured route!",msgType)
|
||||
return
|
||||
}
|
||||
|
||||
routerId := slf.GetRouterId(clientId,&routerInfo.ServiceName)
|
||||
routerId := r.GetRouterId(clientId,&routerInfo.ServiceName)
|
||||
if routerId ==0 {
|
||||
routerId = slf.loadBalance.SelectNode(routerInfo.ServiceName)
|
||||
slf.SetRouterId(clientId,&routerInfo.ServiceName,routerId)
|
||||
routerId = r.loadBalance.SelectNode(routerInfo.ServiceName)
|
||||
r.SetRouterId(clientId,&routerInfo.ServiceName,routerId)
|
||||
}
|
||||
|
||||
if routerId>0 {
|
||||
slf.rpcHandler.RawGoNode(rpc.RPC_PROCESSOR_PB,routerId,routerInfo.Rpc,msg,proto.Uint64(clientId))
|
||||
r.rpcHandler.RawGoNode(rpc.RpcProcessorPb,routerId,routerInfo.Rpc,msg,proto.Uint64(clientId))
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *Router) Load(){
|
||||
func (r *Router) Load(){
|
||||
}
|
||||
|
||||
func (slf *Router) RouterEvent(clientId uint64,eventType string) bool{
|
||||
routerInfo:= slf.GetEventRouterService(eventType)
|
||||
func (r *Router) RouterEvent(clientId uint64,eventType string) bool{
|
||||
routerInfo:= r.GetEventRouterService(eventType)
|
||||
if routerInfo==nil {
|
||||
log.Error("The event type is %s with no register!",eventType)
|
||||
return false
|
||||
}
|
||||
|
||||
routerId := slf.GetRouterId(clientId,&routerInfo.ServiceName)
|
||||
routerId := r.GetRouterId(clientId,&routerInfo.ServiceName)
|
||||
if routerId ==0 {
|
||||
routerId = slf.loadBalance.SelectNode(routerInfo.ServiceName)
|
||||
slf.SetRouterId(clientId,&routerInfo.ServiceName,routerId)
|
||||
routerId = r.loadBalance.SelectNode(routerInfo.ServiceName)
|
||||
r.SetRouterId(clientId,&routerInfo.ServiceName,routerId)
|
||||
}
|
||||
|
||||
if routerId>0 {
|
||||
slf.rpcHandler.RawGoNode(rpc.RPC_PROCESSOR_PB,routerId,routerInfo.Rpc,[]byte{},proto.Uint64(clientId))
|
||||
r.rpcHandler.RawGoNode(rpc.RpcProcessorPb,routerId,routerInfo.Rpc,[]byte{},proto.Uint64(clientId))
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -230,13 +230,13 @@ func (slf *Router) RouterEvent(clientId uint64,eventType string) bool{
|
||||
}
|
||||
|
||||
|
||||
func (slf *Router) OnDisconnected(clientId uint64){
|
||||
delete(slf.mapClientRouterCache,clientId)
|
||||
func (r *Router) OnDisconnected(clientId uint64){
|
||||
delete(r.mapClientRouterCache,clientId)
|
||||
//通知事件
|
||||
slf.RouterEvent(clientId,"DisConnect")
|
||||
r.RouterEvent(clientId,"DisConnect")
|
||||
}
|
||||
|
||||
func (slf *Router) OnConnected(clientId uint64){
|
||||
slf.mapClientRouterCache[clientId] = map[string]int{}
|
||||
slf.RouterEvent(clientId,"Connect")
|
||||
func (r *Router) OnConnected(clientId uint64){
|
||||
r.mapClientRouterCache[clientId] = map[string]int{}
|
||||
r.RouterEvent(clientId,"Connect")
|
||||
}
|
||||
|
||||
@@ -23,76 +23,75 @@ type TcpGateService struct {
|
||||
|
||||
processor processor.IRawProcessor
|
||||
tcpService *tcpservice.TcpService
|
||||
|
||||
loadBalance ILoadBalance
|
||||
router IRouter
|
||||
}
|
||||
|
||||
|
||||
func (slf *TcpGateService) OnInit() error {
|
||||
slf.OnLoad()
|
||||
func (gateService *TcpGateService) OnInit() error {
|
||||
gateService.OnLoad()
|
||||
|
||||
//注册监听客户连接断开事件
|
||||
slf.processor.SetDisConnectedHandler(slf.router.OnDisconnected)
|
||||
gateService.processor.SetDisConnectedHandler(gateService.router.OnDisconnected)
|
||||
//注册监听客户连接事件
|
||||
slf.processor.SetConnectedHandler(slf.router.OnConnected)
|
||||
gateService.processor.SetConnectedHandler(gateService.router.OnConnected)
|
||||
|
||||
//注册监听消息类型MsgType_MsgReq,并注册回调
|
||||
slf.processor.SetRawMsgHandler(slf.router.RouterMessage)
|
||||
gateService.processor.SetRawMsgHandler(gateService.router.RouterMessage)
|
||||
//将protobuf消息处理器设置到TcpService服务中
|
||||
slf.tcpService.SetProcessor(slf.processor,slf.GetEventHandler())
|
||||
gateService.tcpService.SetProcessor(gateService.processor, gateService.GetEventHandler())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *TcpGateService) OnLoad() {
|
||||
func (gateService *TcpGateService) OnLoad() {
|
||||
//设置默认LoadBalance
|
||||
if slf.loadBalance == nil {
|
||||
slf.loadBalance = &LoadBalance{}
|
||||
if gateService.loadBalance == nil {
|
||||
gateService.loadBalance = &LoadBalance{}
|
||||
}
|
||||
|
||||
//设置默认Router
|
||||
if slf.router == nil {
|
||||
slf.router = NewRouter(slf.loadBalance,slf,slf.GetServiceCfg())
|
||||
if gateService.router == nil {
|
||||
gateService.router = NewRouter(gateService.loadBalance, gateService, gateService.GetServiceCfg())
|
||||
}
|
||||
|
||||
//新建内置的protobuf处理器,您也可以自定义路由器,比如json
|
||||
if slf.processor == nil {
|
||||
slf.processor = processor.NewPBRawProcessor()
|
||||
if gateService.processor == nil {
|
||||
gateService.processor = processor.NewPBRawProcessor()
|
||||
}
|
||||
|
||||
//加载路由
|
||||
slf.router.Load()
|
||||
gateService.router.Load()
|
||||
|
||||
//设置默认的TcpService服务
|
||||
if slf.tcpService == nil {
|
||||
slf.tcpService = node.GetService("TcpService").(*tcpservice.TcpService)
|
||||
if gateService.tcpService == nil {
|
||||
gateService.tcpService = node.GetService("TcpService").(*tcpservice.TcpService)
|
||||
}
|
||||
|
||||
if slf.tcpService == nil {
|
||||
if gateService.tcpService == nil {
|
||||
panic("TcpService is not installed!")
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *TcpGateService) SetLoadBalance(loadBalance ILoadBalance){
|
||||
slf.loadBalance = loadBalance
|
||||
func (gateService *TcpGateService) SetLoadBalance(loadBalance ILoadBalance){
|
||||
gateService.loadBalance = loadBalance
|
||||
}
|
||||
|
||||
func (slf *TcpGateService) SetRouter(router IRouter){
|
||||
slf.router = router
|
||||
func (gateService *TcpGateService) SetRouter(router IRouter){
|
||||
gateService.router = router
|
||||
}
|
||||
|
||||
func (slf *TcpGateService) SetRawProcessor(processor processor.IRawProcessor){
|
||||
slf.processor = processor
|
||||
func (gateService *TcpGateService) SetRawProcessor(processor processor.IRawProcessor){
|
||||
gateService.processor = processor
|
||||
}
|
||||
|
||||
func (slf *TcpGateService) SetTcpGateService(tcpService *tcpservice.TcpService){
|
||||
slf.tcpService = tcpService
|
||||
func (gateService *TcpGateService) SetTcpGateService(tcpService *tcpservice.TcpService){
|
||||
gateService.tcpService = tcpService
|
||||
}
|
||||
|
||||
func (slf *TcpGateService) RPC_Dispatch(replyMsg *ReplyMessage) error {
|
||||
func (gateService *TcpGateService) RPC_Dispatch(replyMsg *ReplyMessage) error {
|
||||
for _,id := range replyMsg.ClientList {
|
||||
err := slf.tcpService.SendRawMsg(id,replyMsg.Msg)
|
||||
err := gateService.tcpService.SendRawMsg(id,replyMsg.Msg)
|
||||
if err != nil {
|
||||
log.Debug("SendRawMsg fail:%+v!",err)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ type TcpService struct {
|
||||
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[uint64] *Client
|
||||
//initClientId uint64
|
||||
process processor.IProcessor
|
||||
}
|
||||
|
||||
@@ -30,13 +29,6 @@ const(
|
||||
TPT_UnknownPack TcpPackType = 3
|
||||
)
|
||||
|
||||
type TcpPack struct {
|
||||
Type TcpPackType //0表示连接 1表示断开 2表示数据
|
||||
MsgProcessor processor.IProcessor
|
||||
ClientId uint64
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
const Default_MaxConnNum = 3000
|
||||
const Default_PendingWriteNum = 10000
|
||||
const Default_LittleEndian = false
|
||||
@@ -47,10 +39,24 @@ const (
|
||||
MaxNodeId = 1<<10 - 1 //Uint10
|
||||
MaxSeed = 1<<22 - 1 //MaxUint24
|
||||
)
|
||||
|
||||
var seed uint32
|
||||
var seedLocker sync.Mutex
|
||||
|
||||
func (slf *TcpService) genId() uint64 {
|
||||
type TcpPack struct {
|
||||
Type TcpPackType //0表示连接 1表示断开 2表示数据
|
||||
MsgProcessor processor.IProcessor
|
||||
ClientId uint64
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
id uint64
|
||||
tcpConn *network.TCPConn
|
||||
tcpService *TcpService
|
||||
}
|
||||
|
||||
func (tcpService *TcpService) genId() uint64 {
|
||||
if node.GetNodeId()>MaxNodeId{
|
||||
panic("nodeId exceeds the maximum!")
|
||||
}
|
||||
@@ -60,7 +66,6 @@ func (slf *TcpService) genId() uint64 {
|
||||
seedLocker.Unlock()
|
||||
|
||||
nowTime := uint64(time.Now().Second())
|
||||
|
||||
return (uint64(node.GetNodeId())<<54)|(nowTime<<22)|uint64(seed)
|
||||
}
|
||||
|
||||
@@ -68,49 +73,51 @@ func GetNodeId(agentId uint64) int {
|
||||
return int(agentId>>54)
|
||||
}
|
||||
|
||||
func (slf *TcpService) OnInit() error{
|
||||
iConfig := slf.GetServiceCfg()
|
||||
func (tcpService *TcpService) OnInit() error{
|
||||
iConfig := tcpService.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
return fmt.Errorf("%s service config is error!", tcpService.GetName())
|
||||
}
|
||||
tcpCfg := iConfig.(map[string]interface{})
|
||||
addr,ok := tcpCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
return fmt.Errorf("%s service config is error!", tcpService.GetName())
|
||||
}
|
||||
slf.tcpServer.Addr = addr.(string)
|
||||
slf.tcpServer.MaxConnNum = Default_MaxConnNum
|
||||
slf.tcpServer.PendingWriteNum = Default_PendingWriteNum
|
||||
slf.tcpServer.LittleEndian = Default_LittleEndian
|
||||
slf.tcpServer.MinMsgLen = Default_MinMsgLen
|
||||
slf.tcpServer.MaxMsgLen = Default_MaxMsgLen
|
||||
|
||||
tcpService.tcpServer.Addr = addr.(string)
|
||||
tcpService.tcpServer.MaxConnNum = Default_MaxConnNum
|
||||
tcpService.tcpServer.PendingWriteNum = Default_PendingWriteNum
|
||||
tcpService.tcpServer.LittleEndian = Default_LittleEndian
|
||||
tcpService.tcpServer.MinMsgLen = Default_MinMsgLen
|
||||
tcpService.tcpServer.MaxMsgLen = Default_MaxMsgLen
|
||||
MaxConnNum,ok := tcpCfg["MaxConnNum"]
|
||||
if ok == true {
|
||||
slf.tcpServer.MaxConnNum = int(MaxConnNum.(float64))
|
||||
tcpService.tcpServer.MaxConnNum = int(MaxConnNum.(float64))
|
||||
}
|
||||
PendingWriteNum,ok := tcpCfg["PendingWriteNum"]
|
||||
if ok == true {
|
||||
slf.tcpServer.PendingWriteNum = int(PendingWriteNum.(float64))
|
||||
tcpService.tcpServer.PendingWriteNum = int(PendingWriteNum.(float64))
|
||||
}
|
||||
LittleEndian,ok := tcpCfg["LittleEndian"]
|
||||
if ok == true {
|
||||
slf.tcpServer.LittleEndian = LittleEndian.(bool)
|
||||
tcpService.tcpServer.LittleEndian = LittleEndian.(bool)
|
||||
}
|
||||
MinMsgLen,ok := tcpCfg["MinMsgLen"]
|
||||
if ok == true {
|
||||
slf.tcpServer.MinMsgLen = uint32(MinMsgLen.(float64))
|
||||
tcpService.tcpServer.MinMsgLen = uint32(MinMsgLen.(float64))
|
||||
}
|
||||
MaxMsgLen,ok := tcpCfg["MaxMsgLen"]
|
||||
if ok == true {
|
||||
slf.tcpServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
||||
tcpService.tcpServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
||||
}
|
||||
slf.mapClient = make( map[uint64] *Client,slf.tcpServer.MaxConnNum)
|
||||
slf.tcpServer.NewAgent =slf.NewClient
|
||||
slf.tcpServer.Start()
|
||||
tcpService.mapClient = make( map[uint64] *Client, tcpService.tcpServer.MaxConnNum)
|
||||
tcpService.tcpServer.NewAgent = tcpService.NewClient
|
||||
tcpService.tcpServer.Start()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *TcpService) TcpEventHandler(ev *event.Event) {
|
||||
func (tcpService *TcpService) TcpEventHandler(ev *event.Event) {
|
||||
pack := ev.Data.(*TcpPack)
|
||||
switch pack.Type {
|
||||
case TPT_Connected:
|
||||
@@ -124,25 +131,25 @@ func (slf *TcpService) TcpEventHandler(ev *event.Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *TcpService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){
|
||||
slf.process = process
|
||||
slf.RegEventReciverFunc(event.Sys_Event_Tcp,handler,slf.TcpEventHandler)
|
||||
func (tcpService *TcpService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){
|
||||
tcpService.process = process
|
||||
tcpService.RegEventReceiverFunc(event.Sys_Event_Tcp,handler, tcpService.TcpEventHandler)
|
||||
}
|
||||
|
||||
func (slf *TcpService) NewClient(conn *network.TCPConn) network.Agent {
|
||||
slf.mapClientLocker.Lock()
|
||||
defer slf.mapClientLocker.Unlock()
|
||||
func (tcpService *TcpService) NewClient(conn *network.TCPConn) network.Agent {
|
||||
tcpService.mapClientLocker.Lock()
|
||||
defer tcpService.mapClientLocker.Unlock()
|
||||
|
||||
for {
|
||||
clientId := slf.genId()
|
||||
_,ok := slf.mapClient[clientId]
|
||||
clientId := tcpService.genId()
|
||||
_,ok := tcpService.mapClient[clientId]
|
||||
if ok == true {
|
||||
continue
|
||||
}
|
||||
|
||||
pClient := &Client{tcpConn:conn, id:clientId}
|
||||
pClient.tcpService = slf
|
||||
slf.mapClient[clientId] = pClient
|
||||
pClient.tcpService = tcpService
|
||||
tcpService.mapClient[clientId] = pClient
|
||||
|
||||
return pClient
|
||||
}
|
||||
@@ -150,12 +157,6 @@ func (slf *TcpService) NewClient(conn *network.TCPConn) network.Agent {
|
||||
return nil
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
id uint64
|
||||
tcpConn *network.TCPConn
|
||||
tcpService *TcpService
|
||||
}
|
||||
|
||||
func (slf *Client) GetId() uint64 {
|
||||
return slf.id
|
||||
}
|
||||
@@ -188,27 +189,27 @@ func (slf *Client) OnClose(){
|
||||
delete (slf.tcpService.mapClient,slf.GetId())
|
||||
}
|
||||
|
||||
func (slf *TcpService) SendMsg(clientId uint64,msg interface{}) error{
|
||||
slf.mapClientLocker.Lock()
|
||||
client,ok := slf.mapClient[clientId]
|
||||
func (tcpService *TcpService) SendMsg(clientId uint64,msg interface{}) error{
|
||||
tcpService.mapClientLocker.Lock()
|
||||
client,ok := tcpService.mapClient[clientId]
|
||||
if ok == false{
|
||||
slf.mapClientLocker.Unlock()
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientId)
|
||||
}
|
||||
|
||||
slf.mapClientLocker.Unlock()
|
||||
bytes,err := slf.process.Marshal(msg)
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
bytes,err := tcpService.process.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.tcpConn.WriteMsg(bytes)
|
||||
}
|
||||
|
||||
func (slf *TcpService) Close(clientId uint64) {
|
||||
slf.mapClientLocker.Lock()
|
||||
defer slf.mapClientLocker.Unlock()
|
||||
func (tcpService *TcpService) Close(clientId uint64) {
|
||||
tcpService.mapClientLocker.Lock()
|
||||
defer tcpService.mapClientLocker.Unlock()
|
||||
|
||||
client,ok := slf.mapClient[clientId]
|
||||
client,ok := tcpService.mapClient[clientId]
|
||||
if ok == false{
|
||||
return
|
||||
}
|
||||
@@ -220,10 +221,10 @@ func (slf *TcpService) Close(clientId uint64) {
|
||||
return
|
||||
}
|
||||
|
||||
func (slf *TcpService) GetClientIp(clientid uint64) string{
|
||||
slf.mapClientLocker.Lock()
|
||||
defer slf.mapClientLocker.Unlock()
|
||||
pClient,ok := slf.mapClient[clientid]
|
||||
func (tcpService *TcpService) GetClientIp(clientid uint64) string{
|
||||
tcpService.mapClientLocker.Lock()
|
||||
defer tcpService.mapClientLocker.Unlock()
|
||||
pClient,ok := tcpService.mapClient[clientid]
|
||||
if ok == false{
|
||||
return ""
|
||||
}
|
||||
@@ -231,14 +232,13 @@ func (slf *TcpService) GetClientIp(clientid uint64) string{
|
||||
return pClient.tcpConn.GetRemoteIp()
|
||||
}
|
||||
|
||||
|
||||
func (slf *TcpService) SendRawMsg(clientId uint64,msg []byte) error{
|
||||
slf.mapClientLocker.Lock()
|
||||
client,ok := slf.mapClient[clientId]
|
||||
func (tcpService *TcpService) SendRawMsg(clientId uint64,msg []byte) error{
|
||||
tcpService.mapClientLocker.Lock()
|
||||
client,ok := tcpService.mapClient[clientId]
|
||||
if ok == false{
|
||||
slf.mapClientLocker.Unlock()
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientId)
|
||||
}
|
||||
slf.mapClientLocker.Unlock()
|
||||
tcpService.mapClientLocker.Unlock()
|
||||
return client.tcpConn.WriteMsg(msg)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ type WSService struct {
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[uint64] *WSClient
|
||||
initClientId uint64
|
||||
process processor.Processor
|
||||
process processor.IProcessor
|
||||
}
|
||||
|
||||
type WSPackType int8
|
||||
@@ -28,55 +28,59 @@ const(
|
||||
WPT_UnknownPack WSPackType = 3
|
||||
)
|
||||
|
||||
type WSPack struct {
|
||||
Type WSPackType //0表示连接 1表示断开 2表示数据
|
||||
MsgProcessor processor.Processor
|
||||
ClientId uint64
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
|
||||
const Default_WS_MaxConnNum = 3000
|
||||
const Default_WS_PendingWriteNum = 10000
|
||||
const Default_WS_MaxMsgLen = 65535
|
||||
|
||||
type WSClient struct {
|
||||
id uint64
|
||||
wsConn *network.WSConn
|
||||
wsService *WSService
|
||||
}
|
||||
|
||||
func (slf *WSService) OnInit() error{
|
||||
iConfig := slf.GetServiceCfg()
|
||||
type WSPack struct {
|
||||
Type WSPackType //0表示连接 1表示断开 2表示数据
|
||||
MsgProcessor processor.IProcessor
|
||||
ClientId uint64
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
func (ws *WSService) OnInit() error{
|
||||
iConfig := ws.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
return fmt.Errorf("%s service config is error!", ws.GetName())
|
||||
}
|
||||
wsCfg := iConfig.(map[string]interface{})
|
||||
addr,ok := wsCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
return fmt.Errorf("%s service config is error!", ws.GetName())
|
||||
}
|
||||
|
||||
slf.wsServer.Addr = addr.(string)
|
||||
slf.wsServer.MaxConnNum = Default_WS_MaxConnNum
|
||||
slf.wsServer.PendingWriteNum = Default_WS_PendingWriteNum
|
||||
slf.wsServer.MaxMsgLen = Default_WS_MaxMsgLen
|
||||
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"]
|
||||
if ok == true {
|
||||
slf.wsServer.MaxConnNum = int(MaxConnNum.(float64))
|
||||
ws.wsServer.MaxConnNum = int(MaxConnNum.(float64))
|
||||
}
|
||||
PendingWriteNum,ok := wsCfg["PendingWriteNum"]
|
||||
if ok == true {
|
||||
slf.wsServer.PendingWriteNum = int(PendingWriteNum.(float64))
|
||||
ws.wsServer.PendingWriteNum = int(PendingWriteNum.(float64))
|
||||
}
|
||||
|
||||
MaxMsgLen,ok := wsCfg["MaxMsgLen"]
|
||||
if ok == true {
|
||||
slf.wsServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
||||
ws.wsServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
||||
}
|
||||
|
||||
slf.mapClient = make( map[uint64] *WSClient,slf.wsServer.MaxConnNum)
|
||||
slf.wsServer.NewAgent =slf.NewWSClient
|
||||
slf.wsServer.Start()
|
||||
ws.mapClient = make( map[uint64] *WSClient, ws.wsServer.MaxConnNum)
|
||||
ws.wsServer.NewAgent = ws.NewWSClient
|
||||
ws.wsServer.Start()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *WSService) WSEventHandler(ev *event.Event) {
|
||||
func (ws *WSService) WSEventHandler(ev *event.Event) {
|
||||
pack := ev.Data.(*WSPack)
|
||||
switch pack.Type {
|
||||
case WPT_Connected:
|
||||
@@ -90,37 +94,31 @@ func (slf *WSService) WSEventHandler(ev *event.Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *WSService) SetProcessor(process processor.Processor,handler event.IEventHandler){
|
||||
slf.process = process
|
||||
slf.RegEventReciverFunc(event.Sys_Event_WebSocket,handler,slf.WSEventHandler)
|
||||
func (ws *WSService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){
|
||||
ws.process = process
|
||||
ws.RegEventReceiverFunc(event.Sys_Event_WebSocket,handler, ws.WSEventHandler)
|
||||
}
|
||||
|
||||
func (slf *WSService) NewWSClient(conn *network.WSConn) network.Agent {
|
||||
slf.mapClientLocker.Lock()
|
||||
defer slf.mapClientLocker.Unlock()
|
||||
func (ws *WSService) NewWSClient(conn *network.WSConn) network.Agent {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
|
||||
for {
|
||||
slf.initClientId+=1
|
||||
_,ok := slf.mapClient[slf.initClientId]
|
||||
ws.initClientId+=1
|
||||
_,ok := ws.mapClient[ws.initClientId]
|
||||
if ok == true {
|
||||
continue
|
||||
}
|
||||
|
||||
pClient := &WSClient{wsConn:conn, id:slf.initClientId}
|
||||
pClient.wsService = slf
|
||||
slf.mapClient[slf.initClientId] = pClient
|
||||
pClient := &WSClient{wsConn:conn, id: ws.initClientId}
|
||||
pClient.wsService = ws
|
||||
ws.mapClient[ws.initClientId] = pClient
|
||||
return pClient
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type WSClient struct {
|
||||
id uint64
|
||||
wsConn *network.WSConn
|
||||
wsService *WSService
|
||||
}
|
||||
|
||||
func (slf *WSClient) GetId() uint64 {
|
||||
return slf.id
|
||||
}
|
||||
@@ -149,27 +147,27 @@ func (slf *WSClient) OnClose(){
|
||||
delete (slf.wsService.mapClient,slf.GetId())
|
||||
}
|
||||
|
||||
func (slf *WSService) SendMsg(clientid uint64,msg interface{}) error{
|
||||
slf.mapClientLocker.Lock()
|
||||
client,ok := slf.mapClient[clientid]
|
||||
func (ws *WSService) SendMsg(clientid uint64,msg interface{}) error{
|
||||
ws.mapClientLocker.Lock()
|
||||
client,ok := ws.mapClient[clientid]
|
||||
if ok == false{
|
||||
slf.mapClientLocker.Unlock()
|
||||
ws.mapClientLocker.Unlock()
|
||||
return fmt.Errorf("client %d is disconnect!",clientid)
|
||||
}
|
||||
|
||||
slf.mapClientLocker.Unlock()
|
||||
bytes,err := slf.process.Marshal(msg)
|
||||
ws.mapClientLocker.Unlock()
|
||||
bytes,err := ws.process.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return client.wsConn.WriteMsg(bytes)
|
||||
}
|
||||
|
||||
func (slf *WSService) Close(clientid uint64) {
|
||||
slf.mapClientLocker.Lock()
|
||||
defer slf.mapClientLocker.Unlock()
|
||||
func (ws *WSService) Close(clientid uint64) {
|
||||
ws.mapClientLocker.Lock()
|
||||
defer ws.mapClientLocker.Unlock()
|
||||
|
||||
client,ok := slf.mapClient[clientid]
|
||||
client,ok := ws.mapClient[clientid]
|
||||
if ok == false{
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user