1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -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 {
return &muxReader{
reader: buf.NewBytesReader(reader),
reader: buf.ToBytesReader(reader),
buffer: buf.NewLocal(1024),
}
}

View File

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

View File

@ -175,17 +175,17 @@ func (b *Buffer) String() string {
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 {
return mediumPool.Allocate()
}
// NewSmall returns a buffer with 2K bytes capacity.
// NewSmall returns a buffer with 0 length and 2K capacity.
func NewSmall() *Buffer {
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 {
return &Buffer{
v: make([]byte, size),

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,6 @@ import (
"testing"
. "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/testing/assert"
)
@ -20,7 +19,7 @@ func TestWriter(t *testing.T) {
writeBuffer := bytes.NewBuffer(make([]byte, 0, 1024*1024))
writer := NewWriter(bufio.NewWriter(writeBuffer))
writer := NewWriter(NewBufferedWriter(writeBuffer))
err := writer.Write(lb)
assert.Error(err).IsNil()
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
import (
"bufio"
"context"
"io"
"net"
@ -15,7 +16,6 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
"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.SetReadDeadline(time.Now().Add(time.Second * 8))
reader := bufio.OriginalReaderSize(conn, 2048)
reader := bufio.NewReaderSize(conn, 2048)
request, err := http.ReadRequest(reader)
if err != nil {
@ -223,7 +223,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, rea
defer input.Close()
requestDone := signal.ExecuteAsync(func() error {
requestWriter := bufio.NewWriter(buf.NewBytesWriter(ray.InboundInput()))
requestWriter := buf.NewBufferedWriter(buf.ToBytesWriter(ray.InboundInput()))
err := request.Write(requestWriter)
if err != nil {
return err
@ -235,13 +235,13 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, rea
})
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)
if err != nil {
log.Warning("HTTP: Failed to read response: ", err)
response = generateResponse(503, "Service Unavailable")
}
responseWriter := bufio.NewWriter(writer)
responseWriter := buf.NewBufferedWriter(writer)
if err := response.Write(responseWriter); err != nil {
return err
}

View File

@ -8,7 +8,6 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"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)
if request.Command == protocol.RequestCommandTCP {
bufferedWriter := bufio.NewWriter(conn)
bufferedWriter := buf.NewBufferedWriter(conn)
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
if err != nil {
log.Info("Shadowsocks|Client: Failed to write request: ", err)

View File

@ -10,7 +10,6 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"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 {
conn.SetReadDeadline(time.Now().Add(time.Second * 8))
bufferedReader := bufio.NewReader(conn)
bufferedReader := buf.NewBufferedReader(conn)
request, bodyReader, err := ReadTCPSession(s.user, bufferedReader)
if err != nil {
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 {
bufferedWriter := bufio.NewWriter(conn)
bufferedWriter := buf.NewBufferedWriter(conn)
responseWriter, err := WriteTCPResponse(request, bufferedWriter)
if err != nil {
log.Warning("Shadowsocks|Server: Failed to write response: ", err)

View File

@ -11,7 +11,6 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"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 {
conn.SetReadDeadline(time.Now().Add(time.Second * 8))
reader := bufio.NewReader(conn)
reader := buf.NewBufferedReader(conn)
inboundDest, ok := proxy.InboundEntryPointFromContext(ctx)
if !ok {

View File

@ -13,7 +13,6 @@ import (
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
@ -150,7 +149,7 @@ func transferResponse(timer *signal.ActivityTimer, session *encoding.ServerSessi
}
data.Release()
if bufferedWriter, ok := output.(*bufio.BufferedWriter); ok {
if bufferedWriter, ok := output.(*buf.BufferedWriter); ok {
if err := bufferedWriter.SetBuffered(false); err != nil {
return err
}
@ -172,7 +171,7 @@ func transferResponse(timer *signal.ActivityTimer, session *encoding.ServerSessi
// Process implements proxy.Inbound.Process().
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))
reader := bufio.NewReader(connection)
reader := buf.NewBufferedReader(connection)
session := encoding.NewServerSession(v.clients, v.sessionHistory)
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)
})
writer := bufio.NewWriter(connection)
writer := buf.NewBufferedWriter(connection)
response := &protocol.ResponseHeader{
Command: v.generateCommand(ctx, request),
}

View File

@ -9,7 +9,6 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"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)
requestDone := signal.ExecuteAsync(func() error {
writer := bufio.NewWriter(conn)
writer := buf.NewBufferedWriter(conn)
session.EncodeRequestHeader(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 {
defer output.Close()
reader := bufio.NewReader(conn)
reader := buf.NewBufferedReader(conn)
header, err := session.DecodeResponseHeader(reader)
if err != nil {
return err