mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-30 04:21:29 +00:00
49 lines
1 KiB
Go
49 lines
1 KiB
Go
|
package debugio
|
||
|
|
||
|
import (
|
||
|
"net"
|
||
|
"sync"
|
||
|
|
||
|
"github.com/sagernet/sing/common/buf"
|
||
|
"github.com/sagernet/sing/common/bufio"
|
||
|
N "github.com/sagernet/sing/common/network"
|
||
|
)
|
||
|
|
||
|
type RaceConn struct {
|
||
|
N.ExtendedConn
|
||
|
readAccess sync.Mutex
|
||
|
writeAccess sync.Mutex
|
||
|
}
|
||
|
|
||
|
func NewRaceConn(conn net.Conn) N.ExtendedConn {
|
||
|
return &RaceConn{ExtendedConn: bufio.NewExtendedConn(conn)}
|
||
|
}
|
||
|
|
||
|
func (c *RaceConn) Read(p []byte) (n int, err error) {
|
||
|
c.readAccess.Lock()
|
||
|
defer c.readAccess.Unlock()
|
||
|
return c.ExtendedConn.Read(p)
|
||
|
}
|
||
|
|
||
|
func (c *RaceConn) Write(p []byte) (n int, err error) {
|
||
|
c.writeAccess.Lock()
|
||
|
defer c.writeAccess.Unlock()
|
||
|
return c.ExtendedConn.Write(p)
|
||
|
}
|
||
|
|
||
|
func (c *RaceConn) ReadBuffer(buffer *buf.Buffer) error {
|
||
|
c.readAccess.Lock()
|
||
|
defer c.readAccess.Unlock()
|
||
|
return c.ExtendedConn.ReadBuffer(buffer)
|
||
|
}
|
||
|
|
||
|
func (c *RaceConn) WriteBuffer(buffer *buf.Buffer) error {
|
||
|
c.writeAccess.Lock()
|
||
|
defer c.writeAccess.Unlock()
|
||
|
return c.ExtendedConn.WriteBuffer(buffer)
|
||
|
}
|
||
|
|
||
|
func (c *RaceConn) Upstream() any {
|
||
|
return c.ExtendedConn
|
||
|
}
|