-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (74 loc) · 2.2 KB
/
main.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
package blastoff
import (
"log"
"sync"
"github.com/codecat/go-enet"
"github.com/google/uuid"
)
func CreateServer(address enet.Address) *BlastoffServer {
return &BlastoffServer{
Address: address,
remoteAddressMap: make(map[uuid.UUID]enet.Address),
peerMap: make(map[enet.Peer]peerData),
}
}
func NewAddress(ip string, port uint16) enet.Address {
return enet.NewAddress(ip, port)
}
var defaultRemote enet.Address
func (server *BlastoffServer) AddRemote(uuid uuid.UUID, address enet.Address) {
server.remoteAddressMap[uuid] = address
if defaultRemote == nil {
defaultRemote = address
}
}
func (server *BlastoffServer) Start() {
enet.Initialize()
host, err := enet.NewHost(server.Address, 1024, 0, 0, 0)
if err != nil {
log.Fatalf("Couldn't create host: %s\n", err.Error())
return
}
err = host.CompressWithRangeCoder()
if err != nil {
log.Fatalf("Couldn't enable compression mode: %s\n", err.Error())
return
}
log.Printf("Server started %s:%d\n", server.Address.String(), server.Address.GetPort())
var wg sync.WaitGroup
wg.Add(1)
go func() {
// The main Blastoff server loop
// These peers are attempting to connect to remotes
for {
ev := host.Service(10)
if ev.GetType() == enet.EventNone {
continue
}
switch ev.GetType() {
case enet.EventConnect:
log.Printf("New peer connected: %s\n", ev.GetPeer().GetAddress())
server.peerMap[ev.GetPeer()] = peerData{
Peer: ev.GetPeer(),
PacketChannel: make(chan bridgePacket, 10),
CloseSignal: make(chan bool, 10),
}
go server.bridgePeerToRemote(ev.GetPeer(), server.peerMap[ev.GetPeer()].PacketChannel, server.peerMap[ev.GetPeer()].CloseSignal)
case enet.EventDisconnect:
log.Printf("Peer disconnected: %s\n", ev.GetPeer().GetAddress())
server.peerMap[ev.GetPeer()].CloseSignal <- true
delete(server.peerMap, ev.GetPeer())
case enet.EventReceive:
packet := ev.GetPacket()
// Forward all client messages to the remote
server.peerMap[ev.GetPeer()].PacketChannel <- bridgePacket{packet, ev.GetChannelID()}
}
}
wg.Done()
}()
wg.Wait()
// Destroy the host when we're done with it
host.Destroy()
// Uninitialize enet
enet.Deinitialize()
}