public T Dequeue() { int lastIndex = data.Count - 1; PriorityData frontItem = data[0]; data[0] = data[lastIndex]; data.RemoveAt(lastIndex); --lastIndex; int parentIndex = 0; while (true) { int leftChildIndex = parentIndex * 2 + 1; if (leftChildIndex > lastIndex) { break; } int rightChildIndex = leftChildIndex + 1; if (rightChildIndex <= lastIndex && data[rightChildIndex].priority < data[leftChildIndex].priority) { leftChildIndex = rightChildIndex; } if (data[parentIndex].priority <= data[leftChildIndex].priority) { break; } PriorityData tmp = data[parentIndex]; data[parentIndex] = data[leftChildIndex]; data[leftChildIndex] = tmp; parentIndex = leftChildIndex; } return(frontItem.data); }
public async Task ConnectionShouldIgnorePriorityData( bool isServer, uint streamId, uint streamDependency, bool isExclusive, byte weight) { var inPipe = new BufferedPipe(1024); var outPipe = new BufferedPipe(1024); var http2Con = await ConnectionUtils.BuildEstablishedConnection( isServer, inPipe, outPipe, loggerProvider); var prioData = new PriorityData { StreamDependency = streamDependency, StreamDependencyIsExclusive = isExclusive, Weight = weight, }; await inPipe.WritePriority(streamId, prioData); // Send a ping afterwards // If we get a response the priority frame in between was ignored var pingData = new byte[8]; for (var i = 0; i < 8; i++) { pingData[i] = (byte)i; } await inPipe.WritePing(pingData, false); await outPipe.ReadAndDiscardPong(); }
private void SaveButtonClick() { string name = string.IsNullOrWhiteSpace(nameInput.Text) ? "Unnamed Task" : nameInput.Text; string description = descriptionInput.Text; ComboData <PriorityData> prioritySelection = priorityInput.SelectedItem as ComboData <PriorityData>; PriorityData priority = prioritySelection.Value; ComboData <UserData> userSelection = userInput.SelectedItem as ComboData <UserData>; UserData user = userSelection.Value; ComboData <StatusData> statusSelection = statusInput.SelectedItem as ComboData <StatusData>; StatusData status = statusSelection.Value; if (Task != null) { Task.Name = name; Task.Description = description; Task.Priority = priority; Task.User = user; Task.Status = status; new TaskModel(Task).Save(); } else { Task = new TaskData(name, description, priority, user, status); Task.ID = new TaskModel(Task).Save(); } Project.CloseCreateDialog(); OpenProjectView(); }
private async ValueTask <Http2Error?> HandlePriorityFrame(FrameHeader fh) { if (fh.StreamId == 0 || fh.Length != PriorityData.Size) { var errc = ErrorCode.ProtocolError; if (fh.Length != PriorityData.Size) { errc = ErrorCode.FrameSizeError; } return(new Http2Error { StreamId = 0, Code = errc, Message = "接收到无效的优先级帧头", }); } await inputStream.ReadAll( new ArraySegment <byte>(receiveBuffer, 0, PriorityData.Size)); var prioData = PriorityData.DecodeFrom( new ArraySegment <byte>(receiveBuffer, 0, PriorityData.Size)); return(HandlePriorityData(fh.StreamId, prioData)); }
public static void Postfix(Pawn pawn, TargetInfo t, ref float __result, WorkGiver_Scanner __instance) { if (!__instance.Prioritized || !pawn.CanAffectedByPriority()) { return; } Map map = pawn.Map; if (map == null) { map = t.Map; } float modPriority = MainMod.Data.GetPriorityOnCell(map, t.Cell); if (t.HasThing && !MainMod.ModConfig.patchGenClosest && PriorityData.CanPrioritize(t.Thing)) { modPriority += MainMod.Data.GetPriority(t.Thing); } modPriority *= MainMod.ModConfig.priorityMultiplier; __result += modPriority; }
private Http2Error?HandlePriorityData( uint streamId, PriorityData data) { if (streamId == data.StreamDependency) { return(new Http2Error { Code = ErrorCode.ProtocolError, StreamId = streamId, Message = "优先级错误:流不能依赖于自身", }); } return(null); }
public override AcceptanceReport CanDesignateThing(Thing t) { //Can't prioritize other faction's things if (t.Faction?.IsPlayer != true) { return(false); } //Can't do when fogged if (t.Fogged()) { return(false); } return(MainMod.Data.Filter.Allows(t) && PriorityData.CanPrioritize(t)); }
public void Enqueue(T item, float priority) { data.Add(new PriorityData(item, priority)); int itemIndex = data.Count - 1; while (itemIndex > 0) { int parentIndex = (itemIndex - 1) / 2; if (data[itemIndex].priority <= data[parentIndex].priority) { break; } PriorityData tmp = data[itemIndex]; data[itemIndex] = data[parentIndex]; data[parentIndex] = tmp; itemIndex = parentIndex; } }
public static async Task WritePriority( this IWriteAndCloseableByteStream stream, uint streamId, PriorityData prioData) { var fh = new FrameHeader { Type = FrameType.Priority, Flags = 0, Length = PriorityData.Size, StreamId = streamId, }; await stream.WriteFrameHeader(fh); var payload = new byte[PriorityData.Size]; prioData.EncodeInto(new ArraySegment <byte>(payload)); await stream.WriteAsync(new ArraySegment <byte>(payload)); }
public async Task ConnectionShouldGoAwayOnPriorityStreamIdZero() { var inPipe = new BufferedPipe(1024); var outPipe = new BufferedPipe(1024); var http2Con = await ConnectionUtils.BuildEstablishedConnection( true, inPipe, outPipe, loggerProvider); var prioData = new PriorityData { StreamDependency = 1, StreamDependencyIsExclusive = false, Weight = 0, }; await inPipe.WritePriority(0, prioData); await outPipe.AssertGoAwayReception(ErrorCode.ProtocolError, 0u); await outPipe.AssertStreamEnd(); }
public void GetMostImportantCubeStateUpdates(ConnectionData connectionData, ref int numStateUpdates, ref int[] cubeIds, ref CubeState[] cubeState) { Assert.IsTrue(numStateUpdates >= 0); Assert.IsTrue(numStateUpdates <= Constants.NumCubes); if (numStateUpdates == 0) { return; } var prioritySorted = new PriorityData[Constants.NumCubes]; for (int i = 0; i < Constants.NumCubes; ++i) { prioritySorted[i] = connectionData.priorityData[i]; } Array.Sort(prioritySorted, (x, y) => y.accumulator.CompareTo(x.accumulator)); int maxStateUpdates = numStateUpdates; numStateUpdates = 0; for (int i = 0; i < Constants.NumCubes; ++i) { if (numStateUpdates == maxStateUpdates) { break; } // IMPORTANT: Negative priority means don't send this cube! if (prioritySorted[i].accumulator < 0.0f) { continue; } cubeIds[numStateUpdates] = prioritySorted[i].cubeId; cubeState[numStateUpdates] = lastSnapshot.cubeState[cubeIds[numStateUpdates]]; ++numStateUpdates; } }
/// <summary> /// 读取和解码包含单个头段帧和0个或多个连续帧的头段块。 /// </summary> /// <param name="firstHeader"></param> /// <param name="ensureBuffer"></param> /// <returns></returns> public async ValueTask <Result> ReadHeaders( FrameHeader firstHeader, Func <int, byte[]> ensureBuffer) { if (firstHeader.Length > maxFrameSize) { return(new Result { Error = new Http2Error { StreamId = 0, Code = ErrorCode.FrameSizeError, Message = "超过最大帧大小", }, }); } PriorityData?prioData = null; var allowedHeadersSize = maxHeaderFieldsSize; var headers = new List <HeaderField>(); var initialFlags = firstHeader.Flags; var f = (HeadersFrameFlags)firstHeader.Flags; var isEndOfStream = f.HasFlag(HeadersFrameFlags.EndOfStream); var isEndOfHeaders = f.HasFlag(HeadersFrameFlags.EndOfHeaders); var isPadded = f.HasFlag(HeadersFrameFlags.Padded); var hasPriority = f.HasFlag(HeadersFrameFlags.Priority); var minLength = 0; if (isPadded) { minLength += 1; } if (hasPriority) { minLength += 5; } if (firstHeader.Length < minLength) { return(new Result { Error = new Http2Error { StreamId = 0, Code = ErrorCode.ProtocolError, Message = "帧内容大小无效", }, }); } byte[] buffer = ensureBuffer(firstHeader.Length); await reader.ReadAll(new ArraySegment <byte>(buffer, 0, firstHeader.Length)); var offset = 0; var padLen = 0; if (isPadded) { padLen = buffer[0]; offset++; } if (hasPriority) { prioData = PriorityData.DecodeFrom( new ArraySegment <byte>(buffer, offset, 5)); offset += 5; } var contentLen = firstHeader.Length - offset - padLen; if (contentLen < 0) { return(new Result { Error = new Http2Error { StreamId = 0, Code = ErrorCode.ProtocolError, Message = "帧内容大小无效", }, }); } hpackDecoder.AllowTableSizeUpdates = true; var decodeResult = hpackDecoder.DecodeHeaderBlockFragment( new ArraySegment <byte>(buffer, offset, contentLen), allowedHeadersSize, headers); var err = DecodeResultToError(decodeResult); if (err != null) { return(new Result { Error = err }); } allowedHeadersSize -= decodeResult.HeaderFieldsSize; while (!isEndOfHeaders) { var contHeader = await FrameHeader.ReceiveAsync(reader, buffer); if (contHeader.Type != FrameType.Continuation || contHeader.StreamId != firstHeader.StreamId || contHeader.Length > maxFrameSize || contHeader.Length == 0) { return(new Result { Error = new Http2Error { StreamId = 0, Code = ErrorCode.ProtocolError, Message = "延续帧无效", }, }); } var contFlags = ((ContinuationFrameFlags)contHeader.Flags); isEndOfHeaders = contFlags.HasFlag(ContinuationFrameFlags.EndOfHeaders); buffer = ensureBuffer(contHeader.Length); await reader.ReadAll(new ArraySegment <byte>(buffer, 0, contHeader.Length)); offset = 0; contentLen = contHeader.Length; decodeResult = hpackDecoder.DecodeHeaderBlockFragment( new ArraySegment <byte>(buffer, offset, contentLen), allowedHeadersSize, headers); var err2 = DecodeResultToError(decodeResult); if (err2 != null) { return(new Result { Error = err2 }); } allowedHeadersSize -= decodeResult.HeaderFieldsSize; } if (!hpackDecoder.HasInitialState) { return(new Result { Error = new Http2Error { Code = ErrorCode.CompressionError, StreamId = 0u, Message = "接收到不完整的头块", }, }); } return(new Result { Error = null, HeaderData = new CompleteHeadersFrameData { StreamId = firstHeader.StreamId, Headers = headers, Priority = prioData, EndOfStream = isEndOfStream, }, }); }