예제 #1
0
 public void SendMessage()
 {
     if (accessConnection.IsConnected)
     {
         try
         {
             while (StartToSend)
             {
                 accessConnection.RefreshData(processPattern);
                 //accessConnection.process.SetProcessPattern(processPattern);
                 accessConnection.data.SetInformation(_clientName);
                 if (accessConnection.data.recordCount != 0)
                 {
                     try
                     {
                         byte[] sendMessageBytes = accessConnection.data.SendData();
                         clientSock.Send(sendMessageBytes);
                         for (int i = 0; i < 3; i++)
                         {
                             System.Threading.Thread.Sleep(_interval * 1000);
                             try
                             {
                                 byte[] messageBytes = new byte[100 * 1024];
                                 clientSock.Receive(messageBytes);
                                 string          message  = Encoding.UTF8.GetString(messageBytes);
                                 ResponseMessage response = ResponseMessage.Deserialize(message);
                                 if (response.Length == accessConnection.data.recordCount)
                                 {
                                     _currentState.Add(response.Time.ToString() + "|成功发送消息");
                                     accessConnection.ChangeTimeStamp();
                                     break;
                                 }
                             }
                             catch
                             {
                                 if (i == 3)
                                 {
                                     _currentState.Add("发送失败超过3次, 主动与服务端断开连接");
                                     throw new Exception("未收到返回确认");
                                 }
                                 else
                                 {
                                     _currentState.Add(DateTime.Now.ToString() + "|发送消息失败, 进行第" + (i + 1).ToString() + "次重试");
                                     continue;
                                 }
                             };
                         }
                     }
                     catch (Exception e)
                     {
                         _currentState.Add("服务端断连,请检查");
                         ServerConntected = false;
                         StartToSend      = false;
                     }
                 }
                 else
                 {
                     System.Threading.Thread.Sleep(_interval * 1000);
                 }
             }
         }
         catch
         {
             _currentState.Add("数据库读取出错,请检查");
             StartToSend = false;
         }
     }
     else
     {
         throw new Exception("数据库连接断开");
     }
 }