internal static void TcpWriteCallback(IAsyncResult ar) { TcpTestConnectionContext context = (TcpTestConnectionContext)ar.AsyncState; try { context.testStream.EndWrite(ar); if (context.testDurationSeconds > 0 && context.testTimer.ElapsedMilliseconds >= context.testDurationSeconds * 1000) { context.testClient.Close(); } else { context.testStream.BeginWrite(context.bufferToWrite, 0, context.bufferToWrite.Length, TcpWriteCallback, context); if (!context.control.CheckConnected()) { context.testClient.Close(); } } } catch { context.testClient.Close(); } }
public static void OpenTestConnection(ControlConnectionContext control, int testDurationSeconds) { TcpClient testClient = null; try { IPEndPoint remoteEP = (IPEndPoint)control.client.Client.RemoteEndPoint; testClient = new TcpClient(); TcpTestConnectionContext context = new TcpTestConnectionContext(control, null, testClient, testDurationSeconds); testClient.BeginConnect(remoteEP.Address, remoteEP.Port, EndConnect, context); } catch { testClient?.Close(); } }
private void HandleTcpClient(TcpClient client) { try { NetworkStream stream = client.GetStream(); string first5Bytes = ByteUtil.ReadUtf8(stream, 6); if (first5Bytes == "STSCC0") // Control Connection { int testDirection = stream.ReadByte(); // Generate a unique key for this control connection and tell it to the remote client. ControlConnectionContext control = new ControlConnectionContext(client, closingControlConnection => { currentControlConnections.TryRemove(closingControlConnection.key, out ControlConnectionContext ignored); }); control.testDirection = testDirection; currentControlConnections[control.key] = control; ByteUtil.WriteUtf8_16(control.key, stream); control.StartBandwidthMeasurement(); } else if (first5Bytes == "STSST0") // TCP Speed Test connection { // Read the control connection unique key from the remote client. string ccKey = ByteUtil.ReadUtf8_16(stream); if (currentControlConnections.TryGetValue(ccKey, out ControlConnectionContext control)) { TcpTestConnectionContext context = new TcpTestConnectionContext(control, stream, client, 0); // From remote perspective: 0: Download, 1: Upload, 2: Duplex if (control.testDirection == 0 || control.testDirection == 2) { stream.BeginWrite(context.bufferToWrite, 0, context.bufferToWrite.Length, TcpTestConnectionClient.TcpWriteCallback, context); } stream.BeginRead(context.bufferToRead, 0, context.bufferToRead.Length, TcpTestConnectionClient.TcpReadCallback, context); } } else { client.Close(); } } catch { client.Close(); } }
internal static void TcpReadCallback(IAsyncResult ar) { TcpTestConnectionContext context = (TcpTestConnectionContext)ar.AsyncState; try { int read = context.testStream.EndRead(ar); if (read <= 0) { context.testClient.Close(); } else { context.testStream.BeginRead(context.bufferToRead, 0, context.bufferToRead.Length, TcpReadCallback, context); context.control.CountDataRead(read); } } catch { context.testClient.Close(); } }
private static void EndConnect(IAsyncResult ar) { TcpTestConnectionContext context = (TcpTestConnectionContext)ar.AsyncState; try { context.testClient.EndConnect(ar); context.testStream = context.testClient.GetStream(); ByteUtil.WriteUtf8("STSST0", context.testStream); ByteUtil.WriteUtf8_16(context.control.key, context.testStream); if (context.control.testDirection == 1 || context.control.testDirection == 2) { context.testStream.BeginWrite(context.bufferToWrite, 0, context.bufferToWrite.Length, TcpWriteCallback, context); } context.testStream.BeginRead(context.bufferToRead, 0, context.bufferToRead.Length, TcpReadCallback, context); } catch { context.testClient.Close(); } }