コード例 #1
0
        /// <summary>
        /// After writing the header we write the packet data in three passes.
        /// The first pass is a fill of events up to a percentage of the packet.
        /// The second pass is the payload value, which will try to fill the
        /// remaining packet space. If more space is available, we will try
        /// to fill it with any remaining events, up to the maximum packet size.
        /// </summary>
        public static void EncodePacket <T>(this BitBuffer buffer, T packet, Int32Compressor eventTypeCompressor, Action <BitBuffer, int, T> onEncodePayload) where T : Packet
        {
            // Write: [Header]
            {
                // Write: [LocalTick]
                buffer.WriteTick(packet.SenderTick);

                // Write: [AckTick]
                buffer.WriteTick(packet.AckTick);

                // Write: [AckReliableEventId]
                buffer.WriteSequenceId(packet.AckEventId);
            }

            // Write: [Events] (Early Pack)
            EncodeEvents(Config.PACKCAP_EARLY_EVENTS);

            // Write: [Payload]
            //packet.EncodePayload(buffer, 1); // Leave one byte for the event count
            onEncodePayload(buffer, 1, packet);

            // Write: [Events] (Fill Pack)
            EncodeEvents(Config.PACKCAP_MESSAGE_TOTAL);

            /// <summary>
            /// Writes as many events as possible up to maxSize and returns the number
            /// of events written in the batch. Also increments the total counter.
            /// </summary>
            void EncodeEvents(int maxSize)
            {
                packet.EventsWritten += buffer.PackToSize(maxSize, Config.MAXSIZE_EVENT, GetNextEvents(),
                                                          evnt =>
                {
                    /// <summary>
                    /// Note that the packetTick may not be the tick this event was created on
                    /// if we're re-trying to send this event in subsequent packets. This tick
                    /// is intended for use in tick diffs for compression.
                    /// </summary>
                    //Event.Encode
                    var packetTick = packet.SenderTick;

                    // Write: [EventType]
                    buffer.WriteInt(eventTypeCompressor, evnt.TypeCode);

                    // Write: [EventId]
                    buffer.WriteSequenceId(evnt.EventId);

                    // Write: [HasEntityId]
                    buffer.WriteBool(evnt.EntityId.IsValid);

                    if (evnt.EntityId.IsValid)
                    {
                        // Write: [EntityId]
                        buffer.WriteEntityId(evnt.EntityId);
                    }

                    // Write: [EventData]
                    evnt.EncodeData(buffer, packetTick);
                },
                                                          evnt =>
                {
                    // Event.RegisterSent
                    if (evnt.Attempts > 0)
                    {
                        evnt.Attempts--;
                    }
                });

                IEnumerable <Event> GetNextEvents()
                {
                    for (int i = packet.EventsWritten; i < packet.PendingEvents.Count; i++)
                    {
                        yield return(packet.PendingEvents[i]);
                    }
                }
            }
        }
コード例 #2
0
 public static void Encode <T>(this BitBuffer buffer, List <T> pendingList, List <T> sentList, int maxTotalSize, int maxIndividualSize, Action <T> encode) where T : IPoolable <T>
 {
     buffer.PackToSize(maxTotalSize, maxIndividualSize, pendingList, encode, (val) => sentList.Add(val));
 }