This repository has been archived by the owner on Feb 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathserver.go
78 lines (70 loc) · 1.51 KB
/
server.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
package rtmp
import (
"bufio"
"log"
"net"
"time"
)
func ListenAndServe(addr string) error {
server := &Server{Addr: addr}
return server.ListenAndServe()
}
type Server struct {
Addr string // If empty, use ":1935".
ErrorLog *log.Logger // If nil, logging goes to os.Stderr.
}
func (srv *Server) ListenAndServe() error {
addr := srv.Addr
if addr == "" {
addr = ":1935"
}
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
return srv.Serve(ln)
}
func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
var tempDelay time.Duration // how long to sleep on accept failure
for {
rw, e := l.Accept()
if e != nil {
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
srv.logf("rtmp: Accept error: %v; retrying in %v", e, tempDelay)
time.Sleep(tempDelay)
continue
}
return e
}
tempDelay = 0
c := srv.newConn(rw)
go c.serve()
}
}
func (srv *Server) newConn(nc net.Conn) *conn {
return &conn{
netconn: nc,
server: srv,
bufr: bufio.NewReaderSize(nc, 1024*64),
bufw: bufio.NewWriterSize(nc, 1024*64),
readbuf: make([]byte, 4096),
writebuf: make([]byte, 4096),
state: StateUninitialized,
}
}
func (srv *Server) logf(format string, args ...interface{}) {
if srv.ErrorLog != nil {
srv.ErrorLog.Printf(format, args...)
} else {
log.Printf(format, args...)
}
}