go-libp2p-daemon/stream.go

65 lines
1.1 KiB
Go
Raw Normal View History

package p2pd
import (
"io"
"net"
"sync"
ggio "github.com/gogo/protobuf/io"
inet "github.com/libp2p/go-libp2p-net"
2018-12-11 19:35:02 +00:00
manet "github.com/multiformats/go-multiaddr-net"
)
func (d *Daemon) doStreamPipe(c net.Conn, s inet.Stream) {
var wg sync.WaitGroup
wg.Add(2)
pipe := func(dst io.WriteCloser, src io.Reader) {
_, err := io.Copy(dst, src)
if err != nil && err != io.EOF {
log.Debugf("stream error: %s", err.Error())
2018-09-13 14:44:08 +00:00
s.Reset()
}
dst.Close()
wg.Done()
}
go pipe(c, s)
go pipe(s, c)
wg.Wait()
}
func (d *Daemon) handleStream(s inet.Stream) {
p := s.Protocol()
d.mx.Lock()
maddr, ok := d.handlers[p]
d.mx.Unlock()
if !ok {
log.Debugf("unexpected stream: %s", p)
2018-09-13 14:44:08 +00:00
s.Reset()
return
}
c, err := manet.Dial(maddr)
if err != nil {
2018-12-12 17:02:11 +00:00
log.Debugf("error dialing handler at %s: %s", maddr.String(), err.Error())
2018-09-13 14:44:08 +00:00
s.Reset()
return
}
defer c.Close()
w := ggio.NewDelimitedWriter(c)
msg := makeStreamInfo(s)
err = w.WriteMsg(msg)
if err != nil {
log.Debugf("error accepting stream: %s", err.Error())
2018-09-13 14:44:08 +00:00
s.Reset()
return
}
d.doStreamPipe(c, s)
}