mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 01:52:26 +08:00
peerstream updated (data race)
This commit is contained in:
2
Godeps/Godeps.json
generated
2
Godeps/Godeps.json
generated
@ -141,7 +141,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/jbenet/go-peerstream",
|
"ImportPath": "github.com/jbenet/go-peerstream",
|
||||||
"Rev": "5023d0d6b3efeb50c2c30535d011bdcb2351e212"
|
"Rev": "1c71a3e04eeef9297a12ecdff75a0b28ffa8bf90"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/jbenet/go-random",
|
"ImportPath": "github.com/jbenet/go-random",
|
||||||
|
2
Godeps/_workspace/src/github.com/jbenet/go-peerstream/conn.go
generated
vendored
2
Godeps/_workspace/src/github.com/jbenet/go-peerstream/conn.go
generated
vendored
@ -159,7 +159,6 @@ func (s *Swarm) addConn(netConn net.Conn, server bool) (*Conn, error) {
|
|||||||
// first, check if we already have it...
|
// first, check if we already have it...
|
||||||
for c := range s.conns {
|
for c := range s.conns {
|
||||||
if c.netConn == netConn {
|
if c.netConn == netConn {
|
||||||
s.connLock.Unlock()
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,7 +166,6 @@ func (s *Swarm) addConn(netConn net.Conn, server bool) (*Conn, error) {
|
|||||||
// create a new spdystream connection
|
// create a new spdystream connection
|
||||||
ssConn, err := ss.NewConnection(netConn, server)
|
ssConn, err := ss.NewConnection(netConn, server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.connLock.Unlock()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
77
Godeps/_workspace/src/github.com/jbenet/go-peerstream/example/blockhandler/blockhandler.go
generated
vendored
Normal file
77
Godeps/_workspace/src/github.com/jbenet/go-peerstream/example/blockhandler/blockhandler.go
generated
vendored
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
ps "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
|
||||||
|
)
|
||||||
|
|
||||||
|
func die(err error) {
|
||||||
|
fmt.Fprintf(os.Stderr, "error: %s\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// create a new Swarm
|
||||||
|
swarm := ps.NewSwarm()
|
||||||
|
defer swarm.Close()
|
||||||
|
|
||||||
|
// tell swarm what to do with a new incoming streams.
|
||||||
|
// EchoHandler just echos back anything they write.
|
||||||
|
swarm.SetStreamHandler(ps.EchoHandler)
|
||||||
|
|
||||||
|
l, err := net.Listen("tcp", "localhost:8001")
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := swarm.AddListener(l); err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
nc, err := net.Dial("tcp", "localhost:8001")
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := swarm.AddConn(nc)
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
nRcvStream := 0
|
||||||
|
bio := bufio.NewReader(os.Stdin)
|
||||||
|
swarm.SetStreamHandler(func(s *ps.Stream) {
|
||||||
|
log("handling new stream %d", nRcvStream)
|
||||||
|
nRcvStream++
|
||||||
|
|
||||||
|
line, err := bio.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
_ = line
|
||||||
|
// line = "read: " + line
|
||||||
|
// s.Write([]byte(line))
|
||||||
|
s.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
nSndStream := 0
|
||||||
|
for {
|
||||||
|
<-time.After(200 * time.Millisecond)
|
||||||
|
s, err := swarm.NewStreamWithConn(c)
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
log("sender got new stream %d", nSndStream)
|
||||||
|
nSndStream++
|
||||||
|
s.Wait()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func log(s string, ifs ...interface{}) {
|
||||||
|
fmt.Fprintf(os.Stderr, s+"\n", ifs...)
|
||||||
|
}
|
6
Godeps/_workspace/src/github.com/jbenet/go-peerstream/swarm.go
generated
vendored
6
Godeps/_workspace/src/github.com/jbenet/go-peerstream/swarm.go
generated
vendored
@ -110,28 +110,34 @@ func (s *Swarm) SelectConn() SelectConn {
|
|||||||
|
|
||||||
// Conns returns all the connections associated with this Swarm.
|
// Conns returns all the connections associated with this Swarm.
|
||||||
func (s *Swarm) Conns() []*Conn {
|
func (s *Swarm) Conns() []*Conn {
|
||||||
|
s.connLock.RLock()
|
||||||
conns := make([]*Conn, 0, len(s.conns))
|
conns := make([]*Conn, 0, len(s.conns))
|
||||||
for c := range s.conns {
|
for c := range s.conns {
|
||||||
conns = append(conns, c)
|
conns = append(conns, c)
|
||||||
}
|
}
|
||||||
|
s.connLock.RUnlock()
|
||||||
return conns
|
return conns
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listeners returns all the listeners associated with this Swarm.
|
// Listeners returns all the listeners associated with this Swarm.
|
||||||
func (s *Swarm) Listeners() []*Listener {
|
func (s *Swarm) Listeners() []*Listener {
|
||||||
|
s.listenerLock.RLock()
|
||||||
out := make([]*Listener, 0, len(s.listeners))
|
out := make([]*Listener, 0, len(s.listeners))
|
||||||
for c := range s.listeners {
|
for c := range s.listeners {
|
||||||
out = append(out, c)
|
out = append(out, c)
|
||||||
}
|
}
|
||||||
|
s.listenerLock.RUnlock()
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Streams returns all the streams associated with this Swarm.
|
// Streams returns all the streams associated with this Swarm.
|
||||||
func (s *Swarm) Streams() []*Stream {
|
func (s *Swarm) Streams() []*Stream {
|
||||||
|
s.streamLock.RLock()
|
||||||
out := make([]*Stream, 0, len(s.streams))
|
out := make([]*Stream, 0, len(s.streams))
|
||||||
for c := range s.streams {
|
for c := range s.streams {
|
||||||
out = append(out, c)
|
out = append(out, c)
|
||||||
}
|
}
|
||||||
|
s.streamLock.RUnlock()
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user