Skip to content

Commit

Permalink
WebSocket Server: Proper locking and connection counting
Browse files Browse the repository at this point in the history
  • Loading branch information
univrsal committed Apr 10, 2024
1 parent 24ba67b commit 436fa46
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 14 additions & 2 deletions core/wss/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,61 @@ func process_command(session *IrosSession, cmd_type string, data map[string]json
if err != nil {
return
}

session.Mutex.Lock()
session.load_element(t, command["args"])
session.Mutex.Unlock()
case "update":
var id string
err := json.Unmarshal(data["id"], &id)
if err != nil {
return
}
session.Mutex.Lock()
session.State[id].Update(command)
session.Mutex.Unlock()
case "delete":
var id string
err := json.Unmarshal(data["id"], &id)

if err != nil {
return
}

session.Mutex.Lock()
delete(session.State, id)
session.Mutex.Unlock()
case "transform":
var id string
var transform elements.ElementTransform
if err := try_unmarshal(&id, data["id"], data["transform"], &transform); err == nil {
session.Mutex.Lock()
session.State[id].SetTransform(transform)
session.Mutex.Unlock()
}
case "move": // move element
var id string
var pos elements.Point

if err := try_unmarshal(&id, data["id"], data["pos"], &pos); err == nil {
session.Mutex.Lock()
session.State[id].SetPosition(pos)
session.Mutex.Unlock()
}

case "scale": // scale element
var id string
var scale elements.Point
if err := try_unmarshal(&id, data["id"], data["scale"], &scale); err == nil {
session.Mutex.Lock()
session.State[id].SetScale(scale)
session.Mutex.Unlock()
}
case "rotate": // rotate element
var id string
var rotation elements.Rotation
if err := try_unmarshal(&id, data["id"], data["rotation"], &rotation); err == nil {
session.Mutex.Lock()
session.State[id].SetRotation(rotation)
session.Mutex.Unlock()
}
default:
// some commands are only for client to client communication and have no effect on the server
Expand Down
14 changes: 7 additions & 7 deletions core/wss/websocket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ func listen(conn *websocket.Conn) {
val, exists := Instance.Sessions[session]

if exists {
for i := range val.Connections {
val.Mutex.Lock()
length := len(val.Connections)
// loop with length
for i := 0; i < length; i++ {
c := val.Connections[i]
// don't send to the sender
if c.Conn == conn {
Expand All @@ -251,12 +254,12 @@ func listen(conn *websocket.Conn) {

if err != nil {
// Remove the failed connection from the slice
val.Mutex.Lock()

val.Connections = append(val.Connections[:i], val.Connections[i+1:]...)
val.Mutex.Unlock()
continue
length--
}
}
val.Mutex.Unlock()

// keep track of session state
var t string
Expand All @@ -274,9 +277,6 @@ func listen(conn *websocket.Conn) {
log.Println("Received command with invalid session")
}
}

// Don't think we should ever reach this
atomic.AddInt32(&util.Stats.NumWSConnections, -1)
}

func (s *WebSocketServer) Start() {
Expand Down

0 comments on commit 436fa46

Please sign in to comment.