39 lines
630 B
Go
39 lines
630 B
Go
package tunnel
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"encoding/gob"
|
|
"io"
|
|
)
|
|
|
|
type DataFrame struct {
|
|
ID ID
|
|
Type Type
|
|
Payload []byte
|
|
}
|
|
|
|
func (d *DataFrame) Encode(w io.Writer) error {
|
|
gz := gzip.NewWriter(w)
|
|
defer gz.Close()
|
|
gob := gob.NewEncoder(gz)
|
|
return gob.Encode(d)
|
|
}
|
|
|
|
func (d *DataFrame) Decode(r io.Reader) error {
|
|
gz, err := gzip.NewReader(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer gz.Close()
|
|
gob := gob.NewDecoder(gz)
|
|
return gob.Decode(d)
|
|
}
|
|
|
|
func (d *DataFrame) Read(p []byte) (int, error) {
|
|
return copy(p, d.Payload), nil
|
|
}
|
|
|
|
func (d *DataFrame) Write(p []byte) (int, error) {
|
|
return copy(d.Payload, p), nil
|
|
}
|