コード例 #1
0
ファイル: MmoActor.cs プロジェクト: azanium/PopBloop-Unity
        /// <summary>
        ///   Subscribes an item.
        /// </summary>
        /// <param name = "item">
        ///   The mmo item.
        /// </param>
        /// <param name = "operation">
        ///   The operation.
        /// </param>
        /// <param name = "interestArea">
        ///   The interestArea.
        /// </param>
        /// <returns>
        ///   Ok or ItemNotFound
        /// </returns>
        private OperationResponse ItemOperationSubscribeItem(Item item, SubscribeItem operation, InterestArea interestArea)
        {
            if (item.Disposed)
            {
                return operation.GetOperationResponse((int)ReturnCode.ItemNotFound, "ItemNotFound");
            }

            lock (interestArea.SyncRoot)
            {
                interestArea.SubscribeItem(item);
            }

            if (operation.PropertiesRevision.HasValue == false || operation.PropertiesRevision.Value != item.PropertiesRevision)
            {
                var properties = new ItemPropertiesSet
                    {
                       ItemId = item.Id, ItemType = item.Type, PropertiesRevision = item.PropertiesRevision, PropertiesSet = new Hashtable(item.Properties)
                    };
                var eventData = new EventData((byte)EventCode.ItemPropertiesSet, properties);
                this.Peer.SendEvent(eventData, new SendParameters { ChannelId = Settings.ItemEventChannel });
            }

            // don't send response
            operation.OnComplete();
            return null;
        }
コード例 #2
0
ファイル: MmoActor.cs プロジェクト: azanium/PopBloop-Unity
        /// <summary>
        ///   Handles operation <see cref = "SubscribeItem" />: Subscribes an existing <see cref = "InterestArea" /> to an existing <see cref = "Item" />.
        ///   The client receives event <see cref = "ItemSubscribed" /> on success.
        ///   If the submitted <see cref = "SubscribeItem.PropertiesRevision" /> is null or smaller than the item <see cref = "Item.PropertiesRevision" /> event <see cref = "ItemProperties" /> is sent to the client.
        /// </summary>
        /// <param name = "peer">
        ///   The client peer.
        /// </param>
        /// <param name = "request">
        ///   The request.
        /// </param>
        /// <param name = "sendParameters">
        ///   The send Parameters.
        /// </param>
        /// <returns>
        ///   Null or an <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.InterestAreaNotFound" /> or <see cref = "ReturnCode.ItemNotFound" />.
        /// </returns>
        public OperationResponse OperationSubscribeItem(PeerBase peer, OperationRequest request, SendParameters sendParameters)
        {
            var operation = new SubscribeItem(peer.Protocol, request);
            if (!operation.IsValid)
            {
                return new OperationResponse(request.OperationCode) { ReturnCode = (int)ReturnCode.InvalidOperationParameter, DebugMessage = operation.GetErrorMessage() };
            }

            operation.OnStart();
            IWorld world = this.World;
            InterestArea interestArea;
            if (this.TryGetInterestArea(operation.InterestAreaId, out interestArea) == false)
            {
                return operation.GetOperationResponse((int)ReturnCode.InterestAreaNotFound, "InterestAreaNotFound");
            }

            Item item;
            bool actorItem = this.TryGetItem(operation.ItemType, operation.ItemId, out item);
            if (actorItem == false)
            {
                if (world.ItemCache.TryGetItem(operation.ItemType, operation.ItemId, out item) == false)
                {
                    return operation.GetOperationResponse((int)ReturnCode.ItemNotFound, "ItemNotFound");
                }
            }

            if (actorItem)
            {
                // we are already in the item thread, invoke directly
                return this.ItemOperationSubscribeItem(item, operation, interestArea);
            }

            // second parameter (peer) allows us to send an error event to the client (in case of an error)
            item.Fiber.Enqueue(() => this.ExecItemOperation(() => this.ItemOperationSubscribeItem(item, operation, interestArea), sendParameters));

            // operation continues later
            return null;
        }