-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Self dialing #638
Closed
Closed
[WIP] Self dialing #638
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efdf2d3
Add MemoryTransport
bigs 498cfd3
Add PipeTransport
bigs bc9e5fd
Refactor PipeTransport to make a pipe per stream
bigs 3fe37e5
Implement fully duplexed pipestreams
bigs 5726b6c
Remove memory transport
bigs 14158a1
Add self-dialing and memorytransport support
bigs d1165ed
Make read/write deadlines only use appropriate pipes
bigs f70c013
Implement stream reset error
bigs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package pipetransport | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"sync" | ||
|
||
ic "github.com/libp2p/go-libp2p-crypto" | ||
peer "github.com/libp2p/go-libp2p-peer" | ||
tpt "github.com/libp2p/go-libp2p-transport" | ||
streammux "github.com/libp2p/go-stream-muxer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
type PipeConn struct { | ||
streams chan *PipeStream | ||
|
||
mclosed *sync.RWMutex | ||
closed bool | ||
|
||
addr ma.Multiaddr | ||
id peer.ID | ||
pubKey ic.PubKey | ||
transport *PipeTransport | ||
} | ||
|
||
func NewPipeConn(id peer.ID, addr ma.Multiaddr, pubKey ic.PubKey, transport *PipeTransport) *PipeConn { | ||
return &PipeConn{ | ||
streams: make(chan *PipeStream), | ||
addr: addr, | ||
id: id, | ||
pubKey: pubKey, | ||
transport: transport, | ||
mclosed: new(sync.RWMutex), | ||
closed: false, | ||
} | ||
} | ||
|
||
func (c *PipeConn) Close() error { | ||
c.mclosed.Lock() | ||
defer c.mclosed.Unlock() | ||
|
||
if c.closed { | ||
return fmt.Errorf("tried to close already closed connection") | ||
} | ||
|
||
c.closed = true | ||
close(c.streams) | ||
return nil | ||
} | ||
|
||
func (c *PipeConn) IsClosed() bool { | ||
c.mclosed.RLock() | ||
defer c.mclosed.RUnlock() | ||
|
||
return c.closed | ||
} | ||
|
||
func (c *PipeConn) AcceptStream() (streammux.Stream, error) { | ||
stream, ok := <-c.streams | ||
if !ok { | ||
return nil, io.EOF | ||
} | ||
return stream, nil | ||
} | ||
|
||
func (c *PipeConn) OpenStream() (streammux.Stream, error) { | ||
c.mclosed.RLock() | ||
defer c.mclosed.RUnlock() | ||
|
||
if c.closed { | ||
return nil, fmt.Errorf("opening stream on closed connection") | ||
} | ||
|
||
connA, connB := net.Pipe() | ||
connC, connD := net.Pipe() | ||
errchA := make(chan error, 1) | ||
errchB := make(chan error, 1) | ||
streamA := NewPipeStream(connA, connC, errchB, errchA) | ||
streamB := NewPipeStream(connD, connB, errchA, errchB) | ||
c.streams <- streamB | ||
return streamA, nil | ||
} | ||
|
||
func (c *PipeConn) LocalMultiaddr() ma.Multiaddr { | ||
return c.addr | ||
} | ||
|
||
func (c *PipeConn) RemoteMultiaddr() ma.Multiaddr { | ||
return c.addr | ||
} | ||
|
||
func (c *PipeConn) LocalPeer() peer.ID { | ||
return c.transport.id | ||
} | ||
|
||
func (c *PipeConn) LocalPrivateKey() ic.PrivKey { | ||
return c.transport.privKey | ||
} | ||
|
||
func (c *PipeConn) RemotePeer() peer.ID { | ||
return c.id | ||
} | ||
|
||
func (c *PipeConn) RemotePublicKey() ic.PubKey { | ||
return c.pubKey | ||
} | ||
|
||
func (c *PipeConn) Transport() tpt.Transport { | ||
return c.transport | ||
} | ||
|
||
var _ tpt.Conn = (*PipeConn)(nil) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing else to say :)