Ejemplo n.º 1
0
        private async Task DataReceiver(ClientMetadata md)
        {
            string header = "[WatsonWsServer " + md.IpPort + "] ";

            Logger?.Invoke(header + "starting data receiver");
            byte[] buffer = new byte[65536];

            try
            {
                while (true)
                {
                    MessageReceivedEventArgs msg = await MessageReadAsync(md, buffer).ConfigureAwait(false);

                    if (msg != null)
                    {
                        if (EnableStatistics)
                        {
                            _Stats.IncrementReceivedMessages();
                            _Stats.AddReceivedBytes(msg.Data.Count);
                        }

                        if (msg.Data != null)
                        {
                            Task unawaited = Task.Run(() => MessageReceived?.Invoke(this, msg), md.TokenSource.Token);
                        }
                        else
                        {
                            await Task.Delay(10).ConfigureAwait(false);
                        }
                    }
                }
            }
            catch (TaskCanceledException)
            {
                // thrown when disposed
            }
            catch (OperationCanceledException)
            {
                // thrown when disposed
            }
            catch (WebSocketException)
            {
                // thrown by MessageReadAsync
            }
            catch (Exception e)
            {
                Logger?.Invoke(header + "exception: " + Environment.NewLine + e.ToString());
            }
            finally
            {
                string ipPort = md.IpPort;
                ClientDisconnected?.Invoke(this, new ClientDisconnectedEventArgs(md.IpPort));
                md.Ws.Dispose();
                Logger?.Invoke(header + "disconnected");
                _Clients.TryRemove(ipPort, out _);
            }
        }
Ejemplo n.º 2
0
        private async Task DataReceiver()
        {
            byte[] buffer = new byte[65536];

            try
            {
                while (true)
                {
                    if (_Token.IsCancellationRequested)
                    {
                        break;
                    }
                    MessageReceivedEventArgs msg = await MessageReadAsync(buffer);

                    if (msg != null)
                    {
                        if (EnableStatistics)
                        {
                            _Stats.IncrementReceivedMessages();
                            _Stats.AddReceivedBytes(msg.Data.Count);
                        }

                        if (msg.MessageType != WebSocketMessageType.Close)
                        {
                            if (_AwaitingSyncResponseEvent != null)
                            {
                                _AwaitingSyncResponseEvent?.Invoke(this, msg);
                            }
                            else
                            {
                                MessageReceived?.Invoke(this, msg);
                            }
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Logger?.Invoke(_Header + "data receiver canceled");
            }
            catch (WebSocketException)
            {
                Logger?.Invoke(_Header + "websocket disconnected");
            }
            catch (Exception e)
            {
                Logger?.Invoke(_Header + "exception: " + Environment.NewLine + e.ToString());
            }

            ServerDisconnected?.Invoke(this, EventArgs.Empty);
        }
Ejemplo n.º 3
0
        private async Task DataReceiver(ClientMetadata md)
        {
            string header = "[WatsonWsServer " + md.IpPort + "] ";

            Logger?.Invoke(header + "starting data receiver");

            try
            {
                while (true)
                {
                    MessageReceivedEventArgs msg = await MessageReadAsync(md);

                    if (msg != null)
                    {
                        _Stats.IncrementReceivedMessages();
                        _Stats.AddReceivedBytes(msg.Data.Length);

                        if (msg.Data != null)
                        {
                            MessageReceived?.Invoke(this, msg);
                        }
                        else
                        {
                            await Task.Delay(100);
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                // thrown when disposed
            }
            catch (WebSocketException)
            {
                // thrown by MessageReadAsync
            }
            catch (Exception e)
            {
                Logger?.Invoke(header + "exception: " + Environment.NewLine + e.ToString());
            }
            finally
            {
                string ipPort = md.IpPort;
                ClientDisconnected?.Invoke(this, new ClientDisconnectedEventArgs(md.IpPort));
                md.Ws.Dispose();
                Logger?.Invoke(header + "disconnected");
                _Clients.TryRemove(ipPort, out _);
            }
        }
Ejemplo n.º 4
0
        private async Task DataReceiver()
        {
            try
            {
                while (true)
                {
                    if (_Token.IsCancellationRequested)
                    {
                        break;
                    }
                    MessageReceivedEventArgs msg = await MessageReadAsync();

                    if (msg != null)
                    {
                        if (EnableStatistics)
                        {
                            _Stats.IncrementReceivedMessages();
                            _Stats.AddReceivedBytes(msg.Data.Length);
                        }

                        Task internal_unawaited = Task.Run(() => InternalMessageReceived?.Invoke(this, msg), _Token);
                        if (msg.MessageType != WebSocketMessageType.Close)
                        {
                            Task unawaited = Task.Run(() => MessageReceived?.Invoke(this, msg), _Token);
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Logger?.Invoke(_Header + "data receiver canceled");
            }
            catch (WebSocketException)
            {
                Logger?.Invoke(_Header + "websocket disconnected");
            }
            catch (Exception e)
            {
                Logger?.Invoke(_Header + "exception: " + Environment.NewLine + e.ToString());
            }

            ServerDisconnected?.Invoke(this, EventArgs.Empty);
            InternalServerDisconnected?.Invoke(this, EventArgs.Empty);
        }