protected override long reserveEarliestAvailable(int requiredPermits, long nowMicros) { //重新计算桶内令牌数storedPermits lock (mutex()) { resync(nowMicros); } lock (mutex()) { long returnValue = nextFreeTicketMicros; //下一次请求可以获取令牌的起始时间 //本次消耗的令牌数 double storedPermitsToSpend = Math.Min(requiredPermits, this.storedPermits); //本次消耗的令牌数=min(申请令牌数,当前存储令牌数) #region 预支逻辑 //重新计算下次可获取时间nextFreeTicketMicros double freshPermits = requiredPermits - storedPermitsToSpend; //缺少令牌数=申请令牌数-本次可以消耗的令牌数 long waitMicros = storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend) + (long)(freshPermits * stableIntervalMicros); //缺少令牌数*生成令牌的时间间隔(提前预支) this.nextFreeTicketMicros = LongHelper.SaturatedAdd(nextFreeTicketMicros, waitMicros); //防止溢出 下一次分配时间+预支分配令牌的时间 #endregion //Console.WriteLine("nextFreeTicketMicros:" + this.nextFreeTicketMicros+"_"+ waitMicros); this.storedPermits -= storedPermitsToSpend; return(returnValue); } }
public void UnsignedRightBitMoveTest() { //右无符号位移操作 Assert.IsTrue(LongHelper.UnsignedRightBitMove(-4611686018427387904, 32) == 3221225472); Assert.IsTrue(LongHelper.UnsignedRightBitMove(100, 3) == 12); Assert.IsTrue(LongHelper.UnsignedRightBitMove(-4, 32) == 4294967295); Assert.IsTrue(LongHelper.UnsignedRightBitMove(-9223372036854775804, 63) == 1); }
public void BitCountTest() { Assert.IsTrue(LongHelper.BitCount(32L) == 1); Assert.IsTrue(LongHelper.BitCount(576460752303423520L) == 2); Assert.IsTrue(LongHelper.BitCount(1072) == 3); Assert.IsTrue(LongHelper.BitCount(1080) == 4); Assert.IsTrue(LongHelper.BitCount(-1024) == 54); }
/// <summary>Gets the User Version PRAGMA of the database.</summary> /// <returns>User Version PRAGMA</returns> public async Task <long> LoadUserVersion() { DataSet ds = await SQLiteHelper.FillDataSet(_con, "PRAGMA user_version"); if (ds.Tables[0].Rows.Count > 0) { return(LongHelper.Parse(ds.Tables[0].Rows[0].ItemArray[0])); } return(0); }
public void SaturateCalculationTest() { Assert.IsTrue(LongHelper.SaturatedAdd(5, 5) == 10); Assert.IsTrue(LongHelper.SaturatedAdd(long.MaxValue, 5) == long.MaxValue); Assert.IsTrue(LongHelper.SaturatedAdd(long.MinValue, -5) == long.MinValue); Assert.IsTrue(LongHelper.SaturatedSubtract(long.MinValue, 5) == long.MinValue); Assert.IsTrue(LongHelper.SaturatedMultiply(5, 5) == 25); Assert.IsTrue(LongHelper.SaturatedMultiply(long.MaxValue / 2, 3) == long.MaxValue); }
public void NumberOfTrailingZerosTest() { Assert.IsTrue(LongHelper.NumberOfTrailingZeros(1) == 0); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(65) == 0); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(2) == 1); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(4) == 2); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(44) == 2); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(16) == 4); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(48) == 4); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(64) == 6); Assert.IsTrue(LongHelper.NumberOfTrailingZeros(1L << 63) == 63); Assert.IsTrue(LongHelper.NumberOfTrailingZeros((1L << 63) | (1L << 60)) == 60); }
private void HeaderLinesCallback(IAsyncResult ar) { String lHeaderLine; try { lHeaderLine = this.DataConnection.EndReadLine(ar); } catch (ConnectionClosedException) { Done(); return; } catch (SocketException) { Done(); return; } catch (ObjectDisposedException) { Done(); return; } // HTTP Request Type is already known String lHttpMethod = this.fContext.CurrentRequest.Header.RequestType; Boolean lRequireBody = (lHttpMethod == "POST") || (lHttpMethod == "PUT") || (lHttpMethod == "MERGE"); Boolean lHaveData = true; while (lHaveData) { lHaveData = false; if (lHeaderLine == "") { // we've got the last line. Process it if (lRequireBody) { Int64 lContentLength; #if FULLFRAMEWORK if (!Int64.TryParse(fContext.CurrentRequest.Header.GetHeaderValue("Content-Length"), out lContentLength)) #else if (!LongHelper.TryParse(fContext.CurrentRequest.Header.GetHeaderValue("Content-Length"), out lContentLength)) #endif { lContentLength = 0; } if (lContentLength > ((AsyncHttpServer)this.Owner).MaxPostSize) { this.SendInvalidRequest(new Exception("Content-Length too large")); return; } try { ((AsyncHttpServer)this.Owner).TriggerBeforeHaveData(new AsyncHttpRequestEventArgs(this.DataConnection, this.fContext)); } catch (Exception ex) { this.SendInvalidRequest(ex); return; } if (this.fContext.ResponseSent) { return; // already triggered the required functions. } try { Byte[] lData = new Byte[(Int32)lContentLength]; DataConnection.BeginRead(lData, 0, (Int32)lContentLength, WantBodyCallback, lData); } catch (SocketException) { Done(); } catch (ObjectDisposedException) { Done(); } return; } else { try { this.fOwner.TriggerHttpRequest(new AsyncHttpRequestEventArgs(this.DataConnection, this.fContext)); return; } catch (Exception ex) { this.SendInvalidRequest(ex); return; } } } if (fContext.CurrentRequest.Header.Count >= fContext.CurrentRequest.Header.MaxHeaderLines && fContext.CurrentRequest.Header.MaxHeaderLinesEnabled) { SendInvalidRequest(); return; } Int32 lPosition = lHeaderLine.IndexOf(":", StringComparison.Ordinal); if (lPosition == -1) { SendInvalidRequest(); return; } String lName = lHeaderLine.Substring(0, lPosition); String lValue = null; if (lHeaderLine.Length > lPosition + 1) { lValue = lHeaderLine.Substring(lPosition + 2); } fContext.CurrentRequest.Header.SetHeaderValue(lName, lValue); lHeaderLine = DataConnection.BufferReadLine(); if (lHeaderLine != null) { lHaveData = true; continue; } try { DataConnection.BeginReadLine(HeaderLinesCallback, null); } catch (SocketException) { Done(); } catch (ObjectDisposedException) { Done(); } } }