-
Notifications
You must be signed in to change notification settings - Fork 9
/
example_test.go
66 lines (64 loc) · 1.58 KB
/
example_test.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
package lossy_test
import (
"fmt"
"github.com/cevatbarisyilmaz/lossy"
"log"
"math/rand"
"net"
"time"
)
func Example() {
packetConn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 0,
})
if err != nil {
log.Fatal(err)
}
conn, err := net.DialUDP("udp", nil, packetConn.LocalAddr().(*net.UDPAddr))
if err != nil {
log.Fatal(err)
}
lossyConn := lossy.NewConn(conn, 1024, time.Second, time.Second*2, 0.25, lossy.UDPv4MinHeaderOverhead)
var bytesWritten int
const packetCount = 32
go func() {
for i := 0; i < packetCount; i++ {
packet := make([]byte, i*64)
bytesWritten += len(packet) // Ignoring the packet headers
rand.Read(packet)
_, err := lossyConn.Write(packet)
if err != nil {
log.Fatal(err)
}
}
fmt.Println("Sent", packetCount, "packets with total size of", bytesWritten, "bytes")
}()
const timeOut = time.Second * 5
var bytesRead int
var packetsReceived int
startTime := time.Now()
for {
err = packetConn.SetReadDeadline(time.Now().Add(timeOut))
if err != nil {
log.Fatal(err)
}
buffer := make([]byte, 32*64)
n, addr, err := packetConn.ReadFrom(buffer)
if err != nil {
if nerr, ok := err.(net.Error); ok {
if nerr.Timeout() {
break
}
}
log.Fatal(err)
}
if addr.String() != conn.LocalAddr().String() {
log.Fatal("hijacked by", addr.String())
}
packetsReceived++
bytesRead += n // Ignoring the packet headers
}
dur := time.Now().Sub(startTime) - timeOut
fmt.Println("Received", packetsReceived, "packets with total size of", bytesRead, "bytes in", dur.Seconds(), "seconds")
}