mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 20:37:40 +03:00
Add bufio cached packet conn
This commit is contained in:
parent
53c9f8d137
commit
9cd9268a7e
2 changed files with 47 additions and 1 deletions
|
@ -30,6 +30,7 @@ linters:
|
||||||
- gocognit
|
- gocognit
|
||||||
- golint
|
- golint
|
||||||
- goimports
|
- goimports
|
||||||
|
- nosnakecase
|
||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
revive:
|
revive:
|
||||||
|
@ -42,3 +43,8 @@ linters-settings:
|
||||||
- composites
|
- composites
|
||||||
- fieldalignment
|
- fieldalignment
|
||||||
- shadow
|
- shadow
|
||||||
|
gci:
|
||||||
|
sections:
|
||||||
|
- standard
|
||||||
|
- prefix(github.com/sagernet/sing)
|
||||||
|
- default
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CachedConn struct {
|
type CachedConn struct {
|
||||||
|
@ -135,3 +137,41 @@ func (r *CachedReader) Close() error {
|
||||||
r.buffer.Release()
|
r.buffer.Release()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CachedPacketConn struct {
|
||||||
|
N.PacketConn
|
||||||
|
buffer *buf.Buffer
|
||||||
|
destination M.Socksaddr
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCachedPacketConn(conn N.PacketConn, buffer *buf.Buffer, destination M.Socksaddr) *CachedPacketConn {
|
||||||
|
return &CachedPacketConn{
|
||||||
|
PacketConn: conn,
|
||||||
|
buffer: buffer,
|
||||||
|
destination: destination,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CachedPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||||
|
if c.buffer != nil {
|
||||||
|
_, err = buffer.ReadFrom(c.buffer)
|
||||||
|
if err != nil {
|
||||||
|
return M.Socksaddr{}, err
|
||||||
|
}
|
||||||
|
c.buffer = nil
|
||||||
|
return c.destination, nil
|
||||||
|
}
|
||||||
|
return c.PacketConn.ReadPacket(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CachedPacketConn) Upstream() any {
|
||||||
|
return c.PacketConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CachedPacketConn) ReaderReplaceable() bool {
|
||||||
|
return c.buffer == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CachedPacketConn) WriterReplaceable() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue