public IBWControlContext LookupContext(IBWControllable bc) { lock (this.SyncRoot) { return(_contextMap[bc] as IBWControlContext); } }
public IBWControlContext LookupContext(IBWControllable bc) { lock (_contextMap.SyncRoot) { return _contextMap[bc] as IBWControlContext; } }
public void OnOOBControlMessage(IMessageComponent source, IPipe pipe, OOBControlMessage oobCtrlMsg) { if ("ConnectionConsumer".Equals(oobCtrlMsg.Target)) { if ("pendingCount".Equals(oobCtrlMsg.ServiceName)) { oobCtrlMsg.Result = this._connection.PendingMessages; } else if ("pendingVideoCount".Equals(oobCtrlMsg.ServiceName)) { IClientStream streamByChannelId = null; if (this._connection is IStreamCapableConnection) { streamByChannelId = (this._connection as IStreamCapableConnection).GetStreamByChannelId(this._video.ChannelId); } if (streamByChannelId != null) { oobCtrlMsg.Result = this._connection.GetPendingVideoMessages(streamByChannelId.StreamId); } else { oobCtrlMsg.Result = 0L; } } else if ("writeDelta".Equals(oobCtrlMsg.ServiceName)) { long num = 0L; IBWControllable parentBWControllable = this._connection as IBWControllable; while ((parentBWControllable != null) && (parentBWControllable.BandwidthConfiguration == null)) { parentBWControllable = parentBWControllable.GetParentBWControllable(); } if ((parentBWControllable != null) && (parentBWControllable.BandwidthConfiguration != null)) { IBandwidthConfigure bandwidthConfiguration = parentBWControllable.BandwidthConfiguration; if (bandwidthConfiguration is IConnectionBWConfig) { num = (bandwidthConfiguration as IConnectionBWConfig).DownstreamBandwidth / 8L; } } if (num <= 0L) { num = 0x1e000L; } oobCtrlMsg.Result = new long[] { this._connection.WrittenBytes - this._connection.ClientBytesRead, num / 2L }; } else if ("chunkSize".Equals(oobCtrlMsg.ServiceName)) { int num2 = (int)oobCtrlMsg.ServiceParameterMap["chunkSize"]; if (num2 != this._chunkSize) { this._chunkSize = num2; ChunkSize message = new ChunkSize(this._chunkSize); this._connection.GetChannel(2).Write(message); } } } }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { lock (_contextMap.SyncRoot) { if (!_contextMap.Contains(bc)) { DummyBWContext context = new DummyBWContext(bc); _contextMap.Add(bc, context); } return _contextMap[bc] as IBWControlContext; } }
private bool ProcessRequest(TokenRequest request) { IBWControllable bc = request.initialBC; while (bc != null) { BWContext context = _contextMap[bc] as BWContext; if (context == null) { RollbackRequest(request); return(false); } lock (context) { if (context.bwConfig != null) { bool result; if (request.type == TokenRequestType.BLOCKING) { result = ProcessBlockingRequest(request, context); } else if (request.type == TokenRequestType.NONBLOCKING) { result = ProcessNonblockingRequest(request, context); } else { result = ProcessBestEffortRequest(request, context); } if (!result) { // for non-blocking mode, the callback is // recorded and will be rolled back when being reset, // so we don't need to do rollback here. if (request.type != TokenRequestType.NONBLOCKING) { RollbackRequest(request); } return(false); } } TokenRequestContext requestContext = new TokenRequestContext(); requestContext.acquiredToken = request.requestToken; requestContext.bc = bc; request.acquiredStack.Push(requestContext); } bc = bc.GetParentBWControllable(); } // for best effort request, we need to rollback over-charged tokens if (request.type == TokenRequestType.BEST_EFFORT) { RollbackRequest(request); } return(true); }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { lock (_contextMap.SyncRoot) { if (!_contextMap.Contains(bc)) { DummyBWContext context = new DummyBWContext(bc); _contextMap.Add(bc, context); } return(_contextMap[bc] as IBWControlContext); } }
private void InvokeCallback(BWContext context) { // loop through all channels in a context for (int i = 0; i < 3; i++) { IList pendingList = context.pendingRequestArray[i] as IList; if (pendingList.Count > 0) { // loop through all pending requests in a channel foreach (TokenRequest request in pendingList) { IBWControllable bc = context.GetBWControllable(); while (bc != null) { BWContext c = _contextMap[bc] as BWContext; if (c == null) { // context has been unregistered, we should ignore // this callback break; } lock (c) { if (c.bwConfig != null && !ProcessNonblockingRequest(request, c)) { break; } } TokenRequestContext requestContext = new TokenRequestContext(); requestContext.acquiredToken = request.requestToken; requestContext.bc = bc; request.acquiredStack.Push(requestContext); bc = bc.GetParentBWControllable(); } if (bc == null) { // successfully got the required tokens try { request.callback.Available(context.buckets[request.channel], (long)request.requestToken); } catch (Exception ex) { log.Error("Error calling request's callback", ex); } } } pendingList.Clear(); } } }
private bool ProcessRequest(TokenRequest request) { for (IBWControllable controllable = request.initialBC; controllable != null; controllable = controllable.GetParentBWControllable()) { BWContext context = this._contextMap[controllable] as BWContext; if (context == null) { this.RollbackRequest(request); return(false); } lock (context) { if (context.bwConfig != null) { bool flag; if (request.type == TokenRequestType.BLOCKING) { flag = this.ProcessBlockingRequest(request, context); } else if (request.type == TokenRequestType.NONBLOCKING) { flag = this.ProcessNonblockingRequest(request, context); } else { flag = this.ProcessBestEffortRequest(request, context); } if (!flag) { if (request.type != TokenRequestType.NONBLOCKING) { this.RollbackRequest(request); } return(false); } } TokenRequestContext context2 = new TokenRequestContext { acquiredToken = request.requestToken, bc = controllable }; request.acquiredStack.Push(context2); } } if (request.type == TokenRequestType.BEST_EFFORT) { this.RollbackRequest(request); } return(true); }
private void InvokeCallback(BWContext context) { for (int i = 0; i < 3; i++) { IList list = context.pendingRequestArray[i] as IList; if (list.Count > 0) { foreach (TokenRequest request in list) { IBWControllable bWControllable = context.GetBWControllable(); while (bWControllable != null) { BWContext context2 = this._contextMap[bWControllable] as BWContext; if (context2 == null) { break; } lock (context2) { if (!((context2.bwConfig == null) || this.ProcessNonblockingRequest(request, context2))) { break; } } TokenRequestContext context3 = new TokenRequestContext { acquiredToken = request.requestToken, bc = bWControllable }; request.acquiredStack.Push(context3); bWControllable = bWControllable.GetParentBWControllable(); } if (bWControllable == null) { try { request.callback.Available(context.buckets[request.channel], (long)request.requestToken); } catch (Exception exception) { log.Error("Error calling request's callback", exception); } } } list.Clear(); } } }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { BWContext context = new BWContext(bc); long[] channelInitialBurst = null; if (bc.BandwidthConfiguration != null) { context.bwConfig = new long[4]; for (int i = 0; i < 4; i++) { context.bwConfig[i] = bc.BandwidthConfiguration.GetChannelBandwidth()[i]; } channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); } context.buckets[0] = new Bucket(this, bc, 0); context.buckets[1] = new Bucket(this, bc, 1); context.buckets[2] = new Bucket(this, bc, 2); context.tokenRc = new double[4]; if (context.bwConfig != null) { // Set the initial value to token resources as "defaultCapacity/2" for (int i = 0; i < 4; i++) { if (channelInitialBurst[i] >= 0) { context.tokenRc[i] = channelInitialBurst[i]; } else { context.tokenRc[i] = _defaultCapacity / 2; } } context.lastSchedule = System.Environment.TickCount; } else { context.lastSchedule = -1; } lock (this.SyncRoot) { _contextMap.Add(bc, context); } return(context); }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { int num; BWContext context = new BWContext(bc); long[] channelInitialBurst = null; if (bc.BandwidthConfiguration != null) { context.bwConfig = new long[4]; for (num = 0; num < 4; num++) { context.bwConfig[num] = bc.BandwidthConfiguration.GetChannelBandwidth()[num]; } channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); } context.buckets[0] = new Bucket(this, bc, 0); context.buckets[1] = new Bucket(this, bc, 1); context.buckets[2] = new Bucket(this, bc, 2); context.tokenRc = new double[4]; if (context.bwConfig != null) { for (num = 0; num < 4; num++) { if (channelInitialBurst[num] >= 0L) { context.tokenRc[num] = channelInitialBurst[num]; } else { context.tokenRc[num] = this._defaultCapacity / 2L; } } context.lastSchedule = Environment.TickCount; } else { context.lastSchedule = -1L; } lock (this.SyncRoot) { this._contextMap.Add(bc, context); } return(context); }
public void Reset() { // TODO wake up all blocked threads IBWControllable bc = _bc; while (bc != null) { BWContext context = _simpleBWControlService.ContextMap[bc] as BWContext; if (context == null) { break; } lock (context) { IList pendingList = context.pendingRequestArray[_channel] as IList; TokenRequest toRemove = null; foreach (TokenRequest request in pendingList) { if (request.initialBC == _bc) { _simpleBWControlService.RollbackRequest(request); toRemove = request; break; } } if (toRemove != null) { pendingList.Remove(toRemove); try { toRemove.callback.Reset(this, (long)toRemove.requestToken); } catch (Exception ex) { log.Error("Error reset request's callback", ex); } break; } } bc = bc.GetParentBWControllable(); } }
public void Reset() { for (IBWControllable controllable = this._bc; controllable != null; controllable = controllable.GetParentBWControllable()) { SimpleBWControlService.BWContext context = this._simpleBWControlService.ContextMap[controllable] as SimpleBWControlService.BWContext; if (context == null) { break; } lock (context) { IList list = context.pendingRequestArray[this._channel] as IList; SimpleBWControlService.TokenRequest request = null; foreach (SimpleBWControlService.TokenRequest request2 in list) { if (request2.initialBC == this._bc) { this._simpleBWControlService.RollbackRequest(request2); request = request2; break; } } if (request != null) { list.Remove(request); try { request.callback.Reset(this, (long)request.requestToken); } catch (Exception exception) { log.Error("Error reset request's callback", exception); } break; } } } }
public void UpdateBWConfigure(IBWControlContext context) { if (!(context is BWContext)) { return; } BWContext c = (BWContext)context; IBWControllable bc = c.GetBWControllable(); lock (c) { if (bc.BandwidthConfiguration == null) { c.bwConfig = null; c.lastSchedule = -1; } else { long[] oldConfig = c.bwConfig; c.bwConfig = new long[4]; for (int i = 0; i < 4; i++) { c.bwConfig[i] = bc.BandwidthConfiguration.GetChannelBandwidth()[i]; } if (oldConfig == null) { // Initialize the last schedule timestamp if necessary c.lastSchedule = System.Environment.TickCount; long[] channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); // Set the initial value to token resources as "defaultCapacity/2" for (int i = 0; i < 4; i++) { if (channelInitialBurst[i] >= 0) { c.tokenRc[i] = channelInitialBurst[i]; } else { c.tokenRc[i] = _defaultCapacity / 2; } } } else { // we have scheduled before, so migration of token is needed if (c.bwConfig[Constants.OverallChannel] >= 0 && oldConfig[Constants.OverallChannel] < 0) { c.tokenRc[Constants.OverallChannel] += c.tokenRc[Constants.AudioChannel] + c.tokenRc[Constants.VideoChannel] + c.tokenRc[Constants.DataChannel]; for (int i = 0; i < 3; i++) { c.tokenRc[i] = 0; } } else if (c.bwConfig[Constants.OverallChannel] < 0 && oldConfig[Constants.OverallChannel] >= 0) { for (int i = 0; i < 3; i++) { if (c.bwConfig[i] >= 0) { c.tokenRc[i] += c.tokenRc[Constants.OverallChannel]; break; } } c.tokenRc[Constants.OverallChannel] = 0; } } } } }
public BWContext(IBWControllable controllable) { _controllable = controllable; pendingRequestArray = new ArrayList(); pendingRequestArray.AddRange(new IList[] { new CopyOnWriteArray(), new CopyOnWriteArray(), new CopyOnWriteArray() }); }
public Bucket(SimpleBWControlService simpleBWControlService, IBWControllable bc, int channel) { _bc = bc; _channel = channel; _simpleBWControlService = simpleBWControlService; }
public IBWControlContext RegisterBWControllable(IBWControllable bc) { BWContext context = new BWContext(bc); long[] channelInitialBurst = null; if (bc.BandwidthConfiguration != null) { context.bwConfig = new long[4]; for (int i = 0; i < 4; i++) { context.bwConfig[i] = bc.BandwidthConfiguration.GetChannelBandwidth()[i]; } channelInitialBurst = bc.BandwidthConfiguration.GetChannelInitialBurst(); } context.buckets[0] = new Bucket(this, bc, 0); context.buckets[1] = new Bucket(this, bc, 1); context.buckets[2] = new Bucket(this, bc, 2); context.tokenRc = new double[4]; if (context.bwConfig != null) { // Set the initial value to token resources as "defaultCapacity/2" for (int i = 0; i < 4; i++) { if (channelInitialBurst[i] >= 0) { context.tokenRc[i] = channelInitialBurst[i]; } else { context.tokenRc[i] = _defaultCapacity / 2; } } context.lastSchedule = System.Environment.TickCount; } else { context.lastSchedule = -1; } lock (this.SyncRoot) { _contextMap.Add(bc, context); } return context; }
public DummyBWContext(IBWControllable controllable) { _controllable = controllable; }
public void OnOOBControlMessage(IMessageComponent source, IPipe pipe, OOBControlMessage oobCtrlMsg) { if (!"ConnectionConsumer".Equals(oobCtrlMsg.Target)) { return; } if ("pendingCount".Equals(oobCtrlMsg.ServiceName)) { oobCtrlMsg.Result = _connection.PendingMessages; } else if ("pendingVideoCount".Equals(oobCtrlMsg.ServiceName)) { IClientStream stream = null; if (_connection is IStreamCapableConnection) { stream = (_connection as IStreamCapableConnection).GetStreamByChannelId(_video.ChannelId); } if (stream != null) { oobCtrlMsg.Result = _connection.GetPendingVideoMessages(stream.StreamId); } else { oobCtrlMsg.Result = (long)0; } } else if ("writeDelta".Equals(oobCtrlMsg.ServiceName)) { long maxStream = 0; IBWControllable bwControllable = _connection as IBWControllable; // Search FC containing valid BWC while (bwControllable != null && bwControllable.BandwidthConfiguration == null) { bwControllable = bwControllable.GetParentBWControllable(); } if (bwControllable != null && bwControllable.BandwidthConfiguration != null) { IBandwidthConfigure bwc = bwControllable.BandwidthConfiguration; if (bwc is IConnectionBWConfig) { maxStream = (bwc as IConnectionBWConfig).DownstreamBandwidth / 8; } } if (maxStream <= 0) { // Use default value // TODO: this should be configured somewhere and sent to the client when connecting maxStream = 120 * 1024; } // Return the current delta between sent bytes and bytes the client // reported to have received, and the interval the client should use // for generating BytesRead messages (half of the allowed bandwidth). oobCtrlMsg.Result = new long[] { _connection.WrittenBytes - _connection.ClientBytesRead, maxStream / 2 }; } else if ("chunkSize".Equals(oobCtrlMsg.ServiceName)) { int newSize = (int)oobCtrlMsg.ServiceParameterMap["chunkSize"]; if (newSize != _chunkSize) { _chunkSize = newSize; SendChunkSize(); } } }
public void UpdateBWConfigure(IBWControlContext context) { if (context is BWContext) { BWContext context2 = (BWContext)context; IBWControllable bWControllable = context2.GetBWControllable(); lock (context2) { if (bWControllable.BandwidthConfiguration == null) { context2.bwConfig = null; context2.lastSchedule = -1L; } else { int num; long[] bwConfig = context2.bwConfig; context2.bwConfig = new long[4]; for (num = 0; num < 4; num++) { context2.bwConfig[num] = bWControllable.BandwidthConfiguration.GetChannelBandwidth()[num]; } if (bwConfig == null) { context2.lastSchedule = Environment.TickCount; long[] channelInitialBurst = bWControllable.BandwidthConfiguration.GetChannelInitialBurst(); for (num = 0; num < 4; num++) { if (channelInitialBurst[num] >= 0L) { context2.tokenRc[num] = channelInitialBurst[num]; } else { context2.tokenRc[num] = this._defaultCapacity / 2L; } } } else if ((context2.bwConfig[3] >= 0L) && (bwConfig[3] < 0L)) { context2.tokenRc[3] += (context2.tokenRc[0] + context2.tokenRc[1]) + context2.tokenRc[2]; for (num = 0; num < 3; num++) { context2.tokenRc[num] = 0.0; } } else if ((context2.bwConfig[3] < 0L) && (bwConfig[3] >= 0L)) { for (num = 0; num < 3; num++) { if (context2.bwConfig[num] >= 0L) { context2.tokenRc[num] += context2.tokenRc[3]; break; } } context2.tokenRc[3] = 0.0; } } } } }