Beispiel #1
0
 private void RecievedPacket(RCONPacket packet)
 {
     if (pendingPackets.ContainsKey(packet.Id))
     {
         pendingPackets[packet.Id].SetResult(packet);
         pendingPackets.Remove(packet.Id);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Sends a command to the RCON server
        /// </summary>
        /// <returns>A task with the response packet</returns>
        public async Task <RCONPacket> SendCommandAsync(string command, RCONPacketType type = RCONPacketType.ExecCommand)
        {
            RCONPacket packet = new RCONPacket(GenerateID(), type, command);
            var        task   = new TaskCompletionSource <RCONPacket>();

            pendingPackets.Add(packet.Id, task);
            await this.socket.SendAsync(packet.ToBytes(), SocketFlags.None);

            return(await task.Task);
        }
Beispiel #3
0
 /// <summary>
 /// Consumes recieved bytes and constructs packets from them, returns how many new packets have been constructed.
 /// </summary>
 /// <param name="buffer">Buffer to read.</param>
 /// <param name="count">How many bytes to read from the buffer</param>
 /// <param name="offset">Starting read offset (defaults to 0)</param>
 /// <returns>Created packet.</returns>
 public int FeedBytes(byte[] buffer, int count, int offset = 0)
 {
     for (int i = offset; i < count + offset; i++)
     {
         if (constructingPacket)
         {
             //Write the byte to memory
             packetBuffer[packetBufferIndex] = buffer[i];
             packetBufferIndex++;
             //The packet is finished
             if (packetBufferIndex >= packetSize)
             {
                 packetBufferIndex = 0;
                 packets.Enqueue(RCONPacket.FromBytes(packetBuffer));
                 constructingPacket = false;
             }
         }
         else
         {
             //We need to find out the packetsize
             sizeBuffer[sizeBufferIndex] = buffer[i];
             sizeBufferIndex++;
             //We got the full 4 byte number
             if (sizeBufferIndex >= 4)
             {
                 packetSize         = 4 + BitConverter.ToInt32(sizeBuffer, 0);
                 sizeBufferIndex    = 0;
                 packetBuffer       = new byte[packetSize];
                 packetBuffer[0]    = sizeBuffer[0];
                 packetBuffer[1]    = sizeBuffer[1];
                 packetBuffer[2]    = sizeBuffer[2];
                 packetBuffer[3]    = sizeBuffer[3];
                 packetBufferIndex  = 4;
                 constructingPacket = true;
             }
         }
     }
     return(0);
 }
Beispiel #4
0
        private async Task Recieve()
        {
            byte[] buffer  = new byte[4096];
            var    builder = new RCONPacketBuilder();

            while (Connected)
            {
                int bytes = await this.socket.ReceiveAsync(buffer, SocketFlags.None);

                builder.FeedBytes(buffer, bytes);
                while (builder.AvailablePackets > 0)
                {
                    RCONPacket packet = builder.GetPacket();
                    if (packet.Type == RCONPacketType.AuthResponse)
                    {
                        authenticationSuccessful.SetResult(packet.Id == 0);
                    }
                    else
                    {
                        RecievedPacket(packet);
                    }
                }
            }
        }