/
trace.go
49 lines (43 loc) · 1.22 KB
/
trace.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
// Package tracing provides tracing of sent and received 9P
// messages.
package tracing
import (
"io"
"aqwari.net/net/styx/styxproto"
)
// A Func can be used to access 9P messages as they
// pass through a styxproto.Encoder or a styxproto.Decoder.
// Messages are not copied; a TraceFn should not modify
// msg, and msg should not be accessed after the TraceFn
// returns.
type Func func(msg styxproto.Msg)
const kilobyte = 1 << 10
// Decoder creates a new styxproto.Decoder that traces messages
// received on r.
func Decoder(r io.Reader, fn Func) *styxproto.Decoder {
rd, wr := io.Pipe()
decoderInput := styxproto.NewDecoderSize(r, 8*kilobyte)
decoderTrace := styxproto.NewDecoderSize(rd, 8*kilobyte)
go func() {
for decoderInput.Next() {
fn(decoderInput.Msg())
styxproto.Write(wr, decoderInput.Msg())
}
wr.Close()
}()
return decoderTrace
}
// Encoder creates a new styxproto.Encoder that traces messages
// before writing them to w.
func Encoder(w io.Writer, fn Func) *styxproto.Encoder {
rd, wr := io.Pipe()
encoder := styxproto.NewEncoder(wr)
decoder := styxproto.NewDecoderSize(rd, 8*kilobyte)
go func() {
for decoder.Next() {
fn(decoder.Msg())
styxproto.Write(w, decoder.Msg())
}
}()
return encoder
}