@cryptotaxi247 / kubo / commits / 5c79fc48e

message pkg

Juan Batiz-Benet committed Sep 11, 2014 at 00:57 UTC 5c79fc48e25584190ea5ad77be0450a6afabeff7
1 file changed +43
net/message/message.go new
+43
@@ -0,0 +1,43 @@
1 +package message
2 +
3 +import (
4 + peer "github.com/jbenet/go-ipfs/peer"
5 +
6 + proto "code.google.com/p/goprotobuf/proto"
7 +)
8 +
9 +// Message represents a packet of information sent to or received from a
10 +// particular Peer.
11 +type Message struct {
12 + // To or from, depending on direction.
13 + Peer *peer.Peer
14 +
15 + // Opaque data
16 + Data []byte
17 +}
18 +
19 +// FromObject creates a message from a protobuf-marshallable message.
20 +func FromObject(p *peer.Peer, data proto.Message) (*Message, error) {
21 + bytes, err := proto.Marshal(data)
22 + if err != nil {
23 + return nil, err
24 + }
25 + return &Message{
26 + Peer: p,
27 + Data: bytes,
28 + }, nil
29 +}
30 +
31 +// Pipe objects represent a bi-directional message channel.
32 +type Pipe struct {
33 + Incoming chan *Message
34 + Outgoing chan *Message
35 +}
36 +
37 +// NewPipe constructs a pipe with channels of a given buffer size.
38 +func NewPipe(bufsize int) *Pipe {
39 + return &Pipe{
40 + Incoming: make(chan *Message, bufsize),
41 + Outgoing: make(chan *Message, bufsize),
42 + }
43 +}