public EntityProxyEnvelope CreateEnvelope() { EntityProxyEnvelope env = EntityProxyEnvelopePool.Acquire(); env.Proxy = this; env.Flags = this.Flags; env.ControlTokenLost = this.ControlTokenLost; env.ControlTokenGained = this.ControlTokenGained; return(env); }
private void ApplyPropertyPriorities(EntityProxyEnvelope env) { for (int i = 0; i < env.Written.Count; ++i) { Priority p = env.Written[i]; // set flag for sending this property again env.Proxy.Set(p.PropertyIndex); // increment priority env.Proxy.PropertyPriority[p.PropertyIndex].PropertyPriority += p.PropertyPriority; } }
public static EntityProxyEnvelope Acquire() { EntityProxyEnvelope obj; if (Pool.Count > 0) { obj = Pool.Pop(); } else { obj = new EntityProxyEnvelope(); } return(obj); }
int IEntitySerializer.Pack(Connection connection, Packet stream, EntityProxyEnvelope env) { int propertyCount = 0; BitSet filter = ((IEntitySerializer)this).GetFilter(connection, env.Proxy); Priority[] tempPriority = Meta.PropertiesTempPriority; Priority[] proxyPriority = env.Proxy.PropertyPriority; for (int i = 0; i < proxyPriority.Length; ++i) { NetAssert.True(proxyPriority[i].PropertyIndex == i); // if this property is set both in our filter and the proxy mask we can consider it for sending if (filter.IsSet(i) && env.Proxy.IsSet(i)) { // increment priority for this property proxyPriority[i].PropertyPriority += Meta.Properties[i].Property.PropertyPriority; proxyPriority[i].PropertyPriority = UE.Mathf.Clamp(proxyPriority[i].PropertyPriority, 0, Core.Config.maxPropertyPriority); // copy to our temp array tempPriority[propertyCount] = proxyPriority[i]; // increment temp count propertyCount += 1; } } // sort temp array based on priority Array.Sort <Priority>(tempPriority, 0, propertyCount, Priority.Comparer.Instance); // write into stream PackProperties(connection, stream, env, tempPriority, propertyCount); for (int i = 0; i < env.Written.Count; ++i) { Priority p = env.Written[i]; // clear priority for written property env.Proxy.PropertyPriority[p.PropertyIndex].PropertyPriority = 0; // clear mask for it env.Proxy.Clear(p.PropertyIndex); } return(env.Written.Count); }
public static void Release(EntityProxyEnvelope obj) { Pool.Push(obj); }
private int PackUpdate(Packet packet, EntityProxy proxy) { int pos = packet.Position; int packCount = 0; EntityProxyEnvelope env = proxy.CreateEnvelope(); packet.WriteBool(true); packet.WriteNetworkId(proxy.NetworkId); if (packet.WriteBool(proxy.Entity.IsController(connection))) { packet.WriteToken(proxy.ControlTokenGained); proxy.ControlTokenLost = null; } else { packet.WriteToken(proxy.ControlTokenLost); proxy.ControlTokenGained = null; } if (packet.WriteBool(proxy.Flags & ProxyFlags.DESTROY_REQUESTED)) { packet.WriteToken(proxy.Entity.DetachToken); } else { // data for first packet if (packet.WriteBool(proxy.Flags & ProxyFlags.CREATE_REQUESTED)) { packet.WriteToken(proxy.Entity.AttachToken); packet.WritePrefabId(proxy.Entity.PrefabId); packet.WriteTypeId(proxy.Entity.Serializer.TypeId); packet.WriteVector3(proxy.Entity.UnityObject.transform.position); packet.WriteQuaternion(proxy.Entity.UnityObject.transform.rotation); if (packet.WriteBool(proxy.Entity.IsSceneObject)) { NetAssert.False(proxy.Entity.SceneId.IsNone, string.Format("'{0}' is marked a scene object but has no scene id ", proxy.Entity.UnityObject.gameObject)); packet.WriteUniqueId(proxy.Entity.SceneId); } } packCount = proxy.Entity.Serializer.Pack(connection, packet, env); } if (packet.Overflowing) { packet.Position = pos; return(-1); } if (packCount == -1) { packet.Position = pos; return(0); } else { var isForce = proxy.Flags & ProxyFlags.FORCE_SYNC; var isCreate = proxy.Flags & ProxyFlags.CREATE_REQUESTED; var isDestroy = proxy.Flags & ProxyFlags.DESTROY_REQUESTED; // if we didn't pack anything and we are not creating or destroying this, just goto next if ((packCount == 0) && !isCreate && !isDestroy && !isForce) { packet.Position = pos; return(0); } // set in progress flags if (isDestroy) { env.Flags = (proxy.Flags |= ProxyFlags.DESTROY_PENDING); } // clear force sync flag proxy.Flags &= ~ProxyFlags.FORCE_SYNC; // clear skipped count proxy.Skipped = 0; // set packet number env.PacketNumber = packet.Number; // put on packets list packet.EntityUpdates.Enqueue(env); // put on proxies pending queue // NetLog.Info("adding envelope to {0}, count: {1}", proxy, proxy.Envelopes.Count + 1); proxy.Envelopes.Enqueue(env); // keep going! return(1); } }
void PackProperties(Connection connection, Packet packet, EntityProxyEnvelope env, Priority[] priority, int count) { int propertyCountPtr = packet.Position; packet.WriteByte(0, Meta.PacketMaxPropertiesBits); // how many bits can we write at the most int bits = System.Math.Min(Meta.PacketMaxBits, packet.Size - packet.Position); for (int i = 0; i < count; ++i) { // this means we can even fit another property id if (bits <= Meta.PropertyIdBits) { break; } // we have written enough properties if (env.Written.Count == Meta.PacketMaxProperties) { break; } Priority p = priority[i]; NetworkPropertyInfo pi = Meta.Properties[p.PropertyIndex]; if (p.PropertyPriority == 0) { break; } int b = Meta.PropertyIdBits + pi.Property.BitCount(Objects[pi.OffsetObjects]); int ptr = packet.Position; if (bits >= b) { // write property id packet.WriteInt(p.PropertyIndex, Meta.PropertyIdBits); if (pi.Property.Write(connection, Objects[pi.OffsetObjects], Storage, packet)) { #if DEBUG int totalBits = packet.Position - ptr; if (totalBits != b) { //NetLog.Warn("Property of type {0} did not write the correct amount of bits, written: {1}, expected: {2}", pi.Property, totalBits, b); } #endif if (packet.Overflowing) { packet.Position = ptr; break; } // use up bits bits -= b; // add to written list env.Written.Add(p); } else { // reset position packet.Position = ptr; } } } // gotta be less then 256 NetAssert.True(env.Written.Count <= Meta.PacketMaxProperties); // write the amount of properties Packet.WriteByteAt(packet.ByteBuffer, propertyCountPtr, Meta.PacketMaxPropertiesBits, (byte)env.Written.Count); }