2015-03-02 14:16:36 +01:00
|
|
|
// Copyright 2014 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2015-03-03 01:13:37 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
2015-03-02 14:16:36 +01:00
|
|
|
|
|
|
|
type websocketForwarder struct {
|
2015-03-03 01:13:37 +01:00
|
|
|
upgrader *websocket.Upgrader
|
|
|
|
register chan *connection
|
|
|
|
unregister chan *connection
|
2015-03-09 15:37:57 +01:00
|
|
|
broadcast chan msg
|
2015-03-03 01:13:37 +01:00
|
|
|
connections map[*connection]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type connection struct {
|
|
|
|
ws *websocket.Conn
|
|
|
|
send chan []byte
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
2015-03-09 15:37:57 +01:00
|
|
|
type msg struct {
|
|
|
|
tiles map[xz]bool
|
|
|
|
pls []*player
|
|
|
|
}
|
|
|
|
|
2015-03-02 14:16:36 +01:00
|
|
|
func newWebsocketForwarder() *websocketForwarder {
|
2015-03-03 01:13:37 +01:00
|
|
|
upgrader := &websocket.Upgrader{ReadBufferSize: 512, WriteBufferSize: 2048}
|
|
|
|
return &websocketForwarder{
|
|
|
|
upgrader: upgrader,
|
|
|
|
register: make(chan *connection),
|
|
|
|
unregister: make(chan *connection),
|
2015-03-09 15:37:57 +01:00
|
|
|
broadcast: make(chan msg),
|
2015-03-03 01:13:37 +01:00
|
|
|
connections: make(map[*connection]bool)}
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) run() {
|
2015-03-03 01:13:37 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-wsf.register:
|
|
|
|
wsf.connections[c] = true
|
|
|
|
case c := <-wsf.unregister:
|
|
|
|
if _, ok := wsf.connections[c]; ok {
|
|
|
|
delete(wsf.connections, c)
|
|
|
|
close(c.send)
|
|
|
|
}
|
2015-03-09 15:37:57 +01:00
|
|
|
case message := <-wsf.broadcast:
|
2015-03-03 01:13:37 +01:00
|
|
|
if len(wsf.connections) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-09 15:37:57 +01:00
|
|
|
encMsg := map[string]interface{}{}
|
|
|
|
|
|
|
|
if tiles := message.tiles; tiles != nil {
|
|
|
|
xzs := make([]xz, 0, len(tiles))
|
|
|
|
for xz := range tiles {
|
|
|
|
xzs = append(xzs, xz)
|
|
|
|
}
|
|
|
|
encMsg["tiles"] = xzs
|
2015-03-03 01:13:37 +01:00
|
|
|
}
|
2015-03-09 15:37:57 +01:00
|
|
|
|
|
|
|
if message.pls != nil {
|
|
|
|
encMsg["players"] = message.pls
|
|
|
|
}
|
|
|
|
|
2015-03-03 01:13:37 +01:00
|
|
|
var buf bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&buf)
|
2015-03-09 15:37:57 +01:00
|
|
|
if err := encoder.Encode(encMsg); err != nil {
|
2015-03-03 01:13:37 +01:00
|
|
|
log.Printf("encoding changes failed: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-03-09 15:37:57 +01:00
|
|
|
m := buf.Bytes()
|
2015-03-03 01:13:37 +01:00
|
|
|
for c := range wsf.connections {
|
|
|
|
select {
|
2015-03-09 15:37:57 +01:00
|
|
|
case c.send <- m:
|
2015-03-03 01:13:37 +01:00
|
|
|
default:
|
|
|
|
delete(wsf.connections, c)
|
|
|
|
close(c.send)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2015-03-03 01:13:37 +01:00
|
|
|
ws, err := wsf.upgrader.Upgrade(rw, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot upgrade to websocket: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c := &connection{ws: ws, send: make(chan []byte, 8)}
|
|
|
|
wsf.register <- c
|
|
|
|
defer func() { wsf.unregister <- c }()
|
|
|
|
go c.writer()
|
|
|
|
c.reader()
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) BaseTilesUpdated(changes map[xz]bool) {
|
2015-03-09 15:37:57 +01:00
|
|
|
wsf.broadcast <- msg{tiles: changes}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) BroadcastPlayers(pls []*player) {
|
|
|
|
wsf.broadcast <- msg{pls: pls}
|
2015-03-03 01:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) writer() {
|
|
|
|
defer c.ws.Close()
|
|
|
|
for msg := range c.send {
|
|
|
|
if c.ws.WriteMessage(websocket.TextMessage, msg) != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) reader() {
|
|
|
|
defer c.ws.Close()
|
|
|
|
for {
|
|
|
|
// Just read the message and ignore it.
|
2015-03-03 01:33:12 +01:00
|
|
|
if _, _, err := c.ws.NextReader(); err != nil {
|
2015-03-03 01:13:37 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|