/// <summary> /// Called when a remote host connects to the session. /// </summary> /// <param name="newClient">The ClientObject representing the remote host.</param> private void ClientAdded(ClientObject newClient) { Clients.Add(newClient.Information.ID, newClient); BroadcastAsync(new Command(null, newClient.Information, CommandType.ClientAdded)); OnClientConnected?.BeginInvoke(newClient.Information, result => { try { OnClientConnected.EndInvoke(result); } catch { } }, null); }
/// <summary> /// Handles incomming commands from the remote host. /// </summary> private void HandleCommands() { while (Active) { try { if (!ClientInterface.Connections["Main"].DataAvailable || Pausing) { Thread.Sleep(10); } Command command = ClientInterface.Connections["Main"].Read <Command>(Password); if (command != null) { switch (command.Type) { case CommandType.Authenticate: case CommandType.Authorized: case CommandType.None: case CommandType.Unauthorized: break; case CommandType.ClientAdded: Clients.Add(command.Sender.ID, command.Sender); OnClientConnected?.BeginInvoke(command.Sender, result => { try { OnClientConnected.EndInvoke(result); } catch { } }, null); break; case CommandType.ClientRemoved: Clients.Remove(command.Sender.ID); OnClientDisconnected?.BeginInvoke(command.Sender, result => { try { OnClientDisconnected.EndInvoke(result); } catch { } }, null); break; case CommandType.Close: Disconnect(false); throw new ThreadInterruptedException(); case CommandType.Custom: OnCustomCommand?.BeginInvoke(command, result => { try { OnCustomCommand.EndInvoke(result); } catch { } }, null); break; default: throw new CommandException($"The command type \"{command.Type}\" was not recognized in this context.", command); } } } catch (ThreadInterruptedException) { break; } catch (Exception error) { Debug.WriteLine($"'{error.GetType()}' thrown in thread {Thread.CurrentThread.ManagedThreadId}. Message: {error.Message}"); } } }