kubo/p2p/protocol/protocol.go
Ho-Sheng Hsiao bf22aeec0a Reorged imports from jbenet/go-ipfs to ipfs/go-ipfs
- Modified Godeps/Godeps.json by hand
- [TEST] Updated welcome docs hash to sharness
- [TEST] Updated contact doc
- [TEST] disabled breaking test (t0080-repo refs local)
2015-03-31 12:52:25 -07:00

41 lines
1.0 KiB
Go

package protocol
import (
"io"
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
)
// ID is an identifier used to write protocol headers in streams.
type ID string
// These are reserved protocol.IDs.
const (
TestingID ID = "/p2p/_testing"
)
// WriteHeader writes a protocol.ID header to an io.Writer. This is so
// multiple protocols can be multiplexed on top of the same transport.
//
// We use go-msgio varint encoding:
// <varint length><string name>\n
// (the varint includes the \n)
func WriteHeader(w io.Writer, id ID) error {
vw := msgio.NewVarintWriter(w)
s := string(id) + "\n" // add \n
return vw.WriteMsg([]byte(s))
}
// ReadHeader reads a protocol.ID header from an io.Reader. This is so
// multiple protocols can be multiplexed on top of the same transport.
// See WriteHeader.
func ReadHeader(r io.Reader) (ID, error) {
vr := msgio.NewVarintReader(r)
msg, err := vr.ReadMsg()
if err != nil {
return ID(""), err
}
msg = msg[:len(msg)-1] // remove \n
return ID(msg), nil
}