From 0bea3243620bbd8352c8e8870a4deb00de1d05ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Fri, 6 Dec 2019 12:03:31 +0100 Subject: [PATCH] small fixes --- transport/base_conn.go | 2 +- transport/net_server.go | 3 +++ transport/websocket_conn.go | 10 +++++----- transport/websocket_server.go | 2 ++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/transport/base_conn.go b/transport/base_conn.go index 98de2db..90521e2 100644 --- a/transport/base_conn.go +++ b/transport/base_conn.go @@ -11,7 +11,6 @@ import ( // A Carrier is a generalized stream that can be used with BaseConn. type Carrier interface { io.ReadWriteCloser - SetReadDeadline(time.Time) error } @@ -136,6 +135,7 @@ func (c *BaseConn) SetMaxWriteDelay(delay time.Duration) { } func (c *BaseConn) resetTimeout() error { + // check timeout if c.readTimeout > 0 { return c.carrier.SetReadDeadline(time.Now().Add(c.readTimeout)) } diff --git a/transport/net_server.go b/transport/net_server.go index f298a95..c3c1440 100644 --- a/transport/net_server.go +++ b/transport/net_server.go @@ -19,6 +19,7 @@ func NewNetServer(listener net.Listener) *NetServer { // CreateNetServer creates a new TCP server that listens on the provided address. func CreateNetServer(address string) (*NetServer, error) { + // create listener listener, err := net.Listen("tcp", address) if err != nil { return nil, err @@ -29,6 +30,7 @@ func CreateNetServer(address string) (*NetServer, error) { // CreateSecureNetServer creates a new TLS server that listens on the provided address. func CreateSecureNetServer(address string, config *tls.Config) (*NetServer, error) { + // create listener listener, err := tls.Listen("tcp", address, config) if err != nil { return nil, err @@ -40,6 +42,7 @@ func CreateSecureNetServer(address string, config *tls.Config) (*NetServer, erro // Accept will return the next available connection or block until a // connection becomes available, otherwise returns an error. func (s *NetServer) Accept() (Conn, error) { + // wait next connection conn, err := s.listener.Accept() if err != nil { return nil, err diff --git a/transport/websocket_conn.go b/transport/websocket_conn.go index 9431b2f..82b3a61 100644 --- a/transport/websocket_conn.go +++ b/transport/websocket_conn.go @@ -57,20 +57,20 @@ func (s *wsStream) Read(buf []byte) (int, error) { } } -func (s *wsStream) Write(p []byte) (n int, err error) { - // create writer if missing +func (s *wsStream) Write(buf []byte) (n int, err error) { + // create writer writer, err := s.conn.NextWriter(websocket.BinaryMessage) if err != nil { return 0, err } - // write packet to writer - n, err = writer.Write(p) + // write buffer + n, err = writer.Write(buf) if err != nil { return n, err } - // close temporary writer + // close writer err = writer.Close() if err != nil { return n, err diff --git a/transport/websocket_server.go b/transport/websocket_server.go index c44be0a..7fba584 100644 --- a/transport/websocket_server.go +++ b/transport/websocket_server.go @@ -36,6 +36,7 @@ func NewWebSocketServer(listener net.Listener, fallback http.Handler) *WebSocket // CreateWebSocketServer creates a new WS server that listens on the provided address. func CreateWebSocketServer(address string, fallback http.Handler) (*WebSocketServer, error) { + // create listener listener, err := net.Listen("tcp", address) if err != nil { return nil, err @@ -47,6 +48,7 @@ func CreateWebSocketServer(address string, fallback http.Handler) (*WebSocketSer // CreateSecureWebSocketServer creates a new WSS server that listens on the // provided address. func CreateSecureWebSocketServer(address string, config *tls.Config, fallback http.Handler) (*WebSocketServer, error) { + // create listener listener, err := tls.Listen("tcp", address, config) if err != nil { return nil, err