Cleanup gun conn code

This commit is contained in:
Hellojack 2022-11-23 06:56:31 +00:00 committed by GitHub
parent a401828ed5
commit 01b4769852
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 42 deletions

View file

@ -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
}

View file

@ -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