public PeerInfo Wave(IPEndPoint facilitatorIp, int timeout, UdpClient udpClient) { long waitTime = timeout * 10000; _udpClient = udpClient; Logger.Debug("Waving at " + facilitatorIp + " from local port " + ((IPEndPoint)udpClient.Client.LocalEndPoint).Port + " for " + timeout + "ms"); var packet = new StandardWaveTunnelRqPacket { internalEndPoint = new IPEndPoint(IPAddress.Any, ((IPEndPoint)udpClient.Client.LocalEndPoint).Port) }; long startTime = DateTime.Now.Ticks; _waveEvent.Reset(); // Setup a listener for a wave response InitReceiverWaveRs(); do { byte[] sendBytes = packet.getBytes(); udpClient.Send(sendBytes, sendBytes.Length, facilitatorIp); if (DateTime.Now.Ticks - startTime > waitTime) { Logger.Error("Wave timeout : " + (DateTime.Now.Ticks - startTime)); //throw new TimeoutException("Timeout occured while waving"); break; } Logger.Debug("Waiting for wave response from " + facilitatorIp + " to local port " + ((IPEndPoint)udpClient.Client.LocalEndPoint).Port); } while (!_waveEvent.WaitOne(2000)); if(_waveResult == null) { _waveResult = new PeerInfo(); } _waveResult.InternalEndPoints = GetLocalEndPoints(_udpClient); return _waveResult; }
internal override UserToUserResponse ProcessWithEngagement(Engagement engagement, UserToUserRequest req) { ListenHandshakeRq request = (ListenHandshakeRq)req; ListenHandshakeRs response = new ListenHandshakeRs(); try { var peerInfo = new PeerInfo() { ExternalEndPoint = request.externalEndPoint != null ? new IPEndPoint(IPAddress.Parse(request.externalEndPoint.address), request.externalEndPoint.port) : null, }; foreach (var ipEndPointElement in request.internalEndPoints) { peerInfo.InternalEndPoints.Add(new IPEndPoint(IPAddress.Parse(ipEndPointElement.address), ipEndPointElement.port)); } _appContext.P2PManager.ReceiveP2PTunnel(request.uniqueId, peerInfo); } catch (Exception e) { Logger.Error("Failed to start listening for UDP traffic from peer : " + e.Message,e); response.error = "LISTEN_ERROR"; response.errorMessage = "Failed to start listening for UDP traffic"; } return response; }
public PeerInfo Wave(IPEndPoint facilitatorIp, int timeout, UdpClient udpClient) { long waitTime = timeout * 10000; _udpClient = udpClient; Logger.Debug("Waving at " + facilitatorIp + " from local port " + ((IPEndPoint)udpClient.Client.LocalEndPoint).Port + " for " + timeout + "ms"); var packet = new StandardWaveTunnelRqPacket { internalEndPoint = new IPEndPoint(IPAddress.Any, ((IPEndPoint)udpClient.Client.LocalEndPoint).Port) }; long startTime = DateTime.Now.Ticks; _waveEvent.Reset(); // Setup a listener for a wave response InitReceiverWaveRs(); do { byte[] sendBytes = packet.getBytes(); udpClient.Send(sendBytes, sendBytes.Length, facilitatorIp); if (DateTime.Now.Ticks - startTime > waitTime) { Logger.Error("Wave timeout : " + (DateTime.Now.Ticks - startTime)); //throw new TimeoutException("Timeout occured while waving"); break; } Logger.Debug("Waiting for wave response from " + facilitatorIp + " to local port " + ((IPEndPoint)udpClient.Client.LocalEndPoint).Port); } while (!_waveEvent.WaitOne(2000)); if (_waveResult == null) { _waveResult = new PeerInfo(); } _waveResult.InternalEndPoints = GetLocalEndPoints(_udpClient); return(_waveResult); }
public void ProcessWaveRs(StandardWaveTunnelRsPacket packet) { Logger.Debug("Processing Wave Response from " + packet.ip); _waveResult = new PeerInfo() { ExternalEndPoint = packet.externalEndPoint, FacilitatorRepeatedEndPoint = packet.ip }; _waveEvent.Set(); }
public IPEndPoint SyncWithPeer(PeerInfo peer, int timeout, UdpClient udpClient) { long waitTime = timeout * 10000; long startTime = DateTime.Now.Ticks; Logger.Debug("Syncing with peer " + peer + " from " + udpClient.Client.LocalEndPoint + " to establish tunnel " + Id); StandardSyncRqTunnelPacket syncRq = new StandardSyncRqTunnelPacket(); _syncEvent.Reset(); _udpClient = udpClient; InitReceiverThread(); int attempts = 0; do { if (DateTime.Now.Ticks - startTime > waitTime) { Logger.Debug("Tunnel " + Id + ", sync Timeout : " + (DateTime.Now.Ticks - startTime)); throw new TimeoutException("Tunnel " + Id + ", timeout occured while attempting sync with peer " + peer); } byte[] syncBytes = syncRq.getBytes(); if ((_syncTypes.Contains(SyncType.All) || _syncTypes.Contains(SyncType.Internal))) { Logger.Debug("Tunnel " + Id + ", sent requests, waiting for sync response from internal of " + peer); foreach (var endPoint in peer.InternalEndPoints) { udpClient.Send(syncBytes, syncBytes.Length, endPoint); } } if (attempts > internalAttempts && (_syncTypes.Contains(SyncType.All) || _syncTypes.Contains(SyncType.External))) { Logger.Debug("Tunnel " + Id + ", sent requests, waiting for sync response from external of " + peer); udpClient.Send(syncBytes, syncBytes.Length, peer.ExternalEndPoint); } // Faciliatator is our last choice, only send here after 3 attempts at the other ones if (attempts >= externalAttempts && (_syncTypes.Contains(SyncType.All) || _syncTypes.Contains(SyncType.Facilitator))) { Logger.Debug("Tunnel " + Id + ", sent requests, waiting for sync response from facilitator of " + peer); udpClient.Send(syncBytes, syncBytes.Length, peer.FacilitatorRepeatedEndPoint); } attempts++; } while (!_syncEvent.WaitOne(1000)); var activeIp = _lastSyncPacketIp; Logger.Debug("Tunnel " + Id + ", synchronisation with " + activeIp + " established"); return(activeIp); }
public IPEndPoint Sync(PeerInfo peer, string syncId, List<SyncType> syncTypes = null) { var syncer = new Syncer(syncId, syncTypes); var activeIp = syncer.SyncWithPeer(peer, 60000, _udpClient); var udtSocket = SetupUdtSocket(); udtSocket.Bind(_udpClient.Client); udtSocket.Connect(activeIp.Address, activeIp.Port); UdtConnection = udtSocket; UdtConnection.BlockingReceive = true; Logger.Debug("[" + Thread.CurrentThread.ManagedThreadId + "] Successfully completed outgoing tunnel with " + activeIp + "-" + syncId); return activeIp; }
private static void RunSyncer(string connectionId, PeerInfo peer, ITunnelEndpoint pendingTunnel, Action<ISocket> receivingMethod) { try { var activeIp = pendingTunnel.WaitForSync(peer, connectionId, GwupeClientAppContext.CurrentAppContext.SettingsManager.SyncTypes); Logger.Info("Successfully completed incoming tunnel with " + activeIp.Address + ":" + activeIp.Port + " [" + connectionId + "]"); // call the callback method Logger.Debug("Handing over the the receiving method for this connection"); receivingMethod(pendingTunnel); } catch (Exception ex) { ThreadPool.QueueUserWorkItem(m => GwupeClientAppContext.CurrentAppContext.SubmitFaultReport( new FaultReport() { Subject = "P2P setup error [" + connectionId + "]", UserReport = "INTERNAL : Failed to sync with peer [" + connectionId + "] : " + ex.Message })); Logger.Error("Failed to sync with peer [" + peer + "] for connection " + connectionId, ex); } }
private static PeerInfo GetPeerInfoFromResponse(InitP2PConnectionRs response) { var peer = new PeerInfo() { ExternalEndPoint = response.externalEndPoint != null ? new IPEndPoint(IPAddress.Parse(response.externalEndPoint.address), Convert.ToInt32(response.externalEndPoint.port)) : null, }; foreach (var ipEndPointElement in response.internalEndPoints) { peer.InternalEndPoints.Add(new IPEndPoint(IPAddress.Parse(ipEndPointElement.address), ipEndPointElement.port)); } return peer; }
// This is where we wait to receive a connection, requested by the server internal void ReceiveP2PTunnel(string connectionId, PeerInfo peerInfo) { TunnelEndpointContainer pendingTunnel = GetPendingTunnel(connectionId); var receivingMethod = _awaitingConnections[connectionId]; _awaitingConnections.Remove(connectionId); // now to complete the tunnel peerInfo.FacilitatorRepeatedEndPoint = pendingTunnel.FacilitatorEndPoint; Thread thread = new Thread(() => RunSyncer(connectionId, peerInfo, pendingTunnel.TunnelEndpoint, receivingMethod)) { Name = "waitforsync-" + connectionId, IsBackground = true }; thread.Start(); }
public IPEndPoint WaitForSyncFromPeer(PeerInfo peer, int timeout, UdpClient udpClient) { long waitTime = timeout * 10000; long startTime = DateTime.Now.Ticks; Logger.Debug("Waiting for sync from peer " + peer + " to " + udpClient.Client.LocalEndPoint + " to establish tunnel " + Id); StandardTunnelNopPacket nop = new StandardTunnelNopPacket(); _expectedPeer = peer; _syncEvent.Reset(); _udpClient = udpClient; InitReceiverThread(); int attempts = 0; do { if (DateTime.Now.Ticks - startTime > waitTime) { Logger.Debug("Tunnel " + Id + ", sync Timeout : " + (DateTime.Now.Ticks - startTime)); throw new TimeoutException("Tunnel " + Id + ", timeout occured while waiting for sync from peer " + peer); } byte[] nopBytes = nop.getBytes(); if ((_syncTypes.Contains(SyncType.All) || _syncTypes.Contains(SyncType.Internal))) { Logger.Debug("Tunnel " + Id + ", sent nops, now waiting for sync request from internal of " + peer); foreach (var endPoint in peer.InternalEndPoints) { udpClient.Send(nopBytes, nopBytes.Length, endPoint); } } if (attempts > internalAttempts && (_syncTypes.Contains(SyncType.All) || _syncTypes.Contains(SyncType.External))) { Logger.Debug("Tunnel " + Id + ", sent nops, now waiting for sync request from external of " + peer); udpClient.Send(nopBytes, nopBytes.Length, peer.ExternalEndPoint); } // Facilitator is our last choice, only send here after 3 attempts at the other ones if (attempts >= externalAttempts && (_syncTypes.Contains(SyncType.All) || _syncTypes.Contains(SyncType.Facilitator))) { Logger.Debug("Tunnel " + Id + ", sent nops, now waiting for sync request from facilitator of " + peer); udpClient.Send(nopBytes, nopBytes.Length, peer.FacilitatorRepeatedEndPoint); } attempts++; } while (!_syncRqEvent.WaitOne(1000)); var activeIp = _lastSyncRqPacketIp; Logger.Debug("Tunnel " + Id + ", synchronisation with " + activeIp + " established"); return activeIp; }
public PeerInfo Wave(IPEndPoint facilitator, int timeout) { _waver = new Waver(); _self = _waver.Wave(facilitator, timeout, _udpClient); return _self; }
public void WaitForSyncFromPeer(PeerInfo peerIp, int timeout) { _syncer = new Syncer(Id); _remoteEndpoint = _syncer.WaitForSyncFromPeer(peerIp, timeout, _udpClient); _peer = peerIp; IsTunnelEstablished = true; StartCheckerForPings(); }
public void SyncWithPeer(PeerInfo peerIp, int timeout) { _syncer = new Syncer(Id); _remoteEndpoint = _syncer.SyncWithPeer(peerIp, timeout, _udpClient); _peer = peerIp; IsTunnelEstablished = true; StartPinger(); }
private void InitSyncClick(object sender, RoutedEventArgs e) { EnableClientServerButtons(); InitSyncWithButton.IsEnabled = false; WaitSyncFromButton.IsEnabled = false; try { var peerInfo = new PeerInfo(); { //ExternalEndPoint = new IPEndPoint(IPAddress.Parse(Destination.Text), Convert.ToInt32(DestinationPort.Text)) }; _socket.Sync(peerInfo, "test"); Status.Text = "Synced with " + Destination.Text + ":" + DestinationPort.Text; } catch (Exception ex) { Dispatcher.Invoke(new Action(() => ErrorBlock.Text = ex.Message + "\n" + ex.StackTrace)); } }
public IPEndPoint WaitForSync(PeerInfo peer, String syncId, List<SyncType> syncTypes = null) { var syncer = new Syncer(syncId, syncTypes); var activeIp = syncer.WaitForSyncFromPeer(peer, 60000, _udpClient); ListeningSocket = SetupUdtSocket(); ListeningSocket.Bind(_udpClient.Client); ListeningSocket.Listen(10); Udt.Socket udtClient = ListeningSocket.Accept(); UdtConnection = udtClient; UdtConnection.BlockingReceive = true; Logger.Debug("Successfully completed incoming tunnel with " + activeIp + "-" + syncId); return activeIp; }
public PeerInfo Wave(IPEndPoint facilitator) { var waver = new Waver(); _self = waver.Wave(facilitator, 10000, _udpClient); Logger.Debug("After wave, local endpoints are " + _self); if (_self.ExternalEndPoint == null) { Logger.Warn("Failed to get external endpoint : " + _self); } if (_self.EndPoints.Count == 0) { throw new Exception("Failed to determine any local endpoints : " + _self.ToString()); } return _self; }