-
Notifications
You must be signed in to change notification settings - Fork 0
/
rw.go
203 lines (166 loc) · 4.27 KB
/
rw.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package channel
import (
"bytes"
"io"
"log"
"github.com/frizinak/binary"
)
type RWFactory interface {
Reader(io.Reader) io.Reader
Writer(io.Writer) io.Writer
BinaryReader(io.Reader) BinaryReader
BinaryWriter(io.Writer) BinaryWriter
}
type BinaryWriter interface {
Err() error
Writer() io.Writer
WriteUint(uint64, byte)
WriteUint8(uint8)
WriteUint16(uint16)
WriteUint32(uint32)
WriteUint64(uint64)
WriteBytes([]byte, byte)
WriteString(string, byte)
}
type BinaryReader interface {
Err() error
Reader() io.Reader
ReadUint(byte) uint64
ReadUint8() uint8
ReadUint16() uint16
ReadUint32() uint32
ReadUint64() uint64
ReadBytes(byte) []byte
ReadString(byte) string
}
func NewRWFactory(debug io.Writer) RWFactory {
if debug != nil {
return &debugRWFactory{
log.New(debug, "< ", log.Ltime|log.Lmicroseconds),
log.New(debug, "> ", log.Ltime|log.Lmicroseconds),
&defaultRWFactory{},
}
}
return &defaultRWFactory{}
}
type defaultRWFactory struct{}
func (d *defaultRWFactory) Reader(r io.Reader) io.Reader { return r }
func (d *defaultRWFactory) Writer(w io.Writer) io.Writer { return w }
func (d *defaultRWFactory) BinaryReader(r io.Reader) BinaryReader { return binary.NewReader(r) }
func (d *defaultRWFactory) BinaryWriter(w io.Writer) BinaryWriter { return binary.NewWriter(w) }
type debugRWFactory struct {
logR, logW *log.Logger
*defaultRWFactory
}
func (d *debugRWFactory) Reader(r io.Reader) io.Reader { return &debugR{d.logR, r} }
func (d *debugRWFactory) Writer(w io.Writer) io.Writer { return &debugW{d.logW, w} }
func ascii(d []byte) interface{} {
if len(d) > 1024 {
return nil
}
s := bytes.Trim(d, "\x00\n ")
for _, n := range s {
if n < 32 && n != '\n' && n != '\r' {
return d
}
}
return string(s)
}
type debugR struct {
log *log.Logger
r io.Reader
}
func (d *debugR) Read(b []byte) (int, error) {
n, err := d.r.Read(b)
if err != nil {
return n, err
}
if a := ascii(b[:n]); a != nil {
d.log.Println(a)
}
return n, err
}
type debugW struct {
log *log.Logger
w io.Writer
}
func (d *debugW) Write(b []byte) (int, error) {
if a := ascii(b); a != nil {
d.log.Println(a)
}
return d.w.Write(b)
}
// type debugBW struct {
// log *log.Logger
// w BinaryWriter
// }
//
// func (d *debugBW) Err() error { return d.w.Err() }
// func (d *debugBW) Writer() io.Writer { return d.w.Writer() }
// func (d *debugBW) WriteUint(a uint64, b byte) { d.w.WriteUint(a, b); d.log.Println("--:", a) }
// func (d *debugBW) WriteUint8(a uint8) { d.w.WriteUint8(a); d.log.Println("08:", a) }
// func (d *debugBW) WriteUint16(a uint16) { d.w.WriteUint16(a); d.log.Println("16:", a) }
// func (d *debugBW) WriteUint32(a uint32) { d.w.WriteUint32(a); d.log.Println("32:", a) }
// func (d *debugBW) WriteUint64(a uint64) { d.w.WriteUint64(a); d.log.Println("64:", a) }
// func (d *debugBW) WriteBytes(a []byte, b byte) { d.w.WriteBytes(a, b); d.log.Println("bt:", a) }
// func (d *debugBW) WriteString(a string, b byte) { d.w.WriteString(a, b); d.log.Println("st:", a) }
//type BinaryReader interface {
// Err() error
//
// Reader() io.Reader
//
// ReadUint(byte) uint64
// ReadUint8() uint8
// ReadUint16() uint16
// ReadUint32() uint32
// ReadUint64() uint64
//
// ReadBytes(byte) []byte
// ReadString(byte) string
//}
type Flusher interface {
Flush() error
}
type WriteFlusher interface {
io.Writer
Flusher
}
type BufferedWriteFlusher struct {
w io.Writer
buf *bytes.Buffer
}
func NewBuffered(w io.Writer) WriteFlusher {
return &BufferedWriteFlusher{w, bytes.NewBuffer(nil)}
}
func (w *BufferedWriteFlusher) Write(b []byte) (int, error) {
return w.buf.Write(b)
}
func (w *BufferedWriteFlusher) Flush() error {
_, err := w.buf.WriteTo(w.w)
return err
}
type PassthroughWriteFlusher struct {
io.Writer
}
func NewPassthrough(w io.Writer) WriteFlusher {
return &PassthroughWriteFlusher{w}
}
func (w *PassthroughWriteFlusher) Flush() error { return nil }
type WriterFlusher struct {
io.Writer
Flusher
}
type FlushFlusher struct {
flushers []Flusher
}
func (w *FlushFlusher) Flush() error {
for _, f := range w.flushers {
if err := f.Flush(); err != nil {
return err
}
}
return nil
}
func NewFlushFlusher(flushers ...Flusher) Flusher {
return &FlushFlusher{flushers}
}