コード例 #1
0
        /// <summary>
        /// Create a new context for a new connection.
        /// </summary>
        private void CreateContext()
        {
            // Create the buffers.
            _requestBuffer  = new Collections.CircularBuffer <byte>(_requestBufferCapacity);
            _responseBuffer = new Collections.CircularBuffer <byte>(_responseBufferCapacity);

            // Create the streams.
            _requestStream  = new RequestStream(_requestBuffer);
            _responseStream = new ResponseStream(_responseBuffer);

            _request  = new Request();
            _response = new Response();
            _context  = new Context();
        }
コード例 #2
0
 /// <summary>
 /// Response stream handler.
 /// </summary>
 /// <param name="buffer">The internal data buffer handler.</param>
 public NetResponseStream(Nequeo.Collections.CircularBuffer <byte> buffer)
     : base(buffer)
 {
 }
コード例 #3
0
        /// <summary>
        /// Dispose(bool disposing) executes in two distinct scenarios.
        /// If disposing equals true, the method has been called directly
        /// or indirectly by a user's code. Managed and unmanaged resources
        /// can be disposed.
        /// If disposing equals false, the method has been called by the
        /// runtime from inside the finalizer and you should not reference
        /// other objects. Only unmanaged resources can be disposed.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this._disposed)
            {
                // Set the web socket state to closed.
                if (_context != null)
                {
                    _context.SocketState = SocketState.Closed;
                }

                // Release the receive and send spin wait handler.
                Interlocked.Exchange(ref _exitWaitReceiveIndicator, 0);
                Interlocked.Exchange(ref _exitWaitSendIndicator, 0);
                Interlocked.Exchange(ref _isContextActive, 0);

                // Note disposing has been done.
                _disposed = true;

                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Close the connection.
                    CloseEx();

                    if (_socket != null)
                    {
                        _socket.Dispose();
                    }

                    if (_sslStream != null)
                    {
                        _sslStream.Dispose();
                    }

                    if (_networkStream != null)
                    {
                        _networkStream.Dispose();
                    }

                    if (_requestBuffer != null)
                    {
                        _requestBuffer.Dispose();
                    }

                    if (_responseBuffer != null)
                    {
                        _responseBuffer.Dispose();
                    }

                    if (_requestStream != null)
                    {
                        _requestStream.Dispose();
                    }

                    if (_responseStream != null)
                    {
                        _responseStream.Dispose();
                    }

                    if (_context != null)
                    {
                        if (_context.ContextState != null)
                        {
                            // If the current state context
                            // implements IDisposable then
                            // dispose of the resources.
                            if (_context.ContextState is IDisposable)
                            {
                                IDisposable disposable = (IDisposable)_context.ContextState;
                                disposable.Dispose();
                            }
                        }
                        _context.Dispose();
                    }
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                _socket                = null;
                _sslStream             = null;
                _networkStream         = null;
                _x509Certificate       = null;
                _endConnectionCallback = null;

                _requestBuffer  = null;
                _responseBuffer = null;

                _requestStream  = null;
                _responseStream = null;

                _request  = null;
                _response = null;

                _context.ContextState = null;
                _context = null;
            }
        }
コード例 #4
0
ファイル: ContextBase.cs プロジェクト: drazenzadravec/nequeo
        /// <summary>
        /// Triggered when all resources are released and parent resources need to be released.
        /// </summary>
        protected override void DisposeOfResources()
        {
            // Set the web socket state to closed.
            if (_context != null)
            {
                _context.SocketState = SocketState.Closed;
            }

            // Release the receive and send spin wait handler.
            Interlocked.Exchange(ref _exitWaitReceiveIndicator, 0);
            Interlocked.Exchange(ref _exitWaitSendIndicator, 0);
            Interlocked.Exchange(ref _isContextActive, 0);
            Interlocked.Exchange(ref _isAsyncModeActive, 0);

            base.State = null;

            _lockReceiver   = null;
            _lockSender     = null;
            _lockFromServer = null;

            if (_requestBuffer != null)
            {
                _requestBuffer.Dispose();
            }

            if (_responseBuffer != null)
            {
                _responseBuffer.Dispose();
            }

            if (_requestStream != null)
            {
                _requestStream.Dispose();
            }

            if (_responseStream != null)
            {
                _responseStream.Dispose();
            }

            if (_context != null)
            {
                if (_context.ContextState != null)
                {
                    // If the current state context
                    // implements IDisposable then
                    // dispose of the resources.
                    if (_context.ContextState is IDisposable)
                    {
                        IDisposable disposable = (IDisposable)_context.ContextState;
                        disposable.Dispose();
                    }
                }

                if (_context.State != null)
                {
                    // If the current state context
                    // implements IDisposable then
                    // dispose of the resources.
                    if (_context.State is IDisposable)
                    {
                        IDisposable disposable = (IDisposable)_context.State;
                        disposable.Dispose();
                    }
                }
                _context.Dispose();
            }

            _requestBuffer  = null;
            _responseBuffer = null;

            _requestStream  = null;
            _responseStream = null;

            _request  = null;
            _response = null;

            _context.State        = null;
            _context.ContextState = null;
            _context = null;
        }
コード例 #5
0
 /// <summary>
 /// Request stream handler.
 /// </summary>
 /// <param name="buffer">The internal data buffer handler.</param>
 public RequestStream(Nequeo.Collections.CircularBuffer <byte> buffer)
     : base(buffer)
 {
 }
コード例 #6
0
 /// <summary>
 /// Request stream handler.
 /// </summary>
 /// <param name="buffer">The internal data buffer handler.</param>
 public StreamBufferBase(Nequeo.Collections.CircularBuffer <byte> buffer)
     : base(buffer)
 {
     _internalBufferInstance = false;
 }
コード例 #7
0
        /// <summary>
        /// On web socket context.
        /// </summary>
        /// <param name="webSocketContext">The asp net web socket context.</param>
        private async void OnChatWebSocketContext(System.Web.WebSockets.AspNetWebSocketContext context)
        {
            WebSocket webSocket = null;

            Nequeo.Net.WebSockets.WebSocketMember    member        = null;
            Nequeo.Collections.CircularBuffer <byte> requestBuffer = null;
            Nequeo.IO.Stream.StreamBufferBase        requestStream = null;

            try
            {
                // Get the current web socket.
                webSocket = context.WebSocket;

                // Create the web socket member and
                // add to the member collection.
                member = new Nequeo.Net.WebSockets.WebSocketMember(webSocket);
                AddMember(member);

                // Holds the receive data.
                bool   hasBeenFound  = false;
                byte[] store         = new byte[0];
                byte[] receiveBuffer = new byte[READ_BUFFER_SIZE];

                // Create the stream buffers.
                requestBuffer = new Collections.CircularBuffer <byte>(base.RequestBufferCapacity);
                requestStream = new Nequeo.IO.Stream.StreamBufferBase(requestBuffer);
                requestBuffer.RemoveItemsWritten = true;

                // Create the current chat state.
                Chat.ChatWebSocketState chatState = new Chat.ChatWebSocketState()
                {
                    Member = member, RequestStream = requestStream, WebSocket = webSocket
                };
                CancellationTokenSource receiveCancelToken = new CancellationTokenSource();

                // While the WebSocket connection remains open run a
                // simple loop that receives data and sends it back.
                while (webSocket.State == WebSocketState.Open)
                {
                    // Receive the next set of data.
                    ArraySegment <byte>    arrayBuffer   = new ArraySegment <byte>(receiveBuffer);
                    WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(arrayBuffer, receiveCancelToken.Token);

                    // Assign the member properties.
                    member.ReceiveResult = receiveResult;
                    member.TimeoutTime   = DateTime.Now;
                    requestStream.Write(receiveBuffer, 0, receiveResult.Count);

                    // If the connection has been closed.
                    if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        // Close the connection.
                        member.Close();
                        break;
                    }
                    else
                    {
                        // Store the data.
                        byte[] temp = null;
                        if (!hasBeenFound)
                        {
                            temp = store.CombineParallel(receiveBuffer);

                            // Find the end of the data.
                            hasBeenFound = Nequeo.Net.Utility.IsParse2CRLF(temp);

                            // Store the data until the end.
                            store = temp;
                            temp  = null;
                        }
                        else
                        {
                            // If this is the end of the message.
                            if (receiveResult.EndOfMessage)
                            {
                                // Clear the store.
                                store        = null;
                                store        = new byte[0];
                                hasBeenFound = false;
                                string resource = "";

                                // Get the request headers.
                                List <Nequeo.Model.NameValue> headers = base.ParseHeaders(requestStream, out resource, base.HeaderTimeout);

                                // All headers have been found.
                                if (headers != null)
                                {
                                    // Get the execution member.
                                    // Set the calling member.
                                    string executionMember = headers.First(m => m.Name.ToUpper().Contains("MEMBER")).Value;
                                    string actionName      = headers.First(m => m.Name.ToUpper().Contains("ACTIONNAME")).Value;

                                    // Assign the values.
                                    chatState.Headers         = headers;
                                    chatState.ExecutionMember = executionMember;
                                    chatState.ErrorCode       = new Exceptions.ErrorCodeException("OK", 200);

                                    try
                                    {
                                        // Validate the current user token.
                                        bool isTokenValid = ValidateToken(chatState);
                                    }
                                    catch (Exceptions.ErrorCodeException exc)
                                    {
                                        // Get the error code.
                                        chatState.ErrorCode = exc;
                                    }
                                    catch (Exception ex)
                                    {
                                        // Internal error.
                                        chatState.ErrorCode = new Exceptions.ErrorCodeException(ex.Message, 500);
                                    }

                                    // Send a message back to the client indicating that
                                    // the message was recivied and was sent.
                                    await webSocket.SendAsync(new ArraySegment <byte>(
                                                                  CreateResponse(chatState.ErrorCode.ErrorCode, true, executionMember, actionName, chatState.ErrorCode.Message)),
                                                              WebSocketMessageType.Binary, true, CancellationToken.None);
                                }
                            }
                        }
                    }
                }

                // Cancel the receive request.
                if (webSocket.State != WebSocketState.Open)
                {
                    receiveCancelToken.Cancel();
                }
            }
            catch { }
            finally
            {
                // If a member context exists.
                if (member != null)
                {
                    try
                    {
                        // Remove the member context
                        // from the collection.
                        RemoveMember(member);
                    }
                    catch { }
                    member = null;
                }

                // Clean up by disposing the WebSocket.
                if (webSocket != null)
                {
                    webSocket.Dispose();
                }

                if (requestBuffer != null)
                {
                    requestBuffer.Dispose();
                }

                if (requestStream != null)
                {
                    requestStream.Dispose();
                }

                if (_communication != null)
                {
                    try
                    {
                        // Remove the client from the communication service.
                        _communication.RemoveClient(member.UniqueIdentifier, base.ServiceName, _machineName, null, actionName: member.UniqueIdentifier);
                    }
                    catch { }
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Dispose(bool disposing) executes in two distinct scenarios.
        /// If disposing equals true, the method has been called directly
        /// or indirectly by a user's code. Managed and unmanaged resources
        /// can be disposed.
        /// If disposing equals false, the method has been called by the
        /// runtime from inside the finalizer and you should not reference
        /// other objects. Only unmanaged resources can be disposed.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            // Check to see if Dispose has already been called.
            if (!this._disposed)
            {
                // Note disposing has been done.
                _disposed = true;

                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.
                    if (_dataAvailable != null)
                    {
                        _dataAvailable.Dispose();
                    }

                    // Release the receive and send spin wait handler.
                    Interlocked.Exchange(ref _exitWaitReceiveIndicator, 0);
                    Interlocked.Exchange(ref _exitWaitSendIndicator, 0);
                    Interlocked.Exchange(ref _isContextActive, 0);

                    if (_requestBuffer != null)
                    {
                        _requestBuffer.Dispose();
                    }

                    if (_responseBuffer != null)
                    {
                        _responseBuffer.Dispose();
                    }

                    if (_requestStream != null)
                    {
                        _requestStream.Dispose();
                    }

                    if (_responseStream != null)
                    {
                        _responseStream.Dispose();
                    }

                    if (_context != null)
                    {
                        if (_context.ContextState != null)
                        {
                            // If the current state context
                            // implements IDisposable then
                            // dispose of the resources.
                            if (_context.ContextState is IDisposable)
                            {
                                IDisposable disposable = (IDisposable)_context.ContextState;
                                disposable.Dispose();
                            }
                        }
                    }
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                _dataAvailable  = null;
                _requestBuffer  = null;
                _responseBuffer = null;

                _requestStream  = null;
                _responseStream = null;

                _request  = null;
                _response = null;

                _context.ContextState = null;
                _context = null;
            }
        }