From 3a6b5ef0642705e5d7dd9583e7e03f3e6264f7f7 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Fri, 2 Feb 2024 19:54:39 -0500 Subject: [PATCH] Move examples/ping-pong into two directories Example is now directly Go runnable --- examples/ping-pong/Makefile | 10 --- examples/ping-pong/README.md | 12 ++-- examples/ping-pong/{ => ping}/conn.go | 0 examples/ping-pong/{ping.go => ping/main.go} | 8 ++- examples/ping-pong/pong/conn.go | 76 ++++++++++++++++++++ examples/ping-pong/{pong.go => pong/main.go} | 28 ++++++-- 6 files changed, 106 insertions(+), 28 deletions(-) delete mode 100644 examples/ping-pong/Makefile rename examples/ping-pong/{ => ping}/conn.go (100%) rename examples/ping-pong/{ping.go => ping/main.go} (94%) create mode 100644 examples/ping-pong/pong/conn.go rename examples/ping-pong/{pong.go => pong/main.go} (74%) diff --git a/examples/ping-pong/Makefile b/examples/ping-pong/Makefile deleted file mode 100644 index 361a0253..00000000 --- a/examples/ping-pong/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -# SPDX-FileCopyrightText: 2023 The Pion community -# SPDX-License-Identifier: MIT - -all: ping pong - -ping: ping.go conn.go - go build -o $@ - -pong: pong.go conn.go - go build -o $@ -tags $@ \ No newline at end of file diff --git a/examples/ping-pong/README.md b/examples/ping-pong/README.md index 1138ecff..e3ca4c8b 100644 --- a/examples/ping-pong/README.md +++ b/examples/ping-pong/README.md @@ -9,21 +9,17 @@ In this example, there are 2 types of peers: **ping** and **pong**. ## Instruction -### Build ping and pong - -```sh -make -``` +### Run ping and pong ### Run pong ```sh -./pong +go run github.com/pion/sctp/examples/ping-pong/pong@latest ``` ### Run ping ```sh -./ping -``` \ No newline at end of file +go run github.com/pion/sctp/examples/ping-pong/ping@latest +``` diff --git a/examples/ping-pong/conn.go b/examples/ping-pong/ping/conn.go similarity index 100% rename from examples/ping-pong/conn.go rename to examples/ping-pong/ping/conn.go diff --git a/examples/ping-pong/ping.go b/examples/ping-pong/ping/main.go similarity index 94% rename from examples/ping-pong/ping.go rename to examples/ping-pong/ping/main.go index 2c5bc7b6..3ee6d293 100644 --- a/examples/ping-pong/ping.go +++ b/examples/ping-pong/ping/main.go @@ -10,6 +10,7 @@ import ( "fmt" "log" "net" + "time" "github.com/pion/logging" "github.com/pion/sctp" @@ -22,7 +23,7 @@ func main() { } defer func() { if closeErr := conn.Close(); closeErr != nil { - panic(err) + log.Panic(err) } }() fmt.Println("dialed udp ponger") @@ -37,7 +38,7 @@ func main() { } defer func() { if closeErr := a.Close(); closeErr != nil { - panic(err) + log.Panic(err) } }() fmt.Println("created a client") @@ -48,7 +49,7 @@ func main() { } defer func() { if closeErr := stream.Close(); closeErr != nil { - panic(err) + log.Panic(err) } }() fmt.Println("opened a stream") @@ -66,6 +67,7 @@ func main() { } fmt.Println("sent:", pingMsg) pingSeqNum++ + time.Sleep(time.Second) } }() diff --git a/examples/ping-pong/pong/conn.go b/examples/ping-pong/pong/conn.go new file mode 100644 index 00000000..3b0b448b --- /dev/null +++ b/examples/ping-pong/pong/conn.go @@ -0,0 +1,76 @@ +// SPDX-FileCopyrightText: 2023 The Pion community +// SPDX-License-Identifier: MIT + +// Package main implements a simple ping-pong example +package main + +import ( + "net" + "sync" + "time" +) + +// Reference: https://github.com/pion/sctp/blob/master/association_test.go +// Since UDP is connectionless, as a server, it doesn't know how to reply +// simply using the `Write` method. So, to make it work, `disconnectedPacketConn` +// will infer the last packet that it reads as the reply address for `Write` + +type disconnectedPacketConn struct { // nolint: unused + mu sync.RWMutex + rAddr net.Addr + pConn net.PacketConn +} + +// Read +func (c *disconnectedPacketConn) Read(p []byte) (int, error) { //nolint:unused + i, rAddr, err := c.pConn.ReadFrom(p) + if err != nil { + return 0, err + } + + c.mu.Lock() + c.rAddr = rAddr + c.mu.Unlock() + + return i, err +} + +// Write writes len(p) bytes from p to the DTLS connection +func (c *disconnectedPacketConn) Write(p []byte) (n int, err error) { //nolint:unused + return c.pConn.WriteTo(p, c.RemoteAddr()) +} + +// Close closes the conn and releases any Read calls +func (c *disconnectedPacketConn) Close() error { //nolint:unused + return c.pConn.Close() +} + +// LocalAddr is a stub +func (c *disconnectedPacketConn) LocalAddr() net.Addr { //nolint:unused + if c.pConn != nil { + return c.pConn.LocalAddr() + } + return nil +} + +// RemoteAddr is a stub +func (c *disconnectedPacketConn) RemoteAddr() net.Addr { //nolint:unused + c.mu.RLock() + defer c.mu.RUnlock() + return c.rAddr +} + +// SetDeadline is a stub +func (c *disconnectedPacketConn) SetDeadline(time.Time) error { //nolint:unused + return nil +} + +// SetReadDeadline is a stub +func (c *disconnectedPacketConn) SetReadDeadline(time.Time) error { //nolint:unused + return nil +} + +// SetWriteDeadline is a stub +func (c *disconnectedPacketConn) SetWriteDeadline(time.Time) error { //nolint:unused + return nil +} diff --git a/examples/ping-pong/pong.go b/examples/ping-pong/pong/main.go similarity index 74% rename from examples/ping-pong/pong.go rename to examples/ping-pong/pong/main.go index 0a12948e..2f7085a3 100644 --- a/examples/ping-pong/pong.go +++ b/examples/ping-pong/pong/main.go @@ -1,8 +1,6 @@ // SPDX-FileCopyrightText: 2023 The Pion community // SPDX-License-Identifier: MIT -// +build pong - package main import ( @@ -25,7 +23,11 @@ func main() { if err != nil { log.Panic(err) } - defer conn.Close() + defer func() { + if closeErr := conn.Close(); closeErr != nil { + log.Panic(closeErr) + } + }() fmt.Println("created a udp listener") config := sctp.Config{ @@ -36,14 +38,22 @@ func main() { if err != nil { log.Panic(err) } - defer a.Close() - fmt.Println("created a server") + defer func() { + if closeErr := a.Close(); closeErr != nil { + log.Panic(closeErr) + } + }() + defer fmt.Println("created a server") stream, err := a.AcceptStream() if err != nil { log.Panic(err) } - defer stream.Close() + defer func() { + if closeErr := stream.Close(); closeErr != nil { + log.Panic(closeErr) + } + }() fmt.Println("accepted a stream") // set unordered = true and 10ms treshold for dropping packets @@ -58,7 +68,11 @@ func main() { pingMsg := string(buff) fmt.Println("received:", pingMsg) - fmt.Sscanf(pingMsg, "ping %d", &pongSeqNum) + _, err = fmt.Sscanf(pingMsg, "ping %d", &pongSeqNum) + if err != nil { + log.Panic(err) + } + pongMsg := fmt.Sprintf("pong %d", pongSeqNum) _, err = stream.Write([]byte(pongMsg)) if err != nil {