Пример #1
0
        /// <summary>
        /// Handles incomming transmissions from the remote host.
        /// </summary>
        /// <param name="thisClient">The ClientObject to handle transmissions from.</param>
        private void HandleClient(ClientObject thisClient)
        {
            SendConnectedClients(thisClient);
            while (Active)
            {
                try
                {
                    if (thisClient.Connections["Main"].DataAvailable && !Pausing)
                    {
                        Command command = thisClient.Connections["Main"].Read <Command>(Password);
                        if (command != null)
                        {
                            if (command.Options.Contains(CommandOption.Forward))
                            {
                                Clients.Values.First(c => c.Information.ID == command.Target.ID).Connections["Main"].Write(Password, command);
                            }
                            else
                            {
                                switch (command.Type)
                                {
                                case CommandType.Authenticate:
                                case CommandType.Authorized:
                                case CommandType.ClientAdded:
                                case CommandType.ClientRemoved:
                                case CommandType.None:
                                case CommandType.Unauthorized:
                                    break;

                                case CommandType.Close:
                                    if (command.Options.Contains(CommandOption.Broadcast))
                                    {
                                        ClientRemoved(thisClient);
                                    }
                                    throw new ThreadInterruptedException();

                                case CommandType.Custom:
                                    if (command.Options.Contains(CommandOption.Broadcast))
                                    {
                                        BroadcastAsync(command);
                                    }
                                    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. Time: {DateTime.Now.ToString()}", command);
                                }
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(0);
                    }
                }
                catch (ThreadInterruptedException) { break; }
                catch (Exception error) { Debug.WriteLine($"'{error.GetType()}' thrown in thread {Thread.CurrentThread.ManagedThreadId}. Message: {error.Message}"); }
            }
        }
Пример #2
0
        /// <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}"); }
            }
        }