mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 17:47:11 +00:00
lock free input channel
This commit is contained in:
parent
d32b34c7db
commit
2084ca28a9
4 changed files with 14 additions and 19 deletions
|
|
@ -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
BIN
trace?seconds=30
vendored
Normal file
Binary file not shown.
BIN
trace?seconds=30.1
vendored
Normal file
BIN
trace?seconds=30.1
vendored
Normal file
Binary file not shown.
BIN
trace?seconds=30.2
vendored
Normal file
BIN
trace?seconds=30.2
vendored
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue