示例#1
0
        /// <summary>
        /// Parse a packet (array of bytes) and returns a RCON_Event object
        /// </summary>
        /// <param name="pckt_bytes">Packet as bytes</param>
        /// <returns>Returns a RCON_Event object</returns>
        public RCON_Event ParsePacket(byte[] pckt_bytes)
        {
            logger.Trace("RCON_Client.ParsePacket(bytes) starts.");

            /*
             * pckt_bytes is a concatenation of:
             * - the start delimiter "┐"
             * - a short (16-bit integer) corresponding to the size of the JSON
             * - a short (16-bit integer) corresponding to eventID of the RCON event
             * - the JSON
             * - the end delimiter "└"
             */

            UTF8Encoding uTF8 = new UTF8Encoding();

            // skip the start delimiter
            int byte_ptr = this.start_del_bytes.Length;

            // number of bytes for 16-bit integer
            int short_bytes = 2;

            logger.Trace("Getting JSON size from the packet as bytes.");
            // convert bytes to short
            short json_size = BitConverter.ToInt16(pckt_bytes, byte_ptr);

            byte_ptr += short_bytes;
            logger.Trace("Getting event ID from the packet as bytes.");
            short eventID = BitConverter.ToInt16(pckt_bytes, byte_ptr);

            byte_ptr += short_bytes;

            logger.Trace("Get the JSON from the packet as bytes.");
            // get json as string from bytes and remove the end delimiter
            string pckt_json = uTF8.GetString(pckt_bytes, byte_ptr, json_size).TrimEnd('└');

            logger.Trace($"Creating an event.");
            RCON_Event rcon_event = new RCON_Event(json_size, eventID, pckt_json, logger);

            logger.Trace("RCON_Client.ParsePacket(bytes) finishes.");
            return(rcon_event);
        }
示例#2
0
        /// <summary>
        /// Receive the next event from the server
        /// </summary>
        /// <returns>Returns a RCON_Event if successfully received the next event, otherwise returns null</returns>
        public RCON_Event ReceiveEvent()
        {
            logger.Trace("RCON_Client.ReceiveEvent() starts.");
            RCON_Event rcon_evt = null;

            try
            {
                logger.Trace("Getting stream from the client.");
                // always get stream, and do not close it afterwards
                NetworkStream stream = this.client.GetStream();
                logger.Trace("Setting timeout of stream's reading to 7000ms.");
                stream.ReadTimeout = 7000;

                byte[] packet_received = new byte[this.client.ReceiveBufferSize];
                if (this.client.ReceiveBufferSize > 0)
                {
                    logger.Trace("Reading data from the stream.");
                    // reads data from stream and put it in the buffer "packet_received"
                    stream.Read(packet_received, 0, packet_received.Length);

                    logger.Trace("Parsing the data received and create a new event from it.");
                    rcon_evt = ParsePacket(packet_received);

                    logger.Debug($"Event {(EventType)rcon_evt.EventID} ({rcon_evt.EventID}) received.");
                }
                logger.Trace("Finished checking for data in the stream");
            }
            catch (Exception e)
            {
                logger.Error("Failed to receive event.");
                logger.Debug($"Error: {e.ToString()}");
            }

            logger.Trace("RCON_Client.ReceiveEvent() finishes.");
            return(rcon_evt);
        }
示例#3
0
 private lib.RCON_Event receiveEvt(lib.RCON_Client rcon)
 {
     lib.RCON_Event evt = rcon.ReceiveEvent();
     //Console.WriteLine("");
     return(evt);
 }