-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.go
59 lines (50 loc) · 1.16 KB
/
room.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package rose
import (
"log"
"runtime"
"github.com/golang/protobuf/proto"
)
// RoomID unique room id type
type RoomID uint64
// Room interface describing a game room.
type Room interface {
// Overwritable
Tick()
AddUser(User)
RemoveUser(User)
HandleMessage(User, MessageType, []byte)
Broadcast(MessageType, proto.Message)
BroadcastExcluded(MessageType, proto.Message, User)
Cleanup()
Base() *RoomBase
}
// Action to run on the room's goroutine
// This can be used to interact with the rooms internals
type Action func(Room)
// roomRun a goroutine. Runs the room code until destruction of the room
func roomRun(room Room) {
// Make sure we clean up
defer room.Cleanup()
// Set up crash recovery
defer func() {
if r := recover(); r != nil {
//TODO Move to handler that is setable from the outside!
trace := make([]byte, 1024)
runtime.Stack(trace, false)
log.Printf("Recovered from a panic\n%+v\n%s\n", r, trace)
return
}
}()
// Get RoomBase
base := room.Base()
// Start processing room
for !base.destroying {
select {
case <-base.ticker.C:
room.Tick()
runtime.Gosched()
case action := <-base.actionQueue:
action(room)
}
}
}