Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new <see cref="DataReader" /> with the desired QoS policies and attaches to it the specified <see cref="DataReaderListener" />.
        /// </summary>
        /// <remarks>
        /// <para>The returned <see cref="DataReader" /> will be attached and belong to the <see cref="Subscriber" />.</para>
        /// <para>The <see cref="ITopicDescription"/> passed to this operation must have been created from the same <see cref="DomainParticipant" /> that was used to
        /// create this <see cref="Subscriber" />. If the <see cref="ITopicDescription"/> was created from a different <see cref="DomainParticipant" />, the operation will fail and
        /// return a <see langword="null"/> result.</para>
        /// </remarks>
        /// <param name="topicDescription">The <see cref="ITopicDescription" /> that the <see cref="DataReader" /> will be associated with.</param>
        /// <param name="qos">The <see cref="DataReaderQos" /> policies to be used for creating the new <see cref="DataReader" />.</param>
        /// <param name="listener">The <see cref="DataReaderListener" /> to be attached to the newly created <see cref="DataReader" />.</param>
        /// <param name="statusMask">The <see cref="StatusMask" /> of which status changes the listener should be notified.</param>
        /// <returns>The newly created <see cref="DataReader" /> on success, otherwise <see langword="null"/>.</returns>
        public DataReader CreateDataReader(ITopicDescription topicDescription, DataReaderQos qos, DataReaderListener listener, StatusMask statusMask)
        {
            if (topicDescription is null)
            {
                throw new ArgumentNullException(nameof(topicDescription));
            }

            DataReaderQosWrapper qosWrapper = default;

            if (qos is null)
            {
                qos = new DataReaderQos();
                var ret = GetDefaultDataReaderQos(qos);
                if (ret == ReturnCode.Ok)
                {
                    qosWrapper = qos.ToNative();
                }
            }
            else
            {
                qosWrapper = qos.ToNative();
            }

            IntPtr nativeListener = IntPtr.Zero;

            if (listener != null)
            {
                nativeListener = listener.ToNative();
            }

            IntPtr td     = topicDescription.ToNativeTopicDescription();
            IntPtr native = UnsafeNativeMethods.CreateDataReader(_native, td, qosWrapper, nativeListener, statusMask);

            qos.Release();

            if (native.Equals(IntPtr.Zero))
            {
                return(null);
            }

            var dr = new DataReader(native)
            {
                Listener = listener,
            };

            EntityManager.Instance.Add((dr as Entity).ToNative(), dr);
            ContainedEntities.Add(dr);

            return(dr);
        }
        public override void HandleComponentState(dynamic state)
        {
            var theState     = state as InventoryComponentState;
            var stateChanged = false;

            if (MaxSlots != theState.MaxSlots)
            {
                MaxSlots     = theState.MaxSlots;
                stateChanged = true;
            }

            var newEntities = new List <int>(theState.ContainedEntities);
            var toRemove    = new List <IEntity>();

            foreach (var e in ContainedEntities)
            {
                if (newEntities.Contains(e.Uid))
                {
                    newEntities.Remove(e.Uid);
                }
                else
                {
                    toRemove.Add(e);
                }
            }
            stateChanged = stateChanged || toRemove.Any() || newEntities.Any();
            foreach (var e in toRemove)
            {
                ContainedEntities.Remove(e);
            }

            foreach (var uid in newEntities)
            {
                ContainedEntities.Add(Owner.EntityManager.GetEntity(uid));
            }

            if (stateChanged && Changed != null)
            {
                Changed(this, MaxSlots, ContainedEntities);
            }
        }
Ejemplo n.º 3
0
        private void UnpackListing(IncomingEntityComponentMessage msg)
        {
            MaxSlots = (int)msg.MessageParameters[1];

            ContainedEntities.Clear();

            for (int i = 0; i < (int)msg.MessageParameters[2]; i++)
            {
                int    msgPos = 3 + i;
                Entity entity = Owner.EntityManager.GetEntity((int)msg.MessageParameters[msgPos]);
                if (entity != null)
                {
                    ContainedEntities.Add(entity);
                }
            }

            if (Changed != null)
            {
                Changed(this, MaxSlots, ContainedEntities);
            }
        }