protected bool UpdateCacheSlice(HivePeer peer, byte op, int? sliceIndex, ref string message) { // get the actor who send the operation request var actor = this.GetActorByPeer(peer); if (actor == null) { return false; } switch (op) { case (byte)CacheOperation.SliceIncreaseIndex: { this.EventCache.Slice++; // notify "other" actors of change var sliceChangedEvent = new CacheSliceChanged(this.GetActorByPeer(peer).ActorNr) { Slice = this.EventCache.Slice }; this.PublishEvent(sliceChangedEvent, this.Actors, new SendParameters()); return true; } case (byte)CacheOperation.SliceSetIndex: { if (sliceIndex == null) { message = "SliceSetIndex: Missing paramter CacheSliceIndex."; return false; } if (this.EventCache.Slice != sliceIndex.Value) { this.EventCache.Slice = sliceIndex.Value; var sliceChangedEvent = new CacheSliceChanged(this.GetActorByPeer(peer).ActorNr) { Slice = this.EventCache.Slice }; this.PublishEvent(sliceChangedEvent, this.Actors, new SendParameters()); } return true; } case (byte)CacheOperation.SlicePurgeIndex: { if (sliceIndex == null) { message = "SlicePurgeIndex: Missing paramter CacheSliceIndex."; return false; } if (this.EventCache.Slice != sliceIndex.Value) { this.EventCache.RemoveSlice(sliceIndex.Value); return true; } message = string.Format("Purging of current slice={0} not allowed.", (int)sliceIndex); return false; } case (byte)CacheOperation.SlicePurgeUpToIndex: { if (sliceIndex == null) { message = "SlicePurgeUpToIndex: Missing paramter CacheSliceIndex."; return false; } if (this.EventCache.Slice > sliceIndex.Value) { this.EventCache.RemoveUpToSlice(sliceIndex.Value); return true; } message = string.Format("Purging uo to current slice={0} not allowed.", (int)sliceIndex); return false; } } message = string.Format("Unknown cache operation={0}.", op); return false; }
/// <summary> /// Sends all cached events to a peer. /// </summary> /// <param name = "litePeer"> /// The lite peer that receives the events. /// </param> /// <param name="joinRequest"></param> protected void PublishEventCache(HivePeer litePeer, JoinGameRequest joinRequest) { var @event = new CustomEvent(0, 0, null); foreach (KeyValuePair<int, EventCache> entry in this.ActorEventCache) { var actor = entry.Key; var cache = entry.Value; @event.ActorNr = actor; foreach (KeyValuePair<byte, Hashtable> eventEntry in cache) { @event.Code = @eventEntry.Key; @event.Data = @eventEntry.Value; var eventData = new EventData(@event.Code, @event); litePeer.SendEvent(eventData, new SendParameters()); } } int cacheSliceRequested = 0; if (joinRequest.CacheSlice.HasValue) { cacheSliceRequested = joinRequest.CacheSlice.Value; } foreach (var slice in this.EventCache.Slices) { if (slice >= cacheSliceRequested) { if (slice != 0) { var sliceChangedEvent = new CacheSliceChanged(0) { Slice = slice }; this.PublishEvent(sliceChangedEvent, this.GetActorByPeer(litePeer), new SendParameters()); } foreach (var customEvent in this.EventCache[slice]) { var eventData = new EventData(customEvent.Code, customEvent); litePeer.SendEvent(eventData, new SendParameters()); } } } }