Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-unixfs
Commits
b5b4390c
Commit
b5b4390c
authored
Dec 16, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor SwarmConn -> Conn
parent
9c119705
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
27 deletions
+27
-27
net/swarm2/swarm.go
net/swarm2/swarm.go
+3
-3
net/swarm2/swarm_conn.go
net/swarm2/swarm_conn.go
+19
-19
net/swarm2/swarm_dial.go
net/swarm2/swarm_dial.go
+2
-2
net/swarm2/swarm_stream.go
net/swarm2/swarm_stream.go
+3
-3
No files found.
net/swarm2/swarm.go
View file @
b5b4390c
...
...
@@ -50,7 +50,7 @@ func (s *Swarm) teardown() error {
return
s
.
swarm
.
Close
()
}
// Close stops the Swarm.
See
// Close stops the Swarm.
func
(
s
*
Swarm
)
Close
()
error
{
return
s
.
cg
.
Close
()
}
...
...
@@ -80,12 +80,12 @@ func (s *Swarm) StreamsWithPeer(p peer.Peer) []*Stream {
}
// ConnectionsToPeer returns all the live connections to p
func
(
s
*
Swarm
)
ConnectionsToPeer
(
p
peer
.
Peer
)
[]
*
Swarm
Conn
{
func
(
s
*
Swarm
)
ConnectionsToPeer
(
p
peer
.
Peer
)
[]
*
Conn
{
return
wrapConns
(
ps
.
ConnsWithGroup
(
p
,
s
.
swarm
.
Conns
()))
}
// Connections returns a slice of all connections.
func
(
s
*
Swarm
)
Connections
()
[]
*
Swarm
Conn
{
func
(
s
*
Swarm
)
Connections
()
[]
*
Conn
{
return
wrapConns
(
s
.
swarm
.
Conns
())
}
...
...
net/swarm2/swarm_conn.go
View file @
b5b4390c
...
...
@@ -11,23 +11,23 @@ import (
ps
"github.com/jbenet/go-peerstream"
)
// a
Swarm
Conn is a simple wrapper around a ps.Conn that also exposes
// a Conn is a simple wrapper around a ps.Conn that also exposes
// some of the methods from the underlying conn.Conn.
// There's **five** "layers" to each connection:
// - 0. the net.Conn - underlying net.Conn (TCP/UDP/UTP/etc)
// - 1. the manet.Conn - provides multiaddr friendly Conn
// - 2. the conn.Conn - provides Peer friendly Conn (inc Secure channel)
// - 3. the peerstream.Conn - provides peerstream / spdysptream happiness
// - 4. the
Swarm
Conn - abstracts everyting out, exposing only key parts of underlying layers
// - 4. the Conn - abstracts everyting out, exposing only key parts of underlying layers
// (I know, this is kinda crazy. it's more historical than a good design. though the
// layers do build up pieces of functionality. and they're all just io.RW :) )
type
Swarm
Conn
ps
.
Conn
type
Conn
ps
.
Conn
func
(
c
*
Swarm
Conn
)
StreamConn
()
*
ps
.
Conn
{
func
(
c
*
Conn
)
StreamConn
()
*
ps
.
Conn
{
return
(
*
ps
.
Conn
)(
c
)
}
func
(
c
*
Swarm
Conn
)
RawConn
()
conn
.
Conn
{
func
(
c
*
Conn
)
RawConn
()
conn
.
Conn
{
// righly panic if these things aren't true. it is an expected
// invariant that these Conns are all of the typewe expect:
// ps.Conn wrapping a conn.Conn
...
...
@@ -36,48 +36,48 @@ func (c *SwarmConn) RawConn() conn.Conn {
}
// LocalMultiaddr is the Multiaddr on this side
func
(
c
*
Swarm
Conn
)
LocalMultiaddr
()
ma
.
Multiaddr
{
func
(
c
*
Conn
)
LocalMultiaddr
()
ma
.
Multiaddr
{
return
c
.
RawConn
()
.
LocalMultiaddr
()
}
// LocalPeer is the Peer on our side of the connection
func
(
c
*
Swarm
Conn
)
LocalPeer
()
peer
.
Peer
{
func
(
c
*
Conn
)
LocalPeer
()
peer
.
Peer
{
return
c
.
RawConn
()
.
LocalPeer
()
}
// RemoteMultiaddr is the Multiaddr on the remote side
func
(
c
*
Swarm
Conn
)
RemoteMultiaddr
()
ma
.
Multiaddr
{
func
(
c
*
Conn
)
RemoteMultiaddr
()
ma
.
Multiaddr
{
return
c
.
RawConn
()
.
RemoteMultiaddr
()
}
// RemotePeer is the Peer on the remote side
func
(
c
*
Swarm
Conn
)
RemotePeer
()
peer
.
Peer
{
func
(
c
*
Conn
)
RemotePeer
()
peer
.
Peer
{
return
c
.
RawConn
()
.
RemotePeer
()
}
// NewStream returns a new Stream from this connection
func
(
c
*
Swarm
Conn
)
NewStream
()
(
*
Stream
,
error
)
{
func
(
c
*
Conn
)
NewStream
()
(
*
Stream
,
error
)
{
s
,
err
:=
c
.
StreamConn
()
.
NewStream
()
return
wrapStream
(
s
),
err
}
func
(
c
*
Swarm
Conn
)
Close
()
error
{
func
(
c
*
Conn
)
Close
()
error
{
return
c
.
StreamConn
()
.
Close
()
}
func
wrapConn
(
psc
*
ps
.
Conn
)
(
*
Swarm
Conn
,
error
)
{
func
wrapConn
(
psc
*
ps
.
Conn
)
(
*
Conn
,
error
)
{
// grab the underlying connection.
if
_
,
ok
:=
psc
.
NetConn
()
.
(
conn
.
Conn
);
!
ok
{
// this should never happen. if we see it ocurring it means that we added
// a Listener to the ps.Swarm that is NOT one of our net/conn.Listener.
return
nil
,
fmt
.
Errorf
(
"swarm connHandler: invalid conn (not a conn.Conn): %s"
,
psc
)
}
return
(
*
Swarm
Conn
)(
psc
),
nil
return
(
*
Conn
)(
psc
),
nil
}
// wrapConns returns a *
Swarm
Conn for all these ps.Conns
func
wrapConns
(
conns1
[]
*
ps
.
Conn
)
[]
*
Swarm
Conn
{
conns2
:=
make
([]
*
Swarm
Conn
,
len
(
conns1
))
// wrapConns returns a *Conn for all these ps.Conns
func
wrapConns
(
conns1
[]
*
ps
.
Conn
)
[]
*
Conn
{
conns2
:=
make
([]
*
Conn
,
len
(
conns1
))
for
i
,
c1
:=
range
conns1
{
if
c2
,
err
:=
wrapConn
(
c1
);
err
==
nil
{
conns2
[
i
]
=
c2
...
...
@@ -88,9 +88,9 @@ func wrapConns(conns1 []*ps.Conn) []*SwarmConn {
// newConnSetup does the swarm's "setup" for a connection. returns the underlying
// conn.Conn this method is used by both swarm.Dial and ps.Swarm connHandler
func
(
s
*
Swarm
)
newConnSetup
(
ctx
context
.
Context
,
psConn
*
ps
.
Conn
)
(
*
Swarm
Conn
,
error
)
{
func
(
s
*
Swarm
)
newConnSetup
(
ctx
context
.
Context
,
psConn
*
ps
.
Conn
)
(
*
Conn
,
error
)
{
// wrap with a
Swarm
Conn
// wrap with a Conn
sc
,
err
:=
wrapConn
(
psConn
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -114,7 +114,7 @@ func (s *Swarm) newConnSetup(ctx context.Context, psConn *ps.Conn) (*SwarmConn,
return
sc
,
nil
}
// func runHandshake3(ctx context.Context, s *Swarm, c *
Swarm
Conn) error {
// func runHandshake3(ctx context.Context, s *Swarm, c *Conn) error {
// log.Event(ctx, "newConnection", c.LocalPeer(), c.RemotePeer())
// stream, err := c.NewStream()
...
...
net/swarm2/swarm_dial.go
View file @
b5b4390c
...
...
@@ -17,7 +17,7 @@ import (
// the connection will happen over. Swarm can use whichever it choses.
// This allows us to use various transport protocols, do NAT traversal/relay,
// etc. to achive connection.
func
(
s
*
Swarm
)
Dial
(
p
peer
.
Peer
)
(
*
Swarm
Conn
,
error
)
{
func
(
s
*
Swarm
)
Dial
(
p
peer
.
Peer
)
(
*
Conn
,
error
)
{
ctx
:=
context
.
TODO
()
if
p
.
ID
()
.
Equal
(
s
.
local
.
ID
())
{
...
...
@@ -77,7 +77,7 @@ func (s *Swarm) Dial(p peer.Peer) (*SwarmConn, error) {
// dialConnSetup is the setup logic for a connection from the dial side. it
// needs to add the Conn to the StreamSwarm, then run newConnSetup
func
dialConnSetup
(
ctx
context
.
Context
,
s
*
Swarm
,
connC
conn
.
Conn
)
(
*
Swarm
Conn
,
error
)
{
func
dialConnSetup
(
ctx
context
.
Context
,
s
*
Swarm
,
connC
conn
.
Conn
)
(
*
Conn
,
error
)
{
psC
,
err
:=
s
.
swarm
.
AddConn
(
connC
)
if
err
!=
nil
{
...
...
net/swarm2/swarm_stream.go
View file @
b5b4390c
...
...
@@ -5,7 +5,7 @@ import (
)
// a Stream is a wrapper around a ps.Stream that exposes a way to get
// our
Swarm
Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
// our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
type
Stream
ps
.
Stream
// StreamHandler is called when new streams are opened from remote peers.
...
...
@@ -18,8 +18,8 @@ func (s *Stream) Stream() *ps.Stream {
}
// Conn returns the Conn associated with this Stream
func
(
s
*
Stream
)
Conn
()
*
Swarm
Conn
{
return
(
*
Swarm
Conn
)(
s
.
Stream
()
.
Conn
())
func
(
s
*
Stream
)
Conn
()
*
Conn
{
return
(
*
Conn
)(
s
.
Stream
()
.
Conn
())
}
// Write writes bytes to a stream, calling write data for each call.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment