forked from nstehr/bobcaygeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
119 lines (104 loc) · 2.86 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package rtsp
import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net"
"tailscale.com/net/interfaces"
)
// RequestHandler callback function that gets invoked when a request is received
type RequestHandler func(req *Request, resp *Response, localAddr string, remoteAddr string)
// Server Server for handling Rtsp control requests
type Server struct {
port int
handlers map[Method]RequestHandler
done chan bool
reqChan chan *Request
ip string
}
// NewServer instantiates a new RtspServer
func NewServer(port int) *Server {
return &Server{
port: port,
done: make(chan bool),
handlers: make(map[Method]RequestHandler),
reqChan: make(chan *Request),
}
}
// AddHandler registers a handler for a given RTSP method
func (r *Server) AddHandler(m Method, rh RequestHandler) {
r.handlers[m] = rh
}
// Stop stops the RTSP server
func (r *Server) Stop() {
log.Println("Stopping RTSP server")
r.done <- true
}
// Start creates listening socket for the RTSP connection
func (r *Server) Start(verbose bool) {
// Get the default outbound interface address
_, myIP, ok := interfaces.LikelyHomeRouterIP()
if !ok {
log.Errorf("Error getting local outbound IP address: ok=%v\n", ok)
return
}
r.ip = myIP.String()
log.Printf("Starting RTSP server on address: %s:%d\n", r.ip, r.port)
tcpListen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", r.ip, r.port))
if err != nil {
log.Errorln("Error listening:", err.Error())
return
}
defer tcpListen.Close()
// Handle TCP connections.
go func() {
for {
// Listen for an incoming connection.
conn, err := tcpListen.Accept()
if err != nil {
log.Errorln("Error accepting: ", err.Error())
return
}
go r.read(conn, r.handlers, verbose)
}
}()
<-r.done
}
func (r *Server) read(conn net.Conn, handlers map[Method]RequestHandler, verbose bool) {
defer conn.Close()
for {
request, err := readRequest(conn)
if err != nil {
if errors.Is(err, io.EOF) {
log.Println("Client closed connection")
} else {
log.Println("Error reading data: ", err.Error())
}
return
}
if verbose {
log.Println("Received Request")
log.Println(request.String())
}
handler, exists := handlers[request.Method]
if !exists {
log.Printf("Method: %s does not have a handler. Skipping", request.Method)
continue
}
resp := NewResponse()
// for now we just stick in the protocol (protocol/version) from the request
resp.protocol = request.protocol
// same with CSeq
resp.Headers["CSeq"] = request.Headers["CSeq"]
// invokes the client specified handler to build the response
localAddr := conn.LocalAddr().(*net.TCPAddr).IP
remoteAddr := conn.RemoteAddr().(*net.TCPAddr).IP
handler(request, resp, localAddr.String(), remoteAddr.String())
if verbose {
log.Println("Outbound Response")
log.Println(resp.String())
}
_, _ = writeResponse(conn, resp)
}
}