-
Notifications
You must be signed in to change notification settings - Fork 1
/
net.go
58 lines (48 loc) · 1.28 KB
/
net.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
package gna
import (
"sync"
"time"
)
/*Net abstracts the Networking, it is meant to be embedded into your
server Instance, providing the Terminate and NetAbs methods.
*/
type Net struct {
rTimeout time.Duration
wTimeout time.Duration
done chan struct{}
ticker *time.Ticker
Players *Group
acu *playerBucket
dc chan *Player
started bool
mu sync.Mutex
}
/*NetAbs exposes the underlying networking abstraction,
this is for internal use only.
*/
func (n *Net) NetAbs() *Net {
return n
}
func (n *Net) fillDefault() {
n.rTimeout = stdReadTimeout
n.wTimeout = stdWriteTimeout
n.done = make(chan struct{})
n.ticker = time.NewTicker(time.Second / time.Duration(stdTPS))
n.Players = &Group{pMap: make(map[uint64]*Player, 16)}
n.acu = &playerBucket{dt: make([]*Input, 64)}
n.dc = make(chan *Player, 1)
}
/*Terminate closes the connection with all players and stops the updates*/
func (n *Net) Terminate() {
n.Players.Close()
n.done <- struct{}{}
}
/*GetData empties the Net acumulator, retrieving the Inputs*/
func (n *Net) GetData() []*Input {
return n.acu.consume()
}
/*Dispatch sends the data to the corresponding dispatchers for each shipper,
the structs that implement ship() are: Players and Groups*/
func (*Net) Dispatch(s shipper, data interface{}) {
s.ship(data)
}