Beispiel #1
0
        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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
 /// <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);
 }
Beispiel #4
0
        /// <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);
        }
Beispiel #5
0
        /// <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);
            }
        }