fix lint warnings for websocket

pull/642/merge
Darien Raymond 2017-10-21 09:45:27 +02:00
parent afe190316d
commit 6dd23b45b0
4 changed files with 20 additions and 16 deletions

View File

@ -17,7 +17,7 @@ var (
// connection is a wrapper for net.Conn over WebSocket connection. // connection is a wrapper for net.Conn over WebSocket connection.
type connection struct { type connection struct {
wsc *websocket.Conn conn *websocket.Conn
reader io.Reader reader io.Reader
mergingReader buf.Reader mergingReader buf.Reader
@ -26,7 +26,7 @@ type connection struct {
func newConnection(conn *websocket.Conn) *connection { func newConnection(conn *websocket.Conn) *connection {
return &connection{ return &connection{
wsc: conn, conn: conn,
} }
} }
@ -59,7 +59,7 @@ func (c *connection) getReader() (io.Reader, error) {
return c.reader, nil return c.reader, nil
} }
_, reader, err := c.wsc.NextReader() _, reader, err := c.conn.NextReader()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -69,7 +69,7 @@ func (c *connection) getReader() (io.Reader, error) {
// Write implements io.Writer. // Write implements io.Writer.
func (c *connection) Write(b []byte) (int, error) { func (c *connection) Write(b []byte) (int, error) {
if err := c.wsc.WriteMessage(websocket.BinaryMessage, b); err != nil { if err := c.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
return 0, err return 0, err
} }
return len(b), nil return len(b), nil
@ -83,16 +83,16 @@ func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
} }
func (c *connection) Close() error { func (c *connection) Close() error {
c.wsc.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)) c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5))
return c.wsc.Close() return c.conn.Close()
} }
func (c *connection) LocalAddr() net.Addr { func (c *connection) LocalAddr() net.Addr {
return c.wsc.LocalAddr() return c.conn.LocalAddr()
} }
func (c *connection) RemoteAddr() net.Addr { func (c *connection) RemoteAddr() net.Addr {
return c.wsc.RemoteAddr() return c.conn.RemoteAddr()
} }
func (c *connection) SetDeadline(t time.Time) error { func (c *connection) SetDeadline(t time.Time) error {
@ -103,9 +103,9 @@ func (c *connection) SetDeadline(t time.Time) error {
} }
func (c *connection) SetReadDeadline(t time.Time) error { func (c *connection) SetReadDeadline(t time.Time) error {
return c.wsc.SetReadDeadline(t) return c.conn.SetReadDeadline(t)
} }
func (c *connection) SetWriteDeadline(t time.Time) error { func (c *connection) SetWriteDeadline(t time.Time) error {
return c.wsc.SetWriteDeadline(t) return c.conn.SetWriteDeadline(t)
} }

View File

@ -68,7 +68,5 @@ func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error)
return nil, newError("failed to dial to (", uri, "): ", reason).Base(err) return nil, newError("failed to dial to (", uri, "): ", reason).Base(err)
} }
return &connection{ return newConnection(conn), nil
wsc: conn,
}, nil
} }

View File

@ -90,19 +90,24 @@ func (ln *Listener) listenws(address net.Address, port net.Port) error {
ln.listener = listener ln.listener = listener
go func() { go func() {
http.Serve(listener, &requestHandler{ err := http.Serve(listener, &requestHandler{
path: ln.config.GetNormailzedPath(), path: ln.config.GetNormailzedPath(),
ln: ln, ln: ln,
}) })
if err != nil {
log.Trace(newError("failed to serve http for WebSocket").Base(err).AtWarning())
}
}() }()
return nil return nil
} }
// Addr implements net.Listener.Addr().
func (ln *Listener) Addr() net.Addr { func (ln *Listener) Addr() net.Addr {
return ln.listener.Addr() return ln.listener.Addr()
} }
// Close implements net.Listener.Close().
func (ln *Listener) Close() error { func (ln *Listener) Close() error {
return ln.listener.Close() return ln.listener.Close()
} }

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"v2ray.com/core/common"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
tlsgen "v2ray.com/core/testing/tls" tlsgen "v2ray.com/core/testing/tls"
@ -88,7 +89,7 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
}) })
listen, err := ListenWS(ctx, net.DomainAddress("localhost"), 13143, func(ctx context.Context, conn internet.Connection) bool { listen, err := ListenWS(ctx, net.DomainAddress("localhost"), 13143, func(ctx context.Context, conn internet.Connection) bool {
go func() { go func() {
conn.Close() common.Must(conn.Close())
}() }()
return true return true
}) })
@ -97,5 +98,5 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13143)) conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13143))
assert.Error(err).IsNil() assert.Error(err).IsNil()
conn.Close() common.Must(conn.Close())
} }