func (chn *Channel) sendProto(cID uint32, cmd uint8, result string, err string, ns string) { var method string if cmd == base.OPEN_RESOURCE_CMD { method = "open" } else if cmd == base.CLOSE_RESOURCE_CMD { method = "close" } else if cmd == base.REGISTER_RESOURCE { method = "register" } proto := base.GetProto() proto.RD.BaseHD.CommandId = 0x80 | cmd proto.RD.HD.ClientIdent = cID proto.RD.HD.ContextType = base.CONTEXT_JSON proto.RD.HD.TransferType = base.TRANSFER_RAW responseJson := base.ResponseJson{ns, method, result, err} b, jsonE := json.Marshal(responseJson) if jsonE != nil { mylog.GetErrorLogger().Println("handleOpenResource : ", jsonE) } proto.EncodeBody(b) go chn.Send(proto) }
func (chn *Channel) handleEventNotify(event notifyEvent) { func() { chn.resMutex.Lock() defer chn.resMutex.Unlock() resourcer, ok := chn.openRes[event.clientID] if ok { ReleaseResourcer(resourcer) delete(chn.openRes, event.clientID) } }() proto := base.GetProto() proto.RD.BaseHD.CommandId = base.LEAVE_NOTIFY proto.RD.HD.ClientIdent = event.clientID proto.RD.HD.ContextType = base.CONTEXT_JSON proto.RD.HD.TransferType = base.TRANSFER_RAW leaveNotify := base.LeaveNotifyParamJson{event.resourceID} request := base.RequestJson{"", "ns", "notify", leaveNotify} requestJson, err := json.Marshal(request) if err != nil { mylog.GetErrorLogger().Println("Resource Unregister Json ", err) } proto.EncodeBody(requestJson) chn.Send(proto) }
func (r *Resourcer) broadcastData(proto *base.Proto) { r.mutex.Lock() defer r.mutex.Unlock() if !atomic.CompareAndSwapUint32(&r.valid, 1, 1) { return } tmpClientDataChn := make([]*ResourceClient, 0) for _, chn := range r.ClientDataChn { sendLen := uint64(proto.RD.HD.BodyLen) stat.GetLocalStatistInst().RecvData(sendLen) sendP := base.GetProto() sendP.RD = proto.RD sendP.RD.BaseHD.CommandId |= 0x80 sendP.BD = proto.BD sendP.RD.HD.ClientIdent = chn.ClientID if chn.ClientInf.Send(sendP) { tmpClientDataChn = append(tmpClientDataChn, chn) stat.GetLocalStatistInst().SendData(sendLen) } } r.ClientDataChn = tmpClientDataChn }
func (chn *Channel) handleReqCloseRes(clientID uint32, data []byte) { mylog.GetErrorLogger().Println("handleReqCloseRes", string(data[4:])) defer func() { if r := recover(); r != nil { chn.sendProto(clientID, base.CLOSE_RESOURCE_CMD, "", base.PROTOERR400, "huamail.defualt") chn.handleError(base.DTerror{"handleReqCloseRes Panic"}) } }() var responseResult, responseErr string = "", base.NOFOUNF404 request, _, err := chn.getReqParam(clientID, data[4:]) if err != nil { chn.sendProto(clientID, base.CLOSE_RESOURCE_CMD, responseResult, err.Error(), "huamail.defualt") return } func() { chn.resMutex.Lock() defer chn.resMutex.Unlock() resourcer, ok := chn.openRes[clientID] if ok { mylog.GetErrorLogger().Println("Chn Close Resource ID ", resourcer.GetID()) clientDataID := new(ResourceClient) clientDataID.ClientInf = chn clientDataID.ClientID = clientID responseResult, responseErr = resourcer.Close(clientDataID, "", false) ReleaseResourcer(resourcer) delete(chn.openRes, clientID) } }() proto := base.GetProto() proto.RD.BaseHD.CommandId = 0x80 | base.CLOSE_RESOURCE_CMD proto.RD.HD.ClientIdent = clientID responseJson := base.ResponseJson{request.Ns, request.Method, responseResult, responseErr} b, err := json.Marshal(responseJson) if err != nil { mylog.GetErrorLogger().Println("handleCloseResource : ", err) } proto.EncodeBody(b) chn.Send(proto) stat.GetLocalStatistInst().CloseRes() }
// 心跳处理,主动发起心跳 func (chn *Channel) timerSendHeartTask() { mylog.GetErrorLogger().Println(" Begin Chn Heart Task") defer func() { if r := recover(); r != nil { chn.handleError(base.DTerror{"Notify Panic"}) } }() proto := base.GetProto() proto.RD.BaseHD.CommandId = base.HEART_CMD var failCount int = 0 for { if atomic.CompareAndSwapUint32(&chn.valid, 0, 0) { break } if !chn.Send(proto) { chn.handleError(base.DTerror{"Send Error"}) break } select { case <-chn.heartChn: failCount = 0 case <-time.After(2 * time.Second): failCount++ } if failCount >= 5 { chn.handleError(base.DTerror{"Heart Send Fail"}) break } time.Sleep(10 * time.Second) } mylog.GetErrorLogger().Println(" End Chn Heart Task") }
func (chn *Channel) handleReadSocketTask() { var productID uint32 = 1 defer func() { if r := recover(); r != nil { chn.handleError(base.DTerror{"handleReadSocketTask Panic"}) } }() for { proto := base.GetProto() e := proto.ReadBinaryProto(chn.connSocket) if e != nil { chn.handleError(base.DTerror{"handleReadSocketTask"}) break } consumerID := proto.RD.HD.ClientIdent cmdID := proto.RD.BaseHD.CommandId & 0x7f isRequest := proto.RD.BaseHD.CommandId & 0x80 switch cmdID { case base.HEART_CMD: if isRequest == 0 { heartProto := base.GetProto() heartProto.RD.BaseHD.CommandId = base.HEART_CMD | 0x80 chn.Send(heartProto) } else { chn.heartChn <- true } case base.OPEN_RESOURCE_CMD: // 打开资源 if isRequest == 0 { //fmt.Println("打开资源") go chn.handleReqOpenRes(consumerID, proto.BD.Data) } else { //fmt.Println("回应资源") go chn.handleRspRes(proto) } case base.CLOSE_RESOURCE_CMD: // 关闭资源 if isRequest == 0 { go chn.handleReqCloseRes(consumerID, proto.BD.Data) } else { go chn.handleRspRes(proto) } case base.CRTL_RESOURCE_CMD: // 控制资源 case base.REGISTER_RESOURCE: // 注册资源 go chn.handleRegisterRes(productID, proto.BD.Data) productID++ case base.UNREGISTER_RESOURCE: // 注销资源 case base.DATA_STREAM: // 数据流 chn.handleRspRes(proto) default: go chn.handleOtherCMD(proto) } base.PutProto(proto) } //fmt.Println("End Cu Read Task") }
func (r *Resourcer) Close(chn *ResourceClient, ns string, all bool) (string, string) { mylog.GetErrorLogger().Println("Close Resource") r.mutex.Lock() defer r.mutex.Unlock() mylog.GetErrorLogger().Println("Close unattent") openCount := r.unattent(chn, all) if openCount != 0 { return "", base.OK } { r.Result = "" r.Error = "" proto := base.GetProto() proto.RD.BaseHD.CommandId = base.CLOSE_RESOURCE_CMD proto.RD.HD.ClientIdent = r.SourceDataChn.ClientID proto.RD.HD.ContextType = base.CONTEXT_JSON proto.RD.HD.TransferType = base.TRANSFER_RAW /*todo 1. 生成token 2. */ var closeResParam = base.CloseResParamJson{r.GetID()} request := base.RequestJson{"", ns, "close", closeResParam} b, e := json.Marshal(request) if e != nil { mylog.GetErrorLogger().Println("Resource Close Json ", e) } proto.EncodeBody(b) if !r.SourceDataChn.ClientInf.Send(proto) { mylog.GetErrorLogger().Println("Resource Close Send Error ") return "", base.NOFOUNF404 } } select { case msg, ok := <-r.ClientOpenChn: fmt.Println("ClientOpenChn Close ") if !ok { mylog.GetErrorLogger().Println("ClientOpenChn Close ") return "", base.NOFOUNF404 } var responseJson base.ResponseJson e := json.Unmarshal(msg[4:], &responseJson) if e != nil { mylog.GetErrorLogger().Println("Resource Close ", e, string(msg[4:])) return r.Result, base.PROTOERR400 } return responseJson.Result, responseJson.Error case <-time.After(2 * time.Second): //TimeOut return r.Result, base.NOFOUNF404 } return r.Result, r.Error }
func (r *Resourcer) Open(chn *ResourceClient, request *base.RequestJson, token *base.TokenJson) (string, string) { fmt.Println("1 Resourcer Open Resource ") r.mutex.Lock() defer r.mutex.Unlock() fmt.Println("2 Resourcer Open Resource ") /*verErr := r.verificate(token) if verErr != "" { return "", verErr }*/ if !atomic.CompareAndSwapUint32(&r.valid, 1, 1) { return "", base.NOFOUNF404 } if bytes.Equal([]byte(r.Error), []byte(base.OK)) { mylog.GetErrorLogger().Println("Resource Has Opend") r.attent(chn) return r.Result, r.Error } { proto := base.GetProto() proto.RD.BaseHD.CommandId = base.OPEN_RESOURCE_CMD proto.RD.HD.ClientIdent = r.DtClientID proto.RD.HD.ContextType = base.CONTEXT_JSON proto.RD.HD.TransferType = base.TRANSFER_RAW b, err := json.Marshal(request) if err != nil { mylog.GetErrorLogger().Println("Resource Open Json ", err) } proto.EncodeBody(b) if !r.SourceDataChn.ClientInf.Send(proto) { close(r.ClientOpenChn) r.ClientOpenChn = nil mylog.GetErrorLogger().Println("Resource Open Send Error ", err) return r.Result, base.NOFOUNF404 } } select { case msg, ok := <-r.ClientOpenChn: // msg只是包含数据 if !ok { mylog.GetErrorLogger().Println("ClientOpenChn Open ") return r.Result, base.NOFOUNF404 } var responseJson base.ResponseJson e := json.Unmarshal(msg[4:], &responseJson) if e != nil { mylog.GetErrorLogger().Println(e) return r.Result, base.PROTOERR400 } r.Result = strings.ToLower(responseJson.Result) r.Error = strings.ToLower(responseJson.Error) if strings.EqualFold(r.Error, base.OK) { r.attent(chn) } return r.Result, r.Error case <-time.After(20 * time.Second): //TimeOut return r.Result, base.NOFOUNF404 } return r.Result, r.Error }