using System; using System.Net.Sockets; namespace MLAPI.Transports.Tasks { /// /// Represents one or more socket tasks. /// public class SocketTasks { /// /// Gets or sets the underlying SocketTasks. /// /// The tasks. public SocketTask[] Tasks { get; set; } /// /// Gets a value indicating whether this all tasks is done. /// /// true if is done; otherwise, false. public bool IsDone { get { for (int i = 0; i < Tasks.Length; i++) { if (!Tasks[i].IsDone) { return false; } } return true; } } /// /// Gets a value indicating whether all tasks were sucessful. /// /// true if success; otherwise, false. public bool Success { get { for (int i = 0; i < Tasks.Length; i++) { if (!Tasks[i].Success) { return false; } } return true; } } /// /// Gets a value indicating whether any tasks were successful. /// /// true if any success; otherwise, false. public bool AnySuccess { get { for (int i = 0; i < Tasks.Length; i++) { if (Tasks[i].Success) { return true; } } return false; } } /// /// Gets a value indicating whether any tasks are done. /// /// true if any done; otherwise, false. public bool AnyDone { get { for (int i = 0; i < Tasks.Length; i++) { if (Tasks[i].IsDone) { return true; } } return false; } } } /// /// A single socket task. /// public class SocketTask { // Used for states /// /// Gets or sets a value indicating whether this is done. /// /// true if is done; otherwise, false. public bool IsDone { get; set; } /// /// Gets or sets a value indicating whether this is success. /// /// true if success; otherwise, false. public bool Success { get; set; } // These are all set by the transport /// /// Gets or sets the transport exception. /// /// The transport exception. public Exception TransportException { get; set; } /// /// Gets or sets the socket error. /// /// The socket error. public SocketError SocketError { get; set; } /// /// Gets or sets the transport code. /// /// The transport code. public int TransportCode { get; set; } /// /// Gets or sets the message. /// /// The message. public string Message { get; set; } /// /// Gets or sets the state. /// /// The state. public object State { get; set; } /// /// Gets a done task. /// /// The done. public static SocketTask Done => new SocketTask { IsDone = true, Message = null, SocketError = SocketError.Success, State = null, Success = true, TransportCode = -1, TransportException = null }; /// /// Gets a faulty task. /// /// The fault. public static SocketTask Fault => new SocketTask { IsDone = true, Message = null, SocketError = SocketError.SocketError, State = null, Success = false, TransportCode = -1, TransportException = null }; /// /// Gets a working task. /// /// The working. public static SocketTask Working => new SocketTask { IsDone = false, Message = null, SocketError = SocketError.SocketError, State = null, Success = false, TransportCode = -1, TransportException = null }; /// /// Converts to a SocketTasks. /// /// The tasks. public SocketTasks AsTasks() => new SocketTasks { Tasks = new SocketTask[] { this } }; } }