namespace Unosquare.Swan { using System; using System.Text; /// /// The event arguments for connection failure events. /// /// public class ConnectionFailureEventArgs : EventArgs { /// /// Initializes a new instance of the class. /// /// The ex. public ConnectionFailureEventArgs(Exception ex) { Error = ex; } /// /// Gets the error. /// /// /// The error. /// public Exception Error { get; } } /// /// Event arguments for when data is received. /// /// public class ConnectionDataReceivedEventArgs : EventArgs { /// /// Initializes a new instance of the class. /// /// The buffer. /// The trigger. /// if set to true [more available]. public ConnectionDataReceivedEventArgs(byte[] buffer, ConnectionDataReceivedTrigger trigger, bool moreAvailable) { Buffer = buffer ?? throw new ArgumentNullException(nameof(buffer)); Trigger = trigger; HasMoreAvailable = moreAvailable; } /// /// Gets the buffer. /// /// /// The buffer. /// public byte[] Buffer { get; } /// /// Gets the cause as to why this event was thrown. /// /// /// The trigger. /// public ConnectionDataReceivedTrigger Trigger { get; } /// /// Gets a value indicating whether the receive buffer has more bytes available. /// /// /// true if this instance has more available; otherwise, false. /// public bool HasMoreAvailable { get; } /// /// Gets the string from the given buffer. /// /// The buffer. /// The encoding. /// A that contains the results of decoding the specified sequence of bytes. public static string GetStringFromBuffer(byte[] buffer, Encoding encoding) => encoding.GetString(buffer).TrimEnd('\r', '\n'); /// /// Gets the string from buffer. /// /// The encoding. /// A that contains the results of decoding the specified sequence of bytes. public string GetStringFromBuffer(Encoding encoding) => GetStringFromBuffer(Buffer, encoding); } }