mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 02:30:39 +08:00
add dial and listen methods to the core for super awesomeness
This commit is contained in:
56
core/net.go
Normal file
56
core/net.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
|
net "github.com/jbenet/go-ipfs/p2p/net"
|
||||||
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
|
pro "github.com/jbenet/go-ipfs/p2p/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ipfsListener struct {
|
||||||
|
nd *IpfsNode
|
||||||
|
conCh chan net.Stream
|
||||||
|
proto pro.ID
|
||||||
|
ctx context.Context
|
||||||
|
cancel func()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (il *ipfsListener) Accept() (net.Stream, error) {
|
||||||
|
select {
|
||||||
|
case c := <-il.conCh:
|
||||||
|
return c, nil
|
||||||
|
case <-il.ctx.Done():
|
||||||
|
return nil, il.ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (il *ipfsListener) Close() error {
|
||||||
|
il.cancel()
|
||||||
|
// TODO: unregister handler from peerhost
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nd *IpfsNode) Listen(protocol string) (*ipfsListener, error) {
|
||||||
|
ctx, cancel := context.WithCancel(nd.Context())
|
||||||
|
|
||||||
|
list := &ipfsListener{
|
||||||
|
proto: pro.ID(protocol),
|
||||||
|
conCh: make(chan net.Stream),
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
}
|
||||||
|
|
||||||
|
nd.PeerHost.SetStreamHandler(list.proto, func(s net.Stream) {
|
||||||
|
select {
|
||||||
|
case list.conCh <- s:
|
||||||
|
case <-ctx.Done():
|
||||||
|
s.Close()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nd *IpfsNode) Dial(protocol string, p peer.ID) (net.Stream, error) {
|
||||||
|
return nd.PeerHost.NewStream(pro.ID(protocol), p)
|
||||||
|
}
|
Reference in New Issue
Block a user