static void ProcessNotifyClockQueue(PseudoTcpSocket conn, Queue notifyClockQueue) { PLog.DEBUG("Entering ProcessNotifyClockQueue with queue size={0}", notifyClockQueue.Count); if (notifyClockQueue.Count != 0) { PLog.DEBUG("...and head timestamp={0}, current time={1}", notifyClockQueue.Peek(), Environment.TickCount); } if (notifyClockQueue.Count == 0) { UdpCallbacks.AdjustClock(conn, notifyClockQueue); return; } bool keepChecking = true; while (keepChecking && notifyClockQueue.Count > 0) { int iTimestamp = (int)notifyClockQueue.Peek(); if (Environment.TickCount > iTimestamp) { SyncPseudoTcpSocket.NotifyClock(conn); notifyClockQueue.Dequeue(); } else { keepChecking = false; } } }
static int SendFragment(PseudoTcpSocket conn, byte[] buffer, int fragmentSize, Queue notifyClockQueue) { int sent; do { PLog.DEBUG("Trying to send {0} bytes", fragmentSize); sent = SyncPseudoTcpSocket.Send(conn, buffer, (uint)fragmentSize); if (sent == -1) { PLog.DEBUG("sent==-1 so processing notifyClockQueue"); ProcessNotifyClockQueue(conn, notifyClockQueue); } else { UdpCallbacks.AdjustClock(conn, notifyClockQueue); } UdpCallbacks.PollingSleep(sent, -1); } while (sent == -1); PLog.DEBUG("Tried sending fragment sized {0} with result {1}", fragmentSize, sent); return(sent); }
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); }
static internal void Run(PseudoTcpSocket conn) { PLog.DEBUG("In Receiver.Run"); int ini = Environment.TickCount; byte[] ackBuffer = new byte[1]; ackBuffer [0] = 1; long got = 0; byte[] fnlBytes = new byte[4]; do { got = SyncPseudoTcpSocket.Recv(conn, fnlBytes, 4); PLog.DEBUG("Reading filename length, got={0}", got); UdpCallbacks.PollingSleep(got, -1); } while (got == -1); SyncPseudoTcpSocket.Send(conn, ackBuffer, 1); uint fileNameLengthBytes = (uint)BitConverter.ToInt32(fnlBytes, 0); PLog.DEBUG("Got filename length=" + fileNameLengthBytes); byte[] fnBytes = new byte[fileNameLengthBytes]; do { got = SyncPseudoTcpSocket.Recv(conn, fnBytes, fileNameLengthBytes); PLog.DEBUG("Reading filename, got={0}", got); UdpCallbacks.PollingSleep(got, -1); } while (got == -1); SyncPseudoTcpSocket.Send(conn, ackBuffer, 1); PLog.DEBUG("filename bytes=" + BitConverter.ToString(fnBytes)); string fileName = System.Text.Encoding.UTF8.GetString(fnBytes); Console.WriteLine("Receiving file: {0}", fileName); byte[] fileSizeBytes = new byte[sizeof(long)]; do { got = SyncPseudoTcpSocket.Recv(conn, fileSizeBytes, (uint)fileSizeBytes.Length); PLog.DEBUG("Reading file size, got={0}", got); UdpCallbacks.PollingSleep(got, -1); } while (got == -1); SyncPseudoTcpSocket.Send(conn, ackBuffer, 1); long size = (long)BitConverter.ToInt64(fileSizeBytes, 0); Console.WriteLine("File size={0} bytes", size); byte[] buffer = new byte[4 * 1024 * 1024]; int i = 0; ConsoleProgress.Draw(i++, 0, size, ini, Console.WindowWidth / 2); using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) { long len = 0; long read = 0; while (read < size) { do { PLog.DEBUG("{0} Reading file data... so far total read={1}", Environment.TickCount, read); len = SyncPseudoTcpSocket.Recv(conn, buffer, (uint)buffer.Length); UdpCallbacks.PollingSleep(len, -1); } while (len == -1); PLog.DEBUG("Read {0} bytes of file data", len); fileStream.Write(buffer, 0, (int)len); read += len; ConsoleProgress.Draw(i++, read, size, ini, Console.WindowWidth / 2); } double MB = (double)size * 8.0 / (1024.0 * 1024.0); double rxTime = (Environment.TickCount - ini) / 1000.0; Console.WriteLine(); Console.WriteLine("Receive time=" + rxTime + " secs"); Console.WriteLine("Av bandwidth=" + MB / rxTime + " Mbits/sec"); } }