diff --git a/internal/compression.go b/conn-compression.go similarity index 99% rename from internal/compression.go rename to conn-compression.go index 7b5586d..9505f8f 100644 --- a/internal/compression.go +++ b/conn-compression.go @@ -1,4 +1,4 @@ -package internal +package server import ( "compress/flate" diff --git a/internal/error.go b/conn-error.go similarity index 99% rename from internal/error.go rename to conn-error.go index 17890c8..79593fd 100644 --- a/internal/error.go +++ b/conn-error.go @@ -1,4 +1,4 @@ -package internal +package server import ( "errors" diff --git a/internal/conn-json.go b/conn-json.go similarity index 98% rename from internal/conn-json.go rename to conn-json.go index d003ac4..5c1a539 100644 --- a/internal/conn-json.go +++ b/conn-json.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package internal +package server import ( "encoding/json" diff --git a/internal/mask.go b/conn-mask.go similarity index 98% rename from internal/mask.go rename to conn-mask.go index 2f8acc6..6ba0207 100644 --- a/internal/mask.go +++ b/conn-mask.go @@ -1,4 +1,4 @@ -package internal +package server import "unsafe" diff --git a/internal/prepared.go b/conn-prepared.go similarity index 99% rename from internal/prepared.go rename to conn-prepared.go index f5ccef6..a88ad60 100644 --- a/internal/prepared.go +++ b/conn-prepared.go @@ -1,4 +1,4 @@ -package internal +package server import ( "bytes" diff --git a/internal/conn-read.go b/conn-read.go similarity index 95% rename from internal/conn-read.go rename to conn-read.go index ebd3ed2..71d80a3 100644 --- a/internal/conn-read.go +++ b/conn-read.go @@ -4,7 +4,7 @@ // +build go1.5 -package internal +package server import "io" diff --git a/internal/conn.go b/conn.go similarity index 99% rename from internal/conn.go rename to conn.go index c62fe39..d500c1b 100644 --- a/internal/conn.go +++ b/conn.go @@ -1,4 +1,4 @@ -package internal +package server import ( "bufio" diff --git a/fasthttp/websocket/client.go b/fasthttp/websocket/client.go index 386a868..abd3d6e 100644 --- a/fasthttp/websocket/client.go +++ b/fasthttp/websocket/client.go @@ -16,9 +16,8 @@ import ( "sync" "time" - logging "git.loafle.net/commons/logging-go" - server "git.loafle.net/commons/server-go" - "git.loafle.net/commons/server-go/internal" + "git.loafle.net/commons/logging-go" + "git.loafle.net/commons/server-go" ) var errMalformedURL = errors.New("malformed ws or wss URL") @@ -82,14 +81,14 @@ type Client struct { stopChan chan struct{} stopWg sync.WaitGroup - conn *internal.Conn + conn *server.Conn readChan chan []byte writeChan chan []byte } func (c *Client) Connect() (readChan <-chan []byte, writeChan chan<- []byte, res *http.Response, err error) { var ( - conn *internal.Conn + conn *server.Conn ) if c.stopChan != nil { @@ -132,7 +131,7 @@ func (c *Client) clientMessage(msg string) string { return fmt.Sprintf("Client[%s]: %s", c.Name, msg) } -func (c *Client) connect() (*internal.Conn, *http.Response, error) { +func (c *Client) connect() (*server.Conn, *http.Response, error) { conn, res, err := c.Dial() if nil != err { return nil, nil, err @@ -145,7 +144,7 @@ func (c *Client) connect() (*internal.Conn, *http.Response, error) { return conn, res, nil } -func (c *Client) handleConnection(conn *internal.Conn) { +func (c *Client) handleConnection(conn *server.Conn) { defer func() { if nil != conn { conn.Close() @@ -184,7 +183,7 @@ func (c *Client) handleConnection(conn *internal.Conn) { } } -func handleClientRead(c *Client, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { +func handleClientRead(c *Client, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { defer func() { close(doneChan) }() @@ -207,7 +206,7 @@ func handleClientRead(c *Client, conn *internal.Conn, doneChan chan<- struct{}, go func() { _, message, err = conn.ReadMessage() if err != nil { - if internal.IsUnexpectedCloseError(err, internal.CloseGoingAway, internal.CloseAbnormalClosure) { + if server.IsUnexpectedCloseError(err, server.CloseGoingAway, server.CloseAbnormalClosure) { logging.Logger().Debugf(c.clientMessage(fmt.Sprintf("Read error %v", err))) } } @@ -234,7 +233,7 @@ func handleClientRead(c *Client, conn *internal.Conn, doneChan chan<- struct{}, } } -func handleClientWrite(c *Client, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { +func handleClientWrite(c *Client, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { defer func() { close(doneChan) }() @@ -248,11 +247,11 @@ func handleClientWrite(c *Client, conn *internal.Conn, doneChan chan<- struct{}, case message, ok := <-c.writeChan: conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout)) if !ok { - conn.WriteMessage(internal.CloseMessage, []byte{}) + conn.WriteMessage(server.CloseMessage, []byte{}) return } - w, err := conn.NextWriter(internal.TextMessage) + w, err := conn.NextWriter(server.TextMessage) if err != nil { return } @@ -263,7 +262,7 @@ func handleClientWrite(c *Client, conn *internal.Conn, doneChan chan<- struct{}, } case <-ticker.C: conn.SetWriteDeadline(time.Now().Add(c.PingTimeout)) - if err := conn.WriteMessage(internal.PingMessage, nil); nil != err { + if err := conn.WriteMessage(server.PingMessage, nil); nil != err { return } case <-c.stopChan: @@ -272,7 +271,7 @@ func handleClientWrite(c *Client, conn *internal.Conn, doneChan chan<- struct{}, } } -func (c *Client) Dial() (*internal.Conn, *http.Response, error) { +func (c *Client) Dial() (*server.Conn, *http.Response, error) { var ( err error challengeKey string @@ -432,7 +431,7 @@ func (c *Client) Dial() (*internal.Conn, *http.Response, error) { } } - conn := internal.NewConn(netConn, false, c.ReadBufferSize, c.WriteBufferSize) + conn := server.NewConn(netConn, false, c.ReadBufferSize, c.WriteBufferSize) if err := req.Write(netConn); err != nil { return nil, nil, err @@ -459,7 +458,7 @@ func (c *Client) Dial() (*internal.Conn, *http.Response, error) { buf := make([]byte, 1024) n, _ := io.ReadFull(resp.Body, buf) resp.Body = ioutil.NopCloser(bytes.NewReader(buf[:n])) - return nil, resp, internal.ErrBadHandshake + return nil, resp, server.ErrBadHandshake } for _, ext := range httpParseExtensions(resp.Header) { @@ -469,10 +468,10 @@ func (c *Client) Dial() (*internal.Conn, *http.Response, error) { _, snct := ext["server_no_context_takeover"] _, cnct := ext["client_no_context_takeover"] if !snct || !cnct { - return nil, resp, internal.ErrInvalidCompression + return nil, resp, server.ErrInvalidCompression } - conn.NewCompressionWriter = internal.CompressNoContextTakeover - conn.NewDecompressionReader = internal.DecompressNoContextTakeover + conn.NewCompressionWriter = server.CompressNoContextTakeover + conn.NewDecompressionReader = server.DecompressNoContextTakeover break } diff --git a/fasthttp/websocket/server.go b/fasthttp/websocket/server.go index 44a6a36..56b06d1 100644 --- a/fasthttp/websocket/server.go +++ b/fasthttp/websocket/server.go @@ -10,7 +10,6 @@ import ( "git.loafle.net/commons/logging-go" "git.loafle.net/commons/server-go" - "git.loafle.net/commons/server-go/internal" "github.com/valyala/fasthttp" ) @@ -184,7 +183,7 @@ func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) { return } - s.upgrader.Upgrade(ctx, responseHeader, func(conn *internal.Conn, err error) { + s.upgrader.Upgrade(ctx, responseHeader, func(conn *server.Conn, err error) { if err != nil { s.onError(ctx, fasthttp.StatusInternalServerError, err) return @@ -195,7 +194,7 @@ func (s *Server) httpHandler(ctx *fasthttp.RequestCtx) { }) } -func (s *Server) handleConnection(servlet Servlet, servletCtx server.ServletCtx, conn *internal.Conn) { +func (s *Server) handleConnection(servlet Servlet, servletCtx server.ServletCtx, conn *server.Conn) { addr := conn.RemoteAddr() defer func() { @@ -260,7 +259,7 @@ func (s *Server) onError(ctx *fasthttp.RequestCtx, status int, reason error) { s.ServerHandler.OnError(s.ctx, ctx, status, reason) } -func handleRead(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, readChan chan []byte) { +func handleRead(s *Server, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, readChan chan []byte) { defer func() { close(doneChan) }() @@ -283,7 +282,7 @@ func handleRead(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopCh go func() { _, message, err = conn.ReadMessage() if err != nil { - if internal.IsUnexpectedCloseError(err, internal.CloseGoingAway, internal.CloseAbnormalClosure) { + if server.IsUnexpectedCloseError(err, server.CloseGoingAway, server.CloseAbnormalClosure) { logging.Logger().Debugf(s.serverMessage(fmt.Sprintf("Read error %v", err))) } } @@ -310,7 +309,7 @@ func handleRead(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopCh } } -func handleWrite(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, writeChan chan []byte) { +func handleWrite(s *Server, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, writeChan chan []byte) { defer func() { close(doneChan) }() @@ -324,11 +323,11 @@ func handleWrite(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopC case message, ok := <-writeChan: conn.SetWriteDeadline(time.Now().Add(s.ServerHandler.GetWriteTimeout())) if !ok { - conn.WriteMessage(internal.CloseMessage, []byte{}) + conn.WriteMessage(server.CloseMessage, []byte{}) return } - w, err := conn.NextWriter(internal.TextMessage) + w, err := conn.NextWriter(server.TextMessage) if err != nil { return } @@ -339,7 +338,7 @@ func handleWrite(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopC } case <-ticker.C: conn.SetWriteDeadline(time.Now().Add(s.ServerHandler.GetPingTimeout())) - if err := conn.WriteMessage(internal.PingMessage, nil); nil != err { + if err := conn.WriteMessage(server.PingMessage, nil); nil != err { return } case <-s.stopChan: diff --git a/fasthttp/websocket/upgrade.go b/fasthttp/websocket/upgrade.go index e68b7d4..c9725cc 100644 --- a/fasthttp/websocket/upgrade.go +++ b/fasthttp/websocket/upgrade.go @@ -11,12 +11,12 @@ import ( "strings" "time" - "git.loafle.net/commons/server-go/internal" + "git.loafle.net/commons/server-go" "github.com/valyala/fasthttp" ) type ( - OnUpgradeFunc func(*internal.Conn, error) + OnUpgradeFunc func(*server.Conn, error) ) // HandshakeError describes an error with the handshake from the peer. @@ -60,7 +60,7 @@ type Upgrader struct { EnableCompression bool } -func (u *Upgrader) returnError(ctx *fasthttp.RequestCtx, status int, reason string) (*internal.Conn, error) { +func (u *Upgrader) returnError(ctx *fasthttp.RequestCtx, status int, reason string) (*server.Conn, error) { err := HandshakeError{reason} if u.Error != nil { u.Error(ctx, status, err) @@ -192,11 +192,11 @@ func (u *Upgrader) Upgrade(ctx *fasthttp.RequestCtx, responseHeader *fasthttp.Re ctx.Request.Header.CopyTo(h) ctx.Hijack(func(netConn net.Conn) { - c := internal.NewConn(netConn, true, u.ReadBufferSize, u.WriteBufferSize) + c := server.NewConn(netConn, true, u.ReadBufferSize, u.WriteBufferSize) c.Subprotocol = subprotocol if compress { - c.NewCompressionWriter = internal.CompressNoContextTakeover - c.NewDecompressionReader = internal.DecompressNoContextTakeover + c.NewCompressionWriter = server.CompressNoContextTakeover + c.NewDecompressionReader = server.DecompressNoContextTakeover } // Clear deadlines set by HTTP server. diff --git a/net/client.go b/net/client.go index b7c3438..df0130e 100644 --- a/net/client.go +++ b/net/client.go @@ -7,9 +7,8 @@ import ( "sync" "time" - logging "git.loafle.net/commons/logging-go" - server "git.loafle.net/commons/server-go" - "git.loafle.net/commons/server-go/internal" + "git.loafle.net/commons/logging-go" + "git.loafle.net/commons/server-go" ) type Client struct { @@ -56,14 +55,14 @@ type Client struct { stopChan chan struct{} stopWg sync.WaitGroup - conn *internal.Conn + conn *server.Conn readChan chan []byte writeChan chan []byte } func (c *Client) Connect() (readChan <-chan []byte, writeChan chan<- []byte, err error) { var ( - conn *internal.Conn + conn *server.Conn ) if c.stopChan != nil { @@ -106,13 +105,13 @@ func (c *Client) clientMessage(msg string) string { return fmt.Sprintf("Client[%s]: %s", c.Name, msg) } -func (c *Client) connect() (*internal.Conn, error) { +func (c *Client) connect() (*server.Conn, error) { netConn, err := c.Dial() if nil != err { return nil, err } - conn := internal.NewConn(netConn, false, c.ReadBufferSize, c.WriteBufferSize) + conn := server.NewConn(netConn, false, c.ReadBufferSize, c.WriteBufferSize) conn.SetCloseHandler(func(code int, text string) error { logging.Logger().Debugf("close") return nil @@ -120,7 +119,7 @@ func (c *Client) connect() (*internal.Conn, error) { return conn, nil } -func (c *Client) handleConnection(conn *internal.Conn) { +func (c *Client) handleConnection(conn *server.Conn) { defer func() { if nil != conn { conn.Close() @@ -159,7 +158,7 @@ func (c *Client) handleConnection(conn *internal.Conn) { } } -func handleClientRead(c *Client, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { +func handleClientRead(c *Client, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { defer func() { close(doneChan) }() @@ -182,7 +181,7 @@ func handleClientRead(c *Client, conn *internal.Conn, doneChan chan<- struct{}, go func() { _, message, err = conn.ReadMessage() if err != nil { - if internal.IsUnexpectedCloseError(err, internal.CloseGoingAway, internal.CloseAbnormalClosure) { + if server.IsUnexpectedCloseError(err, server.CloseGoingAway, server.CloseAbnormalClosure) { logging.Logger().Debugf(c.clientMessage(fmt.Sprintf("Read error %v", err))) } } @@ -209,7 +208,7 @@ func handleClientRead(c *Client, conn *internal.Conn, doneChan chan<- struct{}, } } -func handleClientWrite(c *Client, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { +func handleClientWrite(c *Client, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}) { defer func() { close(doneChan) }() @@ -223,11 +222,11 @@ func handleClientWrite(c *Client, conn *internal.Conn, doneChan chan<- struct{}, case message, ok := <-c.writeChan: conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout)) if !ok { - conn.WriteMessage(internal.CloseMessage, []byte{}) + conn.WriteMessage(server.CloseMessage, []byte{}) return } - w, err := conn.NextWriter(internal.TextMessage) + w, err := conn.NextWriter(server.TextMessage) if err != nil { return } @@ -238,7 +237,7 @@ func handleClientWrite(c *Client, conn *internal.Conn, doneChan chan<- struct{}, } case <-ticker.C: conn.SetWriteDeadline(time.Now().Add(c.PingTimeout)) - if err := conn.WriteMessage(internal.PingMessage, nil); nil != err { + if err := conn.WriteMessage(server.PingMessage, nil); nil != err { return } case <-c.stopChan: diff --git a/net/server.go b/net/server.go index 01c08f5..545c892 100644 --- a/net/server.go +++ b/net/server.go @@ -10,7 +10,6 @@ import ( "git.loafle.net/commons/logging-go" "git.loafle.net/commons/server-go" - "git.loafle.net/commons/server-go/internal" ) type Server struct { @@ -165,14 +164,14 @@ func (s *Server) handleServer(listener net.Listener) error { continue } - conn := internal.NewConn(netConn, true, s.ServerHandler.GetReadBufferSize(), s.ServerHandler.GetWriteBufferSize()) + conn := server.NewConn(netConn, true, s.ServerHandler.GetReadBufferSize(), s.ServerHandler.GetWriteBufferSize()) s.stopWg.Add(1) go s.handleConnection(servlet, servletCtx, conn) } } -func (s *Server) handleConnection(servlet Servlet, servletCtx server.ServletCtx, conn *internal.Conn) { +func (s *Server) handleConnection(servlet Servlet, servletCtx server.ServletCtx, conn *server.Conn) { addr := conn.RemoteAddr() defer func() { @@ -232,7 +231,7 @@ func (s *Server) handleConnection(servlet Servlet, servletCtx server.ServletCtx, } } -func handleRead(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, readChan chan []byte) { +func handleRead(s *Server, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, readChan chan []byte) { defer func() { close(doneChan) }() @@ -255,7 +254,7 @@ func handleRead(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopCh go func() { _, message, err = conn.ReadMessage() if err != nil { - if internal.IsUnexpectedCloseError(err, internal.CloseGoingAway, internal.CloseAbnormalClosure) { + if server.IsUnexpectedCloseError(err, server.CloseGoingAway, server.CloseAbnormalClosure) { logging.Logger().Debugf(s.serverMessage(fmt.Sprintf("Read error %v", err))) } } @@ -282,7 +281,7 @@ func handleRead(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopCh } } -func handleWrite(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, writeChan chan []byte) { +func handleWrite(s *Server, conn *server.Conn, doneChan chan<- struct{}, stopChan <-chan struct{}, writeChan chan []byte) { defer func() { close(doneChan) }() @@ -296,11 +295,11 @@ func handleWrite(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopC case message, ok := <-writeChan: conn.SetWriteDeadline(time.Now().Add(s.ServerHandler.GetWriteTimeout())) if !ok { - conn.WriteMessage(internal.CloseMessage, []byte{}) + conn.WriteMessage(server.CloseMessage, []byte{}) return } - w, err := conn.NextWriter(internal.TextMessage) + w, err := conn.NextWriter(server.TextMessage) if err != nil { return } @@ -311,7 +310,7 @@ func handleWrite(s *Server, conn *internal.Conn, doneChan chan<- struct{}, stopC } case <-ticker.C: conn.SetWriteDeadline(time.Now().Add(s.ServerHandler.GetPingTimeout())) - if err := conn.WriteMessage(internal.PingMessage, nil); nil != err { + if err := conn.WriteMessage(server.PingMessage, nil); nil != err { return } case <-s.stopChan: diff --git a/servlet.go b/servlet.go index 794445e..c1b6d16 100644 --- a/servlet.go +++ b/servlet.go @@ -1,16 +1,12 @@ package server -import ( - "git.loafle.net/commons/server-go/internal" -) - type Servlet interface { ServletCtx(serverCtx ServerCtx) ServletCtx Init(serverCtx ServerCtx) error Destroy(serverCtx ServerCtx) - OnConnect(servletCtx ServletCtx, conn *internal.Conn) + OnConnect(servletCtx ServletCtx, conn *Conn) Handle(servletCtx ServletCtx, stopChan <-chan struct{}, doneChan chan<- struct{}, readChan <-chan []byte, writeChan chan<- []byte) OnDisconnect(servletCtx ServletCtx) }