/// <summary> /// 解绑操作 /// </summary> private void UnBind() { lock (channelLockFlag) { if (channelStateReady == false) { return; } channelStateReady = false; CMPP_TERMINATE unBind = new CMPP_TERMINATE(); //将命令写入连接 try { byte[] b_cmd = unBind.WriteBytes(); client_sk.GetStream().Write(b_cmd, 0, b_cmd.Length); //命令写入完毕,开始读取应答 b_cmd = readBuffer(client_sk.GetStream());//从网络流中读取一个缓冲对象,对象大小由长度字段指定。 CMPP_TERMINATE_RESP unBindResp = new CMPP_TERMINATE_RESP(unBind.MyHead.Sequence_Id); unBindResp.ReadBytes(b_cmd); if (unBindResp.MyHead.Command_Id != Command_Id.CMPP_TERMINATERESP) { WriteLog("登出未成功取出响应包,当前包" + unBindResp.MyHead.Command_Id); } //解绑完毕 client_sk.Close(); client_sk = null; } catch (Exception) { client_sk = null; return; } } }
/// <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); } }