This repository was archived by the owner on Oct 18, 2023. It is now read-only.
forked from torresjeff/rtmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
74 lines (59 loc) · 1.91 KB
/
server.go
File metadata and controls
74 lines (59 loc) · 1.91 KB
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
package rtmp
import (
"bufio"
"fmt"
"io"
"net"
"github.com/codingpa-ws/rtmp/constants"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// Server represents the RTMP server, where a client/app can stream media to. The server listens for incoming connections.
type Server struct {
AppName string
Addr string
Logger *zap.Logger
Broadcaster Broadcaster
}
// Listen starts the server and listens for any incoming connections. If no Addr (host:port) has been assigned to the server, ":1935" is used.
func (s *Server) Listen() error {
if s.Addr == "" {
s.Addr = constants.DefaultAddress
}
tcpAddress, err := net.ResolveTCPAddr("tcp", s.Addr)
if err != nil {
err = errors.Errorf("[server] error resolving tcp address: %s", err)
return err
}
// Start listening on the specified address
listener, err := net.ListenTCP("tcp", tcpAddress)
if err != nil {
return err
}
s.Logger.Info(fmt.Sprint("[server] Listening on ", s.Addr))
for {
conn, err := listener.Accept()
if err != nil {
s.Logger.Error(fmt.Sprint("[server] Error accepting incoming connection ", err))
continue
}
s.Logger.Info(fmt.Sprint("[server] Accepted incoming connection from ", conn.RemoteAddr().String()))
go func(conn net.Conn) {
defer conn.Close()
socketr := bufio.NewReaderSize(conn, constants.BuffioSize)
socketw := bufio.NewWriterSize(conn, constants.BuffioSize)
sess := NewSession(s.Logger, s.Broadcaster)
sess.messageManager = NewMessageManager(sess,
NewHandshaker(socketr, socketw),
NewChunkHandler(socketr, socketw),
)
s.Logger.Info(fmt.Sprint("[server] Starting server session with sessionId ", sess.id))
err := sess.Start()
if err != io.EOF {
s.Logger.Error(fmt.Sprint("[server] Server session with sessionId ", sess.id, " ended with an error: ", err))
} else {
s.Logger.Info(fmt.Sprint("[server] Server session with sessionId ", sess.id, " ended."))
}
}(conn)
}
}