added 'chan *Frame' and channel principle to build the Controller implementation on

This commit is contained in:
xdrm-brackets 2018-04-28 16:04:05 +02:00
parent dcfdb3411a
commit a3f72a9bf7
3 changed files with 18 additions and 10 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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
}