protocol.go 728 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
12
13
14
15
16
	protobufCodec "github.com/multiformats/go-multicodec/protobuf"
	inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
	"log"
)

// node version
const clientVersion = "go-p2p-node/0.0.1"

// helper method - writes a protobuf go data object to a network stream
// 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
17
func sendProtoMessage(data proto.Message, s inet.Stream) bool {
Aviv Eyal's avatar
Aviv Eyal committed
18
19
20
21
	writer := bufio.NewWriter(s)
	enc := protobufCodec.Multicodec(nil).Encoder(writer)
	err := enc.Encode(data)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
22
		log.Println(err)
Aviv Eyal's avatar
Aviv Eyal committed
23
24
25
26
27
		return false
	}
	writer.Flush()
	return true
}