public void ProcessSyncRq(StandardSyncRqTunnelPacket packet) { lock (_syncRqLock) { Logger.Debug("Got a sync request for " + Id + " from " + packet.ip + ", last sync packet was from " + (_lastSyncRqPacketIp == null ? "nowhere" : _lastSyncRqPacketIp.ToString())); if (_lastSyncRqPacketIp == null || _lastSyncRqPacketIp.Equals(packet.ip)) { if (_expectedPeer.EndPoints.Contains(packet.ip)) { _lastSyncRqPacketIp = packet.ip; _syncRqEvent.Set(); StandardSyncRsTunnelPacket syncRs = new StandardSyncRsTunnelPacket(); byte[] syncBytes = syncRs.getBytes(); _udpClient.Send(syncBytes, syncBytes.Length, packet.ip); } else { Logger.Error("Tunnel " + Id + ", got a sync request from an unexpected peer [" + packet.ip + ", not " + _expectedPeer + "], dropping"); } } else { Logger.Debug("Received a sync request packet for " + Id + " from " + packet.ip + " after we have already received our first one from " + _lastSyncRqPacketIp + ", ignoring"); } } }
public BasicTunnelPacket getPacket(byte[] bytes, IPEndPoint ip) { BasicTunnelPacket packet; switch (bytes[BasicTunnelPacket.PKT_POS_TYPE]) { case BasicTunnelPacket.PKT_TYPE_PING: packet = new StandardPingTunnelPacket(); break; case BasicTunnelPacket.PKT_TYPE_PONG: packet = new StandardPongTunnelPacket(); break; case BasicTunnelPacket.PKT_TYPE_SYNC_RS: packet = new StandardSyncRsTunnelPacket(); break; case BasicTunnelPacket.PKT_TYPE_SYNC_RQ: packet = new StandardSyncRqTunnelPacket(); break; case BasicTunnelPacket.PKT_TYPE_WAVE_RS: packet = new StandardWaveTunnelRsPacket(); break; case BasicTunnelPacket.PKT_TYPE_WAVE_RQ: packet = new StandardWaveTunnelRqPacket(); break; case BasicTunnelPacket.PKT_TYPE_DATA: packet = new StandardTunnelDataPacket(); break; case BasicTunnelPacket.PKT_TYPE_NOP: packet = new StandardTunnelNopPacket(); break; case BasicTunnelPacket.PKT_TYPE_CLOSE: packet = new StandardCloseTunnelPacket(); break; default: throw new UnknownPacketException("Failed to determine packet type [" + bytes[BasicTunnelPacket.PKT_POS_TYPE] + "]"); } packet.processPacket(bytes, ip); return packet; }
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; }