Fix concurrent write

This commit is contained in:
世界
2022-09-13 10:41:10 +08:00
parent 79b6bdfda1
commit b271e19a23
20 changed files with 130 additions and 60 deletions

View File

@@ -8,6 +8,7 @@ import (
"net"
"net/http"
"os"
"sync"
"time"
"github.com/sagernet/sing-box/common/baderror"
@@ -28,6 +29,7 @@ type GunConn struct {
create chan struct{}
err error
readRemaining int
writeAccess sync.Mutex
}
func newGunConn(reader io.Reader, writer io.Writer, flusher http.Flusher) *GunConn {
@@ -100,7 +102,9 @@ func (c *GunConn) Write(b []byte) (n int, err error) {
grpcHeader := buf.Get(5)
grpcPayloadLen := uint32(1 + varuintLen + len(b))
binary.BigEndian.PutUint32(grpcHeader[1:5], grpcPayloadLen)
c.writeAccess.Lock()
_, 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 {
c.flusher.Flush()

View File

@@ -14,8 +14,7 @@ import (
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/gorilla/websocket"
"github.com/sagernet/websocket"
)
var _ adapter.V2RayClientTransport = (*Client)(nil)

View File

@@ -11,8 +11,7 @@ import (
C "github.com/sagernet/sing-box/constant"
E "github.com/sagernet/sing/common/exceptions"
"github.com/gorilla/websocket"
"github.com/sagernet/websocket"
)
type WebsocketConn struct {

View File

@@ -19,8 +19,7 @@ import (
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
sHttp "github.com/sagernet/sing/protocol/http"
"github.com/gorilla/websocket"
"github.com/sagernet/websocket"
)
var _ adapter.V2RayServerTransport = (*Server)(nil)