diff --git a/cmd/iface/main.go b/cmd/iface/main.go new file mode 100644 index 0000000..55f4d71 --- /dev/null +++ b/cmd/iface/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "time" + "git.xdrm.io/gws/ws" + "fmt" +) + + +func main(){ + + startTime := time.Now().UnixNano() + + /* (1) Bind WebSocket server */ + serv := ws.CreateServer("0.0.0.0", 4444) + + /* (2) Bind default controller */ + err := serv.BindDefault(func(c ws.Client, f ws.Frame){ + fmt.Printf("Default controller") + }) + if err != nil { panic(err) } + + /* (3) Bind to URI */ + err = serv.Bind("/channel/./room/./", func(c ws.Client, f ws.Frame){ + fmt.Printf("URI controller") + }) + if err != nil { panic(err) } + + /* (4) Launch the server */ + serv.Launch() + + + + + fmt.Printf("+ elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3) + +} \ No newline at end of file diff --git a/ws/public.go b/ws/public.go new file mode 100644 index 0000000..ad39882 --- /dev/null +++ b/ws/public.go @@ -0,0 +1,19 @@ +package ws + +import () + + + +// CreateServer creates a server for a specific HOST and PORT +func CreateServer(host string, port uint16) *Server{ + + inst := new(Server) + + inst.adr = make([]byte, len(host)) + copy(inst.adr, []byte(host)) + + inst.prt = port + + return inst + +} \ No newline at end of file diff --git a/ws/server.go b/ws/server.go new file mode 100644 index 0000000..462f0b9 --- /dev/null +++ b/ws/server.go @@ -0,0 +1,50 @@ +package ws + +import ( + "fmt" + "git.xdrm.io/gws/internal/uri/parser" +) + + +// BindDefault binds a default controller +// it will be called if the URI does not +// match another controller +func (s *Server) BindDefault(c ControllerFunc) error{ + s.def = &Controller{nil, c}; + + return nil +} + +// Bind binds a controller to an URI scheme +func (s *Server) Bind(uri string, c ControllerFunc) error { + + /* (1) Build URI parser */ + uriScheme, err := parser.Build(uri) + if err != nil { return fmt.Errorf("Cannot build URI: %s", err) } + + /* (2) Create controller */ + ctl := &Controller{uri: uriScheme, fun: c} + + /* (3) Add to server */ + s.ctl = append(s.ctl, ctl) + + return nil + +} + + +// Launch launches the websocket server +func (s *Server) Launch(){ + + /* (1) Listen socket + ---------------------------------------------------------*/ + /* (1) Build full url */ + url := fmt.Sprintf("%s:%d", s.adr, s.prt) + + /* (3) Bind listen socket */ + // lsock, err := net.Listen("tcp", ) + + + + +} \ No newline at end of file diff --git a/ws/types.go b/ws/types.go new file mode 100644 index 0000000..90f5f00 --- /dev/null +++ b/ws/types.go @@ -0,0 +1,40 @@ +package ws + +import ( + "git.xdrm.io/gws/internal/uri/parser" + "git.xdrm.io/gws/ws/frame" +) + +// Represents a websocket controller callback function +type ControllerFunc func(Client, Frame) + +// Represents a websocket controller +type Controller struct { + uri *parser.Scheme + fun ControllerFunc +} + + +// Represents a websocket srever +type Server struct { + adr []byte // server listening ip/host + prt uint16 // server listening port + + def *Controller // default controller + ctl []*Controller // URI-bound controllers +} + +// Represents a websocket client +type Client struct { + pro string // choosen protocol (Sec-WebSocket-Protocol) + arg [][]string // URI parameters, index 0 is full URI, then matching groups + dat interface{} // store (for client implementation-specific data) +} + + +// Represents a websocket frame +type Frame struct { + met frame.Header + buf []byte + len uint64 +} \ No newline at end of file