Skip to content

Commit bcecbdc

Browse files
author
Robert Bailey
authored
Add a flag to the simple game server so that it can have a delay (agones-dev#2340)
before marking itself ready. Also update the documentation for the flags and add documentation for the commands that you can send to the game server.
1 parent 6337f0b commit bcecbdc

10 files changed

Lines changed: 65 additions & 27 deletions

File tree

‎examples/simple-game-server/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ WITH_WINDOWS=1
3535

3636
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
3737
project_path := $(dir $(mkfile_path))
38-
server_tag = $(REGISTRY)/simple-game-server:0.4
38+
server_tag = $(REGISTRY)/simple-game-server:0.5
3939
ifeq ($(WITH_WINDOWS), 1)
4040
server_tag_linux_amd64 = $(server_tag)-linux_amd64
4141
else

‎examples/simple-game-server/README.md‎

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
# Simple Game Server
22

3-
A very simple logging server, for the purposes of demoing and testing
4-
running UDP and/or TCP based server on Agones.
3+
A very simple "toy" game server created to demo and test running a UDP and/or
4+
TCP game server on Agones.
55

6-
## Server
7-
Starts a server on port `7654` by default. Can be overwritten by `PORT` env var or `port` flag.
6+
To learn how to deploy your edited version of go server to gcp, please check
7+
out this link: [Edit Your First Game Server (Go)](https://agones.dev/site/docs/getting-started/edit-first-gameserver-go/),
8+
or also look at the [`Makefile`](./Makefile).
89

9-
By default listens on UDP. Set `UDP=FALSE` env var or `-udp=false` flag to turn UDP off.
10+
## Interacting with the Server
1011

11-
To enable TCP, set `TCP=TRUE` env var or `-tcp` flag. Must have at least TCP or UDP enabled.
12+
When the server receives a text packet, it will send back "ACK:<text content>"
13+
for UDP as an echo or "ACK TCP:<text content>" for TCP.
1214

13-
When it receives a text packet, it will send back "ACK:<text content>" for UDP as an echo or "ACK TCP:<text content>" for TCP.
15+
There are some text commands you can send the server to affect its behavior:
1416

15-
If it receives the text "EXIT", then it will `sys.Exit(0)`
17+
| Command | Behavior |
18+
| ------------------- | ---------------------------------------------------------------------------------------- |
19+
| "EXIT" | Causes the game server to exit cleanly calling `os.Exit(0)` |
20+
| "UNHEATHY" | Stopping sending health checks |
21+
| "GAMESERVER" | Sends back the game server name |
22+
| "READY" | Marks the server as Ready |
23+
| "ALLOCATE" | Allocates the game server |
24+
| "RESERVE" | Reserves the game server after the specified duration |
25+
| "WATCH" | Instructs the game server to log changes to the resource |
26+
| "LABEL" | Sets the specified label on the game server resource |
27+
| "CRASH" | Causes the game server to exit / crash immediately |
28+
| "ANNOTATION" | Sets the specified annotation on the game server resource |
29+
| "PLAYER_CAPACITY" | With one argument, gets the player capacity; with two arguments sets the player capacity |
30+
| "PLAYER_CONNECT" | Connects the specified player to the game server |
31+
| "PLAYER_DISCONNECT" | Disconnects the specified player from the game server |
32+
| "PLAYER_CONNECTED" | Returns true/false depending on whether the specified player is connected |
33+
| "GET_PLAYERS" | Returns a list of the connected players |
34+
| "PLAYER_COUNT" | Returns a count of the connected players |
35+
36+
37+
## Configuration
38+
39+
The server has a few configuration options that can be set via command line
40+
flags. Some can also be set using environment variables.
41+
42+
| Flag | Environment Variable | Default |
43+
| ------------------------- | -------------------- | ------- |
44+
| port | PORT | 7654 |
45+
| passthrough | PASSTHROUGH | false |
46+
| ready | READY | true |
47+
| automaticShutdownDelayMin | _n/a_ | 0 |
48+
| readyDelaySec | _n/a_ | 0 |
49+
| udp | UDP | true |
50+
| tcp | TCP | false |
1651

17-
To learn how to deploy your edited version of go server to gcp, please check out this link: [Edit Your First Game Server (Go)](https://agones.dev/site/docs/getting-started/edit-first-gameserver-go/),
18-
or also look at the [`Makefile`](./Makefile).

‎examples/simple-game-server/dev-gameserver.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ spec:
3131
spec:
3232
containers:
3333
- name: simple-game-server
34-
image: gcr.io/agones-images/simple-game-server:0.4
34+
image: gcr.io/agones-images/simple-game-server:0.5

‎examples/simple-game-server/fleet-distributed.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ spec:
3232
spec:
3333
containers:
3434
- name: simple-game-server
35-
image: gcr.io/agones-images/simple-game-server:0.4
35+
image: gcr.io/agones-images/simple-game-server:0.5
3636
resources:
3737
requests:
3838
memory: "64Mi"

‎examples/simple-game-server/fleet-tcp.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec:
2828
spec:
2929
containers:
3030
- name: simple-game-server
31-
image: gcr.io/agones-images/simple-game-server:0.4
31+
image: gcr.io/agones-images/simple-game-server:0.5
3232
env:
3333
# Disables the UDP listener (Enabled by default)
3434
- name: "UDP"

‎examples/simple-game-server/fleet.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ spec:
2727
spec:
2828
containers:
2929
- name: simple-game-server
30-
image: gcr.io/agones-images/simple-game-server:0.4
30+
image: gcr.io/agones-images/simple-game-server:0.5
3131
resources:
3232
requests:
3333
memory: "64Mi"

‎examples/simple-game-server/gameserver-passthrough.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ spec:
2424
spec:
2525
containers:
2626
- name: simple-game-server
27-
image: gcr.io/agones-images/simple-game-server:0.4
27+
image: gcr.io/agones-images/simple-game-server:0.5
2828
env:
2929
- name: "PASSTHROUGH"
3030
value: "TRUE"

‎examples/simple-game-server/gameserver-windows.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spec:
2525
spec:
2626
containers:
2727
- name: simple-game-server
28-
image: gcr.io/agones-images/simple-game-server:0.4
28+
image: gcr.io/agones-images/simple-game-server:0.5
2929
resources:
3030
requests:
3131
memory: "64Mi"

‎examples/simple-game-server/gameserver.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spec:
2525
spec:
2626
containers:
2727
- name: simple-game-server
28-
image: gcr.io/agones-images/simple-game-server:0.4
28+
image: gcr.io/agones-images/simple-game-server:0.5
2929
resources:
3030
requests:
3131
memory: "64Mi"

‎examples/simple-game-server/main.go‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func main() {
4040
passthrough := flag.Bool("passthrough", false, "Get listening port from the SDK, rather than use the 'port' value")
4141
readyOnStart := flag.Bool("ready", true, "Mark this GameServer as Ready on startup")
4242
shutdownDelay := flag.Int("automaticShutdownDelayMin", 0, "If greater than zero, automatically shut down the server this many minutes after the server becomes allocated")
43+
readyDelaySec := flag.Int("readyDelaySec", 0, "If greater than zero and readyOnStart is true, wait this many seconds before marking the game server as ready")
4344
udp := flag.Bool("udp", true, "Server will listen on UDP")
4445
tcp := flag.Bool("tcp", false, "Server will listen on TCP")
4546

@@ -89,7 +90,19 @@ func main() {
8990
port = &p
9091
}
9192

93+
if *tcp {
94+
go tcpListener(port, s, stop)
95+
}
96+
97+
if *udp {
98+
go udpListener(port, s, stop)
99+
}
100+
92101
if *readyOnStart {
102+
if *readyDelaySec > 0 {
103+
log.Printf("Waiting %d seconds before moving to ready", *readyDelaySec)
104+
time.Sleep(time.Duration(*readyDelaySec) * time.Second)
105+
}
93106
log.Print("Marking this server as ready")
94107
ready(s)
95108
}
@@ -98,15 +111,7 @@ func main() {
98111
shutdownAfterAllocation(s, *shutdownDelay)
99112
}
100113

101-
if *tcp {
102-
go tcpListener(port, s, stop)
103-
}
104-
105-
if *udp {
106-
go udpListener(port, s, stop)
107-
}
108-
109-
// prevents program from quitting as the server is listening on goroutines
114+
// Prevent the program from quitting as the server is listening on goroutines.
110115
for {}
111116
}
112117

0 commit comments

Comments
 (0)