public async Task<RioSocket> Connect(Uri adress)
{
var ip = (await Dns.GetHostAddressesAsync(adress.Host)).First(i => i.AddressFamily == AddressFamily.InterNetwork);
sockaddr_in sa = new sockaddr_in();
sa.sin_family = adressFam;
sa.sin_port = WinSock.htons((ushort)adress.Port);
var ipBytes = ip.GetAddressBytes();
unsafe
{
fixed (byte* a = ipBytes)
Unsafe.CopyBlock(sa.sin_addr.Address, a, (uint)ipBytes.Length);
}
RioConnectionOrientedSocket s;
if (_freeSockets.TryDequeue(out s))
{
s.SetInUse(true);
var tcs = new TaskCompletionSource<RioSocket>();
_ongoingConnections.TryAdd(s, tcs);
uint bytesSent;
unsafe
{
s.ResetOverlapped();
s._overlapped->Status = 2;
if (!RioStatic.ConnectEx(s.Socket, sa, sizeof(sockaddr_in), IntPtr.Zero, 0, out bytesSent, s._overlapped))
WinSock.ThrowLastWSAError();
}
return await tcs.Task;
}
else
return await Task.FromException<RioConnectionOrientedSocket>(new ArgumentException("No sockets available in pool"));
}
}
public static async Task ClientTcp() { RioTcpClientPool l = new RioTcpClientPool(new RioFixedBufferPool(Connections, Transfer), new RioFixedBufferPool(Connections, Transfer), (uint)Connections); int totalBytesRecived = 0; int currentRecived = 0; TaskCompletionSource<object> tcs; for (int i = 0; i < Iterations; i++) { var s = await l.Connect(new Uri(Target)); if (Pattern == "PushPull") { while (totalBytesRecived < Transfer) { tcs = new TaskCompletionSource<object>(); s.WriteFixed(new byte[PushBytes]); s.OnIncommingSegment = seg => { totalBytesRecived += seg.CurrentContentLength; currentRecived += seg.CurrentContentLength; if (currentRecived >= PullBytes) tcs.SetResult(null); }; await tcs.Task; } } else if (Pattern == "Push") s.WriteFixed(new byte[Transfer]); else if (Pattern == "Pull") { tcs = new TaskCompletionSource<object>(); s.OnIncommingSegment = seg => { totalBytesRecived += seg.CurrentContentLength; if (totalBytesRecived >= Transfer) tcs.SetResult(null); }; await tcs.Task; } else if (Pattern == "Duplex") { tcs = new TaskCompletionSource<object>(); s.WriteFixed(new byte[Transfer / 2]); s.OnIncommingSegment = seg => { totalBytesRecived += seg.CurrentContentLength; if (totalBytesRecived >= Transfer / 2) tcs.SetResult(null); }; } } }