Example #1
0
      private async Task RunAnnounceLoopAsync() {
         await TaskEx.YieldToThreadPool();

         var announce = new AnnouncementDto { Identity = identity };

         while (!shutdownCancellationToken.IsCancellationRequested) {
            try {
               await payloadSender.BroadcastAsync(announce).ConfigureAwait(false);
               await Task.Delay(kAnnounceIntervalMillis, shutdownCancellationToken).ConfigureAwait(false);
            } catch (OperationCanceledException) {
               // shutdown cancellation token cancelled
            }
         }
      }
      private void HandleAnnouncement(UdpClientRemoteInfo remoteInfo, AnnouncementDto x) {
         announcementsReceivedCounter.Increment();

         var peerIdentity = x.Identity;
         var peerId = peerIdentity.Id;
         bool isNewlyDiscoveredRoute = false;
         RoutingContext addedRoutingContext = null;
         UdpUnicaster addedUnicaster = null;
         var routingContext = routingContextsByPeerId.GetOrAdd(
            peerId,
            add => {
               isNewlyDiscoveredRoute = true;
               var unicastReceivePort = int.Parse((string)x.Identity.Properties[UdpConstants.kUnicastPortIdentityPropertyKey]);
               var unicastIpAddress = remoteInfo.IPEndpoint.Address;
               var unicastEndpoint = new IPEndPoint(unicastIpAddress, unicastReceivePort);
               var unicastRemoteInfo = new UdpClientRemoteInfo {
                  Socket = remoteInfo.Socket,
                  IPEndpoint = unicastEndpoint
               };

               addedUnicaster = udpUnicasterFactory.Create(unicastRemoteInfo);
               return addedRoutingContext = new RoutingContext(addedUnicaster);
            });
         if (addedRoutingContext == routingContext) {
            addedUnicaster.Initialize();
         }
         if (isNewlyDiscoveredRoute) {
            routingTable.Register(peerId, routingContext);
         }

         peerTable.GetOrAdd(peerId).HandleInboundPeerIdentityUpdate(peerIdentity);
      }