mchess-server/connection/type.go
Marco ae3e73f711 Another step towards allowing reconnections
1. Implement thread-safe ringbuffer for websocket messages

This implements a ringbuffer that is used to decouple the raw websocket
connection from the messages that the game handler handles.

2. Change websocket handling

With this commit, we stop waiting for the websocket connection to be
established before the game starts.
Now, the Connection type is responsible for waiting for the websocket
connection before writing.

Some bugs are still happening:
	1. The rejoining client is not told the state of the board
	2. Invalid moves are not handled by the client (not sure why though)
	3. The still-connected client should be told, that the opponent
	   disconnected. Then the client should show the passphrase again

3. Introduce method to send status of board and player

4. Reconnect works (kind of)

With the right changes in the client, the reconnect works (but only for
the first time).

WARNING:
At the moment, we will create a new player whenever connection wants to
join a private game. This will also clear all the disconnect callbacks
that we set in the player.
2023-12-09 21:00:52 +01:00

109 lines
2.1 KiB
Go

package connection
import (
"context"
"log"
"nhooyr.io/websocket"
)
type Connection struct {
ws *websocket.Conn
wsConnectionEstablished chan bool
ctx context.Context
buffer MessageBuffer
disconnectCallback func()
}
func NewConnection(options ...func(*Connection)) *Connection {
connection := Connection{
buffer: *newMessageBuffer(100),
wsConnectionEstablished: make(chan bool),
}
for _, option := range options {
option(&connection)
}
return &connection
}
func WithWebsocket(ws *websocket.Conn) func(*Connection) {
return func(c *Connection) {
c.ws = ws
}
}
func WithContext(ctx context.Context) func(*Connection) {
return func(c *Connection) {
c.ctx = ctx
}
}
func WithDisconnectCallback(cb func()) func(*Connection) {
return func(c *Connection) {
if cb != nil {
c.disconnectCallback = cb
}
}
}
func (conn *Connection) SetDisconnectCallback(cb func()) {
conn.disconnectCallback = cb
}
func (conn *Connection) HasWebsocketConnection() bool {
return conn.ws != nil
}
func (conn *Connection) SetWebsocketConnection(ws *websocket.Conn) {
if ws == nil {
return
}
conn.ws = ws
select {
case conn.wsConnectionEstablished <- true:
default:
}
go func() {
for {
_, msg, err := conn.ws.Read(conn.ctx)
if err != nil {
log.Println("while reading from websocket: %w", err)
if conn.disconnectCallback != nil {
conn.disconnectCallback()
}
return
}
conn.buffer.Insert(string(msg))
}
}()
}
func (conn *Connection) Write(msg []byte) error {
if conn.ws == nil { //if ws is not yet set, we wait for it
<-conn.wsConnectionEstablished
}
log.Printf("Writing message: %s", string(msg))
return conn.ws.Write(conn.ctx, websocket.MessageText, msg)
}
func (conn *Connection) Read() ([]byte, error) {
msg, err := conn.buffer.Get()
if err != nil {
conn.ws = nil
return nil, err // Tell game-handler that connection was lost
}
return []byte(msg), err
}
func (conn *Connection) Close(msg string) {
conn.ws.Close(websocket.StatusCode(400), msg)
conn.ws = nil
}