Refactor bufio

This commit is contained in:
世界
2022-08-11 23:59:22 +08:00
parent c9226aeaaf
commit 97870c9288
14 changed files with 46 additions and 45 deletions

View File

@@ -88,11 +88,6 @@ func (n *Naive) Start() error {
return E.Cause(err, "create TLS config")
}
n.httpServer = &http.Server{
Handler: n,
TLSConfig: n.tlsConfig.Config(),
}
var listenAddr string
if nAddr := netip.Addr(n.listenOptions.Listen); nAddr.IsValid() {
if n.listenOptions.ListenPort != 0 {
@@ -107,6 +102,10 @@ func (n *Naive) Start() error {
}
if common.Contains(n.network, N.NetworkTCP) {
n.httpServer = &http.Server{
Handler: n,
TLSConfig: n.tlsConfig.Config(),
}
tcpListener, err := net.Listen(M.NetworkFromNetAddr("tcp", netip.Addr(n.listenOptions.Listen)), listenAddr)
if err != nil {
return err
@@ -325,6 +324,13 @@ func (c *naivePaddingConn) write(p []byte) (n int, err error) {
return c.writer.Write(p)
}
func (c *naivePaddingConn) FrontHeadroom() int {
if c.writePadding < kFirstPaddings {
return 3 + 255
}
return 0
}
func (c *naivePaddingConn) WriteBuffer(buffer *buf.Buffer) error {
defer buffer.Release()
if c.writePadding < kFirstPaddings {
@@ -345,14 +351,14 @@ func (c *naivePaddingConn) WriteBuffer(buffer *buf.Buffer) error {
func (c *naivePaddingConn) WriteTo(w io.Writer) (n int64, err error) {
if c.readPadding < kFirstPaddings {
return bufio.WriteTo0(c, w)
return bufio.WriteToN(c, w, kFirstPaddings-c.readPadding)
}
return bufio.Copy(w, c.reader)
}
func (c *naivePaddingConn) ReadFrom(r io.Reader) (n int64, err error) {
if c.writePadding < kFirstPaddings {
return bufio.ReadFrom0(c, r)
return bufio.ReadFromN(c, r, kFirstPaddings-c.writePadding)
}
return bufio.Copy(c.writer, r)
}

View File

@@ -29,6 +29,7 @@ func (n *Naive) configureHTTP3Listener(listenAddr string) error {
go func() {
sErr := h3Server.Serve(udpListener)
if sErr == quic.ErrServerClosed {
udpListener.Close()
return
} else if sErr != nil {
n.logger.Error("http3 server serve error: ", sErr)