-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
142 lines (121 loc) · 2.98 KB
/
util.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"github.com/golang/protobuf/proto"
"errors"
"fmt"
"github.com/Starbow/erosd/buffers"
"runtime/debug"
"strconv"
"strings"
)
func genericPanicRecover() {
if r := recover(); r != nil {
fmt.Println("Recovered from a panic", r)
debug.PrintStack()
}
}
//Take a "CMD TxID Len\n" input and split it up
func Unpack(data string) (event string, txid int, size int, err error) {
data = strings.TrimRight(data, "\n")
data = strings.TrimRight(data, "\r")
result := strings.Split(data, " ")
if len(result) != 3 {
err = errors.New("Unable to extract event data.")
return
}
txid, err = strconv.Atoi(result[1])
if err != nil {
return
}
size, err = strconv.Atoi(result[2])
if err != nil {
return
}
event = result[0]
err = nil
return
}
//Data -> proto.Message
func Unmarshal(data []byte, message proto.Message) error {
return proto.Unmarshal(data, message)
}
//message -> data
func Marshal(message proto.Message) (data []byte, err error) {
return proto.Marshal(message)
}
//Broadcast a message to all active connections.
func broadcastMessage(command string, message proto.Message) {
defer genericPanicRecover()
data, err := Marshal(message)
if err != nil {
panic(err)
}
for _, v := range clientConnections {
if v == nil {
continue
}
if v.client == nil {
continue
}
go v.SendServerMessage(command, data)
}
}
//Broadcast a message to a specific client.
func (c *Client) Broadcast(command string, message proto.Message) {
defer genericPanicRecover()
var (
data []byte = []byte{}
err error
)
if message != nil {
data, err = Marshal(message)
if err != nil {
panic(err)
}
}
for _, v := range clientConnections {
if v == nil {
continue
}
if v.client.Id == c.Id {
go v.SendServerMessage(command, data)
}
}
}
// Generates server stats protocol buffer message. This should be elsewhere maybe.
func NewMatchmakingStats(region BattleNetRegion) *protobufs.MatchmakingStats {
var (
stats protobufs.MatchmakingStats
protoRegion protobufs.Region = protobufs.Region(region)
searching int64 = 0
)
if _, ok := matchmaker.regionParticipants[region]; ok {
searching = int64(len(matchmaker.regionParticipants[region]))
}
stats.Region = &protoRegion
stats.SearchingUsers = &searching
return &stats
}
func NewServerStats() *protobufs.ServerStats {
var (
x protobufs.ServerStats
connected int64 = int64(len(clientConnections))
mm int64 = int64(len(matchmaker.participants))
)
x.ActiveUsers = &connected
x.SearchingUsers = &mm
x.Region = []*protobufs.MatchmakingStats{
NewMatchmakingStats(BATTLENET_REGION_NA),
NewMatchmakingStats(BATTLENET_REGION_EU),
NewMatchmakingStats(BATTLENET_REGION_KR),
NewMatchmakingStats(BATTLENET_REGION_CN),
NewMatchmakingStats(BATTLENET_REGION_SEA),
}
return &x
}
func SendBroadcastAlert(predefined int32, message string) {
var bufmsg protobufs.BroadcastAlert
bufmsg.Message = &message
bufmsg.Predefined = &predefined
broadcastMessage("ALT", &bufmsg)
}