-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
88 lines (74 loc) · 2.07 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"log"
"net"
astisrt "github.com/asticode/go-astisrt/pkg"
)
func main() {
// Handle logs
astisrt.SetLogLevel(astisrt.LogLevelError)
astisrt.SetLogHandler(func(ll astisrt.LogLevel, file, area, msg string, line int) { log.Println(msg) })
// Startup srt
if err := astisrt.Startup(); err != nil {
log.Fatal(fmt.Errorf("main: starting up srt failed: %w", err))
}
// Make sure to clean up srt
defer func() {
if err := astisrt.CleanUp(); err != nil {
log.Fatal(fmt.Errorf("main: cleaning up srt failed: %w", err))
}
}()
// Create socket
s, err := astisrt.NewSocket()
if err != nil {
log.Fatal(fmt.Errorf("main: creating socket failed: %w", err))
}
defer s.Close()
// Set listen callback
if err = s.SetListenCallback(func(s *astisrt.Socket, version int, addr *net.UDPAddr, streamID string) bool {
// Check stream id
log.Printf("main: checking stream id %s\n", streamID)
if streamID != "test" {
// Set reject reason
log.Println("main: invalid stream id")
if err := s.SetRejectReason(1404); err != nil {
log.Println(fmt.Errorf("main: setting reject reason failed: %w", err))
}
return false
}
// Update passphrase
if err := s.Options().SetPassphrase("passphrase"); err != nil {
log.Println(fmt.Errorf("main: setting passphrase failed: %w", err))
return false
}
// Log
log.Println("main: connection accepted")
return true
}); err != nil {
log.Fatal(fmt.Errorf("main: setting listen callback failed: %w", err))
}
// Bind
if err = s.Bind("127.0.0.1", 4000); err != nil {
log.Fatal(fmt.Errorf("main: binding failed: %w", err))
}
// Listen
log.Println("main: listening")
if err = s.Listen(1); err != nil {
log.Fatal(fmt.Errorf("main: listening failed: %w", err))
}
// Accept
as, _, err := s.Accept()
if err != nil {
log.Fatal(fmt.Errorf("main: accepting failed: %w", err))
}
// Receive message
b := make([]byte, 1500)
n, err := as.ReceiveMessage(b)
if err != nil {
log.Fatal(fmt.Errorf("main: receiving message failed: %w", err))
return
}
// Log
log.Printf("main: received `%s`\n", b[:n])
}