Example #1
0
 protected bool Equals(ServiceDefine other)
 {
     return(Guid.Equals(other.Guid) && Equals(ProviderIpEndPoint, other.ProviderIpEndPoint));
 }
Example #2
0
 protected virtual void OnServiceRemoved(ServiceDefine obj)
 {
     ServiceRemoved?.Invoke(obj);
 }
Example #3
0
 public void Start(int port = ServiceBroadcaster.BroadcastPort)
 {
     if (_listenservicing)
     {
         return;
     }
     _listenservicing = true;
     _listenservice   = true;
     Task.Run(() => {
         //  Debug.WriteLine($"service-listener start at port{port}");
         var udp = new UdpClient(port);
         try {
             IPEndPoint ipEndPoint = null;
             while (_listenservice)
             {
                 var head = udp.Receive(ref ipEndPoint).ToStruct <ServiceInfo>();
                 //Debug.WriteLine($"get service_info form {ipEndPoint},length={Marshal.SizeOf(head)}");
                 if (!head.GetIsCorrect(Filter))
                 {
                     continue;
                 }
                 //   Debug.WriteLine($"validate service_info form {ipEndPoint}");
                 if (ServiceList.TryGetValue(head.ServiceUid, out ServiceDefine serviceDefine))
                 {
                     serviceDefine.LastTime = DateTime.Now;
                 }
                 else
                 {
                     var define = new ServiceDefine
                     {
                         Guid               = head.ServiceUid,
                         LastTime           = DateTime.Now,
                         ProviderIpEndPoint = new IPEndPoint(new IPAddress(head.IpBytes), head.ServicePort)
                     };
                     ServiceList.TryAdd(head.ServiceUid, define);
                     //     Debug.WriteLine(
                     //       $"new service_info add uid:{define.Guid}\r\n endpoint:{define.ProviderIpEndPoint}");
                     if (_bindServiceFactory != null && _bindServiceFactory.RemoteChannel == null)
                     {
                         _bindServiceFactory.RemoteChannel = define.ProviderIpEndPoint;
                     }
                     OnNewServiceAdded(define);
                 }
             }
             //维护服务列表
             foreach (var guid in ServiceList.Keys)
             {
                 if ((DateTime.Now - ServiceList[guid].LastTime).TotalSeconds > 2)
                 {
                     if (ServiceList.TryRemove(guid, out ServiceDefine define))
                     {
                         //Debug.WriteLine(
                         //$"old service_info removed uid:{define.Guid}\r\n endpoint:{define.ProviderIpEndPoint}");
                         if (_bindServiceFactory != null &&
                             Equals(_bindServiceFactory.RemoteChannel, define.ProviderIpEndPoint))
                         {
                             _bindServiceFactory.RemoteChannel = null;
                         }
                         OnServiceRemoved(define);
                     }
                 }
             }
         }
         finally {
             //    Debug.WriteLine($"service-listener closed at {port}");
             udp.Close();
             _listenservicing = false;
         }
     });
 }
Example #4
0
 protected virtual void OnNewServiceAdded(ServiceDefine obj)
 {
     NewServiceAdded?.Invoke(obj);
 }