/// <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> internal static RailEvent Decode( RailBitBuffer buffer, Tick packetTick) { // Read: [EventType] int factoryType = buffer.ReadInt(RailEvent.FactoryTypeCompressor); RailEvent evnt = RailEvent.Create(factoryType); // Read: [EventId] evnt.EventId = buffer.ReadSequenceId(); // Read: [HasEntityId] bool hasEntityId = buffer.ReadBool(); if (hasEntityId) { // Read: [EntityId] evnt.EntityId = buffer.ReadEntityId(); } // Read: [EventData] evnt.DecodeData(buffer, packetTick); return(evnt); }
/// <summary> /// Overwrites this buffer with an array of byte data. /// </summary> public void Load(byte[] data, int length) { int numChunks = (length / 4) + 1; if (this.chunks.Length < numChunks) { this.chunks = new uint[numChunks]; } for (int i = 0; i < numChunks; i++) { int dataIdx = i * 4; uint chunk = (uint)data[dataIdx] | (uint)data[dataIdx + 1] << 8 | (uint)data[dataIdx + 2] << 16 | (uint)data[dataIdx + 3] << 24; this.chunks[i] = chunk; } int positionInByte = RailBitBuffer.FindHighestBitPosition(data[length - 1]); // Take one off the position to backtrack from the sentinel bit this.writePos = ((length - 1) * 8) + (positionInByte - 1); this.readPos = 0; }
private void DecodeDeltas(RailBitBuffer buffer) { this.deltas.Decode( buffer, () => RailState.DecodeDelta(buffer, this.SenderTick)); #endif }
protected void EncodeCommands(RailBitBuffer buffer) { this.commandUpdates.Encode( buffer, RailConfig.PACKCAP_COMMANDS, RailConfig.MAXSIZE_COMMANDUPDATE, (commandUpdate) => commandUpdate.Encode(buffer)); }
protected override void EncodePayload( RailBitBuffer buffer, int reservedBytes) { #if SERVER // Write: [Deltas] this.EncodeDeltas(buffer, reservedBytes); }
protected override void DecodePayload(RailBitBuffer buffer) { #if SERVER // Read: [Commands] this.DecodeCommands(buffer); // Read: [View] this.DecodeView(buffer); }
private void DecodeHeader(RailBitBuffer buffer) { // Read: [LocalTick] this.senderTick = buffer.ReadTick(); // Read: [AckTick] this.ackTick = buffer.ReadTick(); // Read: [AckReliableEventId] this.ackEventId = buffer.ReadSequenceId(); }
private void EncodeHeader(RailBitBuffer buffer) { // Write: [LocalTick] buffer.WriteTick(this.senderTick); // Write: [AckTick] buffer.WriteTick(this.ackTick); // Write: [AckReliableEventId] buffer.WriteSequenceId(this.ackEventId); }
protected override void EncodePayload( RailBitBuffer buffer, int reservedBytes) { #if CLIENT // Write: [Commands] this.EncodeCommands(buffer); // Write: [View] this.EncodeView(buffer, reservedBytes); }
public void Decode( RailBitBuffer buffer, Func <T> decode) { IEnumerable <T> decoded = buffer.UnpackAll(decode); foreach (T delta in decoded) { this.received.Add(delta); } }
private void EncodeDeltas( RailBitBuffer buffer, int reservedBytes) { this.deltas.Encode( buffer, RailConfig.PACKCAP_MESSAGE_TOTAL - reservedBytes, RailConfig.MAXSIZE_ENTITY, (delta) => RailState.EncodeDelta(buffer, delta)); #endif }
internal static FixedByteBuffer8 Read(RailBitBuffer buffer) { uint first = 0; uint second = 0; first = buffer.ReadUInt(); if (buffer.ReadBool()) { second = buffer.ReadUInt(); } return(new FixedByteBuffer8(first, second)); }
private void DecodeEvents( RailBitBuffer buffer) { IEnumerable <RailEvent> decoded = buffer.UnpackAll( () => RailEvent.Decode(buffer, this.SenderTick)); foreach (RailEvent evnt in decoded) { this.events.Add(evnt); } }
/// <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> private void EncodeEvents( RailBitBuffer buffer, int maxSize) { this.eventsWritten += buffer.PackToSize( maxSize, RailConfig.MAXSIZE_EVENT, this.GetNextEvents(), (evnt) => evnt.Encode(buffer, this.senderTick), (evnt) => evnt.RegisterSent()); }
public void Encode( RailBitBuffer buffer, int maxTotalSize, int maxIndividualSize, Action <T> encode) { buffer.PackToSize( maxTotalSize, maxIndividualSize, this.pending, encode, (val) => this.sent.Add(val)); }
internal void Decode(RailBitBuffer buffer) { // Read: [Header] this.DecodeHeader(buffer); // Read: [Events] (Early Pack) this.DecodeEvents(buffer); // Read: [Payload] this.DecodePayload(buffer); // Read: [Events] (Fill Pack) this.DecodeEvents(buffer); }
/// <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 void Encode(RailBitBuffer buffer) { // Write: [Header] this.EncodeHeader(buffer); // Write: [Events] (Early Pack) this.EncodeEvents(buffer, RailConfig.PACKCAP_EARLY_EVENTS); // Write: [Payload] this.EncodePayload(buffer, 1); // Leave one byte for the event count // Write: [Events] (Fill Pack) this.EncodeEvents(buffer, RailConfig.PACKCAP_MESSAGE_TOTAL); }
internal void Encode(RailBitBuffer buffer) { // Write: [EntityId] buffer.WriteEntityId(this.entityId); // Write: [Count] buffer.Write(BUFFER_COUNT_BITS, (uint)this.commands.Count); // Write: [Commands] foreach (RailCommand command in this.commands.GetValues()) { command.Encode(buffer); } }
protected void EncodeView(RailBitBuffer buffer, int reservedBytes) { buffer.PackToSize( RailConfig.PACKCAP_MESSAGE_TOTAL - reservedBytes, int.MaxValue, this.view.GetOrdered(), (pair) => { buffer.WriteEntityId(pair.Key); // Write: [EntityId] buffer.WriteTick(pair.Value.Tick); // Write: [Tick] buffer.WriteBool(pair.Value.IsFrozen); // Write: [IsFrozen] }); #endif }
internal void Write(RailBitBuffer buffer) { uint first = FixedByteBuffer8.Pack(this.val0, this.val1, this.val2, this.val3); uint second = FixedByteBuffer8.Pack(this.val4, this.val5, this.val6, this.val7); bool writeSecond = (second > 0); buffer.WriteUInt(first); buffer.WriteBool(writeSecond); if (writeSecond) { buffer.WriteUInt(second); } }
protected void OnPayloadReceived(IRailNetPeer peer, byte[] buffer, int length) { RailBitBuffer bitBuffer = this.interpreter.LoadData(buffer, length); this.reusableIncoming.Decode(bitBuffer); if (bitBuffer.IsFinished) { this.ProcessPacket(this.reusableIncoming); } else { RailDebug.LogError("Bad packet read, discarding..."); } this.reusableIncoming.Reset(); }
internal static RailCommandUpdate Decode(RailBitBuffer buffer) { RailCommandUpdate update = RailResource.Instance.CreateCommandUpdate(); // Read: [EntityId] update.entityId = buffer.ReadEntityId(); // Read: [Count] int count = (int)buffer.Read(BUFFER_COUNT_BITS); // Read: [Commands] for (int i = 0; i < count; i++) { update.commands.Store(RailCommand.Decode(buffer)); } return(update); }
public void DecodeView(RailBitBuffer buffer) { IEnumerable <KeyValuePair <EntityId, RailViewEntry> > decoded = buffer.UnpackAll( () => { return(new KeyValuePair <EntityId, RailViewEntry>( buffer.ReadEntityId(), // Read: [EntityId] new RailViewEntry( buffer.ReadTick(), // Read: [Tick] buffer.ReadBool()))); // Read: [IsFrozen] }); foreach (var pair in decoded) { this.view.RecordUpdate(pair.Key, pair.Value); } #endif }
public void WriteString(string value) { if (value == null) { throw new ArgumentNullException("value"); } uint length = (uint)value.Length; RailDebug.Assert(length <= RailConfig.STRING_LENGTH_MAX, value); if (value.Length > RailConfig.STRING_LENGTH_MAX) { length = RailConfig.STRING_LENGTH_MAX; } this.Write(RailBitBuffer.STRING_LENGTH_BITS, length); for (int i = 0; i < length; i++) { this.Write(RailBitBuffer.ASCII_BITS, RailBitBuffer.ToASCII(value[i])); } }
/// <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> internal void Encode( RailBitBuffer buffer, Tick packetTick) { // Write: [EventType] buffer.WriteInt(RailEvent.FactoryTypeCompressor, this.factoryType); // Write: [EventId] buffer.WriteSequenceId(this.EventId); // Write: [HasEntityId] buffer.WriteBool(this.EntityId.IsValid); if (this.EntityId.IsValid) { // Write: [EntityId] buffer.WriteEntityId(this.EntityId); } // Write: [EventData] this.EncodeData(buffer, packetTick); }
protected void DecodeCommands(RailBitBuffer buffer) { this.commandUpdates.Decode( buffer, () => RailCommandUpdate.Decode(buffer)); }
public static EntityId PeekEntityId(this RailBitBuffer buffer) { return(EntityId.Peek(buffer)); }
internal static EntityId Peek(RailBitBuffer buffer) { return(new EntityId(buffer.PeekUInt())); }
internal static EntityId Read(RailBitBuffer buffer) { return(new EntityId(buffer.ReadUInt())); }
internal void Write(RailBitBuffer buffer) { buffer.WriteUInt(this.idValue); }