mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 00:21: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)))
|
||||
c.writeAccess.Unlock()
|
||||
buf.Put(grpcHeader)
|
||||
if c.flusher != nil {
|
||||
if err == nil && c.flusher != nil {
|
||||
c.flusher.Flush()
|
||||
}
|
||||
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 {
|
||||
defer buffer.Release()
|
||||
dataLen := buffer.Len()
|
||||
varLen := uLen(uint64(dataLen))
|
||||
varLen := rw.UVariantLen(uint64(dataLen))
|
||||
header := buffer.ExtendHeader(6 + varLen)
|
||||
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+dataLen))
|
||||
header[5] = 0x0A
|
||||
binary.PutUvarint(header[6:], uint64(dataLen))
|
||||
err := rw.WriteBytes(c.writer, buffer.Bytes())
|
||||
if c.flusher != nil {
|
||||
if err == nil && c.flusher != nil {
|
||||
c.flusher.Flush()
|
||||
}
|
||||
return baderror.WrapH2(err)
|
||||
|
@ -153,31 +144,29 @@ func (c *GunConn) RemoteAddr() net.Addr {
|
|||
}
|
||||
|
||||
func (c *GunConn) SetDeadline(t time.Time) error {
|
||||
responseWriter, loaded := c.writer.(interface {
|
||||
if responseWriter, loaded := c.writer.(interface {
|
||||
SetWriteDeadline(time.Time) error
|
||||
})
|
||||
if !loaded {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
}); loaded {
|
||||
return responseWriter.SetWriteDeadline(t)
|
||||
}
|
||||
return os.ErrInvalid
|
||||
|
||||
}
|
||||
|
||||
func (c *GunConn) SetReadDeadline(t time.Time) error {
|
||||
responseWriter, loaded := c.writer.(interface {
|
||||
if responseWriter, loaded := c.writer.(interface {
|
||||
SetReadDeadline(time.Time) error
|
||||
})
|
||||
if !loaded {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
}); loaded {
|
||||
return responseWriter.SetReadDeadline(t)
|
||||
}
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
||||
func (c *GunConn) SetWriteDeadline(t time.Time) error {
|
||||
responseWriter, loaded := c.writer.(interface {
|
||||
if responseWriter, loaded := c.writer.(interface {
|
||||
SetWriteDeadline(time.Time) error
|
||||
})
|
||||
if !loaded {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
}); loaded {
|
||||
return responseWriter.SetWriteDeadline(t)
|
||||
}
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
|
|
@ -67,33 +67,30 @@ func (c *HTTPConn) RemoteAddr() net.Addr {
|
|||
}
|
||||
|
||||
func (c *HTTPConn) SetDeadline(t time.Time) error {
|
||||
responseWriter, loaded := c.writer.(interface {
|
||||
if responseWriter, loaded := c.writer.(interface {
|
||||
SetWriteDeadline(time.Time) error
|
||||
})
|
||||
if !loaded {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
}); loaded {
|
||||
return responseWriter.SetWriteDeadline(t)
|
||||
}
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
||||
func (c *HTTPConn) SetReadDeadline(t time.Time) error {
|
||||
responseWriter, loaded := c.writer.(interface {
|
||||
if responseWriter, loaded := c.writer.(interface {
|
||||
SetReadDeadline(time.Time) error
|
||||
})
|
||||
if !loaded {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
}); loaded {
|
||||
return responseWriter.SetReadDeadline(t)
|
||||
}
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
||||
func (c *HTTPConn) SetWriteDeadline(t time.Time) error {
|
||||
responseWriter, loaded := c.writer.(interface {
|
||||
if responseWriter, loaded := c.writer.(interface {
|
||||
SetWriteDeadline(time.Time) error
|
||||
})
|
||||
if !loaded {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
}); loaded {
|
||||
return responseWriter.SetWriteDeadline(t)
|
||||
}
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
||||
type ServerHTTPConn struct {
|
||||
|
|
Loading…
Reference in a new issue