Skip to content

Commit

Permalink
feat: refine PacketConnectionReader
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinoKaede authored and xiaokangwang committed Feb 18, 2023
1 parent ca185b3 commit 67c19f7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
8 changes: 2 additions & 6 deletions proxy/trojan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package trojan

import (
"context"
"sync"

core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
Expand Down Expand Up @@ -120,12 +119,9 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)

packetReader := &PacketReader{Reader: conn}
splitReader := &PacketConnectionReader{
readerAccess: &sync.Mutex{},
reader: packetReader,
}
packetConnectionReader := &PacketConnectionReader{reader: packetReader}

return udp.CopyPacketConn(packetConn, splitReader, udp.UpdateActivity(timer))
return udp.CopyPacketConn(packetConn, packetConnectionReader, udp.UpdateActivity(timer))
}

responseDoneAndCloseWriter := task.OnSuccess(getResponse, task.Close(link.Writer))
Expand Down
9 changes: 2 additions & 7 deletions proxy/trojan/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"io"
gonet "net"
"sync"

"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
Expand Down Expand Up @@ -289,15 +288,11 @@ func (r *PacketReader) ReadMultiBufferWithMetadata() (*PacketPayload, error) {
}

type PacketConnectionReader struct {
readerAccess *sync.Mutex
reader *PacketReader
payload *PacketPayload
reader *PacketReader
payload *PacketPayload
}

func (r *PacketConnectionReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) {
r.readerAccess.Lock()
defer r.readerAccess.Unlock()

if r.payload == nil || r.payload.Buffer.IsEmpty() {
r.payload, err = r.reader.ReadMultiBufferWithMetadata()
if err != nil {
Expand Down

0 comments on commit 67c19f7

Please sign in to comment.