-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
43 lines (39 loc) · 885 Bytes
/
common.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
package rtp
import (
"fmt"
"net"
"runtime"
"strconv"
"strings"
"time"
)
func IsPortInUse(port int) bool {
if conn, err := net.DialTimeout("tcp", net.JoinHostPort("", fmt.Sprintf("%d", port)), 3*time.Second); err == nil {
conn.Close()
return true
}
return false
}
func SplitPorts(ports string) (int, int, error) {
portLst := strings.Split(ports, ":")
if len(portLst) != 2 {
return 0, 0, fmt.Errorf("rtsp port type is not right")
}
start, err := strconv.Atoi(strings.Trim(portLst[0], " "))
if err != nil {
return 0, 0, err
}
end, err := strconv.Atoi(strings.Trim(portLst[1], " "))
if err != nil {
return 0, 0, err
}
if end < start || start <= 0 {
return 0, 0, fmt.Errorf("start or end port is not right")
}
return start, end, err
}
func PrintStack() {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
fmt.Printf("==> %s\n", string(buf[:n]))
}