void IConnectedPeerStreamExtension.OnReceivedSignalingPacket(BinaryReader reader) // manager thread { var subtPacketType = (SubtPacketType)reader.ReadByte(); switch (subtPacketType) { case SubtPacketType.RemoteStatus: var p = new SubtRemoteStatusPacket(reader); // SubtLocalPeer.WriteToLog($"received from peer {SubtConnectedPeer.RemotePeerId}: SUBT status packet: {p}"); LatestRemoteStatus = p; _stream.MarkAsActiveByExtension(); if (_rxBwBeforeJB.OutputPerUnit > p.RecentTxBandwidth * 5) { EmitPainToDeveloper($"_rxBwBeforeJB.OutputPerUnit={_rxBwBeforeJB.OutputPerUnit} > p.RecentTxBandwidth={p.RecentTxBandwidth}"); } break; case SubtPacketType.AdjustmentRequest: var adj = new AdjustmentRequestPacket(reader); SubtLocalPeer.WriteToLog($"{this} received adjustment request: {adj}"); if (adj.TxTargetBandwidth > this.TargetTxBandwidth) { // increase if (SubtLocalPeer.ImHealthyAndReadyFor100kbpsU2uSymbiosis) { // check requested BW this.TargetTxBandwidth = Math.Min(adj.TxTargetBandwidth, MaxTxBandwidthToAcceptFromRemoteSide); SubtLocalPeer.WriteToLog($"{this} bandwidth increased to {MiscProcedures.BandwidthToString(this.TargetTxBandwidth)}"); } else { SubtLocalPeer.WriteToLog($"{this} is not healthy to increase bandwidth"); } } else // decrease { this.TargetTxBandwidth = adj.TxTargetBandwidth; SubtLocalPeer.WriteToLog($"{this} bandwidth decreased to {MiscProcedures.BandwidthToString(this.TargetTxBandwidth)}"); } // respond var resp = new AdjustmentResponsePacket(this.TargetTxBandwidth); var respData = resp.Encode(this); _stream.SendPacket(respData, respData.Length); _lastTimeReceivedAdjustmentRequestUTC = SubtLocalPeer.LocalPeer.DateTimeNowUtc; break; case SubtPacketType.AdjustmentResponse: var adjResp = new AdjustmentResponsePacket(reader); if (PendingAdjustmentRequestPacket != null) { // we got response from remote peer SubtLocalPeer.WriteToLog($"{this} received adjustment response: {adjResp}"); // adjust local tx BW, according to remote BW. check what is responded this.TargetTxBandwidth = Math.Min(adjResp.TxTargetBandwidth, PendingAdjustmentRequestPacket.TxTargetBandwidth); PendingAdjustmentRequestPacket = null; PendingAdjustmentRequestPacketData = null; } break; } }
public void SendBandwidthAdjustmentRequest_OnResponseAdjustLocalTxBw(float requestedTxBandwidthAtRemotePeer) { var remotePeerId = SubtConnectedPeer.RemotePeerId; if (remotePeerId != null) { PendingAdjustmentRequestPacket = new AdjustmentRequestPacket(requestedTxBandwidthAtRemotePeer); SubtLocalPeer.WriteToLog($"{this} sends adjustment request {PendingAdjustmentRequestPacket}"); PendingAdjustmentRequestPacketData = PendingAdjustmentRequestPacket.Encode(this); _stream.SendPacket(PendingAdjustmentRequestPacketData, PendingAdjustmentRequestPacketData.Length); } }