Exemple #1
0
        public RconPacket SendReceive(RconPacket packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (!Connected)
            {
                throw new Exception("You must be connected before sending data");
            }

            // Send
            socket.Send(packet);

            RconPacket response;

            do
            {
                // Receive
                byte[] buffer = new byte[socket.ReceiveBufferSize], data;
                int    size = -1, counter = 0;
                using (MemoryStream ms = new MemoryStream())
                {
                    do
                    {
                        int count = socket.Receive(buffer);
                        ms.Write(buffer, 0, count);

                        if (size == -1 && ms.Length >= 4)
                        {
                            size = ms.ToArray().ToInt32(0);
                        }

                        if (socket.Available == 0 && (size > -1 && size + 4 > ms.Length))
                        {
                            Thread.Sleep(50);
                            if (counter++ >= 3)
                            {
                                break;
                            }
                        }
                    } while (socket.Available > 0 || (size > -1 && size + 4 > ms.Length));

                    data = ms.ToArray();
                }

                response = (RconPacket)data;
            }while(response.Id == 0 && response.Body == "Keep Alive");

            return(response);
        }
Exemple #2
0
        public bool Authenticate(string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException(nameof(password), "The password can not be empty");
            }

            if (!Connected)
            {
                throw new Exception("You must be connected before authenticating");
            }

            RconPacket response = SendReceive(new RconPacket(PacketType.ServerdataAuth, password));

            authenticated = response.Id != -1;
            return(response.Id != -1);
        }
Exemple #3
0
        public CommandExecutedEventArgs ExecuteCommand(Command command)
        {
            try
            {
                RconPacket request  = new RconPacket(PacketType.ServerdataExeccommand, command.ToString());
                RconPacket response = rcon.SendReceive(request);

                if (request.Id != response.Id)
                {
                    throw new Exception("Got a response with a wrong ID!");
                }

                return(new CommandExecutedEventArgs()
                {
                    Successful = response != null,
                    Error = "",
                    Response = response?.Body.Trim(),
                    Command = command
                });
            }
            catch (SocketException sEx)
            {
                Disconnect(false);

                return(new CommandExecutedEventArgs()
                {
                    Successful = false,
                    Error = sEx.Message,
                    Response = "",
                    Command = command
                });
            }
            catch (Exception ex)
            {
                return(new CommandExecutedEventArgs()
                {
                    Successful = false,
                    Error = ex.Message,
                    Response = "",
                    Command = command
                });
            }
        }
Exemple #4
0
        private void ExecuteCommandWorker()
        {
            try
            {
                while (true)
                {
                    if (queue.Count > 0)
                    {
                        KeyValuePair <Command, EventHandler <CommandExecutedEventArgs> > entry;
                        lock (queue)
                            entry = queue.Dequeue();

                        try
                        {
                            RconPacket request  = new RconPacket(PacketType.ServerdataExeccommand, entry.Key.ToString());
                            RconPacket response = rcon.SendReceive(request);

                            if (request.Id != response.Id)
                            {
                                throw new Exception("Got a response with a wrong ID!");
                            }

                            var commandExecutedEventArgs = new CommandExecutedEventArgs()
                            {
                                Successful = response != null,
                                Error      = "",
                                Response   = response?.Body.Trim(),
                                Command    = entry.Key
                            };
                            entry.Value?.Invoke(this, commandExecutedEventArgs);
                            CommandExecuted?.Invoke(this, commandExecutedEventArgs);
                        }
                        catch (SocketException sEx)
                        {
                            Disconnect(false);

                            var commandExecutedEventArgs = new CommandExecutedEventArgs()
                            {
                                Successful = false,
                                Error      = sEx.Message,
                                Response   = "",
                                Command    = entry.Key
                            };
                            entry.Value?.Invoke(this, commandExecutedEventArgs);
                            CommandExecuted?.Invoke(this, commandExecutedEventArgs);
                        }
                        catch (Exception ex)
                        {
                            var commandExecutedEventArgs = new CommandExecutedEventArgs()
                            {
                                Successful = false,
                                Error      = ex.Message,
                                Response   = "",
                                Command    = entry.Key
                            };
                            entry.Value?.Invoke(this, commandExecutedEventArgs);
                            CommandExecuted?.Invoke(this, commandExecutedEventArgs);
                        }
                    }

                    if (queue.Count == 0)
                    {
                        resetEvent.Reset();
                    }

                    resetEvent.WaitOne();
                }
            }
            catch (ThreadAbortException tEx)
            {
            }
        }