/// <summary> /// Handles operation <see cref = "AttachInterestArea" />: Attaches an existing <see cref = "InterestArea" /> to an existing <see cref = "Item" />. /// </summary> /// <param name = "peer"> /// The client peer. /// </param> /// <param name = "request"> /// The request. /// </param> /// <param name = "sendParameters"> /// The send Parameters. /// </param> /// <returns> /// An <see cref = "OperationResponse" /> with error code <see cref = "ReturnCode.Ok" />, <see cref = "ReturnCode.InterestAreaNotFound" /> or <see cref = "ReturnCode.ItemNotFound" />. /// </returns> public OperationResponse OperationAttachInterestArea(PeerBase peer, OperationRequest request, SendParameters sendParameters) { var operation = new AttachInterestArea(peer.Protocol, request); if (!operation.IsValid) { return new OperationResponse(request.OperationCode) { ReturnCode = (int)ReturnCode.InvalidOperationParameter, DebugMessage = operation.GetErrorMessage() }; } operation.OnStart(); InterestArea interestArea; if (this.TryGetInterestArea(operation.InterestAreaId, out interestArea) == false) { return operation.GetOperationResponse((int)ReturnCode.InterestAreaNotFound, "InterestAreaNotFound"); } Item item; bool actorItem; if (false == operation.ItemType.HasValue || string.IsNullOrEmpty(operation.ItemId)) { item = this.Avatar; actorItem = true; // set return vaues operation.ItemId = item.Id; operation.ItemType = item.Type; } else { IWorld world = this.World; actorItem = this.TryGetItem(operation.ItemType.Value, operation.ItemId, out item); if (actorItem == false) { if (world.ItemCache.TryGetItem(operation.ItemType.Value, 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.ItemOperationAttachInterestArea(item, operation, interestArea, sendParameters); } // second parameter (peer) allows us to send an error event to the client (in case of an error) item.Fiber.Enqueue( () => this.ExecItemOperation(() => this.ItemOperationAttachInterestArea(item, operation, interestArea, sendParameters), sendParameters)); // response is sent later return null; }
/// <summary> /// The operation attach interest area. /// </summary> /// <param name = "item"> /// The mmo item. /// </param> /// <param name = "operation"> /// The operation. /// </param> /// <param name = "interestArea"> /// The interestArea. /// </param> /// <param name = "sendParameters"> /// The send Parameters. /// </param> /// <returns> /// Ok or ItemNotFound /// </returns> private OperationResponse ItemOperationAttachInterestArea( Item item, AttachInterestArea operation, InterestArea interestArea, SendParameters sendParameters) { if (item.Disposed) { return operation.GetOperationResponse((int)ReturnCode.ItemNotFound, "ItemNotFound"); } lock (interestArea.SyncRoot) { interestArea.Detach(); interestArea.AttachToItem(item); interestArea.UpdateInterestManagement(); } // use item channel to ensure that this event arrives before any move or subscribe events OperationResponse response = operation.GetOperationResponse(MethodReturnValue.Ok); sendParameters.ChannelId = Settings.ItemEventChannel; this.Peer.SendOperationResponse(response, sendParameters); operation.OnComplete(); return null; }