/// <summary> /// This method is called whenever a new command request is enqueued to be sent to /// the IGS SERVER and also whenever /// a command request becomes completed. The method will determine whether the channel /// is currently free (i.e. no other command is being executed) and /// if so, if there are any command requests in the queue, /// the earliest one is dequeued and executed. /// </summary> private void ExecuteRequestFromQueue() { lock (_mutex) { if (_composure != IgsComposure.Ok) { return; // Cannot yet send requests. } if (_requestInProgress == null) { IgsRequest dequeuedItem; if (_outgoingRequests.TryDequeue(out dequeuedItem)) { if (!dequeuedItem.Unattended) { _requestInProgress = dequeuedItem; } OnOutgoingLine(dequeuedItem.Command); _streamWriter.WriteLine(dequeuedItem.Command); if (dequeuedItem.Unattended) { ExecuteRequestFromQueue(); } } } } }
/// <summary> /// Enqueues a command to be send to IGS. /// </summary> /// <param name="command">The single-line command.</param> public void MakeUnattendedRequest(string command) { IgsRequest request = new IgsRequest(command) { Unattended = true }; _outgoingRequests.Enqueue(request); ExecuteRequestFromQueue(); }
/// <summary> /// Enqueues the <paramref name="command"/> to be sent over Telnet to the IGS SERVER, /// then asynchronously receives the entirety of the server's response to this command. /// </summary> /// <param name="command">The command to send over Telnet.</param> /// <returns></returns> internal async Task <IgsResponse> MakeRequestAsync(string command) { IgsRequest request = new IgsRequest(command); _outgoingRequests.Enqueue(request); ExecuteRequestFromQueue(); IgsResponse lines = await request.GetAllLines(); lock (_mutex) { _requestInProgress = null; } ExecuteRequestFromQueue(); return(lines); }
private void ConnectionLost() { LoggedIn = false; if (Composure == IgsComposure.Disconnected) { return; // Don't do this twice. // Thread safety problems may occur. Oh well, it's networking. Hopefully they won't. // If yes, we should eliminate the possibilities for connection loss elsewehere and maybe ensure that a single // point of connection failure exists, possibly in OnFaulted. We'll see. } _client = null; Composure = IgsComposure.Disconnected; ConnectionEstablished = false; foreach (var game in _availableConnectors) { game.Value.Disconnect(); } Data.GamesInProgress.Clear(); Data.OnlineUsers.Clear(); this.GamesYouHaveOpened.Clear(); this.GamesBeingObserved.Clear(); _availableConnectors.Clear(); IgsRequest notYetHandledRequest; while (_outgoingRequests.TryDequeue(out notYetHandledRequest)) { notYetHandledRequest.Disconnect(); } lock (_mutex) { _requestInProgress?.Disconnect(); _requestInProgress = null; } Events.RaiseDisconnected(); }