private static void SendRecvAPM_Stream_TCP(IPAddress listenAt, bool useMultipleBuffers) { const int BytesToSend = 123456; const int ListenBacklog = 1; const int LingerTime = 10; const int TestTimeout = 30000; var server = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp); server.BindToAnonymousPort(listenAt); server.Listen(ListenBacklog); var serverFinished = new TaskCompletionSource<bool>(); int bytesReceived = 0; var receivedChecksum = new Fletcher32(); server.AcceptAPM(remote => { Action<int> recvHandler = null; bool first = true; if (!useMultipleBuffers) { var recvBuffer = new byte[256]; recvHandler = received => { if (!first) { if (received == 0) { remote.Dispose(); server.Dispose(); serverFinished.SetResult(true); return; } bytesReceived += received; receivedChecksum.Add(recvBuffer, 0, received); } else { first = false; } remote.ReceiveAPM(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, recvHandler); }; } else { var recvBuffers = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[123]), new ArraySegment<byte>(new byte[256], 2, 100), new ArraySegment<byte>(new byte[1], 0, 0), new ArraySegment<byte>(new byte[64], 9, 33) }; recvHandler = received => { if (!first) { if (received == 0) { remote.Dispose(); server.Dispose(); serverFinished.SetResult(true); return; } bytesReceived += received; for (int i = 0, remaining = received; i < recvBuffers.Count && remaining > 0; i++) { ArraySegment<byte> buffer = recvBuffers[i]; int toAdd = Math.Min(buffer.Count, remaining); receivedChecksum.Add(buffer.Array, buffer.Offset, toAdd); remaining -= toAdd; } } else { first = false; } remote.ReceiveAPM(recvBuffers, SocketFlags.None, recvHandler); }; } recvHandler(0); }); EndPoint clientEndpoint = server.LocalEndPoint; var client = new Socket(clientEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); int bytesSent = 0; var sentChecksum = new Fletcher32(); client.ConnectAPM(clientEndpoint, () => { Action<int> sendHandler = null; var random = new Random(); var remaining = BytesToSend; bool first = true; if (!useMultipleBuffers) { var sendBuffer = new byte[512]; sendHandler = sent => { if (!first) { bytesSent += sent; sentChecksum.Add(sendBuffer, 0, sent); remaining -= sent; if (remaining <= 0) { client.LingerState = new LingerOption(true, LingerTime); client.Dispose(); return; } } else { first = false; } random.NextBytes(sendBuffer); client.SendAPM(sendBuffer, 0, Math.Min(sendBuffer.Length, remaining), SocketFlags.None, sendHandler); }; } else { var sendBuffers = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[23]), new ArraySegment<byte>(new byte[256], 2, 100), new ArraySegment<byte>(new byte[1], 0, 0), new ArraySegment<byte>(new byte[64], 9, 9) }; sendHandler = sent => { if (!first) { bytesSent += sent; for (int i = 0, r = sent; i < sendBuffers.Count && r > 0; i++) { ArraySegment<byte> buffer = sendBuffers[i]; int toAdd = Math.Min(buffer.Count, r); sentChecksum.Add(buffer.Array, buffer.Offset, toAdd); r -= toAdd; } remaining -= sent; if (remaining <= 0) { client.LingerState = new LingerOption(true, LingerTime); client.Dispose(); return; } } else { first = false; } for (int i = 0; i < sendBuffers.Count; i++) { random.NextBytes(sendBuffers[i].Array); } client.SendAPM(sendBuffers, SocketFlags.None, sendHandler); }; } sendHandler(0); }); Assert.True(serverFinished.Task.Wait(TestTimeout), "Completed within allowed time"); Assert.Equal(bytesSent, bytesReceived); Assert.Equal(sentChecksum.Sum, receivedChecksum.Sum); }
private void SendFile_APM(IPAddress listenAt, bool sendPreAndPostBuffers) { const int BytesToSend = 123456; const int ListenBacklog = 1; const int LingerTime = 10; const int TestTimeout = 30000; // Create file to send byte[] preBuffer; byte[] postBuffer; Fletcher32 sentChecksum; string filename = CreateFileToSend(BytesToSend, sendPreAndPostBuffers, out preBuffer, out postBuffer, out sentChecksum); // Start server var server = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp); server.BindToAnonymousPort(listenAt); server.Listen(ListenBacklog); var serverFinished = new TaskCompletionSource <bool>(); int bytesReceived = 0; var receivedChecksum = new Fletcher32(); server.AcceptAPM(remote => { Action <int> recvHandler = null; bool first = true; var recvBuffer = new byte[256]; recvHandler = received => { if (!first) { if (received == 0) { remote.Dispose(); server.Dispose(); serverFinished.SetResult(true); return; } bytesReceived += received; receivedChecksum.Add(recvBuffer, 0, received); } else { first = false; } remote.ReceiveAPM(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, recvHandler); }; recvHandler(0); }); // Run client EndPoint clientEndpoint = server.LocalEndPoint; var client = new Socket(clientEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); client.ConnectAPM(clientEndpoint, () => { client.SendFileAPM(filename, preBuffer, postBuffer, TransmitFileOptions.UseDefaultWorkerThread, () => { client.LingerState = new LingerOption(true, LingerTime); client.Dispose(); }); }); Assert.True(serverFinished.Task.Wait(TestTimeout), "Completed within allowed time"); Assert.Equal(BytesToSend, bytesReceived); Assert.Equal(sentChecksum.Sum, receivedChecksum.Sum); // Clean up the file we created File.Delete(filename); }
private static void SendRecvAPM_Stream_TCP(IPAddress listenAt, bool useMultipleBuffers) { const int BytesToSend = 123456; const int ListenBacklog = 1; const int LingerTime = 10; const int TestTimeout = 30000; var server = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp); server.BindToAnonymousPort(listenAt); server.Listen(ListenBacklog); var serverFinished = new TaskCompletionSource <bool>(); int bytesReceived = 0; var receivedChecksum = new Fletcher32(); server.AcceptAPM(remote => { Action <int> recvHandler = null; bool first = true; if (!useMultipleBuffers) { var recvBuffer = new byte[256]; recvHandler = received => { if (!first) { if (received == 0) { remote.Dispose(); server.Dispose(); serverFinished.SetResult(true); return; } bytesReceived += received; receivedChecksum.Add(recvBuffer, 0, received); } else { first = false; } remote.ReceiveAPM(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, recvHandler); }; } else { var recvBuffers = new List <ArraySegment <byte> > { new ArraySegment <byte>(new byte[123]), new ArraySegment <byte>(new byte[256], 2, 100), new ArraySegment <byte>(new byte[1], 0, 0), new ArraySegment <byte>(new byte[64], 9, 33) }; recvHandler = received => { if (!first) { if (received == 0) { remote.Dispose(); server.Dispose(); serverFinished.SetResult(true); return; } bytesReceived += received; for (int i = 0, remaining = received; i < recvBuffers.Count && remaining > 0; i++) { ArraySegment <byte> buffer = recvBuffers[i]; int toAdd = Math.Min(buffer.Count, remaining); receivedChecksum.Add(buffer.Array, buffer.Offset, toAdd); remaining -= toAdd; } } else { first = false; } remote.ReceiveAPM(recvBuffers, SocketFlags.None, recvHandler); }; } recvHandler(0); }); EndPoint clientEndpoint = server.LocalEndPoint; var client = new Socket(clientEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); int bytesSent = 0; var sentChecksum = new Fletcher32(); client.ConnectAPM(clientEndpoint, () => { Action <int> sendHandler = null; var random = new Random(); var remaining = BytesToSend; bool first = true; if (!useMultipleBuffers) { var sendBuffer = new byte[512]; sendHandler = sent => { if (!first) { bytesSent += sent; sentChecksum.Add(sendBuffer, 0, sent); remaining -= sent; if (remaining <= 0) { client.LingerState = new LingerOption(true, LingerTime); client.Dispose(); return; } } else { first = false; } random.NextBytes(sendBuffer); client.SendAPM(sendBuffer, 0, Math.Min(sendBuffer.Length, remaining), SocketFlags.None, sendHandler); }; } else { var sendBuffers = new List <ArraySegment <byte> > { new ArraySegment <byte>(new byte[23]), new ArraySegment <byte>(new byte[256], 2, 100), new ArraySegment <byte>(new byte[1], 0, 0), new ArraySegment <byte>(new byte[64], 9, 9) }; sendHandler = sent => { if (!first) { bytesSent += sent; for (int i = 0, r = sent; i < sendBuffers.Count && r > 0; i++) { ArraySegment <byte> buffer = sendBuffers[i]; int toAdd = Math.Min(buffer.Count, r); sentChecksum.Add(buffer.Array, buffer.Offset, toAdd); r -= toAdd; } remaining -= sent; if (remaining <= 0) { client.LingerState = new LingerOption(true, LingerTime); client.Dispose(); return; } } else { first = false; } for (int i = 0; i < sendBuffers.Count; i++) { random.NextBytes(sendBuffers[i].Array); } client.SendAPM(sendBuffers, SocketFlags.None, sendHandler); }; } sendHandler(0); }); Assert.True(serverFinished.Task.Wait(TestTimeout), "Completed within allowed time"); Assert.Equal(bytesSent, bytesReceived); Assert.Equal(sentChecksum.Sum, receivedChecksum.Sum); }
private void SendFile_APM(IPAddress listenAt, bool sendPreAndPostBuffers) { const int BytesToSend = 123456; const int ListenBacklog = 1; const int LingerTime = 10; const int TestTimeout = 30000; // Create file to send byte[] preBuffer; byte[] postBuffer; Fletcher32 sentChecksum; string filename = CreateFileToSend(BytesToSend, sendPreAndPostBuffers, out preBuffer, out postBuffer, out sentChecksum); // Start server var server = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp); server.BindToAnonymousPort(listenAt); server.Listen(ListenBacklog); var serverFinished = new TaskCompletionSource<bool>(); int bytesReceived = 0; var receivedChecksum = new Fletcher32(); server.AcceptAPM(remote => { Action<int> recvHandler = null; bool first = true; var recvBuffer = new byte[256]; recvHandler = received => { if (!first) { if (received == 0) { remote.Dispose(); server.Dispose(); serverFinished.SetResult(true); return; } bytesReceived += received; receivedChecksum.Add(recvBuffer, 0, received); } else { first = false; } remote.ReceiveAPM(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, recvHandler); }; recvHandler(0); }); // Run client EndPoint clientEndpoint = server.LocalEndPoint; var client = new Socket(clientEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); client.ConnectAPM(clientEndpoint, () => { client.SendFileAPM(filename, preBuffer, postBuffer, TransmitFileOptions.UseDefaultWorkerThread, () => { client.LingerState = new LingerOption(true, LingerTime); client.Dispose(); }); }); Assert.True(serverFinished.Task.Wait(TestTimeout), "Completed within allowed time"); Assert.Equal(BytesToSend, bytesReceived); Assert.Equal(sentChecksum.Sum, receivedChecksum.Sum); // Clean up the file we created File.Delete(filename); }