From c3942fd5f0b017b55a06cf29864e1c6575c17dd9 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 21 Dec 2016 15:48:39 +0100 Subject: [PATCH] fix lint warnings --- proxy/dokodemo/dokodemo.go | 2 +- proxy/shadowsocks/server.go | 2 +- proxy/socks/server.go | 2 +- transport/internet/kcp/listener.go | 4 +- transport/internet/tcp/connection.go | 2 +- transport/internet/udp/hub.go | 56 +++++++++++++++------------- 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index b32006bd..a8a93dfc 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -24,7 +24,7 @@ type DokodemoDoor struct { port v2net.Port packetDispatcher dispatcher.PacketDispatcher tcpListener *internet.TCPHub - udpHub *udp.UDPHub + udpHub *udp.Hub udpServer *udp.Server meta *proxy.InboundHandlerMeta } diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 19d2e654..603f2735 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -26,7 +26,7 @@ type Server struct { meta *proxy.InboundHandlerMeta accepting bool tcpHub *internet.TCPHub - udpHub *udp.UDPHub + udpHub *udp.Hub udpServer *udp.Server } diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 6042fc72..3fa27c36 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -33,7 +33,7 @@ type Server struct { packetDispatcher dispatcher.PacketDispatcher config *ServerConfig tcpListener *internet.TCPHub - udpHub *udp.UDPHub + udpHub *udp.Hub udpAddress v2net.Destination udpServer *udp.Server meta *proxy.InboundHandlerMeta diff --git a/transport/internet/kcp/listener.go b/transport/internet/kcp/listener.go index 294678c0..b3db87c0 100644 --- a/transport/internet/kcp/listener.go +++ b/transport/internet/kcp/listener.go @@ -83,7 +83,7 @@ type Listener struct { running bool sessions map[ConnectionID]*Connection awaitingConns chan *Connection - hub *udp.UDPHub + hub *udp.Hub tlsConfig *tls.Config config *Config reader PacketReader @@ -279,7 +279,7 @@ func (v *Listener) Put(internal.ConnectionId, net.Conn) {} type Writer struct { id ConnectionID dest v2net.Destination - hub *udp.UDPHub + hub *udp.Hub listener *Listener } diff --git a/transport/internet/tcp/connection.go b/transport/internet/tcp/connection.go index 0c9b6009..043052a9 100644 --- a/transport/internet/tcp/connection.go +++ b/transport/internet/tcp/connection.go @@ -28,9 +28,9 @@ func (v *RawConnection) SysFd() (int, error) { type Connection struct { id internal.ConnectionId + reusable bool conn net.Conn listener ConnectionManager - reusable bool config *Config } diff --git a/transport/internet/udp/hub.go b/transport/internet/udp/hub.go index 4aca426a..05913907 100644 --- a/transport/internet/udp/hub.go +++ b/transport/internet/udp/hub.go @@ -13,31 +13,35 @@ import ( "v2ray.com/core/transport/internet/internal" ) -type UDPPayload struct { +// Payload represents a single UDP payload. +type Payload struct { payload *buf.Buffer session *proxy.SessionInfo } -type UDPPayloadHandler func(*buf.Buffer, *proxy.SessionInfo) +// PayloadHandler is function to handle Payload. +type PayloadHandler func(*buf.Buffer, *proxy.SessionInfo) -type UDPPayloadQueue struct { - queue []chan UDPPayload - callback UDPPayloadHandler +// PayloadQueue is a queue of Payload. +type PayloadQueue struct { + queue []chan Payload + callback PayloadHandler } -func NewUDPPayloadQueue(option ListenOption) *UDPPayloadQueue { - queue := &UDPPayloadQueue{ +// NewPayloadQueue returns a new PayloadQueue. +func NewPayloadQueue(option ListenOption) *PayloadQueue { + queue := &PayloadQueue{ callback: option.Callback, - queue: make([]chan UDPPayload, option.Concurrency), + queue: make([]chan Payload, option.Concurrency), } for i := range queue.queue { - queue.queue[i] = make(chan UDPPayload, 64) + queue.queue[i] = make(chan Payload, 64) go queue.Dequeue(queue.queue[i]) } return queue } -func (v *UDPPayloadQueue) Enqueue(payload UDPPayload) { +func (v *PayloadQueue) Enqueue(payload Payload) { size := len(v.queue) idx := 0 if size > 1 { @@ -53,33 +57,33 @@ func (v *UDPPayloadQueue) Enqueue(payload UDPPayload) { } } -func (v *UDPPayloadQueue) Dequeue(queue <-chan UDPPayload) { +func (v *PayloadQueue) Dequeue(queue <-chan Payload) { for payload := range queue { v.callback(payload.payload, payload.session) } } -func (v *UDPPayloadQueue) Close() { +func (v *PayloadQueue) Close() { for _, queue := range v.queue { close(queue) } } type ListenOption struct { - Callback UDPPayloadHandler + Callback PayloadHandler ReceiveOriginalDest bool Concurrency int } -type UDPHub struct { +type Hub struct { sync.RWMutex conn *net.UDPConn cancel *signal.CancelSignal - queue *UDPPayloadQueue + queue *PayloadQueue option ListenOption } -func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UDPHub, error) { +func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*Hub, error) { if option.Concurrency < 1 { option.Concurrency = 1 } @@ -102,9 +106,9 @@ func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UD return nil, err } } - hub := &UDPHub{ + hub := &Hub{ conn: udpConn, - queue: NewUDPPayloadQueue(option), + queue: NewPayloadQueue(option), option: option, cancel: signal.NewCloseSignal(), } @@ -112,7 +116,7 @@ func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UD return hub, nil } -func (v *UDPHub) Close() { +func (v *Hub) Close() { v.Lock() defer v.Unlock() @@ -122,14 +126,14 @@ func (v *UDPHub) Close() { v.queue.Close() } -func (v *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error) { +func (v *Hub) WriteTo(payload []byte, dest v2net.Destination) (int, error) { return v.conn.WriteToUDP(payload, &net.UDPAddr{ IP: dest.Address.IP(), Port: int(dest.Port), }) } -func (v *UDPHub) start() { +func (v *Hub) start() { v.cancel.WaitThread() defer v.cancel.FinishThread() @@ -156,23 +160,23 @@ func (v *UDPHub) start() { if v.option.ReceiveOriginalDest && noob > 0 { session.Destination = RetrieveOriginalDest(oobBytes[:noob]) } - v.queue.Enqueue(UDPPayload{ + v.queue.Enqueue(Payload{ payload: buffer, session: session, }) } } -func (v *UDPHub) Running() bool { +func (v *Hub) Running() bool { return !v.cancel.Cancelled() } -// Connection return the net.Conn underneath v hub. +// Connection return the net.Conn underneath this hub. // Private: Visible for testing only -func (v *UDPHub) Connection() net.Conn { +func (v *Hub) Connection() net.Conn { return v.conn } -func (v *UDPHub) Addr() net.Addr { +func (v *Hub) Addr() net.Addr { return v.conn.LocalAddr() }