1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-18 05:31:35 +08:00
Files
kubo/net/message/message.go
2014-09-22 04:05:12 -07:00

44 lines
969 B
Go

package message
import (
peer "github.com/jbenet/go-ipfs/peer"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
)
// Message represents a packet of information sent to or received from a
// particular Peer.
type Message struct {
// To or from, depending on direction.
Peer *peer.Peer
// Opaque data
Data []byte
}
// FromObject creates a message from a protobuf-marshallable message.
func FromObject(p *peer.Peer, data proto.Message) (*Message, error) {
bytes, err := proto.Marshal(data)
if err != nil {
return nil, err
}
return &Message{
Peer: p,
Data: bytes,
}, nil
}
// Pipe objects represent a bi-directional message channel.
type Pipe struct {
Incoming chan *Message
Outgoing chan *Message
}
// NewPipe constructs a pipe with channels of a given buffer size.
func NewPipe(bufsize int) *Pipe {
return &Pipe{
Incoming: make(chan *Message, bufsize),
Outgoing: make(chan *Message, bufsize),
}
}