mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Cleanup gun conn code
This commit is contained in:
parent
a401828ed5
commit
01b4769852
|
@ -106,31 +106,22 @@ func (c *GunConn) Write(b []byte) (n int, err error) {
|
||||||
_, err = bufio.Copy(c.writer, io.MultiReader(bytes.NewReader(grpcHeader), bytes.NewReader(protobufHeader[:varuintLen+1]), bytes.NewReader(b)))
|
_, err = bufio.Copy(c.writer, io.MultiReader(bytes.NewReader(grpcHeader), bytes.NewReader(protobufHeader[:varuintLen+1]), bytes.NewReader(b)))
|
||||||
c.writeAccess.Unlock()
|
c.writeAccess.Unlock()
|
||||||
buf.Put(grpcHeader)
|
buf.Put(grpcHeader)
|
||||||
if c.flusher != nil {
|
if err == nil && c.flusher != nil {
|
||||||
c.flusher.Flush()
|
c.flusher.Flush()
|
||||||
}
|
}
|
||||||
return len(b), baderror.WrapH2(err)
|
return len(b), baderror.WrapH2(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func uLen(x uint64) int {
|
|
||||||
i := 0
|
|
||||||
for x >= 0x80 {
|
|
||||||
x >>= 7
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
return i + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GunConn) WriteBuffer(buffer *buf.Buffer) error {
|
func (c *GunConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
dataLen := buffer.Len()
|
dataLen := buffer.Len()
|
||||||
varLen := uLen(uint64(dataLen))
|
varLen := rw.UVariantLen(uint64(dataLen))
|
||||||
header := buffer.ExtendHeader(6 + varLen)
|
header := buffer.ExtendHeader(6 + varLen)
|
||||||
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+dataLen))
|
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+dataLen))
|
||||||
header[5] = 0x0A
|
header[5] = 0x0A
|
||||||
binary.PutUvarint(header[6:], uint64(dataLen))
|
binary.PutUvarint(header[6:], uint64(dataLen))
|
||||||
err := rw.WriteBytes(c.writer, buffer.Bytes())
|
err := rw.WriteBytes(c.writer, buffer.Bytes())
|
||||||
if c.flusher != nil {
|
if err == nil && c.flusher != nil {
|
||||||
c.flusher.Flush()
|
c.flusher.Flush()
|
||||||
}
|
}
|
||||||
return baderror.WrapH2(err)
|
return baderror.WrapH2(err)
|
||||||
|
@ -153,31 +144,29 @@ func (c *GunConn) RemoteAddr() net.Addr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GunConn) SetDeadline(t time.Time) error {
|
func (c *GunConn) SetDeadline(t time.Time) error {
|
||||||
responseWriter, loaded := c.writer.(interface {
|
if responseWriter, loaded := c.writer.(interface {
|
||||||
SetWriteDeadline(time.Time) error
|
SetWriteDeadline(time.Time) error
|
||||||
})
|
}); loaded {
|
||||||
if !loaded {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
return responseWriter.SetWriteDeadline(t)
|
return responseWriter.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
return os.ErrInvalid
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (c *GunConn) SetReadDeadline(t time.Time) error {
|
func (c *GunConn) SetReadDeadline(t time.Time) error {
|
||||||
responseWriter, loaded := c.writer.(interface {
|
if responseWriter, loaded := c.writer.(interface {
|
||||||
SetReadDeadline(time.Time) error
|
SetReadDeadline(time.Time) error
|
||||||
})
|
}); loaded {
|
||||||
if !loaded {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
return responseWriter.SetReadDeadline(t)
|
return responseWriter.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
|
return os.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
func (c *GunConn) SetWriteDeadline(t time.Time) error {
|
func (c *GunConn) SetWriteDeadline(t time.Time) error {
|
||||||
responseWriter, loaded := c.writer.(interface {
|
if responseWriter, loaded := c.writer.(interface {
|
||||||
SetWriteDeadline(time.Time) error
|
SetWriteDeadline(time.Time) error
|
||||||
})
|
}); loaded {
|
||||||
if !loaded {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
return responseWriter.SetWriteDeadline(t)
|
return responseWriter.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
return os.ErrInvalid
|
||||||
|
}
|
||||||
|
|
|
@ -67,34 +67,31 @@ func (c *HTTPConn) RemoteAddr() net.Addr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HTTPConn) SetDeadline(t time.Time) error {
|
func (c *HTTPConn) SetDeadline(t time.Time) error {
|
||||||
responseWriter, loaded := c.writer.(interface {
|
if responseWriter, loaded := c.writer.(interface {
|
||||||
SetWriteDeadline(time.Time) error
|
SetWriteDeadline(time.Time) error
|
||||||
})
|
}); loaded {
|
||||||
if !loaded {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
return responseWriter.SetWriteDeadline(t)
|
return responseWriter.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
return os.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
func (c *HTTPConn) SetReadDeadline(t time.Time) error {
|
func (c *HTTPConn) SetReadDeadline(t time.Time) error {
|
||||||
responseWriter, loaded := c.writer.(interface {
|
if responseWriter, loaded := c.writer.(interface {
|
||||||
SetReadDeadline(time.Time) error
|
SetReadDeadline(time.Time) error
|
||||||
})
|
}); loaded {
|
||||||
if !loaded {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
return responseWriter.SetReadDeadline(t)
|
return responseWriter.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
|
return os.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
func (c *HTTPConn) SetWriteDeadline(t time.Time) error {
|
func (c *HTTPConn) SetWriteDeadline(t time.Time) error {
|
||||||
responseWriter, loaded := c.writer.(interface {
|
if responseWriter, loaded := c.writer.(interface {
|
||||||
SetWriteDeadline(time.Time) error
|
SetWriteDeadline(time.Time) error
|
||||||
})
|
}); loaded {
|
||||||
if !loaded {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
return responseWriter.SetWriteDeadline(t)
|
return responseWriter.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
return os.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
type ServerHTTPConn struct {
|
type ServerHTTPConn struct {
|
||||||
HTTPConn
|
HTTPConn
|
||||||
|
|
Loading…
Reference in a new issue