Example #1
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);
             return true;
         }
     }
     catch (Exception ex)
     {
         Log.writeCloudLog("云通讯服务:" + ex.Message);
     }
     return false;
 }
Example #2
0
 /// <summary>
 /// 当前服务器连接是否正常
 /// </summary>
 /// <returns></returns>
 public static bool ServiceTest()
 {
     if (mc == null)
     {
         mc = new MessageClient(_ip, _port);
     }
     if (mc.Connect())
     {
         return true;
     }
     return false;
 }       
Example #3
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;
        }
Example #4
0
 /// <summary>
 /// 当前服务器连接是否正常
 /// </summary>
 /// <returns></returns>
 public static bool ServiceTest()
 {
     if (mc == null)
     {
         mc = new MessageClient(GlobalStaticObj_Server.Instance.CloundServerIp, GlobalStaticObj_Server.Instance.CloundServerPort);
     }
     if (mc.Connect())
     {
         return true;
     }
     return false;
 }