2016-09-11 20:56:07 +00:00
|
|
|
package floodsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
2016-09-13 02:59:24 +00:00
|
|
|
pb "github.com/libp2p/go-floodsub/pb"
|
2016-09-11 20:56:07 +00:00
|
|
|
|
2018-10-04 22:41:48 +00:00
|
|
|
inet "gx/ipfs/QmQdLcvoy3JuSqhV6iwQ9T6Cv7hWLAdzob4jUZRPqFL67Z/go-libp2p-net"
|
|
|
|
ggio "gx/ipfs/QmdxUuburamoF6zF9qjeQC4WYcWGbWuRmdLacMEsW8ioD8/gogo-protobuf/io"
|
|
|
|
proto "gx/ipfs/QmdxUuburamoF6zF9qjeQC4WYcWGbWuRmdLacMEsW8ioD8/gogo-protobuf/proto"
|
2016-09-11 20:56:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// get the initial RPC containing all of our subscriptions to send to new peers
|
|
|
|
func (p *PubSub) getHelloPacket() *RPC {
|
|
|
|
var rpc RPC
|
|
|
|
for t := range p.myTopics {
|
|
|
|
as := &pb.RPC_SubOpts{
|
|
|
|
Topicid: proto.String(t),
|
|
|
|
Subscribe: proto.Bool(true),
|
|
|
|
}
|
|
|
|
rpc.Subscriptions = append(rpc.Subscriptions, as)
|
|
|
|
}
|
|
|
|
return &rpc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PubSub) handleNewStream(s inet.Stream) {
|
|
|
|
r := ggio.NewDelimitedReader(s, 1<<20)
|
|
|
|
for {
|
|
|
|
rpc := new(RPC)
|
|
|
|
err := r.ReadMsg(&rpc.RPC)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2017-09-15 23:28:46 +00:00
|
|
|
s.Reset()
|
2018-01-26 23:00:47 +00:00
|
|
|
log.Infof("error reading rpc from %s: %s", s.Conn().RemotePeer(), err)
|
2017-09-15 23:28:46 +00:00
|
|
|
} else {
|
|
|
|
// Just be nice. They probably won't read this
|
|
|
|
// but it doesn't hurt to send it.
|
|
|
|
s.Close()
|
2016-09-11 20:56:07 +00:00
|
|
|
}
|
2017-10-14 18:13:23 +00:00
|
|
|
select {
|
|
|
|
case p.peerDead <- s.Conn().RemotePeer():
|
|
|
|
case <-p.ctx.Done():
|
|
|
|
}
|
2016-09-11 20:56:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc.from = s.Conn().RemotePeer()
|
|
|
|
select {
|
|
|
|
case p.incoming <- rpc:
|
|
|
|
case <-p.ctx.Done():
|
2017-09-15 23:28:46 +00:00
|
|
|
// Close is useless because the other side isn't reading.
|
|
|
|
s.Reset()
|
2016-09-11 20:56:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PubSub) handleSendingMessages(ctx context.Context, s inet.Stream, outgoing <-chan *RPC) {
|
|
|
|
bufw := bufio.NewWriter(s)
|
|
|
|
wc := ggio.NewDelimitedWriter(bufw)
|
|
|
|
|
|
|
|
writeMsg := func(msg proto.Message) error {
|
|
|
|
err := wc.WriteMsg(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufw.Flush()
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:46:56 +00:00
|
|
|
defer inet.FullClose(s)
|
2016-09-11 20:56:07 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case rpc, ok := <-outgoing:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := writeMsg(&rpc.RPC)
|
|
|
|
if err != nil {
|
2017-09-15 23:28:46 +00:00
|
|
|
s.Reset()
|
2018-01-26 23:00:47 +00:00
|
|
|
log.Infof("writing message to %s: %s", s.Conn().RemotePeer(), err)
|
2017-10-14 18:13:23 +00:00
|
|
|
select {
|
|
|
|
case p.peerDead <- s.Conn().RemotePeer():
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2016-09-11 20:56:07 +00:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func rpcWithSubs(subs ...*pb.RPC_SubOpts) *RPC {
|
|
|
|
return &RPC{
|
|
|
|
RPC: pb.RPC{
|
|
|
|
Subscriptions: subs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 20:22:16 +00:00
|
|
|
|
|
|
|
func rpcWithMessages(msgs ...*pb.Message) *RPC {
|
|
|
|
return &RPC{RPC: pb.RPC{Publish: msgs}}
|
|
|
|
}
|
2018-02-19 16:45:10 +00:00
|
|
|
|
|
|
|
func rpcWithControl(msgs []*pb.Message,
|
|
|
|
ihave []*pb.ControlIHave,
|
|
|
|
iwant []*pb.ControlIWant,
|
|
|
|
graft []*pb.ControlGraft,
|
|
|
|
prune []*pb.ControlPrune) *RPC {
|
|
|
|
return &RPC{
|
|
|
|
RPC: pb.RPC{
|
|
|
|
Publish: msgs,
|
|
|
|
Control: &pb.ControlMessage{
|
|
|
|
Ihave: ihave,
|
|
|
|
Iwant: iwant,
|
|
|
|
Graft: graft,
|
|
|
|
Prune: prune,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 18:07:09 +00:00
|
|
|
|
|
|
|
func copyRPC(rpc *RPC) *RPC {
|
2018-08-28 18:35:00 +00:00
|
|
|
res := new(RPC)
|
|
|
|
*res = *rpc
|
2018-08-28 18:17:00 +00:00
|
|
|
if rpc.Control != nil {
|
2018-08-28 18:35:00 +00:00
|
|
|
res.Control = new(pb.ControlMessage)
|
|
|
|
*res.Control = *rpc.Control
|
2018-08-28 18:17:00 +00:00
|
|
|
}
|
|
|
|
return res
|
2018-08-28 18:07:09 +00:00
|
|
|
}
|