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 Servebuff(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) { stream.Write(responseBytes, 0, responseBytes.Length); } } 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) { stream.Write(responseBytes, 0, responseBytes.Length); } } } } stream.Flush(); oldleftoverLength = leftoverLength; for (int i = r - leftoverLength; i < r; i++) { current += buffer[i]; current = current << 4; } } } catch (Exception ex) { Console.WriteLine(ex); } finally { socket.Dispose(); } }