// This region should only be accessed by the MAIN thread /// <summary> /// Queues a notification to be sent to the given peer. /// Deep-copies the user data given. /// </summary> internal void QueueNotification(NetPeer target, byte[] buffer, ushort length) { NetEvent notification = this.CreateEvent( NetEventType.Notification, target); if (notification.ReadData(buffer, 0, length) == false) { throw new OverflowException("Data too long for notification"); } this.notificationIn.Enqueue(notification); }
/// <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 = ReadU16(buffer, 1); int position = 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));; }
/// <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 = ReadU16(buffer, position); position += 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(NOTIFICATION_HEADER_SIZE + dataLength); }