package quic

import (
	
	

	
	
	
	
)

type connectionFlowController struct {
	receiveFlowController

	// Protects send-side state, which TryWriteAll can access from application goroutines.
	sendMutex     sync.Mutex
	bytesSent     protocol.ByteCount
	sendWindow    protocol.ByteCount
	lastBlockedAt protocol.ByteCount
}

// newConnectionFlowController gets a new flow controller for the connection.
// It is created before we receive the peer's transport parameters, thus it starts with a sendWindow of 0.
func newConnectionFlowController(
	 protocol.ByteCount,
	 protocol.ByteCount,
	 func( protocol.ByteCount) bool,
	 *utils.RTTStats,
	 utils.Logger,
) *connectionFlowController {
	return &connectionFlowController{
		receiveFlowController: receiveFlowController{
			rttStats:             ,
			receiveWindow:        ,
			receiveWindowSize:    ,
			maxReceiveWindowSize: ,
			allowWindowIncrease:  ,
			logger:               ,
		},
	}
}

// IncrementHighestReceived adds an increment to the highestReceived value
func ( *connectionFlowController) ( protocol.ByteCount,  monotime.Time) error {
	.mutex.Lock()
	defer .mutex.Unlock()

	// If this is the first frame received on this connection, start flow-control auto-tuning.
	if .highestReceived == 0 {
		.startNewAutoTuningEpoch()
	}
	.highestReceived += 

	if .checkFlowControlViolation() {
		return &qerr.TransportError{
			ErrorCode:    qerr.FlowControlError,
			ErrorMessage: fmt.Sprintf("received %d bytes for the connection, allowed %d bytes", .highestReceived, .receiveWindow),
		}
	}
	return nil
}

func ( *connectionFlowController) ( protocol.ByteCount) ( bool) {
	.mutex.Lock()
	defer .mutex.Unlock()

	.addBytesRead()
	return .hasWindowUpdate()
}

// TryAddBytesSent adds n bytes if sufficient connection-level send credit is available.
func ( *connectionFlowController) ( protocol.ByteCount) bool {
	.sendMutex.Lock()
	defer .sendMutex.Unlock()

	if .bytesSent > .sendWindow ||  > .sendWindow-.bytesSent {
		return false
	}
	.bytesSent += 
	return true
}

// AddBytesSentWithLimiter adds the limiter-approved portion of the available connection-level send credit.
func ( *connectionFlowController) (
	 protocol.ByteCount,
	 func(int) int,
) (protocol.ByteCount, bool) {
	.sendMutex.Lock()
	defer .sendMutex.Unlock()

	if .bytesSent >= .sendWindow {
		return 0, false
	}
	 = min(, .sendWindow-.bytesSent)
	 := min(
		max(protocol.ByteCount((int())), 0),
		,
	)
	.bytesSent += 
	return ,  < 
}

// UpdateSendWindow is called after receiving a MAX_DATA frame.
func ( *connectionFlowController) ( protocol.ByteCount) ( bool) {
	.sendMutex.Lock()
	defer .sendMutex.Unlock()

	if  > .sendWindow {
		.sendWindow = 
		return true
	}
	return false
}

func ( *connectionFlowController) () protocol.ByteCount {
	.sendMutex.Lock()
	defer .sendMutex.Unlock()

	return .sendWindow - .bytesSent
}

// IsNewlyBlocked says if it is newly blocked by connection flow control.
// For every offset, it only returns true once.
// If it is blocked, the offset is returned.
func ( *connectionFlowController) () (bool, protocol.ByteCount) {
	.sendMutex.Lock()
	defer .sendMutex.Unlock()

	if .bytesSent < .sendWindow || .sendWindow == .lastBlockedAt {
		return false, 0
	}
	.lastBlockedAt = .sendWindow
	return true, .sendWindow
}

func ( *connectionFlowController) ( monotime.Time) protocol.ByteCount {
	.mutex.Lock()
	defer .mutex.Unlock()

	 := .receiveWindowSize
	 := .getWindowUpdate()
	if .logger.Debug() &&  < .receiveWindowSize {
		.logger.Debugf("Increasing receive flow control window for the connection to %d kB", .receiveWindowSize/(1<<10))
	}
	return 
}

// EnsureMinimumWindowSize sets a minimum window size
// it should make sure that the connection-level window is increased when a stream-level window grows
func ( *connectionFlowController) ( protocol.ByteCount,  monotime.Time) {
	.mutex.Lock()
	defer .mutex.Unlock()

	if  <= .receiveWindowSize {
		return
	}
	 := min(, .maxReceiveWindowSize)
	if  :=  - .receiveWindowSize;  > 0 && .allowWindowIncrease() {
		.receiveWindowSize = 
		if .logger.Debug() {
			.logger.Debugf("Increasing receive flow control window for the connection to %d, in response to stream flow control window increase", )
		}
	}
	.startNewAutoTuningEpoch()
}

// Reset rests the flow controller. This happens when 0-RTT is rejected.
// All stream data is invalidated, it's as if we had never opened a stream and never sent any data.
// At that point, we only have sent stream data, but we didn't have the keys to open 1-RTT keys yet.
func ( *connectionFlowController) () {
	.sendMutex.Lock()
	defer .sendMutex.Unlock()

	.bytesSent = 0
	.lastBlockedAt = 0
	.sendWindow = 0
}