예제 #1
0
 private void SendClientsHello()
 {
     byte[] timeBytes = BitConverter.GetBytes(DateTime.UtcNow.Ticks);
     UdpMeshCommon.FlipEndian(ref timeBytes);
     byte[] sendBytes = UdpMeshCommon.GetPayload(-201, timeBytes);
     lock (clients)
     {
         foreach (UdpPeer peer in clients.Values)
         {
             if (peer.guid == UdpMeshCommon.GetMeshAddress())
             {
                 continue;
             }
             //Send to ipv4
             if (peer.usev4)
             {
                 UdpMeshCommon.Send(clientSocketv4, sendBytes, peer.contactV4);
             }
             else
             {
                 foreach (IPEndPoint iPEndPoint in peer.remoteEndpoints)
                 {
                     if (UdpMeshCommon.IsIPv4(iPEndPoint.Address))
                     {
                         string newContactString = iPEndPoint.ToString();
                         if (!contactedIPs.Contains(newContactString))
                         {
                             contactedIPs.Add(newContactString);
                             DebugLog("Attempting new contact v4: " + newContactString);
                         }
                         UdpMeshCommon.Send(clientSocketv4, sendBytes, iPEndPoint);
                     }
                 }
             }
             //Send to ipv6
             if (peer.usev6)
             {
                 UdpMeshCommon.Send(clientSocketv6, sendBytes, peer.contactV6);
             }
             else
             {
                 foreach (IPEndPoint iPEndPoint in peer.remoteEndpoints)
                 {
                     if (UdpMeshCommon.IsIPv6(iPEndPoint.Address))
                     {
                         string newContactString = iPEndPoint.ToString();
                         if (!contactedIPs.Contains(newContactString))
                         {
                             contactedIPs.Add(newContactString);
                             DebugLog("Attempting new contact v6: " + newContactString);
                         }
                         UdpMeshCommon.Send(clientSocketv6, sendBytes, iPEndPoint);
                     }
                 }
             }
         }
     }
 }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:UDPMeshLib.UdpMeshClient"/> class.
 /// </summary>
 /// <param name="serverEndpointv4">Mesh servers IPv4 address to connect to.</param>
 /// <param name="serverEndpointv6">Mesh servers IPv6 address to connect to.</param>
 /// <param name="myAddresses">List of local addresses to inform the server about for in-network meshing</param>
 /// <param name="debugLog">Debug logging callback, leave <see langword="null"/> to disable</param>
 public UdpMeshClient(IPEndPoint serverEndpointv4, IPEndPoint serverEndpointv6, IPAddress[] myAddresses, Action <string> debugLog)
 {
     this.me               = new UdpPeer(UdpMeshCommon.GetMeshAddress());
     this.myAddresses      = myAddresses;
     this.serverEndpointv4 = serverEndpointv4;
     this.serverEndpointv6 = serverEndpointv6;
     this.debugLog         = debugLog;
     callbacks[-1]         = HandleServerReport;
     callbacks[-2]         = HandleClientInfo;
     callbacks[-3]         = HandleRelayMessage;
     callbacks[-201]       = HandleHeartBeat;
     callbacks[-202]       = HandleHeartBeatReply;
     UdpStun.callback      = HandleStun;
 }