2015-07-26 16:33:29 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2015-03-02 14:16:36 +01:00
|
|
|
// 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 (
|
|
|
|
"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
|
2022-03-01 22:29:56 +01:00
|
|
|
connections map[*connection]struct{}
|
|
|
|
funcs chan func(*websocketForwarder)
|
2022-03-03 22:57:02 +01:00
|
|
|
init func(*websocketForwarder, *connection)
|
2015-03-03 01:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type connection struct {
|
|
|
|
ws *websocket.Conn
|
|
|
|
send chan []byte
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 20:22:37 +01:00
|
|
|
type (
|
|
|
|
tilesMsg struct {
|
2022-03-03 22:30:06 +01:00
|
|
|
Tiles []xz `json:"tiles"`
|
2022-03-03 20:22:37 +01:00
|
|
|
}
|
|
|
|
plsMsg struct {
|
2022-03-03 22:30:06 +01:00
|
|
|
Pls []*player `json:"players"`
|
2022-03-03 20:22:37 +01:00
|
|
|
}
|
|
|
|
)
|
2015-03-09 15:37:57 +01:00
|
|
|
|
2015-03-02 14:16:36 +01:00
|
|
|
func newWebsocketForwarder() *websocketForwarder {
|
2022-03-01 22:29:56 +01:00
|
|
|
upgrader := &websocket.Upgrader{
|
|
|
|
ReadBufferSize: 512,
|
|
|
|
WriteBufferSize: 2048,
|
|
|
|
//CheckOrigin: func(*http.Request) bool { return true },
|
|
|
|
}
|
2015-03-03 01:13:37 +01:00
|
|
|
return &websocketForwarder{
|
|
|
|
upgrader: upgrader,
|
2022-03-01 22:29:56 +01:00
|
|
|
connections: make(map[*connection]struct{}),
|
|
|
|
funcs: make(chan func(*websocketForwarder)),
|
|
|
|
}
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) run() {
|
2022-03-01 22:29:56 +01:00
|
|
|
for fn := range wsf.funcs {
|
|
|
|
fn(wsf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) register(c *connection) {
|
|
|
|
wsf.funcs <- func(wsf *websocketForwarder) {
|
|
|
|
wsf.connections[c] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) unregister(c *connection) {
|
|
|
|
wsf.funcs <- func(wsf *websocketForwarder) {
|
|
|
|
if _, ok := wsf.connections[c]; ok {
|
|
|
|
delete(wsf.connections, c)
|
|
|
|
close(c.send)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:57:02 +01:00
|
|
|
func (wsf *websocketForwarder) setInit(init func(*websocketForwarder, *connection)) {
|
|
|
|
wsf.funcs <- func(wsf *websocketForwarder) {
|
|
|
|
wsf.init = init
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 20:22:37 +01:00
|
|
|
func (wsf *websocketForwarder) send(m interface{}) {
|
2022-03-01 22:29:56 +01:00
|
|
|
wsf.funcs <- func(wsf *websocketForwarder) {
|
|
|
|
if len(wsf.connections) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:57:02 +01:00
|
|
|
data, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("encoding failed. %v\n", err)
|
2022-03-01 22:29:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for c := range wsf.connections {
|
|
|
|
select {
|
|
|
|
case c.send <- data:
|
|
|
|
default:
|
2015-03-03 01:13:37 +01:00
|
|
|
delete(wsf.connections, c)
|
|
|
|
close(c.send)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 22:57:02 +01:00
|
|
|
func (wsf *websocketForwarder) singleSend(c *connection, m interface{}) {
|
|
|
|
wsf.funcs <- func(wsf *websocketForwarder) {
|
|
|
|
_, ok := wsf.connections[c]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("encoding failed. %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case c.send <- data:
|
|
|
|
default:
|
|
|
|
delete(wsf.connections, c)
|
|
|
|
close(c.send)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 22:29:56 +01:00
|
|
|
func (wsf *websocketForwarder) BaseTilesUpdated(changes []xz) {
|
2022-03-03 20:22:37 +01:00
|
|
|
wsf.send(&tilesMsg{Tiles: changes})
|
2022-03-01 22:29:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wsf *websocketForwarder) BroadcastPlayers(pls []*player) {
|
2022-03-03 20:22:37 +01:00
|
|
|
wsf.send(&plsMsg{Pls: pls})
|
2022-03-01 22:29:56 +01:00
|
|
|
}
|
|
|
|
|
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)}
|
2022-03-01 22:29:56 +01:00
|
|
|
wsf.register(c)
|
|
|
|
defer wsf.unregister(c)
|
2015-03-03 01:13:37 +01:00
|
|
|
go c.writer()
|
2022-03-03 22:57:02 +01:00
|
|
|
if wsf.init != nil {
|
|
|
|
wsf.init(wsf, c)
|
|
|
|
}
|
2015-03-03 01:13:37 +01:00
|
|
|
c.reader()
|
2015-03-02 14:16:36 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|