mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
format code
This commit is contained in:
parent
19308037e5
commit
f09d65b2e5
102
io/bufferset.go
102
io/bufferset.go
@ -1,75 +1,75 @@
|
|||||||
package io
|
package io
|
||||||
|
|
||||||
import(
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SizeSmall = 16
|
SizeSmall = 16
|
||||||
SizeMedium = 128
|
SizeMedium = 128
|
||||||
SizeLarge = 512
|
SizeLarge = 512
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrorNoChannel = errors.New("No suitable channels found.")
|
ErrorNoChannel = errors.New("No suitable channels found.")
|
||||||
)
|
)
|
||||||
|
|
||||||
type VBufferSet struct {
|
type VBufferSet struct {
|
||||||
small chan []byte
|
small chan []byte
|
||||||
medium chan []byte
|
medium chan []byte
|
||||||
large chan []byte
|
large chan []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVBufferSet() *VBufferSet {
|
func NewVBufferSet() *VBufferSet {
|
||||||
bSet := new(VBufferSet)
|
bSet := new(VBufferSet)
|
||||||
bSet.small = make(chan []byte, 128)
|
bSet.small = make(chan []byte, 128)
|
||||||
bSet.medium = make(chan []byte, 128)
|
bSet.medium = make(chan []byte, 128)
|
||||||
bSet.large = make(chan []byte, 128)
|
bSet.large = make(chan []byte, 128)
|
||||||
return bSet
|
return bSet
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bSet *VBufferSet) detectBucket(size int, strict bool) (chan []byte, error) {
|
func (bSet *VBufferSet) detectBucket(size int, strict bool) (chan []byte, error) {
|
||||||
if strict {
|
if strict {
|
||||||
if size == SizeSmall {
|
if size == SizeSmall {
|
||||||
return bSet.small, nil
|
return bSet.small, nil
|
||||||
} else if size == SizeMedium {
|
} else if size == SizeMedium {
|
||||||
return bSet.medium, nil
|
return bSet.medium, nil
|
||||||
} else if size == SizeLarge {
|
} else if size == SizeLarge {
|
||||||
return bSet.large, nil
|
return bSet.large, nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if size <= SizeSmall {
|
if size <= SizeSmall {
|
||||||
return bSet.small, nil
|
return bSet.small, nil
|
||||||
} else if size <= SizeMedium {
|
} else if size <= SizeMedium {
|
||||||
return bSet.medium, nil
|
return bSet.medium, nil
|
||||||
} else if size <= SizeLarge {
|
} else if size <= SizeLarge {
|
||||||
return bSet.large, nil
|
return bSet.large, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, ErrorNoChannel
|
return nil, ErrorNoChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bSet *VBufferSet) FetchBuffer(minSize int) []byte {
|
func (bSet *VBufferSet) FetchBuffer(minSize int) []byte {
|
||||||
var buffer []byte
|
var buffer []byte
|
||||||
byteChan, err := bSet.detectBucket(minSize, false)
|
byteChan, err := bSet.detectBucket(minSize, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return make([]byte, minSize)
|
return make([]byte, minSize)
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case buffer = <- byteChan:
|
case buffer = <-byteChan:
|
||||||
default:
|
default:
|
||||||
buffer = make([]byte, minSize)
|
buffer = make([]byte, minSize)
|
||||||
}
|
}
|
||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bSet *VBufferSet) ReturnBuffer(buffer []byte) {
|
func (bSet *VBufferSet) ReturnBuffer(buffer []byte) {
|
||||||
byteChan, err := bSet.detectBucket(len(buffer), true)
|
byteChan, err := bSet.detectBucket(len(buffer), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case byteChan <- buffer:
|
case byteChan <- buffer:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ package json
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
_ "fmt"
|
_ "fmt"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,62 +1,62 @@
|
|||||||
package tcp
|
package tcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"io"
|
||||||
"io"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VFreeConnection struct {
|
type VFreeConnection struct {
|
||||||
network string
|
network string
|
||||||
address string
|
address string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVFreeConnection(network string, address string) *VFreeConnection {
|
func NewVFreeConnection(network string, address string) *VFreeConnection {
|
||||||
conn := new(VFreeConnection)
|
conn := new(VFreeConnection)
|
||||||
conn.network = network
|
conn.network = network
|
||||||
conn.address = address
|
conn.address = address
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vconn *VFreeConnection) Start(input <-chan []byte) chan<- []byte {
|
func (vconn *VFreeConnection) Start(input <-chan []byte) chan<- []byte {
|
||||||
output := make(chan []byte, 128)
|
output := make(chan []byte, 128)
|
||||||
conn, err := net.Dial(vconn.network, vconn.address)
|
conn, err := net.Dial(vconn.network, vconn.address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
finish := make(chan bool, 2)
|
finish := make(chan bool, 2)
|
||||||
go vconn.DumpInput(conn, input, finish)
|
go vconn.DumpInput(conn, input, finish)
|
||||||
go vconn.DumpOutput(conn, output, finish)
|
go vconn.DumpOutput(conn, output, finish)
|
||||||
go vconn.CloseConn(conn, finish)
|
go vconn.CloseConn(conn, finish)
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vconn *VFreeConnection) DumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {
|
func (vconn *VFreeConnection) DumpInput(conn net.Conn, input <-chan []byte, finish chan<- bool) {
|
||||||
for {
|
for {
|
||||||
data, open := <-input
|
data, open := <-input
|
||||||
if !open {
|
if !open {
|
||||||
finish <- true
|
finish <- true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
conn.Write(data)
|
conn.Write(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vconn *VFreeConnection) DumpOutput(conn net.Conn, output chan<- []byte, finish chan<- bool) {
|
func (vconn *VFreeConnection) DumpOutput(conn net.Conn, output chan<- []byte, finish chan<- bool) {
|
||||||
for {
|
for {
|
||||||
buffer := make([]byte, 128)
|
buffer := make([]byte, 128)
|
||||||
nBytes, err := conn.Read(buffer)
|
nBytes, err := conn.Read(buffer)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
finish <- true
|
finish <- true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
output <- buffer[:nBytes]
|
output <- buffer[:nBytes]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vconn *VFreeConnection) CloseConn(conn net.Conn, finish <-chan bool) {
|
func (vconn *VFreeConnection) CloseConn(conn net.Conn, finish <-chan bool) {
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
<-finish
|
<-finish
|
||||||
}
|
}
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user