Пример #1
0
 private async Task ReceivePacketsAsync(ReadListeningHandler clientReadListener, CancellationToken cancellationToken)
 {
     while (!cancellationToken.IsCancellationRequested)
     {
         if (await clientReadListener.ReceiveAsync() is Packet packet)
         {
             await ProcessReceivedPacketAsync(packet);
         }
     }
 }
Пример #2
0
 /// <summary>
 /// 读取Packet
 /// </summary>
 /// <param name="clientReadListener"></param>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 private async Task TryReceivePacketsAsync(ReadListeningHandler clientReadListener, CancellationToken cancellationToken)
 {
     try
     {
         while (!cancellationToken.IsCancellationRequested)
         {
             if (await clientReadListener.ReceiveAsync() is Packet packet)
             {
                 await TryProcessReceivedPacketAsync(packet);
             }
         }
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, "Unhandled exception while receiving packets.");
     }
 }