ktls_read_wait.go 1014 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build linux && go1.25 && badlinkname
  5. package ktls
  6. import (
  7. "github.com/sagernet/sing/common/buf"
  8. N "github.com/sagernet/sing/common/network"
  9. )
  10. func (c *Conn) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
  11. c.readWaitOptions = options
  12. return false
  13. }
  14. func (c *Conn) WaitReadBuffer() (buffer *buf.Buffer, err error) {
  15. c.rawConn.In.Lock()
  16. defer c.rawConn.In.Unlock()
  17. for c.rawConn.Input.Len() == 0 {
  18. err = c.readRecord()
  19. if err != nil {
  20. return
  21. }
  22. }
  23. buffer = c.readWaitOptions.NewBuffer()
  24. n, err := c.rawConn.Input.Read(buffer.FreeBytes())
  25. if err != nil {
  26. buffer.Release()
  27. return
  28. }
  29. buffer.Truncate(n)
  30. if n != 0 && c.rawConn.Input.Len() == 0 && c.rawConn.Input.Len() > 0 &&
  31. c.rawConn.RawInput.Bytes()[0] == recordTypeAlert {
  32. _ = c.rawConn.ReadRecord()
  33. }
  34. c.readWaitOptions.PostReturn(buffer)
  35. return
  36. }