ReadU16() 개인적인 정적인 메소드

Reads a U16 from a buffer at a location in Big Endian order.
private static ReadU16 ( byte buffer, int position ) : ushort
buffer byte
position int
리턴 ushort
예제 #1
0
        /// <summary>
        /// Reads a collection of notifications packed in the buffer.
        /// </summary>
        internal static bool ReadCarrier(
            Func <NetEventType, NetPeer, NetEvent> eventFactory,
            NetPeer peer,
            byte[] buffer,
            int length,
            out ushort notificationAck,
            out ushort notificationSeq,
            Queue <NetEvent> destinationQueue)
        {
            // Read header (already know the type)
            notificationAck = NetEncoding.ReadU16(buffer, 1);
            notificationSeq = NetEncoding.ReadU16(buffer, 3);
            int position = NetEncoding.CARRIER_HEADER_SIZE;

            // Validate
            int maxDataPack = NetEncoding.MAX_NOTIFICATION_PACK;

            if ((position > length) || ((length - position) > maxDataPack))
            {
                return(false);
            }

            // Read notifications
            while (position < length)
            {
                NetEvent evnt      = eventFactory.Invoke(NetEventType.Notification, peer);
                int      bytesRead =
                    NetEncoding.ReadNotification(buffer, length, position, evnt);
                if (bytesRead < 0)
                {
                    return(false);
                }

                destinationQueue.Enqueue(evnt);
                position += bytesRead;
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// Reads payload data from the given buffer.
        /// </summary>
        internal static bool ReadPayload(
            Func <NetEventType, NetPeer, NetEvent> eventFactory,
            NetPeer peer,
            byte[] buffer,
            int length,
            out ushort sequence,
            out NetEvent evnt)
        {
            evnt = null;

            // Read header (already know the type)
            sequence = NetEncoding.ReadU16(buffer, 1);
            int position = NetEncoding.PAYLOAD_HEADER_SIZE;

            ushort dataLength = (ushort)(length - position);

            if ((position + dataLength) > length)
            {
                return(false); // We're reading past the end of the packet data
            }
            evnt = eventFactory.Invoke(NetEventType.Payload, peer);
            return(evnt.ReadData(buffer, position, dataLength));;
        }
예제 #3
0
        /// <summary>
        /// Reads a length-prefixed notification block.
        /// </summary>
        private static int ReadNotification(
            byte[] buffer,
            int length,
            int position,
            NetEvent destination)
        {
            // Read the length we added
            ushort dataLength = NetEncoding.ReadU16(buffer, position);

            position += NetEncoding.NOTIFICATION_HEADER_SIZE;

            // Avoid a crash if the packet is bad (or malicious)
            if ((position + dataLength) > length)
            {
                return(-1);
            }

            // Read the data into the event's buffer
            if (destination.ReadData(buffer, position, dataLength) == false)
            {
                return(-1);
            }
            return(NetEncoding.NOTIFICATION_HEADER_SIZE + dataLength);
        }