6fd297bb62
Change-Id: I7407261bdb2d788c879f2e67e617a615d9ff8f8b Reviewed-on: https://boringssl-review.googlesource.com/1505 Reviewed-by: Adam Langley <agl@google.com>
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
// Copyright 2014 The Go 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 main
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"net"
|
|
)
|
|
|
|
type packetAdaptor struct {
|
|
net.Conn
|
|
}
|
|
|
|
func newPacketAdaptor(conn net.Conn) net.Conn {
|
|
return &packetAdaptor{conn}
|
|
}
|
|
|
|
func (p *packetAdaptor) Read(b []byte) (int, error) {
|
|
var length uint32
|
|
if err := binary.Read(p.Conn, binary.BigEndian, &length); err != nil {
|
|
return 0, err
|
|
}
|
|
out := make([]byte, length)
|
|
n, err := p.Conn.Read(out)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if n != int(length) {
|
|
return 0, errors.New("internal error: length mismatch!")
|
|
}
|
|
return copy(b, out), nil
|
|
}
|
|
|
|
func (p *packetAdaptor) Write(b []byte) (int, error) {
|
|
length := uint32(len(b))
|
|
if err := binary.Write(p.Conn, binary.BigEndian, length); err != nil {
|
|
return 0, err
|
|
}
|
|
n, err := p.Conn.Write(b)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if n != len(b) {
|
|
return 0, errors.New("internal error: length mismatch!")
|
|
}
|
|
return len(b), nil
|
|
}
|