mirror of
https://github.com/codecat/go-enet.git
synced 2025-12-08 05:59:47 +00:00
Docker, github actions and godoc usage example
This commit is contained in:
parent
c9a7864fa2
commit
162e3c38b7
5 changed files with 148 additions and 0 deletions
12
.github/workflows/test.yaml
vendored
Normal file
12
.github/workflows/test.yaml
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
name: "Test"
|
||||||
|
|
||||||
|
on: push
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: make test
|
||||||
23
Dockerfile
Normal file
23
Dockerfile
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
ARG GO_VERSION=1.19
|
||||||
|
|
||||||
|
FROM golang:${GO_VERSION}
|
||||||
|
|
||||||
|
ARG ENET_VERSION=1.3.17
|
||||||
|
|
||||||
|
# Install enet.
|
||||||
|
# Installs to: /usr/local/lib/libenet.so
|
||||||
|
RUN apt update && \
|
||||||
|
apt install -y autoconf libtool && \
|
||||||
|
cd /tmp && \
|
||||||
|
git clone https://github.com/lsalzman/enet.git && \
|
||||||
|
cd /tmp/enet && \
|
||||||
|
git checkout v${ENET_VERSION} && \
|
||||||
|
autoreconf -vfi && \
|
||||||
|
./configure && make && make install
|
||||||
|
|
||||||
|
# Ensure we can find enet at runtime.
|
||||||
|
ENV LD_LIBRARY_PATH=/usr/local/lib
|
||||||
|
|
||||||
|
RUN mkdir -p /go-enet
|
||||||
|
WORKDIR /go-enet
|
||||||
|
COPY . .
|
||||||
3
Makefile
Normal file
3
Makefile
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
test:
|
||||||
|
docker build -t go-enet .
|
||||||
|
docker run --rm -e "GODEBUG=cgocheck=2" go-enet go test -v -test.timeout=30s -count=1 ./...
|
||||||
107
enet_client_server_test.go
Normal file
107
enet_client_server_test.go
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
package enet_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/codecat/go-enet"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example_clientServer() {
|
||||||
|
// This example demonstrates some basic usage of the enet library.
|
||||||
|
// Here we set up a client & server, send a message between them, then
|
||||||
|
// disconnect & cleanup.
|
||||||
|
|
||||||
|
port := uint16(1234)
|
||||||
|
|
||||||
|
fmt.Printf("enet version: %s\n", enet.LinkedVersion())
|
||||||
|
|
||||||
|
// Initialize enet
|
||||||
|
enet.Initialize()
|
||||||
|
|
||||||
|
// Make our server.
|
||||||
|
server, err := enet.NewHost(enet.NewListenAddress(port), 32, 1, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(fmt.Errorf("unable to create server: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// For this example, we're going to wait until a disconnect event has been
|
||||||
|
// properly handled. Set this up here.
|
||||||
|
disconnected := make(chan bool, 0)
|
||||||
|
|
||||||
|
// Setup our server handling running in a separate goroutine.
|
||||||
|
go func() {
|
||||||
|
for true {
|
||||||
|
ev := server.Service(10)
|
||||||
|
|
||||||
|
switch ev.GetType() {
|
||||||
|
case enet.EventConnect:
|
||||||
|
fmt.Printf("[SERVER] new connection from client\n")
|
||||||
|
case enet.EventReceive:
|
||||||
|
fmt.Printf("[SERVER] received packet from client: %s\n", ev.GetPacket().GetData())
|
||||||
|
|
||||||
|
// We must destroy the packet when we're done with it
|
||||||
|
ev.GetPacket().Destroy()
|
||||||
|
|
||||||
|
// Send back a message to the client.
|
||||||
|
err := ev.GetPeer().SendString("message received!", 0, enet.PacketFlagReliable)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
case enet.EventDisconnect:
|
||||||
|
fmt.Printf("[SERVER] client disconnected")
|
||||||
|
close(disconnected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Make a client that will speak to the server.
|
||||||
|
client, err := enet.NewHost(nil, 32, 1, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to the server.
|
||||||
|
peer, err := client.Connect(enet.NewAddress("localhost", port), 1, 0)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep checking the client until we get a response from the server.
|
||||||
|
done := false
|
||||||
|
for !done {
|
||||||
|
ev := client.Service(10)
|
||||||
|
|
||||||
|
switch ev.GetType() {
|
||||||
|
case enet.EventReceive:
|
||||||
|
fmt.Printf("[CLIENT] received packet from server: %s\n", string(ev.GetPacket().GetData()))
|
||||||
|
ev.GetPacket().Destroy()
|
||||||
|
done = true
|
||||||
|
case enet.EventNone:
|
||||||
|
// If nothing else to do, send a packet.
|
||||||
|
fmt.Printf("[CLIENT] sending packet to server\n")
|
||||||
|
err = peer.SendString("hello world", 0, enet.PacketFlagReliable)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Immediately disconnect the client.
|
||||||
|
peer.DisconnectNow(0)
|
||||||
|
|
||||||
|
// Wait for the disconnection to be handled by the server.
|
||||||
|
<-disconnected
|
||||||
|
|
||||||
|
// Cleanup.
|
||||||
|
client.Destroy()
|
||||||
|
server.Destroy()
|
||||||
|
enet.Deinitialize()
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// enet version: 1.3.17
|
||||||
|
// [SERVER] new connection from client
|
||||||
|
// [CLIENT] sending packet to server
|
||||||
|
// [SERVER] received packet from client: hello world
|
||||||
|
// [CLIENT] received packet from server: message received!
|
||||||
|
// [SERVER] client disconnected
|
||||||
|
}
|
||||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module github.com/codecat/go-enet
|
||||||
|
|
||||||
|
go 1.19
|
||||||
Loading…
Add table
Add a link
Reference in a new issue