1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 07:26:24 -05:00

merge bufio into buf

This commit is contained in:
Darien Raymond 2017-02-15 22:51:01 +01:00
parent 7199ffcaa2
commit 020b436827
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
16 changed files with 54 additions and 75 deletions

View File

@ -12,7 +12,7 @@ type muxReader struct {
func NewReader(reader buf.Reader) *muxReader { func NewReader(reader buf.Reader) *muxReader {
return &muxReader{ return &muxReader{
reader: buf.NewBytesReader(reader), reader: buf.ToBytesReader(reader),
buffer: buf.NewLocal(1024), buffer: buf.NewLocal(1024),
} }
} }

View File

@ -75,6 +75,7 @@ func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
outboundRay.OutboundInput().CloseError() outboundRay.OutboundInput().CloseError()
} }
// Dial implements proxy.Dialer.Dial().
func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
if h.senderSettings != nil { if h.senderSettings != nil {
if h.senderSettings.ProxySettings.HasTag() { if h.senderSettings.ProxySettings.HasTag() {
@ -109,8 +110,8 @@ type Connection struct {
localAddr net.Addr localAddr net.Addr
remoteAddr net.Addr remoteAddr net.Addr
reader *buf.BufferToBytesReader reader io.Reader
writer *buf.BytesToBufferWriter writer io.Writer
} }
func NewConnection(stream ray.Ray) *Connection { func NewConnection(stream ray.Ray) *Connection {
@ -124,8 +125,8 @@ func NewConnection(stream ray.Ray) *Connection {
IP: []byte{0, 0, 0, 0}, IP: []byte{0, 0, 0, 0},
Port: 0, Port: 0,
}, },
reader: buf.NewBytesReader(stream.InboundOutput()), reader: buf.ToBytesReader(stream.InboundOutput()),
writer: buf.NewBytesWriter(stream.InboundInput()), writer: buf.ToBytesWriter(stream.InboundInput()),
} }
} }
@ -163,14 +164,17 @@ func (v *Connection) RemoteAddr() net.Addr {
return v.remoteAddr return v.remoteAddr
} }
// SetDeadline implements net.Conn.SetDeadline().
func (v *Connection) SetDeadline(t time.Time) error { func (v *Connection) SetDeadline(t time.Time) error {
return nil return nil
} }
// SetReadDeadline implements net.Conn.SetReadDeadline().
func (v *Connection) SetReadDeadline(t time.Time) error { func (v *Connection) SetReadDeadline(t time.Time) error {
return nil return nil
} }
// SetWriteDeadline implement net.Conn.SetWriteDeadline().
func (v *Connection) SetWriteDeadline(t time.Time) error { func (v *Connection) SetWriteDeadline(t time.Time) error {
return nil return nil
} }

View File

@ -175,17 +175,17 @@ func (b *Buffer) String() string {
return string(b.Bytes()) return string(b.Bytes())
} }
// New creates a Buffer with 8K bytes of arbitrary content. // New creates a Buffer with 0 length and 8K capacity.
func New() *Buffer { func New() *Buffer {
return mediumPool.Allocate() return mediumPool.Allocate()
} }
// NewSmall returns a buffer with 2K bytes capacity. // NewSmall returns a buffer with 0 length and 2K capacity.
func NewSmall() *Buffer { func NewSmall() *Buffer {
return smallPool.Allocate() return smallPool.Allocate()
} }
// NewLocal creates and returns a buffer on current thread. // NewLocal creates and returns a buffer with 0 length and given capacity on current thread.
func NewLocal(size int) *Buffer { func NewLocal(size int) *Buffer {
return &Buffer{ return &Buffer{
v: make([]byte, size), v: make([]byte, size),

View File

@ -1,23 +1,21 @@
package bufio package buf
import ( import (
"io" "io"
"v2ray.com/core/common/buf"
) )
// BufferedReader is a reader with internal cache. // BufferedReader is a reader with internal cache.
type BufferedReader struct { type BufferedReader struct {
reader io.Reader reader io.Reader
buffer *buf.Buffer buffer *Buffer
buffered bool buffered bool
} }
// NewReader creates a new BufferedReader based on an io.Reader. // NewReader creates a new BufferedReader based on an io.Reader.
func NewReader(rawReader io.Reader) *BufferedReader { func NewBufferedReader(rawReader io.Reader) *BufferedReader {
return &BufferedReader{ return &BufferedReader{
reader: rawReader, reader: rawReader,
buffer: buf.NewLocal(1024), buffer: NewLocal(1024),
buffered: true, buffered: true,
} }
} }
@ -42,7 +40,7 @@ func (v *BufferedReader) Read(b []byte) (int, error) {
return v.reader.Read(b) return v.reader.Read(b)
} }
if v.buffer.IsEmpty() { if v.buffer.IsEmpty() {
err := v.buffer.AppendSupplier(buf.ReadFrom(v.reader)) err := v.buffer.AppendSupplier(ReadFrom(v.reader))
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -1,23 +1,22 @@
package bufio_test package buf_test
import ( import (
"crypto/rand" "crypto/rand"
"testing" "testing"
"v2ray.com/core/common/buf" . "v2ray.com/core/common/buf"
. "v2ray.com/core/common/bufio"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
func TestBufferedReader(t *testing.T) { func TestBufferedReader(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := buf.New() content := New()
assert.Error(content.AppendSupplier(buf.ReadFrom(rand.Reader))).IsNil() assert.Error(content.AppendSupplier(ReadFrom(rand.Reader))).IsNil()
len := content.Len() len := content.Len()
reader := NewReader(content) reader := NewBufferedReader(content)
assert.Bool(reader.IsBuffered()).IsTrue() assert.Bool(reader.IsBuffered()).IsTrue()
payload := make([]byte, 16) payload := make([]byte, 16)

View File

@ -1,9 +1,8 @@
package bufio package buf
import ( import (
"io" "io"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
) )
@ -11,15 +10,15 @@ import (
// This type is not thread safe. // This type is not thread safe.
type BufferedWriter struct { type BufferedWriter struct {
writer io.Writer writer io.Writer
buffer *buf.Buffer buffer *Buffer
buffered bool buffered bool
} }
// NewWriter creates a new BufferedWriter. // NewWriter creates a new BufferedWriter.
func NewWriter(rawWriter io.Writer) *BufferedWriter { func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
return &BufferedWriter{ return &BufferedWriter{
writer: rawWriter, writer: rawWriter,
buffer: buf.NewLocal(1024), buffer: NewLocal(1024),
buffered: true, buffered: true,
} }
} }
@ -29,7 +28,7 @@ func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
totalBytes := int64(0) totalBytes := int64(0)
for { for {
oriSize := v.buffer.Len() oriSize := v.buffer.Len()
err := v.buffer.AppendSupplier(buf.ReadFrom(reader)) err := v.buffer.AppendSupplier(ReadFrom(reader))
totalBytes += int64(v.buffer.Len() - oriSize) totalBytes += int64(v.buffer.Len() - oriSize)
if err != nil { if err != nil {
if errors.Cause(err) == io.EOF { if errors.Cause(err) == io.EOF {

View File

@ -1,20 +1,19 @@
package bufio_test package buf_test
import ( import (
"crypto/rand" "crypto/rand"
"testing" "testing"
"v2ray.com/core/common/buf" . "v2ray.com/core/common/buf"
. "v2ray.com/core/common/bufio"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
func TestBufferedWriter(t *testing.T) { func TestBufferedWriter(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := buf.New() content := New()
writer := NewWriter(content) writer := NewBufferedWriter(content)
assert.Bool(writer.IsBuffered()).IsTrue() assert.Bool(writer.IsBuffered()).IsTrue()
payload := make([]byte, 16) payload := make([]byte, 16)
@ -32,9 +31,9 @@ func TestBufferedWriter(t *testing.T) {
func TestBufferedWriterLargePayload(t *testing.T) { func TestBufferedWriterLargePayload(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := buf.NewLocal(128 * 1024) content := NewLocal(128 * 1024)
writer := NewWriter(content) writer := NewBufferedWriter(content)
assert.Bool(writer.IsBuffered()).IsTrue() assert.Bool(writer.IsBuffered()).IsTrue()
payload := make([]byte, 64*1024) payload := make([]byte, 64*1024)

View File

@ -33,7 +33,8 @@ func ReadFullFrom(reader io.Reader, size int) Supplier {
} }
} }
// Pipe dumps all content from reader to writer, until an error happens. // Pipe dumps all payload from reader to writer, until an error occurs.
// ActivityTimer gets updated as soon as there is a payload.
func Pipe(timer *signal.ActivityTimer, reader Reader, writer Writer) error { func Pipe(timer *signal.ActivityTimer, reader Reader, writer Writer) error {
for { for {
buffer, err := reader.Read() buffer, err := reader.Read()
@ -73,7 +74,8 @@ func NewReader(reader io.Reader) Reader {
} }
} }
func NewBytesReader(stream Reader) *BufferToBytesReader { // ToBytesReader converts a Reaaer to io.Reader.
func ToBytesReader(stream Reader) io.Reader {
return &BufferToBytesReader{ return &BufferToBytesReader{
stream: stream, stream: stream,
} }
@ -86,7 +88,8 @@ func NewWriter(writer io.Writer) Writer {
} }
} }
func NewBytesWriter(writer Writer) *BytesToBufferWriter { // ToBytesWriter converts a Writer to io.Writer
func ToBytesWriter(writer Writer) io.Writer {
return &BytesToBufferWriter{ return &BytesToBufferWriter{
writer: writer, writer: writer,
} }

View File

@ -6,7 +6,6 @@ import (
"testing" "testing"
. "v2ray.com/core/common/buf" . "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
@ -20,7 +19,7 @@ func TestWriter(t *testing.T) {
writeBuffer := bytes.NewBuffer(make([]byte, 0, 1024*1024)) writeBuffer := bytes.NewBuffer(make([]byte, 0, 1024*1024))
writer := NewWriter(bufio.NewWriter(writeBuffer)) writer := NewWriter(NewBufferedWriter(writeBuffer))
err := writer.Write(lb) err := writer.Write(lb)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Bytes(expectedBytes).Equals(writeBuffer.Bytes()) assert.Bytes(expectedBytes).Equals(writeBuffer.Bytes())

View File

@ -1,17 +0,0 @@
// Package bufio is a replacement of the standard golang package bufio.
package bufio
import (
"bufio"
"io"
)
// OriginalReader invokes bufio.NewReader() from Golang standard library.
func OriginalReader(reader io.Reader) *bufio.Reader {
return bufio.NewReader(reader)
}
// OriginalReaderSize invokes bufio.NewReaderSize() from Golang standard library.
func OriginalReaderSize(reader io.Reader, size int) *bufio.Reader {
return bufio.NewReaderSize(reader, size)
}

View File

@ -1,6 +1,7 @@
package http package http
import ( import (
"bufio"
"context" "context"
"io" "io"
"net" "net"
@ -15,7 +16,6 @@ import (
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/signal" "v2ray.com/core/common/signal"
@ -72,7 +72,7 @@ func (s *Server) Process(ctx context.Context, network v2net.Network, conn intern
conn.SetReusable(false) conn.SetReusable(false)
conn.SetReadDeadline(time.Now().Add(time.Second * 8)) conn.SetReadDeadline(time.Now().Add(time.Second * 8))
reader := bufio.OriginalReaderSize(conn, 2048) reader := bufio.NewReaderSize(conn, 2048)
request, err := http.ReadRequest(reader) request, err := http.ReadRequest(reader)
if err != nil { if err != nil {
@ -223,7 +223,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, rea
defer input.Close() defer input.Close()
requestDone := signal.ExecuteAsync(func() error { requestDone := signal.ExecuteAsync(func() error {
requestWriter := bufio.NewWriter(buf.NewBytesWriter(ray.InboundInput())) requestWriter := buf.NewBufferedWriter(buf.ToBytesWriter(ray.InboundInput()))
err := request.Write(requestWriter) err := request.Write(requestWriter)
if err != nil { if err != nil {
return err return err
@ -235,13 +235,13 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, rea
}) })
responseDone := signal.ExecuteAsync(func() error { responseDone := signal.ExecuteAsync(func() error {
responseReader := bufio.OriginalReader(buf.NewBytesReader(ray.InboundOutput())) responseReader := bufio.NewReader(buf.ToBytesReader(ray.InboundOutput()))
response, err := http.ReadResponse(responseReader, request) response, err := http.ReadResponse(responseReader, request)
if err != nil { if err != nil {
log.Warning("HTTP: Failed to read response: ", err) log.Warning("HTTP: Failed to read response: ", err)
response = generateResponse(503, "Service Unavailable") response = generateResponse(503, "Service Unavailable")
} }
responseWriter := bufio.NewWriter(writer) responseWriter := buf.NewBufferedWriter(writer)
if err := response.Write(responseWriter); err != nil { if err := response.Write(responseWriter); err != nil {
return err return err
} }

View File

@ -8,7 +8,6 @@ import (
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -96,7 +95,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2) timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
if request.Command == protocol.RequestCommandTCP { if request.Command == protocol.RequestCommandTCP {
bufferedWriter := bufio.NewWriter(conn) bufferedWriter := buf.NewBufferedWriter(conn)
bodyWriter, err := WriteTCPRequest(request, bufferedWriter) bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
if err != nil { if err != nil {
log.Info("Shadowsocks|Client: Failed to write request: ", err) log.Info("Shadowsocks|Client: Failed to write request: ", err)

View File

@ -10,7 +10,6 @@ import (
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -131,7 +130,7 @@ func (v *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection
func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error { func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
conn.SetReadDeadline(time.Now().Add(time.Second * 8)) conn.SetReadDeadline(time.Now().Add(time.Second * 8))
bufferedReader := bufio.NewReader(conn) bufferedReader := buf.NewBufferedReader(conn)
request, bodyReader, err := ReadTCPSession(s.user, bufferedReader) request, bodyReader, err := ReadTCPSession(s.user, bufferedReader)
if err != nil { if err != nil {
log.Access(conn.RemoteAddr(), "", log.AccessRejected, err) log.Access(conn.RemoteAddr(), "", log.AccessRejected, err)
@ -157,7 +156,7 @@ func (s *Server) handleConnection(ctx context.Context, conn internet.Connection,
} }
requestDone := signal.ExecuteAsync(func() error { requestDone := signal.ExecuteAsync(func() error {
bufferedWriter := bufio.NewWriter(conn) bufferedWriter := buf.NewBufferedWriter(conn)
responseWriter, err := WriteTCPResponse(request, bufferedWriter) responseWriter, err := WriteTCPResponse(request, bufferedWriter)
if err != nil { if err != nil {
log.Warning("Shadowsocks|Server: Failed to write response: ", err) log.Warning("Shadowsocks|Server: Failed to write response: ", err)

View File

@ -11,7 +11,6 @@ import (
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -63,7 +62,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet
func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error { func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
conn.SetReadDeadline(time.Now().Add(time.Second * 8)) conn.SetReadDeadline(time.Now().Add(time.Second * 8))
reader := bufio.NewReader(conn) reader := buf.NewBufferedReader(conn)
inboundDest, ok := proxy.InboundEntryPointFromContext(ctx) inboundDest, ok := proxy.InboundEntryPointFromContext(ctx)
if !ok { if !ok {

View File

@ -13,7 +13,6 @@ import (
"v2ray.com/core/app/proxyman" "v2ray.com/core/app/proxyman"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -150,7 +149,7 @@ func transferResponse(timer *signal.ActivityTimer, session *encoding.ServerSessi
} }
data.Release() data.Release()
if bufferedWriter, ok := output.(*bufio.BufferedWriter); ok { if bufferedWriter, ok := output.(*buf.BufferedWriter); ok {
if err := bufferedWriter.SetBuffered(false); err != nil { if err := bufferedWriter.SetBuffered(false); err != nil {
return err return err
} }
@ -172,7 +171,7 @@ func transferResponse(timer *signal.ActivityTimer, session *encoding.ServerSessi
// Process implements proxy.Inbound.Process(). // Process implements proxy.Inbound.Process().
func (v *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher dispatcher.Interface) error { func (v *Handler) Process(ctx context.Context, network net.Network, connection internet.Connection, dispatcher dispatcher.Interface) error {
connection.SetReadDeadline(time.Now().Add(time.Second * 8)) connection.SetReadDeadline(time.Now().Add(time.Second * 8))
reader := bufio.NewReader(connection) reader := buf.NewBufferedReader(connection)
session := encoding.NewServerSession(v.clients, v.sessionHistory) session := encoding.NewServerSession(v.clients, v.sessionHistory)
request, err := session.DecodeRequestHeader(reader) request, err := session.DecodeRequestHeader(reader)
@ -210,7 +209,7 @@ func (v *Handler) Process(ctx context.Context, network net.Network, connection i
return transferRequest(timer, session, request, reader, input) return transferRequest(timer, session, request, reader, input)
}) })
writer := bufio.NewWriter(connection) writer := buf.NewBufferedWriter(connection)
response := &protocol.ResponseHeader{ response := &protocol.ResponseHeader{
Command: v.generateCommand(ctx, request), Command: v.generateCommand(ctx, request),
} }

View File

@ -9,7 +9,6 @@ import (
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -111,7 +110,7 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2) timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
requestDone := signal.ExecuteAsync(func() error { requestDone := signal.ExecuteAsync(func() error {
writer := bufio.NewWriter(conn) writer := buf.NewBufferedWriter(conn)
session.EncodeRequestHeader(request, writer) session.EncodeRequestHeader(request, writer)
bodyWriter := session.EncodeRequestBody(request, writer) bodyWriter := session.EncodeRequestBody(request, writer)
@ -143,7 +142,7 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
responseDone := signal.ExecuteAsync(func() error { responseDone := signal.ExecuteAsync(func() error {
defer output.Close() defer output.Close()
reader := bufio.NewReader(conn) reader := buf.NewBufferedReader(conn)
header, err := session.DecodeResponseHeader(reader) header, err := session.DecodeResponseHeader(reader)
if err != nil { if err != nil {
return err return err