Close input chan after quit game

This commit is contained in:
giongto35 2019-06-05 11:51:22 +08:00 committed by giongto35
parent 61fe8923fa
commit 6b3545efc0
3 changed files with 34 additions and 29 deletions

View file

@ -247,7 +247,8 @@ function doButtonUp(name) {
// TODO: Stop game
conn.send(JSON.stringify({ "id": "quit", "data": "", "room_id": roomID }));
$("#room-txt").val("");
roomID = ""
$("#room-txt").val(roomID);
popup("Quit!");
break;
}

View file

@ -96,7 +96,18 @@ func (h *Handler) detachPeerConn(pc *webrtc.WebRTC) {
if room == nil {
return
}
room.CleanSession(pc)
// If room has no sessions, close room
if !room.EmptySessions() {
room.RemoveSession(pc)
if room.EmptySessions() {
log.Println("No session in room")
room.Close()
// Signal end of input Channel
log.Println("Signal input chan")
pc.InputChannel <- -1
}
}
}
// getRoom returns room from roomID

View file

@ -120,33 +120,29 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int
}
}()
go func() {
for input := range peerconnection.InputChannel {
if peerconnection.Done || !peerconnection.IsConnected() || !r.IsRunning {
return
}
for input := range peerconnection.InputChannel {
// NOTE: when room is no longer running. InputChannel needs to have extra event to go inside the loop
if peerconnection.Done || !peerconnection.IsConnected() || !r.IsRunning {
break
}
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)
select {
case r.inputChannel <- input:
default:
}
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)
select {
case r.inputChannel <- input:
default:
}
}
}()
}
log.Println("Peerconn done")
}
func (r *Room) CleanSession(peerconnection *webrtc.WebRTC) {
r.removeSession(peerconnection)
}
func (r *Room) removeSession(w *webrtc.WebRTC) {
// RemoveSession removes a peerconnection from room and return true if there is no more room
func (r *Room) RemoveSession(w *webrtc.WebRTC) {
log.Println("Cleaning session: ", w.ID)
// TODO: get list of r.rtcSessions in lock
for i, s := range r.rtcSessions {
@ -154,13 +150,6 @@ func (r *Room) removeSession(w *webrtc.WebRTC) {
if s.ID == w.ID {
r.rtcSessions = append(r.rtcSessions[:i], r.rtcSessions[i+1:]...)
log.Println("Removed session ", s.ID, " from room: ", r.ID)
// If room has no sessions, close room
// Note: this logic cannot be brought outside of forloop because we only close room if room had at least one session
if len(r.rtcSessions) == 0 {
log.Println("No session in room")
r.Close()
}
break
}
}
@ -236,6 +225,10 @@ func (r *Room) LoadGame() error {
return err
}
func (r *Room) EmptySessions() bool {
return len(r.rtcSessions) == 0
}
func (r *Room) IsRunningSessions() bool {
// If there is running session
for _, s := range r.rtcSessions {