private void DealCache(Action <DMSCRProtocol> processDtaCallBack) { string m = ""; foreach (string v in msgCache) { m = m + v; } if (m.Length > 15) { Console.WriteLine("缓存: [" + m + "]"); string headerAndComm = HEADER.SXZL.ToString() + ","; if (m.Contains(headerAndComm)) { int ind = m.IndexOf(headerAndComm); m = m.Substring(ind); if (m.Length > 15) { DMSCRProtocol dMProtocol; dMProtocol = new DMSCRProtocol(m); processDtaCallBack(dMProtocol); msgCache.Clear(); } } } }
/// <summary> /// 解析数据或者叫做读取数据 /// </summary> public void ReadMessage(int newDataAmount, Action <DMSCRProtocol> processDtaCallBack) { if (newDataAmount > 15) { string msg = Encoding.UTF8.GetString(Data, 0, newDataAmount); this.GetContentString = msg; //处理分包 string headerAndComm = HEADER.SXZL.ToString() + ","; string[] msgs = msg.Split(headerAndComm, StringSplitOptions.RemoveEmptyEntries); if (msgs.Length > 1) { Console.WriteLine("发现连包:" + msgs.Length); Console.WriteLine("连包:" + msg); } DMSCRProtocol dMProtocol; foreach (string m in msgs) { string m0 = headerAndComm + m; //加回头 if (m0.Length < 15) { continue;//连包太小直接越过好了。 } try { dMProtocol = new DMSCRProtocol(m0); processDtaCallBack(dMProtocol); } catch (Exception ex) { Console.WriteLine("协议出错:" + ex.Message + "msg:" + m0); } } //直接用消息创造协议包 //DMSCRProtocol dMProtocol = new DMSCRProtocol(msg); msgCache.Clear();//正常完成的话,丢弃掉缓存 errCount = 0; } else { string msg = Encoding.UTF8.GetString(Data, 0, newDataAmount); Console.WriteLine("消息短于15: [" + msg + "]"); if (msg.Length > 0) { msgCache.Add(msg); DealCache(processDtaCallBack); } errCount++; if (errCount > 15) { throw new Exception("错误累计数量15,消息短于15:" + msg); } } }
private void OnProcessMessage(DMSCRProtocol dMProtocol) { dmservermainthread.HandleRequest(dMProtocol, this); }
/// <summary> /// 暂时废弃 /// </summary> /// <param name="ar"></param> //public static void ReadCallback(IAsyncResult ar) //{ // StateObject state = (StateObject)ar.AsyncState; // Socket clientSocket = state.workSocket; // int count=0; // try // { // //count = clientSocket.EndReceive(ar); // //string msg = Encoding.UTF8.GetString(state.buffer, 0, count); // //Console.WriteLine(msg); // try // { // //导入数据处理器 // ProtocolProcessor.DeviceInformationProcessing(ar); // } // catch (Exception ex) // { // Console.WriteLine(ex.Message); // } // } // catch (System.Net.Sockets.SocketException se) // { // Console.WriteLine(se.Message); // } // if (count > 0) // { // state.stringBuilder.Append(Encoding.ASCII.GetString(state.buffer, 0, count)); // clientSocket.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE, 0, // new AsyncCallback(ReadCallback), state); // } // else // { // if (state.stringBuilder.Length > 1) // { // //All of the data has been read, so displays it to the console // string strContent; // strContent = state.stringBuilder.ToString(); // Console.WriteLine(String.Format("Read {0} byte from socket" + "data = {1} ", strContent.Length, strContent)); // } // //长连接,不再主动断开 // //clientSocket.Close(); // } //} /// <summary> /// 处理设备发来的消息消息 /// </summary> /// <param name="dMProtocol"></param> /// <param name="dmclient"></param> public void HandleRequest(DMSCRProtocol dMProtocol, DMClient dmclient) { if (dMProtocol.Header == HEADER.SXZL) { switch (dMProtocol.Command) { //注册时判断是否是初始化服务,如果符合标准进行设备初始化,实际上是赋予设备新ID case COMMAND.REGISTER: dmclient.DeviceID = dMProtocol.DeviceID; dmclient.Stat = dMProtocol.Stat; dmclient.Powerstat = dMProtocol.Powerstat; //if (IsProvideInitialization && dmclient.RemoteIP.Equals(this.PremiseDeviceIP) && dmclient.DeviceID.Equals("SZ000001")) if (IsProvideInitialization && dmclient.DeviceID.Equals("SZ000001")) { InitializationDevice(dmclient); } else { AddClient(dMProtocol.DeviceID, dmclient); SendReply(COMMAND.REP_REGISTER, dmclient); } break; case COMMAND.READCARD: dmclient.Stat = dMProtocol.Stat; dmclient.Powerstat = dMProtocol.Powerstat; if (VerifyClientRegistration(dmclient)) { SendReply(COMMAND.REP_READCARD, dmclient); /* 发送到LIMS服务器 */ string cardresult = SendMsgToLIMS(dMProtocol.DeviceID, dMProtocol.OtherInfo[0], dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt); Console.WriteLine("Card Result : " + cardresult); SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat); } break; case COMMAND.REP_ASK: //log.Info("REP_ASK"); ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt); SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat); break; case COMMAND.REP_REMIND: //log.Info("REP_REMIND"); ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt); SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat); break; case COMMAND.REP_UPDATE: //log.Info("REP_UPDATE"); ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt); SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat); break; case COMMAND.REP_SETUP: //log.Info("REP_SETUP"); ; ReplyMsgToLIMS(dMProtocol.DeviceID, "", dMProtocol.StatInt, dMProtocol.PowerStatInt, "", dMProtocol.CommandInt); SaveClientStat(dMProtocol.DeviceID, dMProtocol.Stat, dMProtocol.Powerstat); break; case COMMAND.SCR_BOOKING: if (VerifyClientRegistration(dmclient)) { scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); } break; case COMMAND.SCR_BOOK_CALENDAR: if (VerifyClientRegistration(dmclient)) { scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); } break; case COMMAND.SCR_BOOK_DATE: if (VerifyClientRegistration(dmclient)) { scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); } break; case COMMAND.SCR_BOOK_INFO: if (VerifyClientRegistration(dmclient)) { scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); } break; //仪器信息 case COMMAND.SCR_DEV_INFO: if (VerifyClientRegistration(dmclient)) { scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); } break; case COMMAND.SCR_STATE_BOOK: //屏幕设备这个也是登录用 // start 暂时等对屏幕设备登陆用 dmclient.DeviceID = dMProtocol.DeviceID; dmclient.Stat = dMProtocol.Stat; dmclient.Powerstat = dMProtocol.Powerstat; AddClient(dMProtocol.DeviceID, dmclient); // end 暂时等对屏幕设备登陆用 scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); break; case COMMAND.SCR_HELP: if (VerifyClientRegistration(dmclient)) { scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); } break; case COMMAND.SCR_USERINFO: if (VerifyClientRegistration(dmclient)) { scrServerHandle.HandleServerMessage(dMProtocol.DeviceID, dMProtocol.Command, dmclient, (DMSCRProtocol)dMProtocol); } break; } } else { Console.WriteLine("客户端消息头不合法。"); } }