static Udt.Socket PeerConnect(Socket socket, string remoteAddr, int remotePort) { bool bConnected = false; int retry = 0; Udt.Socket client = null; while (!bConnected) { try { DateTime now = InternetTime.Get(); int sleepTimeToSync = SleepTime(now); Console.WriteLine("[{0}] - Waiting {1} sec to sync with other peer", now.ToLongTimeString(), sleepTimeToSync); System.Threading.Thread.Sleep(sleepTimeToSync * 1000); GetExternalEndPoint(socket); if (client != null) { client.Close(); } client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); client.SetSocketOption(Udt.SocketOptionName.Rendezvous, true); client.Bind(socket); Console.Write("\r{0} - Trying to connect to {1}:{2}. ", retry++, remoteAddr, remotePort); client.Connect(remoteAddr, remotePort); Console.WriteLine("Connected successfully to {0}:{1}", remoteAddr, remotePort); bConnected = true; } catch (Exception e) { Console.Write(e.Message.Replace(Environment.NewLine, ". ")); } } return(client); }
static PseudoTcpSocket PeerConnect(string externalAddr, int externalPort, Socket socket, string remoteAddr, int remotePort, CommandLineArguments cla) { bool bConnected = false; int retry = 0; PseudoTcpSocket client = null; while (!bConnected) { try { Console.WriteLine("Getting internet time"); DateTime now = InternetTime.Get(); int nextTimeToSync = NextTime(now); int sleepTimeToSync = nextTimeToSync - now.Second; Console.WriteLine("[{0}] - Waiting {1} sec to sync with other peer", now.ToLongTimeString(), sleepTimeToSync); System.Threading.Thread.Sleep(sleepTimeToSync * 1000); PseudoTcpSocket.Callbacks cbs = new PseudoTcpSocket.Callbacks(); UdpCallbacks icbs = new UdpCallbacks(); cbs.WritePacket = icbs.WritePacket; cbs.PseudoTcpOpened = icbs.Opened; cbs.PseudoTcpWritable = icbs.Writable; cbs.PseudoTcpClosed = icbs.Closed; client = PseudoTcpSocket.Create(0, cbs); client.NotifyMtu(1496); // Per PseudoTcpTests bool success = icbs.Init(externalAddr, externalPort, remoteAddr, remotePort, client, socket, cla.Sender, nextTimeToSync); if (false == success) { continue; } PLog.DEBUG("Created PseudoTcpSocket"); Console.WriteLine("\r{0} - Trying to connect to {1}:{2}. ", retry++, remoteAddr, remotePort); if (cla.Sender) { PLog.DEBUG("Sender: calling PseudoTcpSocket.Connect"); client.Connect(); } int startTime = Environment.TickCount; while (false == bConnected) { Console.WriteLine("priv.state=={0}", client.priv.state); if (PseudoTcpSocket.PseudoTcpState.Values.TCP_ESTABLISHED == client.priv.state) { Console.WriteLine("Connected successfully to {0}:{1}", remoteAddr, remotePort); bConnected = true; } else { if (Environment.TickCount > startTime + Config.MAX_TCP_HANDSHAKE_TIME) { Console.WriteLine("5 secs timed out with priv.state={0}", client.priv.state); break; } else { Console.WriteLine("Waiting for TCP_ESTABLISHED..."); Thread.Sleep(500); } } } } catch (Exception e) { Console.WriteLine(e.Message.Replace(Environment.NewLine, ". ")); Console.WriteLine(e.StackTrace); Console.WriteLine("Inner exception=" + e.InnerException); } } return(client); }