diff --git a/handler/room/room.go b/handler/room/room.go index 30f86133..e3a9b4df 100644 --- a/handler/room/room.go +++ b/handler/room/room.go @@ -103,7 +103,6 @@ func (r *Room) isGameOnLocal(savepath string) bool { func (r *Room) AddConnectionToRoom(peerconnection *webrtc.WebRTC, playerIndex int) { peerconnection.AttachRoomID(r.ID) r.rtcSessions = append(r.rtcSessions, peerconnection) - fmt.Println("Peerconnection ID", peerconnection.ID, " numSessions ", len(r.rtcSessions)) go r.startWebRTCSession(peerconnection, playerIndex) } @@ -115,36 +114,32 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int } }() - for { - if r.Done { - log.Println("Detach peerconnection from room", r.ID) - return - } - if peerconnection.Done { - r.removeSession(peerconnection) - } - select { - case input, ok := <-peerconnection.InputChannel: + go func() { + for { + input, ok := <-peerconnection.InputChannel if !ok { return // might consider continue here } + if peerconnection.Done || !peerconnection.IsConnected() || r.Done { + return + } + if peerconnection.IsConnected() { // the first 8 bits belong to player 1 // the next 8 belongs to player 2 ... // We standardize and put it to inputChannel (16 bits) input = input << ((uint(playerIndex) - 1) * emulator.NumKeys) - r.inputChannel <- input - } - default: - if !peerconnection.IsConnected() { - log.Println("peerconnection is closed", peerconnection) - return + select { + case r.inputChannel <- input: + default: + } } } - // Client stopped - } + }() + + log.Println("Peerconn done") } func (r *Room) CleanSession(peerconnection *webrtc.WebRTC) { diff --git a/trace?seconds=30 b/trace?seconds=30 new file mode 100644 index 00000000..f4f81220 Binary files /dev/null and b/trace?seconds=30 differ diff --git a/trace?seconds=30.1 b/trace?seconds=30.1 new file mode 100644 index 00000000..ff28a9cf Binary files /dev/null and b/trace?seconds=30.1 differ diff --git a/trace?seconds=30.2 b/trace?seconds=30.2 new file mode 100644 index 00000000..1a5b7560 Binary files /dev/null and b/trace?seconds=30.2 differ