public TcpSocketRioSession( TcpSocketRioServerConfiguration configuration, IBufferManager bufferManager, RioConnectionOrientedSocket 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(); _sessionBuffer = _bufferManager.BorrowBuffer(); _sessionBufferCount = 0; _stream = new RioStream(_socket); }
public TcpSocketSaeaSession( TcpSocketSaeaServerConfiguration configuration, IBufferManager bufferManager, SaeaPool saeaPool, ITcpSocketSaeaServerMessageDispatcher dispatcher, TcpSocketSaeaServer server) { if (configuration == null) throw new ArgumentNullException("configuration"); if (bufferManager == null) throw new ArgumentNullException("bufferManager"); if (saeaPool == null) throw new ArgumentNullException("saeaPool"); if (dispatcher == null) throw new ArgumentNullException("dispatcher"); if (server == null) throw new ArgumentNullException("server"); _configuration = configuration; _bufferManager = bufferManager; _saeaPool = saeaPool; _dispatcher = dispatcher; _server = server; _receiveBuffer = _bufferManager.BorrowBuffer(); _sessionBuffer = _bufferManager.BorrowBuffer(); _sessionBufferCount = 0; }
internal void Start() { lock (_opsLock) { try { if (Active) { _closed = false; _stream = NegotiateStream(_tcpClient.GetStream()); _receiveBuffer = _bufferManager.BorrowBuffer(); _sessionBuffer = _bufferManager.BorrowBuffer(); _sessionBufferCount = 0; bool isErrorOccurredInUserSide = false; try { _server.RaiseClientConnected(this); } catch (Exception ex) { isErrorOccurredInUserSide = true; HandleUserSideError(ex); } if (!isErrorOccurredInUserSide) { ContinueReadBuffer(); } else { Close(); } } } catch (Exception ex) { _log.Error(ex.Message, ex); Close(); } } }
public TcpSocketRioSession( TcpSocketRioServerConfiguration configuration, IBufferManager bufferManager, RioConnectionOrientedSocket 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(); _sessionBuffer = _bufferManager.BorrowBuffer(); _sessionBufferCount = 0; _stream = new RioStream(_socket); }
internal async Task Start() { int origin = Interlocked.CompareExchange(ref _state, _connecting, _none); if (origin == _disposed) { throw new ObjectDisposedException(GetType().FullName); } else if (origin != _none) { throw new InvalidOperationException("This tcp socket session has already started."); } try { _receiveBuffer = _bufferManager.BorrowBuffer(); _receiveBufferOffset = 0; if (Interlocked.CompareExchange(ref _state, _connected, _connecting) != _connecting) { throw new ObjectDisposedException(GetType().FullName); } _log.DebugFormat("Session started on [{0}] in dispatcher [{1}] with session count [{2}].", this.StartTime.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"), _dispatcher.GetType().Name, this.Server.SessionCount); bool isErrorOccurredInUserSide = false; try { await _dispatcher.OnSessionStarted(this); } catch (Exception ex) { isErrorOccurredInUserSide = true; HandleUserSideError(ex); } if (!isErrorOccurredInUserSide) { await Process(); } else { await Close(); } } catch (Exception ex) when(ex is TimeoutException) { _log.Error(string.Format("Session [{0}] exception occurred, [{1}].", this, ex.Message), ex); await Close(); } }
public TcpSocketSaeaSession( TcpSocketSaeaServerConfiguration configuration, IBufferManager bufferManager, SaeaPool saeaPool, ITcpSocketSaeaServerMessageDispatcher dispatcher, TcpSocketSaeaServer server) { if (configuration == null) { throw new ArgumentNullException("configuration"); } if (bufferManager == null) { throw new ArgumentNullException("bufferManager"); } if (saeaPool == null) { throw new ArgumentNullException("saeaPool"); } if (dispatcher == null) { throw new ArgumentNullException("dispatcher"); } if (server == null) { throw new ArgumentNullException("server"); } _configuration = configuration; _bufferManager = bufferManager; _saeaPool = saeaPool; _dispatcher = dispatcher; _server = server; _receiveBuffer = _bufferManager.BorrowBuffer(); _sessionBuffer = _bufferManager.BorrowBuffer(); _sessionBufferCount = 0; }
public static void AppendBuffer(IBufferManager bufferManager, ref byte[] receiveBuffer, int receiveCount, ref byte[] sessionBuffer, ref int sessionBufferCount) { if (sessionBuffer.Length < (sessionBufferCount + receiveCount)) { byte[] autoExpandedBuffer = bufferManager.BorrowBuffer(); if (autoExpandedBuffer.Length < (sessionBufferCount + receiveCount) * 2) { bufferManager.ReturnBuffer(autoExpandedBuffer); autoExpandedBuffer = new byte[(sessionBufferCount + receiveCount) * 2]; } Array.Copy(sessionBuffer, 0, autoExpandedBuffer, 0, sessionBufferCount); var discardBuffer = sessionBuffer; sessionBuffer = autoExpandedBuffer; bufferManager.ReturnBuffer(discardBuffer); } Array.Copy(receiveBuffer, 0, sessionBuffer, sessionBufferCount, receiveCount); sessionBufferCount = sessionBufferCount + receiveCount; }
public static HttpStatusCode Decode <T>(Request request, IBufferManager bufferManager, IMessageDecoder decoder, out T result) { result = default(T); var buffer = bufferManager.BorrowBuffer(); int count = request.Body.Read(buffer, 0, buffer.Length); if (count == 0 || count == buffer.Length) { bufferManager.ReturnBuffer(buffer); return(HttpStatusCode.RequestEntityTooLarge); } result = decoder.DecodeMessage <T>(buffer, 0, count); bufferManager.ReturnBuffer(buffer); if (result == null) { return(HttpStatusCode.InternalServerError); } return(HttpStatusCode.OK); }
public static void ReplaceBuffer(IBufferManager bufferManager, ref byte[] receiveBuffer, ref int receiveBufferOffset, int receiveCount) { if ((receiveBufferOffset + receiveCount) < receiveBuffer.Length) { receiveBufferOffset = receiveBufferOffset + receiveCount; } else { byte[] autoExpandedBuffer = bufferManager.BorrowBuffer(); if (autoExpandedBuffer.Length < (receiveBufferOffset + receiveCount) * 2) { bufferManager.ReturnBuffer(autoExpandedBuffer); autoExpandedBuffer = new byte[(receiveBufferOffset + receiveCount) * 2]; } Array.Copy(receiveBuffer, 0, autoExpandedBuffer, 0, receiveBufferOffset + receiveCount); receiveBufferOffset = receiveBufferOffset + receiveCount; var discardBuffer = receiveBuffer; receiveBuffer = autoExpandedBuffer; bufferManager.ReturnBuffer(discardBuffer); } }
public static void ShiftBuffer(IBufferManager bufferManager, int shiftStart, ref byte[] sessionBuffer, ref int sessionBufferCount) { if ((sessionBufferCount - shiftStart) < shiftStart) { Array.Copy(sessionBuffer, shiftStart, sessionBuffer, 0, sessionBufferCount - shiftStart); sessionBufferCount = sessionBufferCount - shiftStart; } else { byte[] copyBuffer = bufferManager.BorrowBuffer(); if (copyBuffer.Length < (sessionBufferCount - shiftStart)) { bufferManager.ReturnBuffer(copyBuffer); copyBuffer = new byte[sessionBufferCount - shiftStart]; } Array.Copy(sessionBuffer, shiftStart, copyBuffer, 0, sessionBufferCount - shiftStart); Array.Copy(copyBuffer, 0, sessionBuffer, 0, sessionBufferCount - shiftStart); sessionBufferCount = sessionBufferCount - shiftStart; bufferManager.ReturnBuffer(copyBuffer); } }
public async Task Connect() { int origin = Interlocked.CompareExchange(ref _state, _connecting, _none); if (origin == _disposed) { throw new ObjectDisposedException(GetType().FullName); } else if (origin != _none) { throw new InvalidOperationException("This tcp socket client has already connected to server."); } try { _tcpClient = _localEndPoint != null ? new TcpClient(_localEndPoint) : new TcpClient(_remoteEndPoint.Address.AddressFamily); var awaiter = _tcpClient.ConnectAsync(_remoteEndPoint.Address, _remoteEndPoint.Port); if (!awaiter.Wait(ConnectTimeout)) { await Close(false); throw new TimeoutException(string.Format( "Connect to [{0}] timeout [{1}].", _remoteEndPoint, ConnectTimeout)); } ConfigureClient(); var negotiator = NegotiateStream(_tcpClient.GetStream()); if (!negotiator.Wait(ConnectTimeout)) { await Close(false); throw new TimeoutException(string.Format( "Negotiate SSL/TSL with remote [{0}] timeout [{1}].", _remoteEndPoint, ConnectTimeout)); } _stream = negotiator.Result; _receiveBuffer = _bufferManager.BorrowBuffer(); _receiveBufferOffset = 0; if (Interlocked.CompareExchange(ref _state, _connected, _connecting) != _connecting) { throw new ObjectDisposedException(GetType().FullName); } _log.DebugFormat("Connected to server [{0}] with dispatcher [{1}] on [{2}].", this.RemoteEndPoint, _dispatcher.GetType().Name, DateTime.UtcNow.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff")); bool isErrorOccurredInUserSide = false; try { await _dispatcher.OnServerConnected(this); } catch (Exception ex) { isErrorOccurredInUserSide = true; HandleUserSideError(ex); } if (!isErrorOccurredInUserSide) { Task.Run(async() => { await Process(); }) .Forget(); } else { await Close(); } } catch (ObjectDisposedException) { } catch (Exception ex) { _log.Error(ex.Message, ex); throw; } }
public async Task Connect() { int origin = Interlocked.CompareExchange(ref _state, _connecting, _none); if (origin == _disposed) { throw new ObjectDisposedException(GetType().FullName); } else if (origin != _none) { throw new InvalidOperationException("This tcp socket client has already connected to server."); } try { _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); if (_localEndPoint != null) { _socket.Bind(_localEndPoint); } var saea = _saeaPool.Take(); saea.Saea.RemoteEndPoint = _remoteEndPoint; var socketError = await _socket.ConnectAsync(saea); if (socketError != SocketError.Success) { throw new SocketException((int)socketError); } ConfigureSocket(); _receiveBuffer = _bufferManager.BorrowBuffer(); _receiveBufferOffset = 0; if (Interlocked.CompareExchange(ref _state, _connected, _connecting) != _connecting) { throw new ObjectDisposedException(GetType().FullName); } _log.DebugFormat("Connected to server [{0}] with dispatcher [{1}] on [{2}].", this.RemoteEndPoint, _dispatcher.GetType().Name, DateTime.UtcNow.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff")); bool isErrorOccurredInUserSide = false; try { await _dispatcher.OnServerConnected(this); } catch (Exception ex) { isErrorOccurredInUserSide = true; HandleUserSideError(ex); } if (!isErrorOccurredInUserSide) { Task.Run(async() => { await Process(); }) .Forget(); } else { await Close(); } } catch (ObjectDisposedException) { } catch (Exception ex) when(ex is TimeoutException) { _log.Error(ex.Message, ex); await Close(); } }
internal async Task Start() { int origin = Interlocked.CompareExchange(ref _state, _connecting, _none); if (origin == _disposed) { throw new ObjectDisposedException(GetType().FullName); } else if (origin != _none) { throw new InvalidOperationException("This tcp socket session has already started."); } try { ConfigureClient(); var negotiator = NegotiateStream(_tcpClient.GetStream()); if (!negotiator.Wait(ConnectTimeout)) { await Close(); throw new TimeoutException(string.Format( "Negotiate SSL/TSL with remote [{0}] timeout [{1}].", this.RemoteEndPoint, ConnectTimeout)); } _stream = negotiator.Result; _receiveBuffer = _bufferManager.BorrowBuffer(); _sessionBuffer = _bufferManager.BorrowBuffer(); _sessionBufferCount = 0; if (Interlocked.CompareExchange(ref _state, _connected, _connecting) != _connecting) { throw new ObjectDisposedException(GetType().FullName); } _log.DebugFormat("Session started for [{0}] on [{1}] in dispatcher [{2}] with session count [{3}].", this.RemoteEndPoint, this.StartTime.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"), _dispatcher.GetType().Name, this.Server.SessionCount); bool isErrorOccurredInUserSide = false; try { await _dispatcher.OnSessionStarted(this); } catch (Exception ex) { isErrorOccurredInUserSide = true; HandleUserSideError(ex); } if (!isErrorOccurredInUserSide) { await Process(); } else { await Close(); } } catch (Exception ex) when(ex is TimeoutException) { _log.Error(string.Format("Session [{0}] exception occurred, [{1}].", this, ex.Message), ex); await Close(); } }
internal async Task Start() { int origin = Interlocked.CompareExchange(ref _state, _connecting, _none); if (origin == _disposed) { throw new ObjectDisposedException(GetType().FullName); } else if (origin != _none) { throw new InvalidOperationException("This websocket socket session has already started."); } try { ConfigureClient(); var negotiator = NegotiateStream(_tcpClient.GetStream()); if (!negotiator.Wait(ConnectTimeout)) { await Close(WebSocketCloseCode.TlsHandshakeFailed, "SSL/TLS handshake timeout."); throw new TimeoutException(string.Format( "Negotiate SSL/TSL with remote [{0}] timeout [{1}].", this.RemoteEndPoint, ConnectTimeout)); } _stream = negotiator.Result; _receiveBuffer = _bufferManager.BorrowBuffer(); _receiveBufferOffset = 0; var handshaker = OpenHandshake(); if (!handshaker.Wait(ConnectTimeout)) { throw new TimeoutException(string.Format( "Handshake with remote [{0}] timeout [{1}].", this.RemoteEndPoint, ConnectTimeout)); } if (!handshaker.Result) { var responseBuffer = WebSocketServerHandshaker.CreateOpenningHandshakeBadRequestResponse(this); await _stream.WriteAsync(responseBuffer, 0, responseBuffer.Length); throw new WebSocketException(string.Format( "Handshake with remote [{0}] failed.", this.RemoteEndPoint)); } if (Interlocked.CompareExchange(ref _state, _connected, _connecting) != _connecting) { throw new ObjectDisposedException(GetType().FullName); } _log.DebugFormat("Session started for [{0}] on [{1}] in module [{2}] with session count [{3}].", this.RemoteEndPoint, this.StartTime.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"), _module.GetType().Name, this.Server.SessionCount); bool isErrorOccurredInUserSide = false; try { await _module.OnSessionStarted(this); } catch (Exception ex) { isErrorOccurredInUserSide = true; HandleUserSideError(ex); } if (!isErrorOccurredInUserSide) { _keepAliveTracker.StartTimer(); await Process(); } else { await Abort(); } } catch (ObjectDisposedException) { } catch (Exception ex) when(ex is TimeoutException || ex is WebSocketException) { _log.Error(string.Format("Session [{0}] exception occurred, [{1}].", this, ex.Message), ex); await Abort(); throw; } }