public static void AppendBuffer(
            ISegmentBufferManager bufferManager,
            ref ArraySegment <byte> receiveBuffer,
            int receiveCount,
            ref ArraySegment <byte> sessionBuffer,
            ref int sessionBufferCount)
        {
            if (sessionBuffer.Count < (sessionBufferCount + receiveCount))
            {
                ArraySegment <byte> autoExpandedBuffer = bufferManager.BorrowBuffer();
                if (autoExpandedBuffer.Count < (sessionBufferCount + receiveCount) * 2)
                {
                    bufferManager.ReturnBuffer(autoExpandedBuffer);
                    autoExpandedBuffer = new ArraySegment <byte>(new byte[(sessionBufferCount + receiveCount) * 2]);
                }

                Array.Copy(sessionBuffer.Array, sessionBuffer.Offset, autoExpandedBuffer.Array, autoExpandedBuffer.Offset, sessionBufferCount);

                var discardBuffer = sessionBuffer;
                sessionBuffer = autoExpandedBuffer;
                bufferManager.ReturnBuffer(discardBuffer);
            }

            Array.Copy(receiveBuffer.Array, receiveBuffer.Offset, sessionBuffer.Array, sessionBuffer.Offset + sessionBufferCount, receiveCount);
            sessionBufferCount = sessionBufferCount + receiveCount;
        }
Exemple #2
0
        public TcpSocketSession(TcpClient tcpClient, TcpSocketServerConfiguration configuration
                                , ISegmentBufferManager bufferManager, TcpSocketServer server)
        {
            if (tcpClient == null)
            {
                throw new ArgumentNullException("tcpClient");
            }
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }
            if (bufferManager == null)
            {
                throw new ArgumentNullException("bufferManager");
            }
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            _tcpClient     = tcpClient;
            _configuration = configuration;
            _bufferManager = bufferManager;
            _server        = server;

            _sessionKey    = Guid.NewGuid().ToString();
            this.StartTime = DateTime.UtcNow;

            SetSocketOptions();

            _remoteEndPoint = this.RemoteEndPoint;
            _localEndPoint  = this.LocalEndPoint;
        }
        public TcpSocketClientConfiguration(ISegmentBufferManager bufferManager)
        {
            BufferManager = bufferManager;

            ReceiveBufferSize = 8192;                       // Specifies the total per-socket buffer space reserved for receives. This is unrelated to the maximum message size or the size of a TCP window.
            SendBufferSize    = 8192;                       // Specifies the total per-socket buffer space reserved for sends. This is unrelated to the maximum message size or the size of a TCP window.
            ReceiveTimeout    = TimeSpan.Zero;              // Receive a time-out. This option applies only to synchronous methods; it has no effect on asynchronous methods such as the BeginSend method.
            SendTimeout       = TimeSpan.Zero;              // Send a time-out. This option applies only to synchronous methods; it has no effect on asynchronous methods such as the BeginSend method.
            NoDelay           = true;                       // Disables the Nagle algorithm for send coalescing.
            LingerState       = new LingerOption(false, 0); // The socket will linger for x seconds after Socket.Close is called.
            KeepAlive         = false;                      // Use keep-alives.
            KeepAliveInterval = TimeSpan.FromSeconds(5);    // https://msdn.microsoft.com/en-us/library/system.net.sockets.socketoptionname(v=vs.110).aspx
            ReuseAddress      = false;                      // Allows the socket to be bound to an address that is already in use.

            SslEnabled                    = false;
            SslTargetHost                 = null;
            SslClientCertificates         = new X509CertificateCollection();
            SslEncryptionPolicy           = EncryptionPolicy.RequireEncryption;
            SslEnabledProtocols           = SslProtocols.Ssl3 | SslProtocols.Tls;
            SslCheckCertificateRevocation = false;
            SslPolicyErrorsBypassed       = false;

            ConnectTimeout = TimeSpan.FromSeconds(15);
            FrameBuilder   = new LengthPrefixedFrameBuilder();
        }
        public WebSocketSession(
            WebSocketModule module, HttpListenerContext httpContext, WebSocketContext webSocketContext,
            CancellationToken cancellationToken, ISegmentBufferManager bufferManager, Encoding encoding)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            if (webSocketContext == null)
            {
                throw new ArgumentNullException("webSocketContext");
            }
            if (bufferManager == null)
            {
                throw new ArgumentNullException("bufferManager");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            _httpContext           = httpContext;
            this.Module            = module;
            this.Context           = webSocketContext;
            this.CancellationToken = cancellationToken;
            _bufferManager         = bufferManager;
            this.Encoding          = encoding;

            _sessionKey    = Guid.NewGuid().ToString();
            this.StartTime = DateTime.UtcNow;
        }
        public TcpSocketServerConfiguration(ISegmentBufferManager bufferManager)
        {
            BufferManager = bufferManager;

            ReceiveBufferSize = 8192;
            SendBufferSize    = 8192;
            ReceiveTimeout    = TimeSpan.Zero;
            SendTimeout       = TimeSpan.Zero;
            NoDelay           = true;
            LingerState       = new LingerOption(false, 0);
            KeepAlive         = false;
            KeepAliveInterval = TimeSpan.FromSeconds(5);
            ReuseAddress      = false;

            PendingConnectionBacklog = 200;
            AllowNatTraversal        = true;

            SslEnabled                    = false;
            SslServerCertificate          = null;
            SslEncryptionPolicy           = EncryptionPolicy.RequireEncryption;
            SslEnabledProtocols           = SslProtocols.Ssl3 | SslProtocols.Tls;
            SslClientCertificateRequired  = true;
            SslCheckCertificateRevocation = false;
            SslPolicyErrorsBypassed       = false;

            ConnectTimeout    = TimeSpan.FromSeconds(15);
            FrameBuilder      = new LengthPrefixedFrameBuilder();
            SessionKeyBuilder = new GuidSessionKeyBuilder();
        }
        /// <summary>
        /// Construct a new <see cref="AsyncTcpServerConfiguration"></see> class's instance.
        /// </summary>
        /// <param name="buffer_manager">Segment buffer manager.</param>
        public AsyncTcpServerConfiguration(ISegmentBufferManager buffer_manager)
        {
            this.BufferManager = buffer_manager;

            this.ReceiveBufferSize = 8192;
            this.SendBufferSize    = 8192;
            this.ReceiveTimeout    = TimeSpan.Zero;
            this.SendTimeout       = TimeSpan.Zero;
            this.NoDelay           = true;
            this.LingerState       = new LingerOption(false, 0);
            this.KeepAlive         = false;
            this.KeepAliveInterval = TimeSpan.FromSeconds(5);
            this.ReuseAddress      = false;

            this.PendingConnectionBacklog = 200;
            this.AllowNatTraversal        = true;

            this.SslEnabled                    = false;
            this.SslServerCertificate          = null;
            this.SslEncryptionPolicy           = EncryptionPolicy.RequireEncryption;
            this.SslEnabledProtocols           = SslProtocols.None;
            this.SslClientCertificateRequired  = true;
            this.SslCheckCertificateRevocation = false;
            this.SslPolicyErrorsBypassed       = false;

            this.ConnectTimeout = TimeSpan.FromSeconds(15);
            this.FrameBuilder   = new LengthPrefixedFrameBuilder();
        }
Exemple #7
0
        /// <summary>
        /// Replace buffer from <see cref="ISegmentBufferManager" />.
        /// </summary>
        /// <param name="buffer_manager">Segment buffer manager.</param>
        /// <param name="receive_buffer"></param>
        /// <param name="receive_buffer_offset"></param>
        /// <param name="receive_count"></param>
        public static void ReplaceBuffer(this ISegmentBufferManager buffer_manager,
                                         ref ArraySegment <byte> receive_buffer,
                                         ref int receive_buffer_offset,
                                         int receive_count)
        {
            if ((receive_buffer_offset + receive_count) < receive_buffer.Count)
            {
                receive_buffer_offset += receive_count;
            }
            else
            {
                ArraySegment <byte> autoExpandedBuffer = buffer_manager.BorrowBuffer();
                if (autoExpandedBuffer.Count < (receive_buffer_offset + receive_count) * 2)
                {
                    buffer_manager.ReturnBuffer(autoExpandedBuffer);
                    autoExpandedBuffer = new ArraySegment <byte>(new byte[(receive_buffer_offset + receive_count) * 2]);
                }

                Array.Copy(receive_buffer.Array, receive_buffer.Offset, autoExpandedBuffer.Array, autoExpandedBuffer.Offset, receive_buffer_offset + receive_count);
                receive_buffer_offset += receive_count;

                buffer_manager.ReturnBuffer(receive_buffer);
                receive_buffer = autoExpandedBuffer;
            }
        }
 public WebSocketSession(
     WebSocketModule module, HttpListenerContext httpContext, WebSocketContext webSocketContext,
     CancellationToken cancellationToken, ISegmentBufferManager bufferManager)
     : this(module, httpContext, webSocketContext,
            cancellationToken, bufferManager, Encoding.UTF8)
 {
 }
Exemple #9
0
        /// <summary>
        /// Shift buffer from <see cref="ISegmentBufferManager" />.
        /// </summary>
        /// <param name="buffer_manager">Segment buffer manager.</param>
        /// <param name="shift_start"></param>
        /// <param name="session_buffer"></param>
        /// <param name="session_buffer_count"></param>
        public static void ShiftBuffer(this ISegmentBufferManager buffer_manager,
                                       int shift_start,
                                       ref ArraySegment <byte> session_buffer,
                                       ref int session_buffer_count)
        {
            if ((session_buffer_count - shift_start) < shift_start)
            {
                Array.Copy(session_buffer.Array, session_buffer.Offset + shift_start, session_buffer.Array, session_buffer.Offset, session_buffer_count - shift_start);
                session_buffer_count -= shift_start;
            }
            else
            {
                ArraySegment <byte> copy = buffer_manager.BorrowBuffer();
                if (copy.Count < (session_buffer_count - shift_start))
                {
                    buffer_manager.ReturnBuffer(copy);
                    copy = new ArraySegment <byte>(new byte[session_buffer_count - shift_start]);
                }

                Array.Copy(session_buffer.Array, session_buffer.Offset + shift_start, copy.Array, copy.Offset, session_buffer_count - shift_start);
                Array.Copy(copy.Array, copy.Offset, session_buffer.Array, session_buffer.Offset, session_buffer_count - shift_start);
                session_buffer_count -= shift_start;

                buffer_manager.ReturnBuffer(copy);
            }
        }
Exemple #10
0
        /// <summary>
        /// Append buffer to <see cref="ISegmentBufferManager" />.
        /// </summary>
        /// <param name="buffer_manager">Segment buffer manager.</param>
        /// <param name="receive_buffer"></param>
        /// <param name="receive_count"></param>
        /// <param name="session_buffer"></param>
        /// <param name="session_buffer_count"></param>
        public static void AppendBuffer(this ISegmentBufferManager buffer_manager,
                                        ref ArraySegment <byte> receive_buffer,
                                        int receive_count,
                                        ref ArraySegment <byte> session_buffer,
                                        ref int session_buffer_count)
        {
            if (session_buffer.Count < (session_buffer_count + receive_count))
            {
                ArraySegment <byte> buffer = buffer_manager.BorrowBuffer();

                if (buffer.Count < (session_buffer_count + receive_count) * 2)
                {
                    buffer_manager.ReturnBuffer(buffer);
                    buffer = new ArraySegment <byte>(new byte[(session_buffer_count + receive_count) * 2]);
                }

                Array.Copy(session_buffer.Array, session_buffer.Offset, buffer.Array, buffer.Offset, session_buffer_count);

                buffer_manager.ReturnBuffer(session_buffer);
                session_buffer = buffer;
            }

            Array.Copy(receive_buffer.Array, receive_buffer.Offset, session_buffer.Array, session_buffer.Offset + session_buffer_count, receive_count);
            session_buffer_count += receive_count;
        }
Exemple #11
0
 public DeflateCompression(ISegmentBufferManager bufferAllocator)
 {
     if (bufferAllocator == null)
     {
         throw new ArgumentNullException("bufferAllocator");
     }
     _bufferAllocator = bufferAllocator;
 }
        public TcpSocketRioServerConfiguration(ISegmentBufferManager bufferManager)
        {
            BufferManager = bufferManager;

            ReceiveBufferSize = 8192;

            FrameBuilder = new LengthPrefixedFrameBuilder();
        }
 public WebSocketDispatcher(WebSocketRouteResolver routeResolver, ISegmentBufferManager bufferManager)
 {
     if (routeResolver == null)
     {
         throw new ArgumentNullException("routeResolver");
     }
     if (routeResolver == null)
     {
         throw new ArgumentNullException("routeResolver");
     }
     _routeResolver = routeResolver;
     _bufferManager = bufferManager;
 }
Exemple #14
0
        public TcpSocketSaeaClientConfiguration(ISegmentBufferManager bufferManager)
        {
            BufferManager = bufferManager;

            ReceiveBufferSize = 8192;                       // Specifies the total per-socket buffer space reserved for receives. This is unrelated to the maximum message size or the size of a TCP window.
            SendBufferSize    = 8192;                       // Specifies the total per-socket buffer space reserved for sends. This is unrelated to the maximum message size or the size of a TCP window.
            ReceiveTimeout    = TimeSpan.Zero;              // Receive a time-out. This option applies only to synchronous methods; it has no effect on asynchronous methods such as the BeginSend method.
            SendTimeout       = TimeSpan.Zero;              // Send a time-out. This option applies only to synchronous methods; it has no effect on asynchronous methods such as the BeginSend method.
            NoDelay           = true;                       // Disables the Nagle algorithm for send coalescing.
            LingerState       = new LingerOption(false, 0); // The socket will linger for x seconds after Socket.Close is called.
            KeepAlive         = false;                      // Use keep-alives.
            KeepAliveInterval = TimeSpan.FromSeconds(5);    // https://msdn.microsoft.com/en-us/library/system.net.sockets.socketoptionname(v=vs.110).aspx
            ReuseAddress      = false;                      // Allows the socket to be bound to an address that is already in use.

            FrameBuilder = new LengthPrefixedFrameBuilder();
        }
Exemple #15
0
        public TcpSocketRioSession(
            TcpSocketRioServerConfiguration configuration,
            ISegmentBufferManager 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;

            if (_receiveBuffer == default(ArraySegment <byte>))
            {
                _receiveBuffer = _bufferManager.BorrowBuffer();
            }
            _receiveBufferOffset = 0;

            _stream = new RioStream(_socket);
        }
Exemple #16
0
        /// <summary>
        /// Construct a new <see cref="AsyncUdpClientConfiguration"></see> class's instance.
        /// </summary>
        /// <param name="buffer_manager">Segment buffer manager.</param>
        public AsyncUdpClientConfiguration(ISegmentBufferManager buffer_manager)
        {
            this.BufferManager = buffer_manager;

            this.ReceiveBufferSize = 8192;
            this.SendBufferSize    = 8192;
            this.ReceiveTimeout    = TimeSpan.Zero;
            this.SendTimeout       = TimeSpan.Zero;
            this.KeepAlive         = false;
            this.KeepAliveInterval = TimeSpan.FromSeconds(5);
            this.ReuseAddress      = false;

            this.EnableBroadcast = true;
            this.DontFragment    = true;

            this.ConnectTimeout = TimeSpan.FromSeconds(15);
            this.FrameBuilder   = new LengthPrefixedFrameBuilder();
        }
Exemple #17
0
        public TcpSocketSaeaServerConfiguration(ISegmentBufferManager bufferManager)
        {
            BufferManager = bufferManager;

            ReceiveBufferSize = 8192;
            SendBufferSize    = 8192;
            ReceiveTimeout    = TimeSpan.Zero;
            SendTimeout       = TimeSpan.Zero;
            NoDelay           = true;
            LingerState       = new LingerOption(false, 0);
            KeepAlive         = false;
            KeepAliveInterval = TimeSpan.FromSeconds(5);
            ReuseAddress      = false;

            PendingConnectionBacklog = 200;
            AllowNatTraversal        = true;

            FrameBuilder = new LengthPrefixedFrameBuilder();
        }
Exemple #18
0
        public AsyncWebSocketSession(
            TcpClient tcpClient,
            AsyncWebSocketServerConfiguration configuration,
            ISegmentBufferManager bufferManager,
            AsyncWebSocketRouteResolver routeResolver,
            AsyncWebSocketServer server)
        {
            if (tcpClient == null)
            {
                throw new ArgumentNullException("tcpClient");
            }
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }
            if (bufferManager == null)
            {
                throw new ArgumentNullException("bufferManager");
            }
            if (routeResolver == null)
            {
                throw new ArgumentNullException("routeResolver");
            }
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            _tcpClient     = tcpClient;
            _configuration = configuration;
            _bufferManager = bufferManager;
            _routeResolver = routeResolver;
            _server        = server;

            _sessionKey    = Guid.NewGuid().ToString();
            this.StartTime = DateTime.UtcNow;

            _remoteEndPoint = (_tcpClient != null && _tcpClient.Client.Connected) ?
                              (IPEndPoint)_tcpClient.Client.RemoteEndPoint : null;
            _localEndPoint = (_tcpClient != null && _tcpClient.Client.Connected) ?
                             (IPEndPoint)_tcpClient.Client.LocalEndPoint : null;
        }
        public TcpSocketSaeaSession(
            TcpSocketSaeaServerConfiguration configuration,
            ISegmentBufferManager 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;

            if (_receiveBuffer == default(ArraySegment <byte>))
            {
                _receiveBuffer = _bufferManager.BorrowBuffer();
            }
            _receiveBufferOffset = 0;
        }