Check peerconnection is in room before detach and reconn

This commit is contained in:
giongto35 2019-05-16 01:04:54 +08:00
parent 5b52f47357
commit 9ea0bf223c
4 changed files with 35 additions and 23 deletions

View file

@ -46,6 +46,9 @@ func (s *Session) RouteBrowser() {
req.Data = "ok"
if s.RoomID != "" {
room := s.handler.getRoom(s.RoomID)
if room == nil {
return
}
err := room.SaveGame()
if err != nil {
log.Println("[!] Cannot save game state: ", err)
@ -95,17 +98,22 @@ func (s *Session) RouteBrowser() {
}
}
// Create new room
// Get Room in local server
// TODO: check if roomID is in the current server
room := s.handler.getRoom(s.RoomID)
log.Println("Got Room from local ", room, " ID: ", s.RoomID)
// If room is not running
if room == nil {
// Create new room
room = s.handler.createNewRoom(s.GameName, s.RoomID, s.PlayerIndex)
}
// Attach peerconnection to room
s.handler.detachPeerConn(s.peerconnection)
room.AddConnectionToRoom(s.peerconnection, s.PlayerIndex)
// Attach peerconnection to room. If PC is already in room, don't detach
log.Println("Is PC in room", room.IsPCInRoom(s.peerconnection))
if !room.IsPCInRoom(s.peerconnection) {
s.handler.detachPeerConn(s.peerconnection)
room.AddConnectionToRoom(s.peerconnection, s.PlayerIndex)
}
s.RoomID = room.ID
// Register room to overlord if we are connecting to overlord

View file

@ -112,18 +112,14 @@ func (h *Handler) WS(w http.ResponseWriter, r *http.Request) {
Data: gamelist.GetEncodedGameList(h.gamePath),
}, nil)
wssession.BrowserClient.Listen()
// TODO: Use callback
// If peerconnection is done (client.Done is signalled), we close peerconnection
go func() {
for {
<-client.Done
h.detachPeerConn(wssession.peerconnection)
return
}
<-client.Done
log.Println("Socket terminated, detach connection")
h.detachPeerConn(wssession.peerconnection)
}()
wssession.BrowserClient.Listen()
}
// Detach peerconnection detach/remove a peerconnection from current room

View file

@ -1,7 +1,6 @@
package room
import (
"fmt"
"image"
"io/ioutil"
"log"
@ -147,14 +146,13 @@ func (r *Room) CleanSession(peerconnection *webrtc.WebRTC) {
}
func (r *Room) removeSession(w *webrtc.WebRTC) {
fmt.Println("Cleaning session: ", w)
fmt.Println("Sessions list", r.rtcSessions)
log.Println("Cleaning session: ", w.ID)
// TODO: get list of r.rtcSessions in lock
for i, s := range r.rtcSessions {
fmt.Println("found session: ", s, w)
log.Println("found session: ", w.ID)
if s.ID == w.ID {
r.rtcSessions = append(r.rtcSessions[:i], r.rtcSessions[i+1:]...)
fmt.Println("found session: ", len(r.rtcSessions))
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
@ -169,6 +167,16 @@ func (r *Room) removeSession(w *webrtc.WebRTC) {
}
}
// TODO: Reuse for remove Session
func (r *Room) IsPCInRoom(w *webrtc.WebRTC) bool {
for _, s := range r.rtcSessions {
if s.ID == w.ID {
return true
}
}
return false
}
func (r *Room) Close() {
if r.Done {
return

View file

@ -93,12 +93,12 @@ type WebRTC struct {
// StartClient start webrtc
func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, error) {
defer func() {
if err := recover(); err != nil {
log.Println(err)
w.StopClient()
}
}()
//defer func() {
//if err := recover(); err != nil {
//log.Println(err)
//w.StopClient()
//}
//}()
// reset client
if w.isConnected {