-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtimpus.go
82 lines (62 loc) · 1.45 KB
/
rtimpus.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
package rtimpus
import (
"fmt"
"io"
"net"
"os"
)
const SUPPORTED_PROTOCOL_VERSION = byte(3)
// StartRTMPServer will start an RTMP server on the provided address
func Listen(address string) (*Listener, error) {
laddr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, err
}
listener, err := net.ListenTCP("tcp", laddr)
if err != nil {
return nil, err
}
rtmpListner := &Listener{tcpListener: listener}
return rtmpListner, nil
}
func LoopConnections(listener *Listener) {
for {
conn, err := listener.tcpListener.AcceptTCP()
if err != nil {
fmt.Fprintf(os.Stderr, "something wen't wrong couldn't accept TCP connection:\n%v", err)
continue
}
go listenOnConnection(conn)
}
}
func listenOnConnection(tcpConn *net.TCPConn) *Connection {
connection := new(Connection)
connection.Conn = tcpConn
connection.Phase = Uninitialized
// connection.ClientBW = 128
go func() {
buf := make([]byte, 0, 4096)
tmp := make([]byte, 256)
total := 0
for {
n, err := tcpConn.Read(tmp)
if err != nil {
if err == io.EOF {
fmt.Fprintf(os.Stdout, "received IOF:\n%v\n", err)
break
}
fmt.Fprintf(os.Stderr, "something wen't wrong during exchange of information:\n%v\n", err)
connection.Err = err
return
}
total += n
buf = append(buf, tmp[:n]...)
if len(tmp) > n {
connection.Process(buf[:total])
total = 0
buf = make([]byte, 0, 4096)
}
}
}()
return connection
}