lock free input channel

This commit is contained in:
giongto35 2019-05-11 03:15:50 +08:00
parent c39386ca6b
commit e4b6e2e99f
4 changed files with 14 additions and 19 deletions

View file

@ -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) {

BIN
trace?seconds=30 vendored Normal file

Binary file not shown.

BIN
trace?seconds=30.1 vendored Normal file

Binary file not shown.

BIN
trace?seconds=30.2 vendored Normal file

Binary file not shown.