1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

rename alloc to buf

This commit is contained in:
Darien Raymond 2016-12-09 11:35:27 +01:00
parent cd24d6f2d0
commit 7a80409e30
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
60 changed files with 199 additions and 196 deletions

View File

@ -4,7 +4,7 @@ import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/proxyman" "v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router" "v2ray.com/core/app/router"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
@ -62,7 +62,7 @@ func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.I
} }
if session.Inbound != nil && session.Inbound.AllowPassiveConnection { if session.Inbound != nil && session.Inbound.AllowPassiveConnection {
go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32), direct) go dispatcher.Dispatch(destination, buf.NewLocalBuffer(32), direct)
} else { } else {
go v.FilterPacketAndDispatch(destination, direct, dispatcher) go v.FilterPacketAndDispatch(destination, direct, dispatcher)
} }

View File

@ -1,7 +1,7 @@
package testing package testing
import ( import (
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/transport/ray" "v2ray.com/core/transport/ray"
@ -20,7 +20,7 @@ func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic
if err != nil { if err != nil {
break break
} }
output := alloc.NewBuffer() output := buf.NewBuffer()
output.Append([]byte("Processed: ")) output.Append([]byte("Processed: "))
output.Append(payload.Bytes()) output.Append(payload.Bytes())
payload.Release() payload.Release()

View File

@ -6,7 +6,7 @@ import (
"time" "time"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/dice" "v2ray.com/core/common/dice"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
@ -101,7 +101,7 @@ func (v *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
} }
// Private: Visible for testing. // Private: Visible for testing.
func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Buffer) { func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *buf.Buffer) {
msg := new(dns.Msg) msg := new(dns.Msg)
err := msg.Unpack(payload.Bytes()) err := msg.Unpack(payload.Bytes())
if err != nil { if err != nil {
@ -144,8 +144,8 @@ func (v *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Bu
close(request.response) close(request.response)
} }
func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *alloc.Buffer { func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *buf.Buffer {
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
msg := new(dns.Msg) msg := new(dns.Msg)
msg.Id = id msg.Id = id
msg.RecursionDesired = true msg.RecursionDesired = true
@ -162,7 +162,7 @@ func (v *UDPNameServer) BuildQueryA(domain string, id uint16) *alloc.Buffer {
return buffer return buffer
} }
func (v *UDPNameServer) DispatchQuery(payload *alloc.Buffer) { func (v *UDPNameServer) DispatchQuery(payload *buf.Buffer) {
v.udpServer.Dispatch(&proxy.SessionInfo{Source: pseudoDestination, Destination: v.address}, payload, v.HandleResponse) v.udpServer.Dispatch(&proxy.SessionInfo{Source: pseudoDestination, Destination: v.address}, payload, v.HandleResponse)
} }

View File

@ -1,5 +1,5 @@
// Package alloc provides a light-weight memory allocation mechanism. // Package alloc provides a light-weight memory allocation mechanism.
package alloc package buf
import ( import (
"io" "io"

View File

@ -1,4 +1,4 @@
package alloc package buf
import ( import (
"os" "os"

View File

@ -1,9 +1,9 @@
package alloc_test package buf_test
import ( import (
"testing" "testing"
. "v2ray.com/core/common/alloc" . "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )

View File

@ -5,7 +5,7 @@ import (
"errors" "errors"
"io" "io"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
@ -71,7 +71,7 @@ func (v *AEADAuthenticator) Seal(dst, plainText []byte) ([]byte, error) {
type AuthenticationReader struct { type AuthenticationReader struct {
auth Authenticator auth Authenticator
buffer *alloc.Buffer buffer *buf.Buffer
reader io.Reader reader io.Reader
chunk []byte chunk []byte
@ -81,7 +81,7 @@ type AuthenticationReader struct {
func NewAuthenticationReader(auth Authenticator, reader io.Reader, aggressive bool) *AuthenticationReader { func NewAuthenticationReader(auth Authenticator, reader io.Reader, aggressive bool) *AuthenticationReader {
return &AuthenticationReader{ return &AuthenticationReader{
auth: auth, auth: auth,
buffer: alloc.NewLocalBuffer(32 * 1024), buffer: buf.NewLocalBuffer(32 * 1024),
reader: reader, reader: reader,
aggressive: aggressive, aggressive: aggressive,
} }

View File

@ -4,20 +4,20 @@ import (
"io" "io"
"sync" "sync"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
type BufferedReader struct { type BufferedReader struct {
sync.Mutex sync.Mutex
reader io.Reader reader io.Reader
buffer *alloc.Buffer buffer *buf.Buffer
cached bool cached bool
} }
func NewBufferedReader(rawReader io.Reader) *BufferedReader { func NewBufferedReader(rawReader io.Reader) *BufferedReader {
return &BufferedReader{ return &BufferedReader{
reader: rawReader, reader: rawReader,
buffer: alloc.NewBuffer(), buffer: buf.NewBuffer(),
cached: true, cached: true,
} }
} }

View File

@ -4,7 +4,7 @@ import (
"testing" "testing"
"crypto/rand" "crypto/rand"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
. "v2ray.com/core/common/io" . "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
@ -12,7 +12,7 @@ import (
func TestBufferedReader(t *testing.T) { func TestBufferedReader(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := alloc.NewBuffer() content := buf.NewBuffer()
content.FillFrom(rand.Reader) content.FillFrom(rand.Reader)
len := content.Len() len := content.Len()

View File

@ -3,21 +3,21 @@ package io
import ( import (
"io" "io"
"sync" "sync"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
) )
type BufferedWriter struct { type BufferedWriter struct {
sync.Mutex sync.Mutex
writer io.Writer writer io.Writer
buffer *alloc.Buffer buffer *buf.Buffer
cached bool cached bool
} }
func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter { func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
return &BufferedWriter{ return &BufferedWriter{
writer: rawWriter, writer: rawWriter,
buffer: alloc.NewSmallBuffer(), buffer: buf.NewSmallBuffer(),
cached: true, cached: true,
} }
} }

View File

@ -4,7 +4,7 @@ import (
"crypto/rand" "crypto/rand"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
. "v2ray.com/core/common/io" . "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
@ -12,7 +12,7 @@ import (
func TestBufferedWriter(t *testing.T) { func TestBufferedWriter(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := alloc.NewBuffer() content := buf.NewBuffer()
writer := NewBufferedWriter(content) writer := NewBufferedWriter(content)
assert.Bool(writer.Cached()).IsTrue() assert.Bool(writer.Cached()).IsTrue()
@ -32,7 +32,7 @@ func TestBufferedWriter(t *testing.T) {
func TestBufferedWriterLargePayload(t *testing.T) { func TestBufferedWriterLargePayload(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
content := alloc.NewLocalBuffer(128 * 1024) content := buf.NewLocalBuffer(128 * 1024)
writer := NewBufferedWriter(content) writer := NewBufferedWriter(content)
assert.Bool(writer.Cached()).IsTrue() assert.Bool(writer.Cached()).IsTrue()

View File

@ -4,7 +4,7 @@ import (
"io" "io"
"sync" "sync"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
type ChainWriter struct { type ChainWriter struct {
@ -28,7 +28,7 @@ func (v *ChainWriter) Write(payload []byte) (int, error) {
bytesWritten := 0 bytesWritten := 0
size := len(payload) size := len(payload)
for size > 0 { for size > 0 {
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
nBytes, _ := buffer.Write(payload) nBytes, _ := buffer.Write(payload)
size -= nBytes size -= nBytes
payload = payload[nBytes:] payload = payload[nBytes:]

View File

@ -4,13 +4,13 @@ import (
"io" "io"
"sync" "sync"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
type ChanReader struct { type ChanReader struct {
sync.Mutex sync.Mutex
stream Reader stream Reader
current *alloc.Buffer current *buf.Buffer
eof bool eof bool
} }

View File

@ -4,20 +4,20 @@ import (
"io" "io"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
// Reader extends io.Reader with alloc.Buffer. // Reader extends io.Reader with alloc.Buffer.
type Reader interface { type Reader interface {
common.Releasable common.Releasable
// Read reads content from underlying reader, and put it into an alloc.Buffer. // Read reads content from underlying reader, and put it into an alloc.Buffer.
Read() (*alloc.Buffer, error) Read() (*buf.Buffer, error)
} }
// AdaptiveReader is a Reader that adjusts its reading speed automatically. // AdaptiveReader is a Reader that adjusts its reading speed automatically.
type AdaptiveReader struct { type AdaptiveReader struct {
reader io.Reader reader io.Reader
largeBuffer *alloc.Buffer largeBuffer *buf.Buffer
highVolumn bool highVolumn bool
} }
@ -30,21 +30,21 @@ func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
} }
// Read implements Reader.Read(). // Read implements Reader.Read().
func (v *AdaptiveReader) Read() (*alloc.Buffer, error) { func (v *AdaptiveReader) Read() (*buf.Buffer, error) {
if v.highVolumn && v.largeBuffer.IsEmpty() { if v.highVolumn && v.largeBuffer.IsEmpty() {
if v.largeBuffer == nil { if v.largeBuffer == nil {
v.largeBuffer = alloc.NewLocalBuffer(32 * 1024) v.largeBuffer = buf.NewLocalBuffer(32 * 1024)
} }
nBytes, err := v.largeBuffer.FillFrom(v.reader) nBytes, err := v.largeBuffer.FillFrom(v.reader)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if nBytes < alloc.BufferSize { if nBytes < buf.BufferSize {
v.highVolumn = false v.highVolumn = false
} }
} }
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
if !v.largeBuffer.IsEmpty() { if !v.largeBuffer.IsEmpty() {
buffer.FillFrom(v.largeBuffer) buffer.FillFrom(v.largeBuffer)
return buffer, nil return buffer, nil

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
. "v2ray.com/core/common/io" . "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
@ -19,8 +19,8 @@ func TestAdaptiveReader(t *testing.T) {
b1, err := reader.Read() b1, err := reader.Read()
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Bool(b1.IsFull()).IsTrue() assert.Bool(b1.IsFull()).IsTrue()
assert.Int(b1.Len()).Equals(alloc.BufferSize) assert.Int(b1.Len()).Equals(buf.BufferSize)
assert.Int(buffer.Len()).Equals(cap(rawContent) - alloc.BufferSize) assert.Int(buffer.Len()).Equals(cap(rawContent) - buf.BufferSize)
b2, err := reader.Read() b2, err := reader.Read()
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -4,14 +4,14 @@ import (
"io" "io"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
// Writer extends io.Writer with alloc.Buffer. // Writer extends io.Writer with alloc.Buffer.
type Writer interface { type Writer interface {
common.Releasable common.Releasable
// Write writes an alloc.Buffer into underlying writer. // Write writes an alloc.Buffer into underlying writer.
Write(*alloc.Buffer) error Write(*buf.Buffer) error
} }
// AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer. // AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer.
@ -27,7 +27,7 @@ func NewAdaptiveWriter(writer io.Writer) *AdaptiveWriter {
} }
// Write implements Writer.Write(). Write() takes ownership of the given buffer. // Write implements Writer.Write(). Write() takes ownership of the given buffer.
func (v *AdaptiveWriter) Write(buffer *alloc.Buffer) error { func (v *AdaptiveWriter) Write(buffer *buf.Buffer) error {
defer buffer.Release() defer buffer.Release()
for { for {
nBytes, err := v.writer.Write(buffer.Bytes()) nBytes, err := v.writer.Write(buffer.Bytes())

View File

@ -5,7 +5,7 @@ import (
"crypto/rand" "crypto/rand"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
. "v2ray.com/core/common/io" . "v2ray.com/core/common/io"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
@ -13,7 +13,7 @@ import (
func TestAdaptiveWriter(t *testing.T) { func TestAdaptiveWriter(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
lb := alloc.NewBuffer() lb := buf.NewBuffer()
lb.FillFrom(rand.Reader) lb.FillFrom(rand.Reader)
expectedBytes := append([]byte(nil), lb.Bytes()...) expectedBytes := append([]byte(nil), lb.Bytes()...)

View File

@ -2,10 +2,11 @@ package serial
import ( import (
"hash" "hash"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/buf"
) )
func WriteHash(h hash.Hash) alloc.BytesWriter { func WriteHash(h hash.Hash) buf.BytesWriter {
return func(b []byte) int { return func(b []byte) int {
h.Sum(b[:0]) h.Sum(b[:0])
return h.Size() return h.Size()

View File

@ -2,7 +2,8 @@ package serial
import ( import (
"strconv" "strconv"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/buf"
) )
func Uint16ToBytes(value uint16, b []byte) []byte { func Uint16ToBytes(value uint16, b []byte) []byte {
@ -13,7 +14,7 @@ func Uint16ToString(value uint16) string {
return strconv.Itoa(int(value)) return strconv.Itoa(int(value))
} }
func WriteUint16(value uint16) alloc.BytesWriter { func WriteUint16(value uint16) buf.BytesWriter {
return func(b []byte) int { return func(b []byte) int {
b = Uint16ToBytes(value, b[:0]) b = Uint16ToBytes(value, b[:0])
return 2 return 2
@ -28,7 +29,7 @@ func Uint32ToString(value uint32) string {
return strconv.FormatUint(uint64(value), 10) return strconv.FormatUint(uint64(value), 10)
} }
func WriteUint32(value uint32) alloc.BytesWriter { func WriteUint32(value uint32) buf.BytesWriter {
return func(b []byte) int { return func(b []byte) int {
b = Uint32ToBytes(value, b[:0]) b = Uint32ToBytes(value, b[:0])
return 4 return 4

View File

@ -3,7 +3,8 @@ package serial
import ( import (
"fmt" "fmt"
"strings" "strings"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/buf"
) )
func ToString(v interface{}) string { func ToString(v interface{}) string {
@ -35,7 +36,7 @@ func Concat(v ...interface{}) string {
return strings.Join(values, "") return strings.Join(values, "")
} }
func WriteString(s string) alloc.BytesWriter { func WriteString(s string) buf.BytesWriter {
return func(b []byte) int { return func(b []byte) int {
copy(b, []byte(s)) copy(b, []byte(s))
return len(s) return len(s)

View File

@ -2,7 +2,7 @@ package blackhole
import ( import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/transport/ray" "v2ray.com/core/transport/ray"
@ -25,7 +25,7 @@ func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMe
}, nil }, nil
} }
func (v *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { func (v *BlackHole) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
payload.Release() payload.Release()
v.response.WriteTo(ray.OutboundOutput()) v.response.WriteTo(ray.OutboundOutput())

View File

@ -1,7 +1,7 @@
package blackhole package blackhole
import ( import (
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
"github.com/golang/protobuf/ptypes" "github.com/golang/protobuf/ptypes"
@ -32,7 +32,7 @@ func (v *NoneResponse) AsAny() *any.Any {
} }
func (v *HTTPResponse) WriteTo(writer v2io.Writer) { func (v *HTTPResponse) WriteTo(writer v2io.Writer) {
b := alloc.NewLocalBuffer(512) b := buf.NewLocalBuffer(512)
b.AppendFunc(serial.WriteString(http403response)) b.AppendFunc(serial.WriteString(http403response))
writer.Write(b) writer.Write(b)
} }

View File

@ -5,7 +5,7 @@ import (
"net/http" "net/http"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
. "v2ray.com/core/proxy/blackhole" . "v2ray.com/core/proxy/blackhole"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
@ -14,7 +14,7 @@ import (
func TestHTTPResponse(t *testing.T) { func TestHTTPResponse(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
httpResponse := new(HTTPResponse) httpResponse := new(HTTPResponse)
httpResponse.WriteTo(v2io.NewAdaptiveWriter(buffer)) httpResponse.WriteTo(v2io.NewAdaptiveWriter(buffer))

View File

@ -5,7 +5,7 @@ import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader" "v2ray.com/core/common/loader"
@ -107,7 +107,7 @@ func (v *DokodemoDoor) ListenUDP() error {
return nil return nil
} }
func (v *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.SessionInfo) { func (v *DokodemoDoor) handleUDPPackets(payload *buf.Buffer, session *proxy.SessionInfo) {
if session.Destination.Network == v2net.Network_Unknown && v.address != nil && v.port > 0 { if session.Destination.Network == v2net.Network_Unknown && v.address != nil && v.port > 0 {
session.Destination = v2net.UDPDestination(v.address, v.port) session.Destination = v2net.UDPDestination(v.address, v.port)
} }
@ -119,7 +119,7 @@ func (v *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.Se
v.udpServer.Dispatch(session, payload, v.handleUDPResponse) v.udpServer.Dispatch(session, payload, v.handleUDPResponse)
} }
func (v *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) { func (v *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *buf.Buffer) {
defer payload.Release() defer payload.Release()
v.udpMutex.RLock() v.udpMutex.RLock()
defer v.udpMutex.RUnlock() defer v.udpMutex.RUnlock()

View File

@ -4,7 +4,7 @@ import (
"io" "io"
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/dns" "v2ray.com/core/app/dns"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/dice" "v2ray.com/core/common/dice"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
@ -67,7 +67,7 @@ func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Desti
return newDest return newDest
} }
func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
log.Info("Freedom: Opening connection to ", destination) log.Info("Freedom: Opening connection to ", destination)
defer payload.Release() defer payload.Release()

View File

@ -9,7 +9,7 @@ import (
"v2ray.com/core/app/dns" "v2ray.com/core/app/dns"
"v2ray.com/core/app/proxyman" "v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router" "v2ray.com/core/app/router"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
. "v2ray.com/core/proxy/freedom" . "v2ray.com/core/proxy/freedom"
@ -47,7 +47,7 @@ func TestSinglePacket(t *testing.T) {
traffic := ray.NewRay() traffic := ray.NewRay()
data2Send := "Data to be sent to remote" data2Send := "Data to be sent to remote"
payload := alloc.NewLocalBuffer(2048) payload := buf.NewLocalBuffer(2048)
payload.Append([]byte(data2Send)) payload.Append([]byte(data2Send))
go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic) go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic)

View File

@ -2,7 +2,7 @@
package proxy package proxy
import ( import (
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
@ -58,5 +58,5 @@ type InboundHandler interface {
// An OutboundHandler handles outbound network connection for V2Ray. // An OutboundHandler handles outbound network connection for V2Ray.
type OutboundHandler interface { type OutboundHandler interface {
// Dispatch sends one or more Packets to its destination. // Dispatch sends one or more Packets to its destination.
Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay)
} }

View File

@ -3,7 +3,7 @@ package shadowsocks
import ( import (
"sync" "sync"
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
@ -32,7 +32,7 @@ func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandle
return client, nil return client, nil
} }
func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { func (v *Client) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
defer payload.Release() defer payload.Release()
defer ray.OutboundInput().Release() defer ray.OutboundInput().Release()
defer ray.OutboundOutput().Close() defer ray.OutboundOutput().Close()

View File

@ -6,7 +6,7 @@ import (
"crypto/sha1" "crypto/sha1"
"io" "io"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
@ -27,7 +27,7 @@ func NewAuthenticator(keygen KeyGenerator) *Authenticator {
} }
} }
func (v *Authenticator) Authenticate(data []byte) alloc.BytesWriter { func (v *Authenticator) Authenticate(data []byte) buf.BytesWriter {
hasher := hmac.New(sha1.New, v.key()) hasher := hmac.New(sha1.New, v.key())
hasher.Write(data) hasher.Write(data)
res := hasher.Sum(nil) res := hasher.Sum(nil)
@ -74,8 +74,8 @@ func (v *ChunkReader) Release() {
v.auth = nil v.auth = nil
} }
func (v *ChunkReader) Read() (*alloc.Buffer, error) { func (v *ChunkReader) Read() (*buf.Buffer, error) {
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
if _, err := buffer.FillFullFrom(v.reader, 2); err != nil { if _, err := buffer.FillFullFrom(v.reader, 2); err != nil {
buffer.Release() buffer.Release()
return nil, err return nil, err
@ -83,10 +83,10 @@ func (v *ChunkReader) Read() (*alloc.Buffer, error) {
// There is a potential buffer overflow here. Large buffer is 64K bytes, // There is a potential buffer overflow here. Large buffer is 64K bytes,
// while uin16 + 10 will be more than that // while uin16 + 10 will be more than that
length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize length := serial.BytesToUint16(buffer.BytesTo(2)) + AuthSize
if length > alloc.BufferSize { if length > buf.BufferSize {
// Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer. // Theoretically the size of a chunk is 64K, but most Shadowsocks implementations used <4K buffer.
buffer.Release() buffer.Release()
buffer = alloc.NewLocalBuffer(int(length) + 128) buffer = buf.NewLocalBuffer(int(length) + 128)
} }
buffer.Clear() buffer.Clear()
@ -128,7 +128,7 @@ func (v *ChunkWriter) Release() {
v.auth = nil v.auth = nil
} }
func (v *ChunkWriter) Write(payload *alloc.Buffer) error { func (v *ChunkWriter) Write(payload *buf.Buffer) error {
totalLength := payload.Len() totalLength := payload.Len()
serial.Uint16ToBytes(uint16(totalLength), v.buffer[:0]) serial.Uint16ToBytes(uint16(totalLength), v.buffer[:0])
v.auth.Authenticate(payload.Bytes())(v.buffer[2:]) v.auth.Authenticate(payload.Bytes())(v.buffer[2:])

View File

@ -3,7 +3,7 @@ package shadowsocks_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
. "v2ray.com/core/proxy/shadowsocks" . "v2ray.com/core/proxy/shadowsocks"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
@ -11,7 +11,7 @@ import (
func TestNormalChunkReading(t *testing.T) { func TestNormalChunkReading(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
buffer.AppendBytes( buffer.AppendBytes(
0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18) 0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18)
reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator( reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator(
@ -24,11 +24,11 @@ func TestNormalChunkReading(t *testing.T) {
func TestNormalChunkWriting(t *testing.T) { func TestNormalChunkWriting(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
buffer := alloc.NewLocalBuffer(512) buffer := buf.NewLocalBuffer(512)
writer := NewChunkWriter(buffer, NewAuthenticator(ChunkKeyGenerator( writer := NewChunkWriter(buffer, NewAuthenticator(ChunkKeyGenerator(
[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}))) []byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
b := alloc.NewLocalBuffer(256) b := buf.NewLocalBuffer(256)
b.Append([]byte{11, 12, 13, 14, 15, 16, 17, 18}) b.Append([]byte{11, 12, 13, 14, 15, 16, 17, 18})
err := writer.Write(b) err := writer.Write(b)
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"io" "io"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/crypto" "v2ray.com/core/common/crypto"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
@ -29,7 +29,7 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*ShadowsocksAccount)
buffer := alloc.NewLocalBuffer(512) buffer := buf.NewLocalBuffer(512)
defer buffer.Release() defer buffer.Release()
ivLen := account.Cipher.IVSize() ivLen := account.Cipher.IVSize()
@ -152,7 +152,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr
writer = crypto.NewCryptionWriter(stream, writer) writer = crypto.NewCryptionWriter(stream, writer)
header := alloc.NewLocalBuffer(512) header := buf.NewLocalBuffer(512)
switch request.Address.Family() { switch request.Address.Family() {
case v2net.AddressFamilyIPv4: case v2net.AddressFamilyIPv4:
@ -235,7 +235,7 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (v2io.W
return v2io.NewAdaptiveWriter(crypto.NewCryptionWriter(stream, writer)), nil return v2io.NewAdaptiveWriter(crypto.NewCryptionWriter(stream, writer)), nil
} }
func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*alloc.Buffer, error) { func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf.Buffer, error) {
user := request.User user := request.User
rawAccount, err := user.GetTypedAccount() rawAccount, err := user.GetTypedAccount()
if err != nil { if err != nil {
@ -243,7 +243,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*a
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*ShadowsocksAccount)
buffer := alloc.NewSmallBuffer() buffer := buf.NewSmallBuffer()
ivLen := account.Cipher.IVSize() ivLen := account.Cipher.IVSize()
buffer.FillFullFrom(rand.Reader, ivLen) buffer.FillFullFrom(rand.Reader, ivLen)
iv := buffer.Bytes() iv := buffer.Bytes()
@ -281,7 +281,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *alloc.Buffer) (*a
return buffer, nil return buffer, nil
} }
func DecodeUDPPacket(user *protocol.User, payload *alloc.Buffer) (*protocol.RequestHeader, *alloc.Buffer, error) { func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) {
rawAccount, err := user.GetTypedAccount() rawAccount, err := user.GetTypedAccount()
if err != nil { if err != nil {
return nil, nil, errors.Base(err).Message("Shadowsocks|UDP: Failed to parse account.") return nil, nil, errors.Base(err).Message("Shadowsocks|UDP: Failed to parse account.")
@ -359,8 +359,8 @@ type UDPReader struct {
User *protocol.User User *protocol.User
} }
func (v *UDPReader) Read() (*alloc.Buffer, error) { func (v *UDPReader) Read() (*buf.Buffer, error) {
buffer := alloc.NewSmallBuffer() buffer := buf.NewSmallBuffer()
_, err := buffer.FillFrom(v.Reader) _, err := buffer.FillFrom(v.Reader)
if err != nil { if err != nil {
buffer.Release() buffer.Release()
@ -382,7 +382,7 @@ type UDPWriter struct {
Request *protocol.RequestHeader Request *protocol.RequestHeader
} }
func (v *UDPWriter) Write(buffer *alloc.Buffer) error { func (v *UDPWriter) Write(buffer *buf.Buffer) error {
payload, err := EncodeUDPPacket(v.Request, buffer) payload, err := EncodeUDPPacket(v.Request, buffer)
if err != nil { if err != nil {
return err return err

View File

@ -3,7 +3,7 @@ package shadowsocks_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/loader" "v2ray.com/core/common/loader"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -30,7 +30,7 @@ func TestUDPEncoding(t *testing.T) {
}, },
} }
data := alloc.NewLocalBuffer(256) data := buf.NewLocalBuffer(256)
data.AppendFunc(serial.WriteString("test string")) data.AppendFunc(serial.WriteString("test string"))
encodedData, err := EncodeUDPPacket(request, data) encodedData, err := EncodeUDPPacket(request, data)
assert.Error(err).IsNil() assert.Error(err).IsNil()
@ -60,9 +60,9 @@ func TestTCPRequest(t *testing.T) {
}, },
} }
data := alloc.NewLocalBuffer(256) data := buf.NewLocalBuffer(256)
data.AppendFunc(serial.WriteString("test string")) data.AppendFunc(serial.WriteString("test string"))
cache := alloc.NewBuffer() cache := buf.NewBuffer()
writer, err := WriteTCPRequest(request, cache) writer, err := WriteTCPRequest(request, cache)
assert.Error(err).IsNil() assert.Error(err).IsNil()
@ -88,7 +88,7 @@ func TestUDPReaderWriter(t *testing.T) {
CipherType: CipherType_CHACHA20_IEFT, CipherType: CipherType_CHACHA20_IEFT,
}), }),
} }
cache := alloc.NewBuffer() cache := buf.NewBuffer()
writer := &UDPWriter{ writer := &UDPWriter{
Writer: cache, Writer: cache,
Request: &protocol.RequestHeader{ Request: &protocol.RequestHeader{
@ -105,7 +105,7 @@ func TestUDPReaderWriter(t *testing.T) {
User: user, User: user,
} }
b := alloc.NewBuffer() b := buf.NewBuffer()
b.AppendFunc(serial.WriteString("test payload")) b.AppendFunc(serial.WriteString("test payload"))
err := writer.Write(b) err := writer.Write(b)
assert.Error(err).IsNil() assert.Error(err).IsNil()
@ -114,7 +114,7 @@ func TestUDPReaderWriter(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.String(payload.String()).Equals("test payload") assert.String(payload.String()).Equals("test payload")
b = alloc.NewBuffer() b = buf.NewBuffer()
b.AppendFunc(serial.WriteString("test payload 2")) b.AppendFunc(serial.WriteString("test payload 2"))
err = writer.Write(b) err = writer.Write(b)
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -6,7 +6,7 @@ import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
@ -103,7 +103,7 @@ func (v *Server) Start() error {
return nil return nil
} }
func (v *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) { func (v *Server) handlerUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) {
source := session.Source source := session.Source
request, data, err := DecodeUDPPacket(v.user, payload) request, data, err := DecodeUDPPacket(v.user, payload)
if err != nil { if err != nil {
@ -129,7 +129,7 @@ func (v *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.Session
log.Access(source, dest, log.AccessAccepted, "") log.Access(source, dest, log.AccessAccepted, "")
log.Info("Shadowsocks|Server: Tunnelling request to ", dest) log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: v.meta}, data, func(destination v2net.Destination, payload *alloc.Buffer) { v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: v.meta}, data, func(destination v2net.Destination, payload *buf.Buffer) {
defer payload.Release() defer payload.Release()
data, err := EncodeUDPPacket(request, payload) data, err := EncodeUDPPacket(request, payload)

View File

@ -3,7 +3,7 @@ package protocol
import ( import (
"fmt" "fmt"
"io" "io"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
@ -120,7 +120,7 @@ func (request Socks5UserPassRequest) AuthDetail() string {
} }
func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) { func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) {
buffer := alloc.NewLocalBuffer(512) buffer := buf.NewLocalBuffer(512)
defer buffer.Release() defer buffer.Release()
_, err = buffer.FillFullFrom(reader, 2) _, err = buffer.FillFullFrom(reader, 2)
@ -188,7 +188,7 @@ type Socks5Request struct {
} }
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
buffer := alloc.NewLocalBuffer(512) buffer := buf.NewLocalBuffer(512)
defer buffer.Release() defer buffer.Release()
_, err = buffer.FillFullFrom(reader, 4) _, err = buffer.FillFullFrom(reader, 4)

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
@ -31,7 +31,7 @@ func TestSocks4AuthenticationResponseToBytes(t *testing.T) {
response := NewSocks4AuthenticationResponse(byte(0x10), 443, []byte{1, 2, 3, 4}) response := NewSocks4AuthenticationResponse(byte(0x10), 443, []byte{1, 2, 3, 4})
buffer := alloc.NewLocalBuffer(2048) buffer := buf.NewLocalBuffer(2048)
defer buffer.Release() defer buffer.Release()
response.Write(buffer) response.Write(buffer)

View File

@ -5,7 +5,7 @@ import (
"io" "io"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
@ -30,7 +30,7 @@ func TestHasAuthenticationMethod(t *testing.T) {
func TestAuthenticationRequestRead(t *testing.T) { func TestAuthenticationRequestRead(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
buffer.AppendBytes( buffer.AppendBytes(
0x05, // version 0x05, // version
0x01, // nMethods 0x01, // nMethods
@ -85,7 +85,7 @@ func TestResponseWrite(t *testing.T) {
[16]byte{}, [16]byte{},
v2net.Port(53), v2net.Port(53),
} }
buffer := alloc.NewLocalBuffer(2048) buffer := buf.NewLocalBuffer(2048)
defer buffer.Release() defer buffer.Release()
response.Write(buffer) response.Write(buffer)
@ -106,7 +106,7 @@ func TestSetIPv6(t *testing.T) {
response := NewSocks5Response() response := NewSocks5Response()
response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}) response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
buffer := alloc.NewLocalBuffer(2048) buffer := buf.NewLocalBuffer(2048)
defer buffer.Release() defer buffer.Release()
response.Write(buffer) response.Write(buffer)
assert.Bytes(buffer.Bytes()).Equals([]byte{ assert.Bytes(buffer.Bytes()).Equals([]byte{
@ -119,7 +119,7 @@ func TestSetDomain(t *testing.T) {
response := NewSocks5Response() response := NewSocks5Response()
response.SetDomain("v2ray.com") response.SetDomain("v2ray.com")
buffer := alloc.NewLocalBuffer(2048) buffer := buf.NewLocalBuffer(2048)
defer buffer.Release() defer buffer.Release()
response.Write(buffer) response.Write(buffer)
assert.Bytes(buffer.Bytes()).Equals([]byte{ assert.Bytes(buffer.Bytes()).Equals([]byte{
@ -129,7 +129,7 @@ func TestSetDomain(t *testing.T) {
func TestEmptyAuthRequest(t *testing.T) { func TestEmptyAuthRequest(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
_, _, err := ReadAuthentication(alloc.NewBuffer()) _, _, err := ReadAuthentication(buf.NewBuffer())
assert.Error(err).Equals(io.EOF) assert.Error(err).Equals(io.EOF)
} }
@ -143,7 +143,7 @@ func TestSingleByteAuthRequest(t *testing.T) {
func TestZeroAuthenticationMethod(t *testing.T) { func TestZeroAuthenticationMethod(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
buffer.AppendBytes(5, 0) buffer.AppendBytes(5, 0)
_, _, err := ReadAuthentication(buffer) _, _, err := ReadAuthentication(buffer)
assert.Error(err).Equals(crypto.ErrAuthenticationFailed) assert.Error(err).Equals(crypto.ErrAuthenticationFailed)
@ -151,7 +151,7 @@ func TestZeroAuthenticationMethod(t *testing.T) {
func TestWrongProtocolVersion(t *testing.T) { func TestWrongProtocolVersion(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
buffer.AppendBytes(6, 1, 0) buffer.AppendBytes(6, 1, 0)
_, _, err := ReadAuthentication(buffer) _, _, err := ReadAuthentication(buffer)
assert.Error(err).Equals(proxy.ErrInvalidProtocolVersion) assert.Error(err).Equals(proxy.ErrInvalidProtocolVersion)
@ -160,14 +160,14 @@ func TestWrongProtocolVersion(t *testing.T) {
func TestEmptyRequest(t *testing.T) { func TestEmptyRequest(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
_, err := ReadRequest(alloc.NewBuffer()) _, err := ReadRequest(buf.NewBuffer())
assert.Error(err).Equals(io.EOF) assert.Error(err).Equals(io.EOF)
} }
func TestIPv6Request(t *testing.T) { func TestIPv6Request(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
b := alloc.NewBuffer() b := buf.NewBuffer()
b.AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8) b.AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8)
request, err := ReadRequest(b) request, err := ReadRequest(b)
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -2,7 +2,7 @@ package protocol
import ( import (
"fmt" "fmt"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"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/serial" "v2ray.com/core/common/serial"
@ -16,14 +16,14 @@ type Socks5UDPRequest struct {
Fragment byte Fragment byte
Address v2net.Address Address v2net.Address
Port v2net.Port Port v2net.Port
Data *alloc.Buffer Data *buf.Buffer
} }
func (request *Socks5UDPRequest) Destination() v2net.Destination { func (request *Socks5UDPRequest) Destination() v2net.Destination {
return v2net.UDPDestination(request.Address, request.Port) return v2net.UDPDestination(request.Address, request.Port)
} }
func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) { func (request *Socks5UDPRequest) Write(buffer *buf.Buffer) {
buffer.AppendBytes(0, 0, request.Fragment) buffer.AppendBytes(0, 0, request.Fragment)
switch request.Address.Family() { switch request.Address.Family() {
case v2net.AddressFamilyIPv4: case v2net.AddressFamilyIPv4:
@ -83,7 +83,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
} }
if len(packet) > dataBegin { if len(packet) > dataBegin {
b := alloc.NewSmallBuffer() b := buf.NewSmallBuffer()
b.Append(packet[dataBegin:]) b.Append(packet[dataBegin:])
request.Data = b request.Data = b
} }

View File

@ -1,7 +1,7 @@
package socks package socks
import ( import (
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
@ -23,7 +23,7 @@ func (v *Server) listenUDP() error {
return nil return nil
} }
func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) { func (v *Server) handleUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) {
source := session.Source source := session.Source
log.Info("Socks: Client UDP connection from ", source) log.Info("Socks: Client UDP connection from ", source)
request, err := protocol.ReadUDPRequest(payload.Bytes()) request, err := protocol.ReadUDPRequest(payload.Bytes())
@ -46,7 +46,7 @@ func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionI
log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes") log.Info("Socks: Send packet to ", request.Destination(), " with ", request.Data.Len(), " bytes")
log.Access(source, request.Destination, log.AccessAccepted, "") log.Access(source, request.Destination, log.AccessAccepted, "")
v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: v.meta}, request.Data, func(destination v2net.Destination, payload *alloc.Buffer) { v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: request.Destination(), Inbound: v.meta}, request.Data, func(destination v2net.Destination, payload *buf.Buffer) {
response := &protocol.Socks5UDPRequest{ response := &protocol.Socks5UDPRequest{
Fragment: 0, Fragment: 0,
Address: request.Destination().Address, Address: request.Destination().Address,
@ -55,7 +55,7 @@ func (v *Server) handleUDPPayload(payload *alloc.Buffer, session *proxy.SessionI
} }
log.Info("Socks: Writing back UDP response with ", payload.Len(), " bytes to ", destination) log.Info("Socks: Writing back UDP response with ", payload.Len(), " bytes to ", destination)
udpMessage := alloc.NewLocalBuffer(2048) udpMessage := buf.NewLocalBuffer(2048)
response.Write(udpMessage) response.Write(udpMessage)
v.udpMutex.RLock() v.udpMutex.RLock()

View File

@ -5,7 +5,7 @@ import (
"sync" "sync"
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
@ -18,7 +18,7 @@ type OutboundConnectionHandler struct {
ConnOutput io.Writer ConnOutput io.Writer
} }
func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
input := ray.OutboundInput() input := ray.OutboundInput()
output := ray.OutboundOutput() output := ray.OutboundOutput()

View File

@ -3,7 +3,7 @@ package encoding
import ( import (
"io" "io"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"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/protocol" "v2ray.com/core/common/protocol"
@ -32,7 +32,7 @@ func MarshalCommand(command interface{}, writer io.Writer) error {
return ErrUnknownCommand return ErrUnknownCommand
} }
buffer := alloc.NewLocalBuffer(512) buffer := buf.NewLocalBuffer(512)
defer buffer.Release() defer buffer.Release()
err := factory.Marshal(command, buffer) err := factory.Marshal(command, buffer)

View File

@ -3,7 +3,7 @@ package encoding_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid" "v2ray.com/core/common/uuid"
. "v2ray.com/core/proxy/vmess/encoding" . "v2ray.com/core/proxy/vmess/encoding"
@ -21,7 +21,7 @@ func TestSwitchAccount(t *testing.T) {
ValidMin: 16, ValidMin: 16,
} }
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
err := MarshalCommand(sa, buffer) err := MarshalCommand(sa, buffer)
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -3,7 +3,7 @@ package encoding_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/loader" "v2ray.com/core/common/loader"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -36,7 +36,7 @@ func TestRequestSerialization(t *testing.T) {
Security: protocol.Security(protocol.SecurityType_AES128_GCM), Security: protocol.Security(protocol.SecurityType_AES128_GCM),
} }
buffer := alloc.NewBuffer() buffer := buf.NewBuffer()
client := NewClientSession(protocol.DefaultIDHash) client := NewClientSession(protocol.DefaultIDHash)
client.EncodeRequestHeader(expectedRequest, buffer) client.EncodeRequestHeader(expectedRequest, buffer)

View File

@ -8,7 +8,7 @@ import (
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/proxyman" "v2ray.com/core/app/proxyman"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader" "v2ray.com/core/common/loader"
@ -225,7 +225,7 @@ func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) {
} }
output.Release() output.Release()
if request.Option.Has(protocol.RequestOptionChunkStream) { if request.Option.Has(protocol.RequestOptionChunkStream) {
if err := bodyWriter.Write(alloc.NewLocalBuffer(8)); err != nil { if err := bodyWriter.Write(buf.NewLocalBuffer(8)); err != nil {
connection.SetReusable(false) connection.SetReusable(false)
} }
} }

View File

@ -4,7 +4,7 @@ import (
"sync" "sync"
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader" "v2ray.com/core/common/loader"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
@ -25,7 +25,7 @@ type VMessOutboundHandler struct {
meta *proxy.OutboundHandlerMeta meta *proxy.OutboundHandlerMeta
} }
func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) { func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
defer ray.OutboundInput().Release() defer ray.OutboundInput().Release()
defer ray.OutboundOutput().Close() defer ray.OutboundOutput().Close()
@ -92,7 +92,7 @@ func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc
return return
} }
func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) { func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *buf.Buffer, input v2io.Reader, finish *sync.Mutex) {
defer finish.Unlock() defer finish.Unlock()
writer := v2io.NewBufferedWriter(conn) writer := v2io.NewBufferedWriter(conn)
@ -116,7 +116,7 @@ func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, co
} }
if request.Option.Has(protocol.RequestOptionChunkStream) { if request.Option.Has(protocol.RequestOptionChunkStream) {
err := bodyWriter.Write(alloc.NewLocalBuffer(8)) err := bodyWriter.Write(buf.NewLocalBuffer(8))
if err != nil { if err != nil {
conn.SetReusable(false) conn.SetReusable(false)
} }

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/tcp"
@ -42,7 +42,7 @@ func TestShadowsocksTCP(t *testing.T) {
//conn.CloseWrite() //conn.CloseWrite()
response := alloc.NewBuffer() response := buf.NewBuffer()
finished := false finished := false
expectedResponse := "Processed: " + payload expectedResponse := "Processed: " + payload
for { for {

View File

@ -2,12 +2,12 @@ package internet
import ( import (
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
type Authenticator interface { type Authenticator interface {
Seal(*alloc.Buffer) Seal(*buf.Buffer)
Open(*alloc.Buffer) bool Open(*buf.Buffer) bool
Overhead() int Overhead() int
} }
@ -53,7 +53,7 @@ func (v *AuthenticatorChain) Overhead() int {
return total return total
} }
func (v *AuthenticatorChain) Open(payload *alloc.Buffer) bool { func (v *AuthenticatorChain) Open(payload *buf.Buffer) bool {
for _, auth := range v.authenticators { for _, auth := range v.authenticators {
if !auth.Open(payload) { if !auth.Open(payload) {
return false return false
@ -62,7 +62,7 @@ func (v *AuthenticatorChain) Open(payload *alloc.Buffer) bool {
return true return true
} }
func (v *AuthenticatorChain) Seal(payload *alloc.Buffer) { func (v *AuthenticatorChain) Seal(payload *buf.Buffer) {
for i := len(v.authenticators) - 1; i >= 0; i-- { for i := len(v.authenticators) - 1; i >= 0; i-- {
auth := v.authenticators[i] auth := v.authenticators[i]
auth.Seal(payload) auth.Seal(payload)

View File

@ -7,7 +7,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"time" "time"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/loader" "v2ray.com/core/common/loader"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
@ -23,7 +23,7 @@ var (
) )
type Reader interface { type Reader interface {
Read(io.Reader) (*alloc.Buffer, error) Read(io.Reader) (*buf.Buffer, error)
} }
type Writer interface { type Writer interface {
@ -32,7 +32,7 @@ type Writer interface {
type NoOpReader struct{} type NoOpReader struct{}
func (v *NoOpReader) Read(io.Reader) (*alloc.Buffer, error) { func (v *NoOpReader) Read(io.Reader) (*buf.Buffer, error) {
return nil, nil return nil, nil
} }
@ -45,8 +45,8 @@ func (v *NoOpWriter) Write(io.Writer) error {
type HeaderReader struct { type HeaderReader struct {
} }
func (*HeaderReader) Read(reader io.Reader) (*alloc.Buffer, error) { func (*HeaderReader) Read(reader io.Reader) (*buf.Buffer, error) {
buffer := alloc.NewSmallBuffer() buffer := buf.NewSmallBuffer()
for { for {
_, err := buffer.FillFrom(reader) _, err := buffer.FillFrom(reader)
if err != nil { if err != nil {
@ -69,10 +69,10 @@ func (*HeaderReader) Read(reader io.Reader) (*alloc.Buffer, error) {
} }
type HeaderWriter struct { type HeaderWriter struct {
header *alloc.Buffer header *buf.Buffer
} }
func NewHeaderWriter(header *alloc.Buffer) *HeaderWriter { func NewHeaderWriter(header *buf.Buffer) *HeaderWriter {
return &HeaderWriter{ return &HeaderWriter{
header: header, header: header,
} }
@ -91,7 +91,7 @@ func (v *HeaderWriter) Write(writer io.Writer) error {
type HttpConn struct { type HttpConn struct {
net.Conn net.Conn
readBuffer *alloc.Buffer readBuffer *buf.Buffer
oneTimeReader Reader oneTimeReader Reader
oneTimeWriter Writer oneTimeWriter Writer
} }
@ -143,7 +143,7 @@ type HttpAuthenticator struct {
} }
func (v HttpAuthenticator) GetClientWriter() *HeaderWriter { func (v HttpAuthenticator) GetClientWriter() *HeaderWriter {
header := alloc.NewSmallBuffer() header := buf.NewSmallBuffer()
config := v.config.Request config := v.config.Request
header.AppendFunc(serial.WriteString(strings.Join([]string{config.Method.GetValue(), config.PickUri(), config.GetFullVersion()}, " "))) header.AppendFunc(serial.WriteString(strings.Join([]string{config.Method.GetValue(), config.PickUri(), config.GetFullVersion()}, " ")))
header.AppendFunc(writeCRLF) header.AppendFunc(writeCRLF)
@ -160,7 +160,7 @@ func (v HttpAuthenticator) GetClientWriter() *HeaderWriter {
} }
func (v HttpAuthenticator) GetServerWriter() *HeaderWriter { func (v HttpAuthenticator) GetServerWriter() *HeaderWriter {
header := alloc.NewSmallBuffer() header := buf.NewSmallBuffer()
config := v.config.Response config := v.config.Response
header.AppendFunc(serial.WriteString(strings.Join([]string{config.GetFullVersion(), config.Status.GetCode(), config.Status.GetReason()}, " "))) header.AppendFunc(serial.WriteString(strings.Join([]string{config.GetFullVersion(), config.Status.GetCode(), config.Status.GetReason()}, " ")))
header.AppendFunc(writeCRLF) header.AppendFunc(writeCRLF)

View File

@ -3,7 +3,7 @@ package http_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
. "v2ray.com/core/transport/internet/headers/http" . "v2ray.com/core/transport/internet/headers/http"
@ -12,8 +12,8 @@ import (
func TestReaderWriter(t *testing.T) { func TestReaderWriter(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
cache := alloc.NewBuffer() cache := buf.NewBuffer()
b := alloc.NewLocalBuffer(256) b := buf.NewLocalBuffer(256)
b.AppendFunc(serial.WriteString("abcd" + ENDING)) b.AppendFunc(serial.WriteString("abcd" + ENDING))
writer := NewHeaderWriter(b) writer := NewHeaderWriter(b)
writer.Write(cache) writer.Write(cache)
@ -41,7 +41,7 @@ func TestRequestHeader(t *testing.T) {
}, },
}).(HttpAuthenticator) }).(HttpAuthenticator)
cache := alloc.NewBuffer() cache := buf.NewBuffer()
err := auth.GetClientWriter().Write(cache) err := auth.GetClientWriter().Write(cache)
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -3,7 +3,7 @@ package srtp_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
. "v2ray.com/core/transport/internet/headers/srtp" . "v2ray.com/core/transport/internet/headers/srtp"
) )
@ -14,7 +14,7 @@ func TestSRTPWrite(t *testing.T) {
content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
srtp := SRTP{} srtp := SRTP{}
payload := alloc.NewLocalBuffer(2048) payload := buf.NewLocalBuffer(2048)
payload.AppendFunc(srtp.Write) payload.AppendFunc(srtp.Write)
payload.Append(content) payload.Append(content)

View File

@ -3,7 +3,7 @@ package utp_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
. "v2ray.com/core/transport/internet/headers/utp" . "v2ray.com/core/transport/internet/headers/utp"
) )
@ -14,7 +14,7 @@ func TestUTPWrite(t *testing.T) {
content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
utp := UTP{} utp := UTP{}
payload := alloc.NewLocalBuffer(2048) payload := buf.NewLocalBuffer(2048)
payload.AppendFunc(utp.Write) payload.AppendFunc(utp.Write)
payload.Append(content) payload.Append(content)

View File

@ -8,7 +8,7 @@ import (
"crypto/cipher" "crypto/cipher"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/dice" "v2ray.com/core/common/dice"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
@ -88,7 +88,7 @@ func (o *ClientConnection) ResetSecurity(header internet.PacketHeader, security
} }
func (o *ClientConnection) Run() { func (o *ClientConnection) Run() {
payload := alloc.NewSmallBuffer() payload := buf.NewSmallBuffer()
defer payload.Release() defer payload.Release()
for { for {

View File

@ -9,7 +9,7 @@ import (
"crypto/cipher" "crypto/cipher"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
@ -140,7 +140,7 @@ func NewListener(address v2net.Address, port v2net.Port, options internet.Listen
return l, nil return l, nil
} }
func (v *Listener) OnReceive(payload *alloc.Buffer, session *proxy.SessionInfo) { func (v *Listener) OnReceive(payload *buf.Buffer, session *proxy.SessionInfo) {
defer payload.Release() defer payload.Release()
src := session.Source src := session.Source

View File

@ -4,7 +4,7 @@ import (
"io" "io"
"sync" "sync"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
type SegmentWriter interface { type SegmentWriter interface {
@ -14,7 +14,7 @@ type SegmentWriter interface {
type BufferedSegmentWriter struct { type BufferedSegmentWriter struct {
sync.Mutex sync.Mutex
mtu uint32 mtu uint32
buffer *alloc.Buffer buffer *buf.Buffer
writer io.Writer writer io.Writer
} }
@ -22,7 +22,7 @@ func NewSegmentWriter(writer io.Writer, mtu uint32) *BufferedSegmentWriter {
return &BufferedSegmentWriter{ return &BufferedSegmentWriter{
mtu: mtu, mtu: mtu,
writer: writer, writer: writer,
buffer: alloc.NewSmallBuffer(), buffer: buf.NewSmallBuffer(),
} }
} }

View File

@ -3,7 +3,7 @@ package kcp
import ( import (
"sync" "sync"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
type ReceivingWindow struct { type ReceivingWindow struct {
@ -145,7 +145,7 @@ func (v *AckList) Flush(current uint32, rto uint32) {
type ReceivingWorker struct { type ReceivingWorker struct {
sync.RWMutex sync.RWMutex
conn *Connection conn *Connection
leftOver *alloc.Buffer leftOver *buf.Buffer
window *ReceivingWindow window *ReceivingWindow
acklist *AckList acklist *AckList
nextNumber uint32 nextNumber uint32

View File

@ -2,7 +2,7 @@ package kcp
import ( import (
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
@ -31,7 +31,7 @@ type Segment interface {
Conversation() uint16 Conversation() uint16
Command() Command Command() Command
ByteSize() int ByteSize() int
Bytes() alloc.BytesWriter Bytes() buf.BytesWriter
} }
const ( const (
@ -44,7 +44,7 @@ type DataSegment struct {
Timestamp uint32 Timestamp uint32
Number uint32 Number uint32
SendingNext uint32 SendingNext uint32
Data *alloc.Buffer Data *buf.Buffer
timeout uint32 timeout uint32
transmit uint32 transmit uint32
@ -64,13 +64,13 @@ func (v *DataSegment) Command() Command {
func (v *DataSegment) SetData(b []byte) { func (v *DataSegment) SetData(b []byte) {
if v.Data == nil { if v.Data == nil {
v.Data = alloc.NewSmallBuffer() v.Data = buf.NewSmallBuffer()
} }
v.Data.Clear() v.Data.Clear()
v.Data.Append(b) v.Data.Append(b)
} }
func (v *DataSegment) Bytes() alloc.BytesWriter { func (v *DataSegment) Bytes() buf.BytesWriter {
return func(b []byte) int { return func(b []byte) int {
b = serial.Uint16ToBytes(v.Conv, b[:0]) b = serial.Uint16ToBytes(v.Conv, b[:0])
b = append(b, byte(CommandData), byte(v.Option)) b = append(b, byte(CommandData), byte(v.Option))
@ -137,7 +137,7 @@ func (v *AckSegment) ByteSize() int {
return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(v.Count)*4 return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(v.Count)*4
} }
func (v *AckSegment) Bytes() alloc.BytesWriter { func (v *AckSegment) Bytes() buf.BytesWriter {
return func(b []byte) int { return func(b []byte) int {
b = serial.Uint16ToBytes(v.Conv, b[:0]) b = serial.Uint16ToBytes(v.Conv, b[:0])
b = append(b, byte(CommandACK), byte(v.Option)) b = append(b, byte(CommandACK), byte(v.Option))
@ -181,7 +181,7 @@ func (v *CmdOnlySegment) ByteSize() int {
return 2 + 1 + 1 + 4 + 4 + 4 return 2 + 1 + 1 + 4 + 4 + 4
} }
func (v *CmdOnlySegment) Bytes() alloc.BytesWriter { func (v *CmdOnlySegment) Bytes() buf.BytesWriter {
return func(b []byte) int { return func(b []byte) int {
b = serial.Uint16ToBytes(v.Conv, b[:0]) b = serial.Uint16ToBytes(v.Conv, b[:0])
b = append(b, byte(v.Cmd), byte(v.Option)) b = append(b, byte(v.Cmd), byte(v.Option))

View File

@ -3,7 +3,7 @@ package kcp_test
import ( import (
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
. "v2ray.com/core/transport/internet/kcp" . "v2ray.com/core/transport/internet/kcp"
) )
@ -19,7 +19,7 @@ func TestBadSegment(t *testing.T) {
func TestDataSegment(t *testing.T) { func TestDataSegment(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
b := alloc.NewLocalBuffer(512) b := buf.NewLocalBuffer(512)
b.Append([]byte{'a', 'b', 'c', 'd'}) b.Append([]byte{'a', 'b', 'c', 'd'})
seg := &DataSegment{ seg := &DataSegment{
Conv: 1, Conv: 1,

View File

@ -4,7 +4,7 @@ import (
"net" "net"
"sync" "sync"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/dice" "v2ray.com/core/common/dice"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
@ -14,11 +14,11 @@ import (
) )
type UDPPayload struct { type UDPPayload struct {
payload *alloc.Buffer payload *buf.Buffer
session *proxy.SessionInfo session *proxy.SessionInfo
} }
type UDPPayloadHandler func(*alloc.Buffer, *proxy.SessionInfo) type UDPPayloadHandler func(*buf.Buffer, *proxy.SessionInfo)
type UDPPayloadQueue struct { type UDPPayloadQueue struct {
queue []chan UDPPayload queue []chan UDPPayload
@ -135,7 +135,7 @@ func (v *UDPHub) start() {
oobBytes := make([]byte, 256) oobBytes := make([]byte, 256)
for v.Running() { for v.Running() {
buffer := alloc.NewSmallBuffer() buffer := buf.NewSmallBuffer()
var noob int var noob int
var addr *net.UDPAddr var addr *net.UDPAddr
var err error var err error

View File

@ -7,7 +7,7 @@ import (
"syscall" "syscall"
"testing" "testing"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
@ -23,7 +23,7 @@ func TestHubSocksOption(t *testing.T) {
} }
hub, err := ListenUDP(v2net.LocalHostIP, v2net.Port(0), ListenOption{ hub, err := ListenUDP(v2net.LocalHostIP, v2net.Port(0), ListenOption{
Callback: func(*alloc.Buffer, *proxy.SessionInfo) {}, Callback: func(*buf.Buffer, *proxy.SessionInfo) {},
ReceiveOriginalDest: true, ReceiveOriginalDest: true,
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -5,14 +5,14 @@ import (
"time" "time"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/transport/ray" "v2ray.com/core/transport/ray"
) )
type UDPResponseCallback func(destination v2net.Destination, payload *alloc.Buffer) type UDPResponseCallback func(destination v2net.Destination, payload *buf.Buffer)
type TimedInboundRay struct { type TimedInboundRay struct {
name string name string
@ -111,7 +111,7 @@ func (v *UDPServer) RemoveRay(name string) {
delete(v.conns, name) delete(v.conns, name)
} }
func (v *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer) bool { func (v *UDPServer) locateExistingAndDispatch(name string, payload *buf.Buffer) bool {
log.Debug("UDP Server: Locating existing connection for ", name) log.Debug("UDP Server: Locating existing connection for ", name)
v.RLock() v.RLock()
defer v.RUnlock() defer v.RUnlock()
@ -130,7 +130,7 @@ func (v *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer
return false return false
} }
func (v *UDPServer) Dispatch(session *proxy.SessionInfo, payload *alloc.Buffer, callback UDPResponseCallback) { func (v *UDPServer) Dispatch(session *proxy.SessionInfo, payload *buf.Buffer, callback UDPResponseCallback) {
source := session.Source source := session.Source
destination := session.Destination destination := session.Destination

View File

@ -5,7 +5,7 @@ import (
"sync" "sync"
"time" "time"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/buf"
) )
const ( const (
@ -44,16 +44,16 @@ func (v *directRay) InboundOutput() InputStream {
type Stream struct { type Stream struct {
access sync.RWMutex access sync.RWMutex
closed bool closed bool
buffer chan *alloc.Buffer buffer chan *buf.Buffer
} }
func NewStream() *Stream { func NewStream() *Stream {
return &Stream{ return &Stream{
buffer: make(chan *alloc.Buffer, bufferSize), buffer: make(chan *buf.Buffer, bufferSize),
} }
} }
func (v *Stream) Read() (*alloc.Buffer, error) { func (v *Stream) Read() (*buf.Buffer, error) {
if v.buffer == nil { if v.buffer == nil {
return nil, io.EOF return nil, io.EOF
} }
@ -71,7 +71,7 @@ func (v *Stream) Read() (*alloc.Buffer, error) {
return result, nil return result, nil
} }
func (v *Stream) Write(data *alloc.Buffer) error { func (v *Stream) Write(data *buf.Buffer) error {
for !v.closed { for !v.closed {
err := v.TryWriteOnce(data) err := v.TryWriteOnce(data)
if err != io.ErrNoProgress { if err != io.ErrNoProgress {
@ -81,7 +81,7 @@ func (v *Stream) Write(data *alloc.Buffer) error {
return io.ErrClosedPipe return io.ErrClosedPipe
} }
func (v *Stream) TryWriteOnce(data *alloc.Buffer) error { func (v *Stream) TryWriteOnce(data *buf.Buffer) error {
v.access.RLock() v.access.RLock()
defer v.access.RUnlock() defer v.access.RUnlock()
if v.closed { if v.closed {