2018-09-29 12:36:47 +00:00
|
|
|
package websocket
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-29 12:36:47 +00:00
|
|
|
"git.xdrm.io/go/websocket/internal/uri/parser"
|
|
|
|
"net"
|
2018-05-02 20:24:50 +00:00
|
|
|
)
|
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
// Represents all channels that need a server
|
2018-09-29 12:36:47 +00:00
|
|
|
type serverChannelSet struct {
|
2018-05-03 17:31:09 +00:00
|
|
|
register chan *client
|
|
|
|
unregister chan *client
|
2018-05-05 16:43:16 +00:00
|
|
|
broadcast chan Message
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a websocket server
|
|
|
|
type Server struct {
|
2018-09-29 12:36:47 +00:00
|
|
|
sock net.Listener // listen socket
|
|
|
|
addr []byte // server listening ip/host
|
|
|
|
port uint16 // server listening port
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
clients map[net.Conn]*client
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
ctl ControllerSet // controllers
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
ch serverChannelSet
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 20:24:50 +00:00
|
|
|
// CreateServer creates a server for a specific HOST and PORT
|
2018-09-29 12:36:47 +00:00
|
|
|
func CreateServer(host string, port uint16) *Server {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
return &Server{
|
2018-09-29 12:36:47 +00:00
|
|
|
addr: []byte(host),
|
|
|
|
port: port,
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
clients: make(map[net.Conn]*client, 0),
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
ctl: ControllerSet{
|
2018-05-02 20:24:50 +00:00
|
|
|
Def: nil,
|
2018-05-03 17:31:09 +00:00
|
|
|
Uri: make([]*Controller, 0),
|
2018-05-02 20:24:50 +00:00
|
|
|
},
|
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
ch: serverChannelSet{
|
2018-05-05 16:43:16 +00:00
|
|
|
register: make(chan *client, 1),
|
|
|
|
unregister: make(chan *client, 1),
|
|
|
|
broadcast: make(chan Message, 1),
|
2018-05-02 20:24:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindDefault binds a default controller
|
|
|
|
// it will be called if the URI does not
|
|
|
|
// match another controller
|
2018-09-29 12:36:47 +00:00
|
|
|
func (s *Server) BindDefault(f ControllerFunc) {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
s.ctl.Def = &Controller{
|
2018-05-02 20:24:50 +00:00
|
|
|
URI: nil,
|
|
|
|
Fun: f,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind binds a controller to an URI scheme
|
2018-05-03 17:31:09 +00:00
|
|
|
func (s *Server) Bind(uri string, f ControllerFunc) error {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
/* (1) Build URI parser */
|
|
|
|
uriScheme, err := parser.Build(uri)
|
2018-09-29 12:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot build URI: %s", err)
|
|
|
|
}
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
/* (2) Create controller */
|
2018-05-03 17:31:09 +00:00
|
|
|
s.ctl.Uri = append(s.ctl.Uri, &Controller{
|
2018-05-02 20:24:50 +00:00
|
|
|
URI: uriScheme,
|
|
|
|
Fun: f,
|
2018-09-29 12:36:47 +00:00
|
|
|
})
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch launches the websocket server
|
2018-05-03 17:31:09 +00:00
|
|
|
func (s *Server) Launch() error {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
/* (1) Listen socket
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Build full url */
|
|
|
|
url := fmt.Sprintf("%s:%d", s.addr, s.port)
|
|
|
|
|
|
|
|
/* (2) Bind socket to listen */
|
|
|
|
s.sock, err = net.Listen("tcp", url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Listen socket: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer s.sock.Close()
|
|
|
|
|
|
|
|
fmt.Printf("+ listening on %s\n", url)
|
|
|
|
|
|
|
|
/* (3) Launch scheduler */
|
|
|
|
go s.scheduler()
|
|
|
|
|
|
|
|
/* (2) For each incoming connection (client)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for {
|
|
|
|
|
|
|
|
/* (1) Wait for client */
|
|
|
|
sock, err := s.sock.Accept()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
go func() {
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* (2) Try to create client */
|
|
|
|
cli, err := buildClient(sock, s.ctl, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" - %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Register client */
|
|
|
|
s.ch.register <- cli
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
}()
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scheduler schedules clients registration and broadcast
|
2018-09-29 12:36:47 +00:00
|
|
|
func (s *Server) scheduler() {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
/* (1) New client */
|
|
|
|
case client := <-s.ch.register:
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
// fmt.Printf(" + client\n")
|
|
|
|
s.clients[client.io.sock] = client
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
/* (2) New client */
|
|
|
|
case client := <-s.ch.unregister:
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
// fmt.Printf(" - client\n")
|
|
|
|
delete(s.clients, client.io.sock)
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
/* (3) Broadcast */
|
|
|
|
case msg := <-s.ch.broadcast:
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
fmt.Printf(" + broadcast\n")
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
for _, c := range s.clients {
|
|
|
|
c.ch.send <- msg
|
|
|
|
}
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("+ server stopped\n")
|
|
|
|
|
|
|
|
}
|