protocol.go 667 Bytes
Newer Older
Aviv Eyal's avatar
Aviv Eyal committed
1
2
3
4
package main

import (
	"bufio"
Aviv Eyal's avatar
Aviv Eyal committed
5
	"github.com/gogo/protobuf/proto"
Aviv Eyal's avatar
Aviv Eyal committed
6
7
8
9
10
11
	protobufCodec "github.com/multiformats/go-multicodec/protobuf"
	inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
	"log"
)

// helper method - writes a protobuf go data object to a network stream
Aviv Eyal's avatar
Aviv Eyal committed
12
13
// data: reference of protobuf go data object to send (not the object itself)
// s: network stream to write the data to
Aviv Eyal's avatar
Aviv Eyal committed
14
func sendProtoMessage(data proto.Message, s inet.Stream) bool {
Aviv Eyal's avatar
Aviv Eyal committed
15
16
17
18
	writer := bufio.NewWriter(s)
	enc := protobufCodec.Multicodec(nil).Encoder(writer)
	err := enc.Encode(data)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
19
		log.Println(err)
Aviv Eyal's avatar
Aviv Eyal committed
20
21
22
23
24
		return false
	}
	writer.Flush()
	return true
}