public RioSegmentReader(RioSocket socket) { _socket = socket; completeReadDelegate = CompleteReadOnThreadPool; completeReadWrapper = (o) => CompleteRead(); }
public RioSegmentReader(RioSocket socket) { _socket = socket; _nextInputSegment = _socket.ReceiveBufferPool.GetBuffer(); _currentInputSegment = _socket.ReceiveBufferPool.GetBuffer(); completeReadDelegate = CompleteRead; }
public TcpSocketRioSession( TcpSocketRioServerConfiguration configuration, IBufferManager bufferManager, RioSocket socket, ITcpSocketRioServerMessageDispatcher dispatcher, TcpSocketRioServer server) { if (configuration == null) throw new ArgumentNullException("configuration"); if (bufferManager == null) throw new ArgumentNullException("bufferManager"); if (socket == null) throw new ArgumentNullException("socket"); if (dispatcher == null) throw new ArgumentNullException("dispatcher"); if (server == null) throw new ArgumentNullException("server"); _configuration = configuration; _bufferManager = bufferManager; _socket = socket; _dispatcher = dispatcher; _server = server; _sessionKey = Guid.NewGuid().ToString(); this.StartTime = DateTime.UtcNow; _receiveBuffer = _bufferManager.BorrowBuffer(); _receiveBufferOffset = 0; _stream = new RioStream(_socket); }
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(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 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 async Task Close() { if (Interlocked.Exchange(ref _state, _disposed) == _disposed) { return; } try { if (_stream != null) { _stream.Dispose(); _stream = null; } if (_socket != null) { _socket.Dispose(); _socket = null; } } catch (Exception) { } if (_receiveBuffer != null) _bufferManager.ReturnBuffer(_receiveBuffer); _receiveBufferOffset = 0; _log.DebugFormat("Session closed on [{0}] in dispatcher [{1}] with session count [{2}].", DateTime.UtcNow.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"), _dispatcher.GetType().Name, this.Server.SessionCount - 1); try { await _dispatcher.OnSessionClosed(this); } catch (Exception ex) { HandleUserSideError(ex); } }
static async Task Servebuff(RioSocket 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(); } }
static void ServeSegment(RioSocket socket) { socket.SetLinger(0); var reader = new RioSegmentReader<ServeState>(socket); reader.State = new ServeState(); reader.State.leftoverLength = 0; reader.State.oldleftoverLength = 0; reader.State.reader = reader; reader.State.socket = socket; reader.OnIncommingSegment = ProcessSocket; reader.Start(); }
private async Task Process(RioSocket acceptedSocket) { var session = new TcpSocketRioSession(_configuration, _bufferManager, acceptedSocket, _dispatcher, this); if (_sessions.TryAdd(session.SessionKey, session)) { _log.DebugFormat("New session [{0}].", session); try { await session.Start(); } finally { TcpSocketRioSession recycle; if (_sessions.TryRemove(session.SessionKey, out recycle)) { _log.DebugFormat("Close session [{0}].", recycle); } } } }