Initial import
This commit is contained in:
72
recorder/ttyrec.go
Normal file
72
recorder/ttyrec.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ttyRecordHeader struct {
|
||||
Sec int32
|
||||
USec int32
|
||||
Len int32
|
||||
}
|
||||
|
||||
func (h *ttyRecordHeader) Now() {
|
||||
var (
|
||||
now = time.Now()
|
||||
tv = syscall.NsecToTimeval(now.UnixNano())
|
||||
)
|
||||
h.Sec = int32(tv.Sec)
|
||||
h.USec = tv.Usec
|
||||
}
|
||||
|
||||
func (h *ttyRecordHeader) WriteTo(w io.Writer) (int64, error) {
|
||||
var b [6]byte
|
||||
binary.LittleEndian.PutUint32(b[0:], uint32(h.Sec))
|
||||
binary.LittleEndian.PutUint32(b[2:], uint32(h.USec))
|
||||
binary.LittleEndian.PutUint32(b[4:], uint32(h.Len))
|
||||
n, err := w.Write(b[:])
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
type ttyRecRecorder struct {
|
||||
mu sync.Mutex
|
||||
wc io.WriteCloser
|
||||
header *ttyRecordHeader
|
||||
}
|
||||
|
||||
func newTTYRecRecorder(wc io.WriteCloser) *ttyRecRecorder {
|
||||
return &ttyRecRecorder{
|
||||
wc: wc,
|
||||
header: new(ttyRecordHeader),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ttyRecRecorder) Close() error {
|
||||
return r.wc.Close()
|
||||
}
|
||||
|
||||
func (w *ttyRecRecorder) Write(p []byte) (n int, err error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
|
||||
w.header.Now()
|
||||
if _, err = w.header.WriteTo(w.wc); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = w.wc.Write(p); err != nil {
|
||||
return
|
||||
}
|
||||
return len(p) + 9, nil
|
||||
}
|
||||
|
||||
func (r *ttyRecRecorder) Reads() io.WriteCloser {
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *ttyRecRecorder) Writes() io.WriteCloser {
|
||||
return nopCloser{io.Discard}
|
||||
}
|
Reference in New Issue
Block a user