This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathutil.go
113 lines (94 loc) · 1.7 KB
/
util.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
package rtmp
import (
"io"
"log"
"os"
"fmt"
"strings"
)
type dummyWriter struct {
}
func (m *dummyWriter) Write(p []byte) (n int, err error) {
return 0, nil
}
type logger int
func (l logger) Printf(format string, v ...interface{}) {
str := fmt.Sprintf(format, v...)
switch {
case strings.HasPrefix(str, "server") && l >= 0,
strings.HasPrefix(str, "stream") && l >= 0,
strings.HasPrefix(str, "event") && l >= 0,
strings.HasPrefix(str, "data") && l >= 0,
strings.HasPrefix(str, "msg") && l >= 1:
l2.Println(str)
}
}
var (
dummyW = &dummyWriter{}
l = logger(0)
l2 *log.Logger
)
func init() {
l2 = log.New(os.Stderr, "", 0)
l2.SetFlags(log.Lmicroseconds)
}
type stream struct {
r io.ReadWriteCloser
}
func (s stream) Read(p []byte) (n int, err error) {
n, err = s.r.Read(p)
if err != nil {
panic(err)
}
return
}
func (s stream) Write(p []byte) (n int, err error) {
n, err = s.r.Write(p)
if err != nil {
panic(err)
}
return
}
func (s stream) Close() {
s.r.Close()
}
func ReadBuf(r io.Reader, n int) (b []byte) {
b = make([]byte, n)
r.Read(b)
return
}
func ReadInt(r io.Reader, n int) (ret int) {
b := ReadBuf(r, n)
for i := 0; i < n; i++ {
ret <<= 8
ret += int(b[i])
}
return
}
func ReadIntLE(r io.Reader, n int) (ret int) {
b := ReadBuf(r, n)
for i := 0; i < n; i++ {
ret <<= 8
ret += int(b[n-i-1])
}
return
}
func WriteBuf(w io.Writer, buf []byte) {
w.Write(buf)
}
func WriteInt(w io.Writer, v int, n int) {
b := make([]byte, n)
for i := 0; i < n; i++ {
b[n-i-1] = byte(v&0xff)
v >>= 8
}
WriteBuf(w, b)
}
func WriteIntLE(w io.Writer, v int, n int) {
b := make([]byte, n)
for i := 0; i < n; i++ {
b[i] = byte(v&0xff)
v >>= 8
}
WriteBuf(w, b)
}