Dispose() public abstract method

public abstract Dispose ( ) : void
return void
        public override async Task CloseAsync()
        {
            if (IsConnected)
            {
                State = ChannelState.ClosedReceived;
            }

            if (socket != null)
            {
                try
                {
                    await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal", CancellationToken.None);

                    socket.Dispose();
                    socket = null;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Fault closing Web socket server socket - {ex.Message}");
                }
            }



            OnClose?.Invoke(this, new ChannelCloseEventArgs(Id));
            await Task.CompletedTask;
        }
示例#2
0
        public override void Dispose()
        {
            int priorState = Interlocked.Exchange(ref state, disposed);

            if (priorState == disposed)
            {
                // No cleanup required.
                return;
            }
            cts.Cancel(false);
            cts.Dispose();
            if (innerWebSocket != null)
            {
                innerWebSocket.Dispose();
            }
        }
示例#3
0
        public override void Dispose()
        {
            var priorState = (InternalState)Interlocked.Exchange(ref _state, (int)InternalState.Disposed);

            if (priorState == InternalState.Disposed)
            {
                // No cleanup required.
                return;
            }
            _cts.Cancel(false);
            _cts.Dispose();
            if (_innerWebSocket != null)
            {
                _innerWebSocket.Dispose();
            }
        }
        protected void Disposing(bool dispose)
        {
            if (dispose & !disposed)
            {
                disposed = true;

                if (State == ChannelState.Open)
                {
                    this.handler.Close();
                }

                if (socket != null)
                {
                    socket.Dispose();
                }
            }
        }
        private async Task HandleWebSocket(WebSocketContext wsContext)
        {
            var socketId = Guid.NewGuid().ToString();

            System.Net.WebSockets.WebSocket webSocket = wsContext.WebSocket;
            try
            {
                _sockets.TryAdd(socketId, webSocket);
                byte[] receiveBuffer = new byte[1024 * 4];
                while (webSocket.State == WebSocketState.Open)
                {
                    //var msg = Encoding.UTF8.GetBytes(WebSocketSerivce.GetNewOrders());
                    //await webSocket.SendAsync(new ArraySegment<byte>(msg, 0, msg.Length), WebSocketMessageType.Text,
                    //    true, CancellationToken.None);

                    WebSocketReceiveResult receiveResult =
                        await webSocket.ReceiveAsync(new ArraySegment <byte>(receiveBuffer), CancellationToken.None);

                    if (receiveResult.MessageType == WebSocketMessageType.Close)
                    {
                        await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);

                        _sockets.TryRemove(socketId, out System.Net.WebSockets.WebSocket removedSocket);
                    }
                    else
                    {
                        foreach (var socket in _sockets.Where(x => x.Key != socketId).Select(x => x.Value))
                        {
                            await socket.SendAsync(new ArraySegment <byte>(receiveBuffer, 0, receiveResult.Count),
                                                   WebSocketMessageType.Text, receiveResult.EndOfMessage, CancellationToken.None);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e);
            }
            finally
            {
                webSocket?.Dispose();
            }
        }
示例#6
0
 public void Dispose()
 {
     _webSocket.Dispose();
 }
 public void Dispose()
 {
     _state = WebSocketState.Closed;
     WebSocket?.Dispose();
 }
        internal async Task ProcessWebSocketRequestAsync(WebSocket webSocket, CancellationToken disconnectToken, Func<object, Task<WebSocketMessage>> messageRetriever, object state)
        {
            bool closedReceived = false;

            try
            {
                // first, set primitives and initialize the object
                WebSocket = webSocket;
                OnOpen();

                // dispatch incoming messages
                while (!disconnectToken.IsCancellationRequested && !closedReceived)
                {
                    WebSocketMessage incomingMessage = await messageRetriever(state).PreserveCulture();
                    switch (incomingMessage.MessageType)
                    {
                        case WebSocketMessageType.Binary:
                            OnMessage((byte[])incomingMessage.Data);
                            break;

                        case WebSocketMessageType.Text:
                            OnMessage((string)incomingMessage.Data);
                            break;

                        default:
                            closedReceived = true;

                            // If we received an incoming CLOSE message, we'll queue a CLOSE frame to be sent.
                            // We'll give the queued frame some amount of time to go out on the wire, and if a
                            // timeout occurs we'll give up and abort the connection.
                            await Task.WhenAny(CloseAsync(), Task.Delay(_closeTimeout)).PreserveCulture();
                            break;
                    }
                }

            }
            catch (OperationCanceledException ex)
            {
                // ex.CancellationToken never has the token that was actually cancelled
                if (!disconnectToken.IsCancellationRequested)
                {
                    Error = ex;
                    OnError();
                }
            }
            catch (ObjectDisposedException)
            {
                // If the websocket was disposed while we were reading then noop
            }
            catch (Exception ex)
            {
                if (IsFatalException(ex))
                {
                    Error = ex;
                    OnError();
                }
            }

            try
            {
#if CLIENT_NET45
                var webSocketState = GetWebSocketState(WebSocket);
                if (webSocketState == WebSocketState.Closed ||
                    webSocketState == WebSocketState.Aborted)
                {
                    // No-op if the socket is already closed or aborted
                }
                else
                {
                    // Initiate the WebSocket closing handshake. Only the client should ever do this.
                    await WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None).PreserveCulture();
                }
#endif
            }
            finally
            {
#if CLIENT_NET45
                WebSocket.Dispose();
#endif
                OnClose();
            }
        }
示例#9
0
 private async Task HandleSocket(WebSocket socket) {
     Guid key = Guid.NewGuid();
     BlockingCollection<object> collection = new BlockingCollection<object>();
     LogFacade.Instance.AddListener(key, entry => collection.Add(entry));
     try {
         bool took = true;
         await CheckHeaders(socket);
         while (socket.State == WebSocketState.Open && !CancellationToken.Token.IsCancellationRequested) {
             if (!took) {
                 await Task.Delay(1000);
                 await WriteSocket(socket, new { Ping = true });
             }
             object item;
             took = collection.TryTake(out item);
             if (took)
                 await WriteSocket(socket, item);
         }
     }
     catch (Exception ex) {
         LogFacade.Instance.LogException("Exception handling web socket connection", ex);
     }
     finally {
         this.GuardedExecution(() => {
             LogFacade.Instance.RemoveListener(key);
             socket.Dispose();
             LogFacade.Instance.LogInfo("Closed web socket connection");
         });
     }
 }
示例#10
0
 public override void Dispose()
 {
     _webSocket.Dispose();
 }