Ejemplo n.º 1
0
        /// <summary>
        /// Raised by the underlying <see cref="Connection"/> when a connection is destroyed
        /// </summary>
        private void Connection_DisconnectedEvent(object sender, EventArgs e)
        {
            // Just pass on the event along
            var args = new object[] { sender, e };

            DisconnectedEvent.RaiseEventSafe(ref args);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loop that listens for data and raises events when data is received.
        /// </summary>
        private async Task DataReceiver(CancellationToken token)
        {
            Logger.Log(Logger.Level.Debug, $"Currently monitoring socket for incoming data");

            try
            {
                // Loop forever.  That's a long time
                while (true)
                {
                    // Determine if we can loop
                    if (token.IsCancellationRequested || Client == null || !Client.Connected)
                    {
                        Logger.Log(Logger.Level.Debug, $"Halting socket monitoring...");
                        break;
                    }

                    // Read data.  This should not return until data is received
                    byte[] data = await DataReadAsync(token);

                    // Obviously, if there's no data, there's an issue
                    if (data == null)
                    {
                        Logger.Log(Logger.Level.Warning, $"Read null bytes from the socket.  Skipping...");
                        // Wait for a bit and try again
                        await Task.Delay(30);

                        continue;
                    }

                    Logger.Log(Logger.Level.Debug, $"Read {data.Length} bytes from the socket.  Raising events.");

                    // Raise the event unawaited so that we can keep looping in case more data comes in
                    _ = Task.Run(() =>
                    {
                        var args = new object[] { this, new DataReceivedEventArgs(data) };
                        DataReceivedEvent.RaiseEventSafe(ref args);
                    });
                }
            }
            catch (TaskCanceledException)
            {
                // We don't really care if the task was cancelled.
            }
            catch (OperationCanceledException)
            {
                // We don't really care if the task was cancelled.
            }
            catch (Exception ex)
            {
                Logger.Log(Logger.Level.Error, $"An error occurred monitoring the socket for data.\n\n{ex.Message}");
            }

            Logger.Log(Logger.Level.Debug, $"Raising the {nameof(DisconnectedEvent)} event");
            _ = Task.Run(() =>
            {
                var args = new object[] { this, EventArgs.Empty };
                DisconnectedEvent.RaiseEventSafe(ref args);
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Raised by the socket wrapper when a disconnection is detected
        /// </summary>
        private void ClientInstance_DisconnectedEvent(object sender, EventArgs e)
        {
            Logger.Log(Logger.Level.Info, "Disconnection detected.  Passing on...");

            // Pass the event on
            var args = new object[] { sender, EventArgs.Empty };

            DisconnectedEvent.RaiseEventSafe(ref args);
        }