// Code generated by go generate. DO NOT EDIT.
// Source: sys_conn_buffers.go

package quic

import (
	"fmt"
	"syscall"

	"github.com/sagernet/quic-go/internal/protocol"
	"github.com/sagernet/quic-go/internal/utils"
)

func setSendBuffer(c syscall.RawConn) error {
	size, err := inspectWriteBuffer(c)
	if err != nil {
		return fmt.Errorf("failed to determine send buffer size: %w", err)
	}
	if size >= protocol.DesiredSendBufferSize {
		utils.DefaultLogger.Debugf("Conn has send buffer of %d kiB (wanted: at least %d kiB)", size/1024, protocol.DesiredSendBufferSize/1024)
		return nil
	}
	// Ignore the error. We check if we succeeded by querying the buffer size afterward.
	_ = setWriteBufferSize(c, protocol.DesiredSendBufferSize)
	newSize, err := inspectWriteBuffer(c)
	if newSize < protocol.DesiredSendBufferSize {
		// Try again with RCVBUFFORCE on Linux
		_ = forceSetSendBuffer(c, protocol.DesiredSendBufferSize)
		newSize, err = inspectWriteBuffer(c)
		if err != nil {
			return fmt.Errorf("failed to determine send buffer size: %w", err)
		}
	}
	if err != nil {
		return fmt.Errorf("failed to determine send buffer size: %w", err)
	}
	if newSize == size {
		return fmt.Errorf("failed to increase send buffer size (wanted: %d kiB, got %d kiB)", protocol.DesiredSendBufferSize/1024, newSize/1024)
	}
	if newSize < protocol.DesiredSendBufferSize {
		return fmt.Errorf("failed to sufficiently increase send buffer size (was: %d kiB, wanted: %d kiB, got: %d kiB)", size/1024, protocol.DesiredSendBufferSize/1024, newSize/1024)
	}
	utils.DefaultLogger.Debugf("Increased send buffer size to %d kiB", newSize/1024)
	return nil
}
