From a3f72a9bf7cf0b2cf962fee8dddd396049a441ba Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 28 Apr 2018 16:04:05 +0200 Subject: [PATCH] added 'chan *Frame' and channel principle to build the Controller implementation on --- ws/private.go | 12 ++++++------ ws/server.go | 10 ++++++++-- ws/types.go | 6 ++++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ws/private.go b/ws/private.go index df67372..7142a01 100644 --- a/ws/private.go +++ b/ws/private.go @@ -75,13 +75,12 @@ func (s *Server) dispatch(sock net.Conn, u *upgrader.T){ /* (2) Add controller to client */ client.ctl = controller - /* (3) Launch controller routine (for new connection) */ - go controller.fun(client, nil) + /* (3) Bind controller */ + go controller.fun(client, controller.dat) /* (4) Launch continuous frame reader */ go client.awaitFrame() - return } @@ -119,12 +118,13 @@ func (c *Client) awaitFrame() { fmt.Printf("+ elapsed: %.3f us\n", float32(time.Now().UnixNano()-startTime)/1e3) - // Call controller - c.ctl.fun(c, frame) + // Trigger data channel + c.ctl.dat <- frame } - fmt.Printf(" - error\n") + fmt.Printf(" - error; disconnecting\n") + close(c.ctl.dat) } diff --git a/ws/server.go b/ws/server.go index 79362a7..e81dd70 100644 --- a/ws/server.go +++ b/ws/server.go @@ -12,7 +12,10 @@ import ( // it will be called if the URI does not // match another controller func (s *Server) BindDefault(c ControllerFunc) error{ - s.def = &Controller{nil, c}; + s.def = new(Controller); + s.def.uri = nil + s.def.fun = c + s.def.dat = make(chan *Frame, maxChannelBufferLength) return nil } @@ -25,7 +28,10 @@ func (s *Server) Bind(uri string, c ControllerFunc) error { if err != nil { return fmt.Errorf("Cannot build URI: %s", err) } /* (2) Create controller */ - ctl := &Controller{uri: uriScheme, fun: c} + ctl := new(Controller) + ctl.uri = uriScheme + ctl.fun = c + ctl.dat = make(chan *Frame, maxChannelBufferLength) /* (3) Add to server */ s.ctl = append(s.ctl, ctl) diff --git a/ws/types.go b/ws/types.go index 5c0240f..9617c46 100644 --- a/ws/types.go +++ b/ws/types.go @@ -6,15 +6,17 @@ import ( "git.xdrm.io/gws/ws/frame" ) -const maxBufferLength = 4096 +const maxBufferLength = 4096 +const maxChannelBufferLength = 1 // Represents a websocket controller callback function -type ControllerFunc func(*Client, *Frame) +type ControllerFunc func(*Client, chan *Frame) // Represents a websocket controller type Controller struct { uri *parser.Scheme fun ControllerFunc + dat chan *Frame // data channel }