private void ClientMessageRecive(CMPP30Client clien, CMPP_DELIVER deliver) { if (this.MessageRecive != null) { MessageRecive(this, deliver); } }
/// <summary> /// 线程池调用处理上行 或 状态 /// </summary> /// <param name="obj"></param> private void ThreadPoolExcuteFuctione(object obj) { CMPP_DELIVER deliver = obj as CMPP_DELIVER; if (deliver.Registered_Delivery == 0 && UserMessageRecive != null) { UserMessageRecive(this, deliver); } else if (StateReport != null) { CMPP_DELIVER_Msg_Content content = new CMPP_DELIVER_Msg_Content(); content.ReadMessage(deliver.Msg_Content); StateReport(this, content); } }
/// <summary> /// 读取失败或无可读数据返回 null /// </summary> /// <returns></returns> private CMPPMsgBody_Base read() { byte[] buffer; try { //判断是否有可用的数据 if (channelStateReady && client_sk != null && client_sk.Available > 0) { buffer = readBuffer(client_sk.GetStream()); } else { return(null);//没有可供读取的数据 } //将读取到的数据构建成对象 CMPPMsgHeader head; //读出头部,判断命令类型 head = CMPPMsgHeader.Read(buffer); //根据指令类型,构建应答对象,对于不处理的指令进行丢弃 CMPPMsgBody_Base data = null; switch (head.Command_Id) { case Command_Id.CMPP_SUBMIT_RESP: data = new CMPP_SUBMIT_RESP(head.Sequence_Id); break; case Command_Id.CMPP_DELIVER: data = new CMPP_DELIVER(head.Sequence_Id); break; case Command_Id.CMPP_ACTIVE_TEST: data = new CMPP_ACTIVE_TEST(head.Sequence_Id); break; case Command_Id.CMPP_ACTIVE_TEST_RESP: data = new CMPP_ACTIVE_TEST_RESP(head.Sequence_Id); break; case Command_Id.CMPP_TERMINATE: data = new CMPP_TERMINATE(head.Sequence_Id); break; case Command_Id.CMPP_TERMINATERESP: data = new CMPP_TERMINATE_RESP(head.Sequence_Id); break; default: break; } if (data != null) { data.ReadBytes(buffer); } return(data); } catch (Exception)//流读取异常 { CloseSoket(); return(null); } }
/// <summary> /// 后台读取线程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void bWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; //处理过程 while (!bw.CancellationPending) { //判断通道是否可用 if (channelStateReady) { //从通道中读取数据对象,若返回值为null 则表示无数据或读取出错(读取出错处理在 read方法进行处理) CMPPMsgBody_Base data = read(); if (data != null) { channelLastUpdate = DateTime.Now; //对读取到的数据进行处理 switch (data.MyHead.Command_Id) { #region 命令响应 case Command_Id.CMPP_SUBMIT_RESP: case Command_Id.CMPP_ACTIVE_TEST_RESP: if (CmppCmdQueue.ContainsKey(data.MyHead.Sequence_Id)) { CmppCmdQueue[data.MyHead.Sequence_Id] = data; } break; #endregion #region 状态回报 上行 case Command_Id.CMPP_DELIVER: ThreadPool.QueueUserWorkItem(ThreadPoolExcuteFuctione, data); //响应服务器 CMPP_DELIVER deliver = data as CMPP_DELIVER; CMPP_DELIVER_RESP deliverResp = new CMPP_DELIVER_RESP(deliver.MyHead.Sequence_Id, deliver.Msg_Id, (uint)DeliverResult.正确); Submit(deliverResp); break; #endregion #region 拆除连接 case Command_Id.CMPP_TERMINATE: Submit(new CMPP_TERMINATE_RESP(data.MyHead.Sequence_Id)); CloseSoket(); break; #endregion #region 连接检测 case Command_Id.CMPP_ACTIVE_TEST: Submit(new CMPP_ACTIVE_TEST_RESP(data.MyHead.Sequence_Id)); break; #endregion default: //未知的命令丢弃 break; } } } //判断通道空闲时间间隔,进行超时处理 if (channelLastUpdate.AddSeconds(ActiveTestInterval) < DateTime.Now) { var err = Submit(new CMPP_ACTIVE_TEST());// if (err != LocalErrCode.成功) { channelLastUpdate = channelLastUpdate.AddSeconds(ActiveTestInterval);//n秒后重试 防止过多发送 WriteLog("长连接链路检测发送失败:" + err.ToString()); } } Thread.Sleep(10);//每个周期休眠10毫秒 } }