Skip to content

Commit

Permalink
Move examples/ping-pong into two directories
Browse files Browse the repository at this point in the history
Example is now directly Go runnable
  • Loading branch information
Sean-Der committed Feb 3, 2024
1 parent 4bb25dc commit 21bfe15
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 28 deletions.
10 changes: 0 additions & 10 deletions examples/ping-pong/Makefile

This file was deleted.

12 changes: 4 additions & 8 deletions examples/ping-pong/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
go run github.com/pion/sctp/examples/ping-pong/ping@latest
```
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"log"
"net"
"time"

"github.com/pion/logging"
"github.com/pion/sctp"
Expand All @@ -22,7 +23,7 @@ func main() {
}
defer func() {
if closeErr := conn.Close(); closeErr != nil {
panic(err)
log.Panic(err)
}
}()
fmt.Println("dialed udp ponger")
Expand All @@ -37,7 +38,7 @@ func main() {
}
defer func() {
if closeErr := a.Close(); closeErr != nil {
panic(err)
log.Panic(err)
}
}()
fmt.Println("created a client")
Expand All @@ -48,7 +49,7 @@ func main() {
}
defer func() {
if closeErr := stream.Close(); closeErr != nil {
panic(err)
log.Panic(err)
}
}()
fmt.Println("opened a stream")
Expand All @@ -66,6 +67,7 @@ func main() {
}
fmt.Println("sent:", pingMsg)
pingSeqNum++
time.Sleep(time.Second)
}
}()

Expand Down
76 changes: 76 additions & 0 deletions examples/ping-pong/pong/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// 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
}
28 changes: 21 additions & 7 deletions examples/ping-pong/pong.go → examples/ping-pong/pong/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

// +build pong

package main

import (
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 21bfe15

Please sign in to comment.