-
Notifications
You must be signed in to change notification settings - Fork 1
/
framer.go
49 lines (38 loc) · 920 Bytes
/
framer.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
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package miniv
// #cgo CFLAGS: -I/usr/include/nacl
// #include "miniv.h"
import "C"
import (
"io"
"os"
)
type Framer struct {
rwc io.ReadWriteCloser
}
func New(f io.ReadWriteCloser) *Framer {
return &Framer{rwc: f}
}
func (f *Framer) fd() C.int {
if file, ok := f.rwc.(*os.File); ok {
return C.int(file.Fd())
}
panic("cannot get the FD")
}
func (f *Framer) ReadFrame() ([]byte, error) {
var n C.ulong
if err := C.frameReadLen(f.fd(), &n); err != C.ERR_OK {
return nil, GoError(err)
}
out := make([]byte, int(n))
_, err := f.rwc.Read(out)
return out, err
}
func (f *Framer) WriteFrame(out []byte) (int, error) {
if err := C.frameWrite(f.fd(), toBuf_t(out)); err != C.ERR_OK {
return 0, GoError(err)
}
return len(out), nil
}