-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
151 lines (130 loc) · 3.97 KB
/
server.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
143
144
145
146
147
148
149
150
151
package main
import (
"errors"
"fmt"
"log"
"net"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/neguse/gomelift/pkg/gamelift"
glog "github.com/neguse/gomelift/pkg/log"
"github.com/neguse/gomelift/pkg/proto/pbuffer"
)
var upgrader = websocket.Upgrader{} // use default options
type Handler struct {
c gamelift.Client
port int
}
func (h *Handler) StartGameSession(event *pbuffer.ActivateGameSession) {
log.Println("StartGameSession", event)
if err := h.c.ActivateGameSession(&pbuffer.GameSessionActivate{
GameSessionId: event.GetGameSession().GetGameSessionId(),
MaxPlayers: event.GetGameSession().GetMaxPlayers(),
Port: event.GetGameSession().GetPort(),
}); err != nil {
log.Panic(err)
}
log.Println("ActivateGameSession complete. sessionID:", *h.c.GetGameSessionId())
}
func (h *Handler) UpdateGameSession(event *pbuffer.UpdateGameSession) {
log.Println("UpdateGameSession", event)
}
func (h *Handler) ProcessTerminate(event *pbuffer.TerminateProcess) {
log.Println("ProcessTerminate", event)
}
func (h *Handler) HealthCheck() bool {
log.Println("HealthCheck")
return true
}
func (h *Handler) AcceptPlayerHandler(w http.ResponseWriter, r *http.Request) {
psess := r.URL.Query().Get("psess")
if err := h.c.AcceptPlayerSession(&pbuffer.AcceptPlayerSession{
GameSessionId: *h.c.GetGameSessionId(),
PlayerSessionId: psess,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) RemovePlayerHandler(w http.ResponseWriter, r *http.Request) {
psess := r.URL.Query().Get("psess")
if err := h.c.RemovePlayerSession(&pbuffer.RemovePlayerSession{
GameSessionId: *h.c.GetGameSessionId(),
PlayerSessionId: psess,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) TerminateHandler(w http.ResponseWriter, r *http.Request) {
if err := h.c.TerminateGameSession(&pbuffer.GameSessionTerminate{
GameSessionId: *h.c.GetGameSessionId(),
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
// This is hidoi.
log.Panic("terminate called")
w.WriteHeader(http.StatusNoContent)
}
// OpenFreeUDPPort opens free UDP port.
// This example does not actually use UDP ports,
// but to avoid port collisions with the HTTP server,
// it binds the same number of UDP port in advance.
func OpenFreeUDPPort(portBase int, num int) (net.PacketConn, int, error) {
for i := 0; i < num; i++ {
port := portBase + i
conn, err := net.ListenPacket("udp", fmt.Sprint(":", port))
if err != nil {
continue
}
return conn, port, nil
}
return nil, 0, errors.New("failed to open free port")
}
func main() {
conn, port, err := OpenFreeUDPPort(9000, 100)
if err != nil {
log.Panic(err)
}
defer conn.Close()
logger := &glog.StandardLogger{}
c := gamelift.NewClient(logger)
h := &Handler{c: c}
c.Handle(h)
if err := c.Open(); err != nil {
log.Panic(err)
}
if err := c.ProcessReady(&pbuffer.ProcessReady{
LogPathsToUpload: []string{},
Port: int32(port),
// MaxConcurrentGameSessions: 0, // not set in original ServerSDK
}); err != nil {
log.Panic(err)
}
res, err := c.GetInstanceCertificate(&pbuffer.GetInstanceCertificate{})
log.Println(res, err)
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
r.HandleFunc("/acceptPlayer", func(w http.ResponseWriter, r *http.Request) {
h.AcceptPlayerHandler(w, r)
})
r.HandleFunc("/removePlayer", func(w http.ResponseWriter, r *http.Request) {
h.RemovePlayerHandler(w, r)
})
r.HandleFunc("/terminate", func(w http.ResponseWriter, r *http.Request) {
h.TerminateHandler(w, r)
})
if err := http.ListenAndServeTLS(fmt.Sprint(":", port), res.CertificatePath, res.PrivateKeyPath, r); err != nil {
log.Panic(err)
}
}