void Flush(bool disposing) { if (_remainingSpaceInOutputSegment == 0) _socket.Flush(); else if (_remainingSpaceInOutputSegment == _outputSegmentTotalLength) { if (disposing) _currentOutputSegment.Dispose(); } else { unsafe { _currentOutputSegment.SegmentPointer->Length = _outputSegmentTotalLength - _remainingSpaceInOutputSegment; } _socket.Send(_currentOutputSegment, RIO_SEND_FLAGS.NONE); _currentOutputSegment.Dispose(); if (disposing) { _remainingSpaceInOutputSegment = 0; _outputSegmentTotalLength = 0; } else { _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _outputSegmentTotalLength = _currentOutputSegment.TotalLength; _remainingSpaceInOutputSegment = _outputSegmentTotalLength; } } }
public void Dispose() { _currentValue?.Dispose(); _currentValue = null; if (_continuation != null) _continuation(); }
public void Flush(bool disposing) { if (_remainingSpaceInOutputSegment == 0) { _socket.Flush(); } else if (_remainingSpaceInOutputSegment == _outputSegmentTotalLength) { _currentOutputSegment.Dispose(); return; } else { unsafe { _currentOutputSegment.SegmentPointer->Length = _outputSegmentTotalLength - _remainingSpaceInOutputSegment; } _socket.SendAndDispose(_currentOutputSegment, RIO_SEND_FLAGS.NONE); if (!disposing) { _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _outputSegmentTotalLength = _currentOutputSegment.TotalLength; _remainingSpaceInOutputSegment = _outputSegmentTotalLength; } else { _remainingSpaceInOutputSegment = 0; _outputSegmentTotalLength = 0; } } }
void CompleteRead() { var tmp = _currentInputSegment; _currentInputSegment = _nextInputSegment; _currentInputSegment.GetResult(); _nextInputSegment = tmp; if (_currentInputSegment.CurrentContentLength != 0) { _socket.BeginReceive(_nextInputSegment); } OnIncommingSegment(_currentInputSegment); if (_currentInputSegment.CurrentContentLength != 0) { if (_nextInputSegment.IsCompleted) { CompleteRead(); } else { _nextInputSegment.OnCompleted(completeReadDelegate); } } else { Dispose(); } }
private void GetNewSegment() { _currentInputSegment = _incommingSegments.GetResult(); if (_currentInputSegment == null) { _readtcs.SetResult(0); return; } _bytesReadInCurrentSegment = 0; _currentContentLength = _currentInputSegment.CurrentContentLength; if (_currentContentLength == 0) { _currentInputSegment.Dispose(); _currentInputSegment = null; _readtcs.SetResult(0); return; } else { _socket.BeginReceive(); CompleteRead(); } }
static void Main(string[] args) { pipeLineDeph = int.Parse(args.FirstOrDefault(f => f.StartsWith("-p"))?.Substring(2) ?? "16"); int connections = int.Parse(args.FirstOrDefault(f => f.StartsWith("-c"))?.Substring(2) ?? "1024"); Console.WriteLine("RioSharp http server"); Console.WriteLine("Optimizing for " + connections + " connections"); Console.WriteLine("Optimizing for pipeline depth of: " + pipeLineDeph); sendPool = new RioFixedBufferPool(10 * connections, 256 * pipeLineDeph); recivePool = new RioFixedBufferPool(10 * connections, 256 * pipeLineDeph); listener = new RioTcpListener(sendPool, recivePool, (uint)connections); currentSegment = listener.PreAllocateWrite(GetResponse()); responseBytes = GetResponse(); //Task.Run(async () => //{ // while (true) // { // UpdateResponse(); // await Task.Delay(60000); // } //}); listener.OnAccepted = new Action<RioSocket>(s => ThreadPool.QueueUserWorkItem(o => Servebuff((RioSocket)o), s)); listener.Listen(new IPEndPoint(new IPAddress(new byte[] { 0, 0, 0, 0 }), 5000), 1024 * connections); Console.WriteLine("Listening on : http://localhost:5000"); Console.WriteLine("Press enter to exit"); Console.ReadLine(); listener.Dispose(); }
public RioSegmentReader(RioSocket socket) { _socket = socket; _nextInputSegment = _socket.ReceiveBufferPool.GetBuffer(); _currentInputSegment = _socket.ReceiveBufferPool.GetBuffer(); completeReadDelegate = CompleteRead; }
public void Flush(bool moreData) { if (_remainingSpaceInOutputSegment == 0) _socket.CommitSend(); else if (_remainingSpaceInOutputSegment == _outputSegmentTotalLength) return; else { unsafe { _currentOutputSegment.SegmentPointer->Length = _outputSegmentTotalLength - _remainingSpaceInOutputSegment; } _socket.SendInternal(_currentOutputSegment, RIO_SEND_FLAGS.NONE); _currentOutputSegment.DisposeWhenComplete(); if (moreData) { _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _outputSegmentTotalLength = _currentOutputSegment.TotalLength; _remainingSpaceInOutputSegment = _outputSegmentTotalLength; } else { _remainingSpaceInOutputSegment = 0; _outputSegmentTotalLength = 0; } } }
private void CompleteRead() { var toCopy = _currentContentLength - _bytesReadInCurrentSegment; if (toCopy > _readCount) { toCopy = _readCount; } unsafe { fixed(byte *p = &_readBuffer[_readoffset]) Buffer.MemoryCopy(_currentInputSegment.RawPointer + _bytesReadInCurrentSegment, p, _readCount, toCopy); } _bytesReadInCurrentSegment += toCopy; if (_currentContentLength == _bytesReadInCurrentSegment) { _currentInputSegment.Dispose(); _currentInputSegment = null; } _readtcs.SetResult(toCopy); }
internal unsafe void SendInternal(RioBufferSegment segment, RIO_SEND_FLAGS flags) { if (!RioStatic.Send(_requestQueue, segment.SegmentPointer, 1, flags, segment.Index)) { WinSock.ThrowLastWSAError(); } }
public RioBufferSegment GetResult() { var res = _currentValue; _currentValue = null; return(res); }
public void Flush(bool moreData) { if (_remainingSpaceInOutputSegment == 0) _socket.CommitSend(); else if (_remainingSpaceInOutputSegment == _outputSegmentTotalLength) return; else { unsafe { _currentOutputSegment.SegmentPointer->Length = _outputSegmentTotalLength - _remainingSpaceInOutputSegment; } _socket.SendInternal(_currentOutputSegment, RIO_SEND_FLAGS.NONE); if (moreData) { _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _outputSegmentTotalLength = _currentOutputSegment.TotalLength; _remainingSpaceInOutputSegment = _outputSegmentTotalLength; } else { _remainingSpaceInOutputSegment = 0; _outputSegmentTotalLength = 0; } } }
internal void Send(RioBufferSegment segment, IPEndPoint remoteAdress, RIO_SEND_FLAGS flags) { Debug.Assert(inUse); var adresssegment = AllocateAdress(remoteAdress); Send(segment, adresssegment, flags); adresssegment.Dispose(); }
internal virtual void Send(RioBufferSegment segment, RioBufferSegment remoteAdress, RIO_SEND_FLAGS flags) { Debug.Assert(inUse); if (!RioStatic.SendEx(_requestQueue, segment.SegmentPointer, 1, RIO_BUF.NullSegment, remoteAdress.SegmentPointer, RIO_BUF.NullSegment, RIO_BUF.NullSegment, flags, segment.Index)) { WinSock.ThrowLastWSAError(); } }
public void WritePreAllocated(RioBufferSegment Segment) { unsafe { if (!RioStatic.Send(_requestQueue, Segment.SegmentPointer, 1, RIO_SEND_FLAGS.DEFER, Segment.Index)) WinSock.ThrowLastWSAError(); } }
internal virtual void SendAndDispose(RioBufferSegment segment, RIO_SEND_FLAGS flags) { Debug.Assert(inUse); segment.DisposeOnComplete(); if (!RioStatic.Send(_requestQueue, segment.SegmentPointer, 1, flags, segment.Index)) { WinSock.ThrowLastWSAError(); } }
public RioStream(RioSocket socket) { _socket = socket; _currentInputSegment = null; _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _getNewSegmentDelegate = GetNewSegment; socket.OnIncommingSegmentUnsafe = (sock, s) => _incommingSegments.Set(s); socket.BeginReceive(); }
public RioStream(RioConnectionOrientedSocket socket) { _socket = socket; _currentInputSegment = null; _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _getNewSegmentDelegate = GetNewSegment; socket.OnIncommingSegment = s => _incommingSegments.Set(s); socket.ReciveInternal(); }
public void Dispose() { _currentValue?.Dispose(); _currentValue = null; if (_continuation != null) { _continuation(); } }
public unsafe RioBufferSegment BeginReceive(RioBufferSegment segment) { segment.SetNotComplete(); if (!RioStatic.Receive(_requestQueue, segment.SegmentPointer, 1, RIO_RECEIVE_FLAGS.NONE, segment.Index)) WinSock.ThrowLastWSAError(); return segment; }
public override RioBufferSegment BeginReceive(RioBufferSegment segment) { Debug.Assert(inUse); lastReceiveStart = RioSocketPool.CurrentTime; Interlocked.Increment(ref pendingRecives); segment._internalCompletionSignal = onreadCompletion; segment.socketId = currentId; return(base.BeginReceive(segment)); }
internal override void Send(RioBufferSegment segment, RIO_SEND_FLAGS flags) { Debug.Assert(inUse); lastSendStart = RioSocketPool.CurrentTime; Interlocked.Increment(ref pendingSends); segment._internalCompletionSignal = onSendCompletion; segment.socketId = currentId; base.Send(segment, flags); }
public RioBufferSegment WritePreAllocated(RioBufferSegment Segment) { unsafe { Segment.SetNotComplete(); if (!RioStatic.Send(_requestQueue, Segment.SegmentPointer, 1, RIO_SEND_FLAGS.DEFER, Segment.Index)) WinSock.ThrowLastWSAError(); } return Segment; }
public void WritePreAllocated(RioBufferSegment Segment) { unsafe { if (!RioStatic.Send(_requestQueue, Segment.SegmentPointer, 1, RIO_SEND_FLAGS.DEFER, Segment.Index)) { WinSock.ThrowLastWSAError(); } } }
public unsafe RioBufferSegment BeginReceive(RioBufferSegment segment) { segment.SetNotComplete(); if (!RioStatic.Receive(_requestQueue, segment.SegmentPointer, 1, RIO_RECEIVE_FLAGS.NONE, segment.Index)) { WinSock.ThrowLastWSAError(); } return(segment); }
public void Start() { _nextInputSegment = _nextInputSegment ?? _socket.ReceiveBufferPool.GetBuffer(); _currentInputSegment = _currentInputSegment ?? _socket.ReceiveBufferPool.GetBuffer(); _socket.BeginReceive(_nextInputSegment); if (_nextInputSegment.IsCompleted) CompleteRead(); else _nextInputSegment.OnCompleted(completeReadDelegate); }
public RioStream(RioSocket socket) { _socket = socket; _currentInputSegment = _socket.ReceiveBufferPool.GetBuffer(); _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _nextInputSegment = _socket.ReceiveBufferPool.GetBuffer(); _getNewSegmentDelegateDelegate = GetNewSegmentDelegateWrapper; _outputSegmentTotalLength = _currentOutputSegment.TotalLength; _remainingSpaceInOutputSegment = _outputSegmentTotalLength; _socket.BeginReceive(_nextInputSegment); _waitCallback = WaitCallbackcallback; }
public RioBufferSegment WritePreAllocated(RioBufferSegment Segment) { unsafe { Segment.SetNotComplete(); if (!RioStatic.Send(_requestQueue, Segment.SegmentPointer, 1, RIO_SEND_FLAGS.DEFER, Segment.Index)) { WinSock.ThrowLastWSAError(); } } return(Segment); }
public virtual RioBufferSegment BeginReceive(RioBufferSegment segment) { Debug.Assert(inUse); segment.SegmentPointer->Length = segment.TotalLength; segment.SetNotComplete(); if (!RioStatic.Receive(_requestQueue, segment.SegmentPointer, 1, RIO_RECEIVE_FLAGS.NONE, segment.Index)) { WinSock.ThrowLastWSAError(); } return(segment); }
public RioFixedBufferPool(int segmentCount, int segmentLength) { AllSegments = new RioBufferSegment[segmentCount]; TotalLength = segmentCount * segmentLength; BufferPointer = Marshal.AllocHGlobal(TotalLength); _segmentpointer = Marshal.AllocHGlobal(Marshal.SizeOf<RIO_BUFSEGMENT>() * segmentCount); for (int i = 0; i < segmentCount; i++) { var b = new RioBufferSegment(this, BufferPointer ,_segmentpointer, i, segmentLength ); AllSegments[i] = b; _availableSegments.Enqueue(b); } }
public RioFixedBufferPool(int segmentCount, int segmentLength) { AllSegments = new RioBufferSegment[segmentCount]; TotalLength = segmentCount * segmentLength; BufferPointer = Marshal.AllocHGlobal(TotalLength); _segmentpointer = Marshal.AllocHGlobal(Marshal.SizeOf <RIO_BUFSEGMENT>() * segmentCount); for (int i = 0; i < segmentCount; i++) { var b = new RioBufferSegment(this, BufferPointer, _segmentpointer, i, segmentLength); AllSegments[i] = b; _availableSegments.Enqueue(b); } }
public RioFixedBufferPool(int segmentCount, int segmentLength) { AllSegments = new RioBufferSegment[segmentCount]; TotalLength = segmentCount * segmentLength; BufferPointer = Kernel32.VirtualAlloc(IntPtr.Zero, (uint)TotalLength, 0x00001000 | 0x00002000, 0x04); _segmentpointer = Kernel32.VirtualAlloc(IntPtr.Zero, (uint)(Marshal.SizeOf <RIO_BUF>() * segmentCount), 0x00001000 | 0x00002000, 0x04); for (int i = 0; i < segmentCount; i++) { var b = new RioBufferSegment(this, BufferPointer, _segmentpointer, i, segmentLength); AllSegments[i] = b; _availableSegments.Enqueue(b); } }
public RioFixedBufferPool(int segmentCount, int segmentLength) { AllSegments = new RioBufferSegment[segmentCount]; TotalLength = segmentCount * segmentLength; BufferPointer = Kernel32.VirtualAlloc(IntPtr.Zero, (uint)TotalLength, 0x00001000 | 0x00002000, 0x04); _segmentpointer = Kernel32.VirtualAlloc(IntPtr.Zero, (uint)(Marshal.SizeOf<RIO_BUF>() * segmentCount), 0x00001000 | 0x00002000, 0x04); for (int i = 0; i < segmentCount; i++) { var b = new RioBufferSegment(this, BufferPointer, _segmentpointer, i, segmentLength); AllSegments[i] = b; _availableSegments.Enqueue(b); } }
int GetNewSegment() { if (_nextInputSegment.CurrentContentLength == 0) { _nextInputSegment.Dispose(); _currentInputSegment.Dispose(); return(0); } else { _bytesReadInCurrentSegment = 0; _nextInputSegment = _socket.BeginReceive(Interlocked.Exchange(ref _currentInputSegment, _nextInputSegment)); return(CompleteRead()); } }
public void Start() { _nextInputSegment = _nextInputSegment ?? _socket.ReceiveBufferPool.GetBuffer(); _currentInputSegment = _currentInputSegment ?? _socket.ReceiveBufferPool.GetBuffer(); _socket.BeginReceive(_nextInputSegment); if (_nextInputSegment.IsCompleted) { CompleteRead(); } else { _nextInputSegment.OnCompleted(completeReadDelegate); } }
public void CompleteRead() { var tmp = _currentInputSegment; _currentInputSegment = _nextInputSegment; _nextInputSegment = tmp; _socket.BeginReceive(_nextInputSegment); OnIncommingSegment(_currentInputSegment); if (_nextInputSegment.IsCompleted) CompleteRead(); else _nextInputSegment.OnCompleted(completeReadDelegate); }
public void Set(RioBufferSegment item) { bool taken = false; _spinLock.Enter(ref taken); //if (!taken) // throw new ArgumentException("fuu"); //if (_currentValue != null) // throw new ArgumentException("fuu"); _currentValue = item; _spinLock.Exit(); if (_continuation != null) ThreadPool.QueueUserWorkItem(_continuationWrapperDelegate, null); }
public FlippingRioStream(RioSocket socket) { _socket = socket; _currentInputSegment = _socket.ReceiveBufferPool.GetBuffer(); _currentOutputSegment = _socket.SendBufferPool.GetBuffer(); _nextInputSegment = _socket.ReceiveBufferPool.GetBuffer(); if (_nextInputSegment._awaitableState != RioBufferSegment._notStarted) { } _getNewSegmentDelegateDelegate = GetNewSegmentDelegateWrapper; _outputSegmentTotalLength = _currentOutputSegment.TotalLength; _remainingSpaceInOutputSegment = _outputSegmentTotalLength; _socket.BeginReceive(_nextInputSegment); _waitCallback = WaitCallbackcallback; }
public void CompleteRead() { var tmp = _currentInputSegment; _currentInputSegment = _nextInputSegment; _nextInputSegment = tmp; _socket.BeginReceive(_nextInputSegment); OnIncommingSegment(_currentInputSegment); if (_nextInputSegment.IsCompleted) { CompleteRead(); } else { _nextInputSegment.OnCompleted(completeReadDelegate); } }
public void Set(RioBufferSegment item) { bool taken = false; _spinLock.Enter(ref taken); //if (!taken) // throw new ArgumentException("fuu"); //if (_currentValue != null) // throw new ArgumentException("fuu"); _currentValue = item; _spinLock.Exit(); if (_continuation != null) { var res = _continuation; _continuation = null; ThreadPool.QueueUserWorkItem(_continuationWrapperDelegate, res); } }
private int GetNewSegment() { var tmp = _currentInputSegment; _nextInputSegment.GetResult(); _currentInputSegment = _nextInputSegment; _bytesReadInCurrentSegment = 0; _currentContentLength = _currentInputSegment.CurrentContentLength; if (_currentContentLength == 0) { _currentInputSegment.Dispose(); tmp.Dispose(); return 0; } else { _nextInputSegment = tmp; _socket.BeginReceive(_nextInputSegment); return CompleteRead(); } }
void CompleteRead() { var tmp = _currentInputSegment; _currentInputSegment = _nextInputSegment; _currentInputSegment.GetResult(); _nextInputSegment = tmp; if (_currentInputSegment.CurrentContentLength != 0) _socket.BeginReceive(_nextInputSegment); OnIncommingSegment(_currentInputSegment); if (_currentInputSegment.CurrentContentLength != 0) { if (_nextInputSegment.IsCompleted) CompleteRead(); else _nextInputSegment.OnCompleted(completeReadDelegate); } else { Dispose(); } }
static void Main(string[] args) { pipeLineDeph = int.Parse(args.FirstOrDefault(f => f.StartsWith("-p"))?.Substring(2) ?? "16"); int connections = int.Parse(args.FirstOrDefault(f => f.StartsWith("-c"))?.Substring(2) ?? "1024"); sendPool = new RioFixedBufferPool(10 * connections, 140 * pipeLineDeph); recivePool = new RioFixedBufferPool(10 * connections, 128 * pipeLineDeph); listener = new RioTcpListener(sendPool, recivePool, 1024); currentSegment = listener.PreAllocateWrite(GetResponse()); responseBytes = GetResponse(); //Task.Run(async () => //{ // while (true) // { // UpdateResponse(); // await Task.Delay(60000); // } //}); listener.OnAccepted = new Action<RioConnectionOrientedSocket>(s => ThreadPool.QueueUserWorkItem(o => Servebuff((RioConnectionOrientedSocket)o), s)); listener.Listen(new IPEndPoint(new IPAddress(new byte[] { 0, 0, 0, 0 }), 5000), 1024 * connections); Console.ReadLine(); }
static unsafe void ProcessSocket(RioBufferSegment s, ServeState state) { uint current = 0; var r = s.CurrentContentLength; var reqs = 0; if (r == 0) { state.reader.Dispose(); state.socket.Dispose(); return; } for (int i = 0; state.leftoverLength != 0 && i < 4 - state.leftoverLength; i++) { current += s.DataPointer[i]; current = current << 8; if (current == endOfRequest) reqs++; } state.leftoverLength = r % 4; var length = r - state.leftoverLength; byte* currentPtr = s.DataPointer + state.oldleftoverLength; var start = currentPtr; var end = currentPtr + length; for (; start <= end; start++) { if (*(uint*)start == endOfRequest) reqs++; } state.oldleftoverLength = state.leftoverLength; for (int i = r - state.leftoverLength; i < r; i++) { current += s.DataPointer[i]; current = current << 4; } state.socket.Send(reqz[reqs]); }
void SetInUse(RioBufferSegment buf, bool value) { buf.InUse = value; }
public bool TryGetBuffer(out RioBufferSegment buf) => _availableSegments.TryDequeue(out buf);
public void ReleaseBuffer(RioBufferSegment buffer) { SetInUse(buffer, false); _availableSegments.Enqueue(buffer); }
int GetNewSegment() { _nextInputSegment.GetResult(); _currentContentLength = _nextInputSegment.CurrentContentLength; if (disposing || _currentContentLength == 0) { _nextInputSegment.Dispose(); _currentInputSegment.Dispose(); toCopy = 0; return 0; } else { _bytesReadInCurrentSegment = 0; _nextInputSegment = _socket.BeginReceive(Interlocked.Exchange(ref _currentInputSegment, _nextInputSegment)); return CompleteRead(); } }
public RioBufferSegment Send(RioBufferSegment Segment) { Debug.Assert(inUse); Send(Segment, RIO_SEND_FLAGS.NONE); return Segment; }
public RioBufferSegment GetResult() { var res = _currentValue; _currentValue = null; return res; }
private void CompleteRead() { var toCopy = _currentContentLength - _bytesReadInCurrentSegment; if (toCopy > _readCount) toCopy = _readCount; unsafe { fixed (byte* p = &_readBuffer[_readoffset]) Buffer.MemoryCopy(_currentInputSegment.RawPointer + _bytesReadInCurrentSegment, p, _readCount, toCopy); } _bytesReadInCurrentSegment += toCopy; if (_currentContentLength == _bytesReadInCurrentSegment) { _currentInputSegment.Dispose(); _currentInputSegment = null; } _readtcs.SetResult(toCopy); }
public void ReleaseBuffer(RioBufferSegment buffer) { _availableSegments.Enqueue(buffer); }
internal unsafe void SendInternal(RioBufferSegment segment, RIO_SEND_FLAGS flags) { if (!RioStatic.Send(_requestQueue, segment.SegmentPointer, 1, flags, segment.Index)) WinSock.ThrowLastWSAError(); }
public virtual RioBufferSegment BeginReceive(RioBufferSegment segment) { Debug.Assert(inUse); segment.SegmentPointer->Length = segment.TotalLength; segment.SetNotComplete(); if (!RioStatic.Receive(_requestQueue, segment.SegmentPointer, 1, RIO_RECEIVE_FLAGS.NONE, segment.Index)) WinSock.ThrowLastWSAError(); return segment; }