public static IProtocol GetProtocol(MessageProtocol mp, string msgId) { if (msgId == LoginProtocol.id) { //登录协议 return mp as LoginProtocol; } else if (ResultProtocol.ids.Contains(msgId)) { //返回结果协议 return mp as ResultProtocol; } else if (msgId == UploadDataProtocol.id) { //数据上传协议 return mp as UploadDataProtocol; } else if (FactoryProtocol.ids.Contains(msgId)) { //车厂数据协议 return mp as FactoryProtocol; } else if (AnnounceProtocol.ids.Contains(msgId)) { //公告协议 return mp as AnnounceProtocol; } else if (msgId == ControlProtocol.id) { //下行控制协议 return mp as ControlProtocol; } return mp; }
public static IProtocol GetProtocol(MessageProtocol mp, string msgId) { if (msgId == LoginProtocol.id) { //登录协议 return(mp as LoginProtocol); } else if (ResultProtocol.ids.Contains(msgId)) { //返回结果协议 return(mp as ResultProtocol); } else if (msgId == UploadDataProtocol.id) { //数据上传协议 return(mp as UploadDataProtocol); } else if (FactoryProtocol.ids.Contains(msgId)) { //车厂数据协议 return(mp as FactoryProtocol); } else if (AnnounceProtocol.ids.Contains(msgId)) { //公告协议 return(mp as AnnounceProtocol); } else if (msgId == ControlProtocol.id) { //下行控制协议 return(mp as ControlProtocol); } return(mp); }
private static MessageProtocol ReceiveMessage() { MessageProtocol _protocol = null; if (ServiceTest()) { //接收数据 string str = mc.Receive(ProtocolTranslator.StartFlag, ProtocolTranslator.EndFlag); if (str.Length > 0) { _protocol = ProtocolTranslator.DeserilizeMessage(str); } } return(_protocol); }
private static MessageProtocol ReceiveMessage() { if (mc == null) { return(null); } //接收数据 string str = mc.Receive(ProtocolTranslator.StartFlag, ProtocolTranslator.EndFlag); MessageProtocol _protocol = null; if (str.Length > 0) { _protocol = ProtocolTranslator.DeserilizeMessage(str); } return(_protocol); }
/// <summary> /// 向发送队列添加消息 /// </summary> /// <param name="protocol"></param> public static void AddSendQueue(MessageProtocol protocol) { if (protocol == null) { return; } lock (mylock) { if (ServiceTest()) { protocols.Enqueue(protocol); } else { GlobalStaticObj_Server.Instance.FileServerLink = false; } } }
/// <summary> /// 向服务端发送消息 /// </summary> /// <param name="protocol">协议对象</param> /// <returns></returns> private static bool SendMessage(MessageProtocol protocol) { if (mc == null) { mc = new MessageClient(_ip, _port); } try { if (!protocol.SerialNumberLock) { protocol.SerialNumber = serialNumber.ToString(); //流水号递增 serialNumber++; if (serialNumber > 1000000) { serialNumber = 0; } } if (mc.Connect()) { //发送命令 string message = ProtocolTranslator.SerilizeMessage(protocol); string key = protocol.StationId + protocol.SerialNumber + protocol.TimeSpan; if (!htProtocols.ContainsKey(key)) { htProtocols.Add(key, protocol); } if (SendComplated != null) { //显示消息 SendComplated(message); } mc.Send(message); Log.writeCloudLog("【数据通讯-发送消息】:" + message); return(true); } } catch (Exception ex) { Log.writeCloudLog("云通讯服务:" + ex.Message); } return(false); }
/// <summary> /// 向服务端发送消息,并接收消息 /// </summary> /// <param name="protocol">协议对象</param> /// <returns></returns> public static MessageProtocol SendAndReceiveMessage(MessageProtocol protocol) { if (mc == null) { mc = new MessageClient(_ip, _port); } MessageProtocol _protocol = null; try { if (!protocol.SerialNumberLock) { protocol.SerialNumber = serialNumber.ToString(); //流水号递增 serialNumber++; if (serialNumber > 1000000) { serialNumber = 0; } } if (mc.Connect()) { //发送命令 string message = ProtocolTranslator.SerilizeMessage(protocol); mc.Send(message); Thread.Sleep(100); string str = mc.Receive(ProtocolTranslator.StartFlag, ProtocolTranslator.EndFlag); _protocol = ProtocolTranslator.DeserilizeMessage(str); } } catch (Exception ex) { Log.writeLineToLog(ex.Message, "云支撑平台文件通讯故障"); } return(_protocol); }
/// <summary> /// 接收解析消息 /// </summary> /// <param name="_receivedMessage"></param> /// <returns></returns> public static MessageProtocol DeserilizeMessage(string _receivedMessage) { if (_receivedMessage.Length == 0) { return(null); } _receivedMessage = _receivedMessage.Replace(_StartFlag.ToString(), "").Replace(_EndFlag.ToString(), ""); string[] arrays = _receivedMessage.Split(separator); MessageProtocol messageProtocol = new MessageProtocol(arrays); #region --获取校验码 string headMessage = string.Empty; headMessage = _receivedMessage.Substring(0, _receivedMessage.LastIndexOf(separator)); headMessage = GetValidCode(headMessage); #endregion messageProtocol.Success = messageProtocol.IsValid(headMessage) ? true : false; if (!messageProtocol.Success) { messageProtocol.IsValid(headMessage); } return(messageProtocol); }
/// <summary> 接收线程 /// </summary> /// <param name="state"></param> public static void _ReceiveThread() { MessageProtocol protocol; bool flag = false; string key = string.Empty; while (true) { protocol = ReceiveMessage(); if (protocol != null) { //状态设置,设置当前时间 GlobalStaticObj_Server.Instance.FileServerLink = true; //组建唯一标识 key = protocol.StationId + protocol.SerialNumber + protocol.TimeSpan; if (htProtocols.ContainsKey(key)) { flag = true; } else { flag = false; } if (protocol.IsSuccess()) { if (ReceiveComplated != null) { //显示消息 ReceiveComplated(ProtocolTranslator.SerilizeMessage(protocol)); } //具体操作是否有效 protocol.Do(ref flag); if (flag) { htProtocols.Remove(key); flag = false; } } if (flag) { //重新发送数据 MessageProtocol mp = htProtocols[key] as MessageProtocol; mp.sendCount++; if (mp.SendCount > 3) { if (htProtocols.ContainsKey(key)) { htProtocols.Remove(key); } if (mp.SendCount == 3) { //写错误日志 mp.ErrorLog(); } } else { //重复发送三次 mp.SerialNumberLock = true; //ddSendQueue(mp); } } } Thread.Sleep(50); } }
/// <summary> /// 向发送队列添加消息 /// </summary> /// <param name="protocol"></param> public static void AddSendQueue(MessageProtocol protocol) { if (protocol == null) { return; } lock (mylock) { if (ServiceTest()) { protocols.Enqueue(protocol); } else { GlobalStaticObj_Server.Instance.ServerLink = false; } } }
/// <summary> /// 向服务端发送消息 /// </summary> /// <param name="protocol">协议对象</param> /// <returns></returns> private static bool SendMessage(MessageProtocol protocol) { if (mc == null) { mc = new MessageClient(_ip, _port); } try { if (!protocol.SerialNumberLock) { protocol.SerialNumber = serialNumber.ToString(); //流水号递增 serialNumber++; if (serialNumber > 1000000) { serialNumber = 0; } } if (mc.Connect()) { //发送命令 string message = ProtocolTranslator.SerilizeMessage(protocol); string key = protocol.StationId + protocol.SerialNumber + protocol.TimeSpan; if (!htProtocols.ContainsKey(key)) { htProtocols.Add(key, protocol); } if (SendComplated != null) { //显示消息 SendComplated(message); } mc.Send(message); return true; } } catch (Exception ex) { Log.writeCloudLog("云通讯服务:" + ex.Message); } return false; }
/// <summary> /// 向服务端发送消息,并接收消息 /// </summary> /// <param name="protocol">协议对象</param> /// <returns></returns> public static MessageProtocol SendAndReceiveMessage(MessageProtocol protocol) { if (mc == null) { mc = new MessageClient(_ip, _port); } MessageProtocol _protocol = null; try { if (!protocol.SerialNumberLock) { protocol.SerialNumber = serialNumber.ToString(); //流水号递增 serialNumber++; if (serialNumber > 1000000) { serialNumber = 0; } } if (mc.Connect()) { //发送命令 string message = ProtocolTranslator.SerilizeMessage(protocol); mc.Send(message); Thread.Sleep(100); string str = mc.Receive(ProtocolTranslator.StartFlag, ProtocolTranslator.EndFlag); _protocol = ProtocolTranslator.DeserilizeMessage(str); } } catch (Exception ex) { Log.writeLineToLog(ex.Message, "云支撑平台文件通讯故障"); } return _protocol; }
/// <summary> /// 接收解析消息 /// </summary> /// <param name="_receivedMessage"></param> /// <returns></returns> public static MessageProtocol DeserilizeMessage(string _receivedMessage) { if (_receivedMessage.Length == 0) { return null; } _receivedMessage = _receivedMessage.Replace(_StartFlag.ToString(), "").Replace(_EndFlag.ToString(), ""); string[] arrays = _receivedMessage.Split(separator); MessageProtocol messageProtocol = new MessageProtocol(arrays); #region --获取校验码 string headMessage = string.Empty; headMessage = _receivedMessage.Substring(0, _receivedMessage.LastIndexOf(separator)); headMessage = GetValidCode(headMessage); #endregion messageProtocol.Success = messageProtocol.IsValid(headMessage) ? true : false; if (!messageProtocol.Success) { messageProtocol.IsValid(headMessage); } return messageProtocol; }