public static void ListenTcp() { RioTcpListener l = new RioTcpListener(new RioFixedBufferPool(16000, 65536), new RioFixedBufferPool(16000, 65536), (uint)Connections * 2, 16000, 16000); l.OnAccepted = s => { RioStream r = new RioStream(s); int totalRecived = 0; int currentRecived = 0; var reader = new RioSegmentReader(s); var pb = new byte[PullBytes]; if (Pattern == "PushPull") { reader.OnIncommingSegment = seg => { totalRecived += seg.CurrentContentLength; currentRecived += seg.CurrentContentLength; if (currentRecived >= PushBytes) { r.Write(pb, 0, pb.Length); //s.WriteFixed(pb); currentRecived = 0; } }; reader.Start(); } else if (Pattern == "Pull") r.Write(new byte[Transfer], 0, Transfer); //s.WriteFixed(new byte[Transfer]); else if (Pattern == "Push") { reader.OnIncommingSegment = seg => { totalRecived += seg.CurrentContentLength; }; reader.Start(); } else if (Pattern == "Duplex") { s.Send(new byte[Transfer / 2]); reader.OnIncommingSegment = seg => { totalRecived += seg.CurrentContentLength; //if (apa >= Transfer / 2) // tcs.SetResult(null); }; } }; l.Listen(new IPEndPoint(new IPAddress(new byte[] { 0, 0, 0, 0 }), Port), 1024); }
static async Task ServeFixed(RioConnectionOrientedSocket socket) { try { var buffer = new byte[128 * pipeLineDeph]; var leftoverLength = 0; var oldleftoverLength = 0; uint endOfRequest = 0x0a0d0a0d; uint current = 0; var stream = new RioStream(socket); while (true) { int r = await stream.ReadAsync(buffer, 0, buffer.Length); if (r == 0) break; for (int i = 0; leftoverLength != 0 && i < 4 - leftoverLength; i++) { current += buffer[i]; current = current << 8; if (current == endOfRequest) socket.WritePreAllocated(currentSegment); } leftoverLength = r % 4; var length = r - leftoverLength; unsafe { fixed (byte* currentPtr = &buffer[oldleftoverLength]) { var start = currentPtr; var end = currentPtr + length; for (; start <= end; start++) { if (*(uint*)start == endOfRequest) socket.WritePreAllocated(currentSegment); } } } oldleftoverLength = leftoverLength; for (int i = r - leftoverLength; i < r; i++) { current += buffer[i]; current = current << 4; } stream.Flush(); } } catch (Exception ex) { Console.WriteLine(ex); } finally { socket.Dispose(); } }
public async static Task<int> Execute() { { var buffer = new byte[256 * pipeLineDeph]; var leftoverLength = 0; var oldleftoverLength = 0; uint endOfRequest = 0x0a0d0a0d; uint current = 0; int responses = 0; int total = 0; RioSocket connection = null; RioStream stream = null; while (timer.Elapsed < span) { if (connection == null) { try { connection = await clientPool.Connect(uri); stream = new RioStream(connection); } catch (Exception) { continue; } } //check if connection is valid? connection.WriteFixed(requestBytes); while (responses < pipeLineDeph) { int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); if (bytesRead == 0) break; for (int i = 0; leftoverLength != 0 && i < 4 - leftoverLength; i++) { current += buffer[i]; current = current << 8; if (current == endOfRequest) responses++; } leftoverLength = bytesRead % 4; var length = bytesRead - leftoverLength; unsafe { fixed (byte* data = &buffer[oldleftoverLength]) { var start = data; var end = data + length; for (; start <= end; start++) { if (*(uint*)start == endOfRequest) responses++; } } } oldleftoverLength = leftoverLength; for (int i = bytesRead - leftoverLength; i < bytesRead; i++) { current += buffer[i]; current = current << 4; } } total += responses; responses = 0; if (!keepAlive) { stream.Dispose(); connection.Dispose(); connection = null; } } connection?.Dispose(); return total; } }
public async static Task<int> doit() { { var buffer = new byte[140 * pipeLineDeph]; var leftoverLength = 0; var oldleftoverLength = 0; uint endOfRequest = 0x0a0d0a0d; uint current = 0; int responses = 0; int total = 0; RioConnectionOrientedSocket connection; connection = await clientPool.Connect(uri); var stream = new RioStream(connection); while (timer.Elapsed < span) { //check if connection is valid? connection.WriteFixed(rb); while (responses < pipeLineDeph) { int r = await stream.ReadAsync(buffer, 0, buffer.Length); if (r == 0) break; for (int i = 0; leftoverLength != 0 && i < 4 - leftoverLength; i++) { current += buffer[i]; current = current << 8; if (current == endOfRequest) responses++; } leftoverLength = r % 4; var length = r - leftoverLength; unsafe { fixed (byte* apa = &buffer[oldleftoverLength]) { var start = apa; var end = apa + length; for (; start <= end; start++) { if (*(uint*)start == endOfRequest) responses++; } } } oldleftoverLength = leftoverLength; for (int i = r - leftoverLength; i < r; i++) { current += buffer[i]; current = current << 4; } } total += responses; responses = 0; if (!keepAlive) connection.Dispose(); } connection.Dispose(); return total; } }