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
p2p
go-p2p-swarm
Commits
cdade26f
Commit
cdade26f
authored
Jun 15, 2018
by
Cole Brown
Committed by
Steven Allen
Aug 27, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for Stat() to Swarm Stream and Conn
parent
c868818b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
29 additions
and
6 deletions
+29
-6
swarm.go
swarm.go
+2
-1
swarm_conn.go
swarm_conn.go
+13
-3
swarm_dial.go
swarm_dial.go
+2
-1
swarm_listen.go
swarm_listen.go
+2
-1
swarm_stream.go
swarm_stream.go
+10
-0
No files found.
swarm.go
View file @
cdade26f
...
@@ -165,7 +165,7 @@ func (s *Swarm) Process() goprocess.Process {
...
@@ -165,7 +165,7 @@ func (s *Swarm) Process() goprocess.Process {
return
s
.
proc
return
s
.
proc
}
}
func
(
s
*
Swarm
)
addConn
(
tc
transport
.
Conn
)
(
*
Conn
,
error
)
{
func
(
s
*
Swarm
)
addConn
(
tc
transport
.
Conn
,
stat
inet
.
Stat
)
(
*
Conn
,
error
)
{
// The underlying transport (or the dialer) *should* filter it's own
// The underlying transport (or the dialer) *should* filter it's own
// connections but we should double check anyways.
// connections but we should double check anyways.
raddr
:=
tc
.
RemoteMultiaddr
()
raddr
:=
tc
.
RemoteMultiaddr
()
...
@@ -197,6 +197,7 @@ func (s *Swarm) addConn(tc transport.Conn) (*Conn, error) {
...
@@ -197,6 +197,7 @@ func (s *Swarm) addConn(tc transport.Conn) (*Conn, error) {
c
:=
&
Conn
{
c
:=
&
Conn
{
conn
:
tc
,
conn
:
tc
,
swarm
:
s
,
swarm
:
s
,
stat
:
stat
,
}
}
c
.
streams
.
m
=
make
(
map
[
*
Stream
]
struct
{})
c
.
streams
.
m
=
make
(
map
[
*
Stream
]
struct
{})
s
.
conns
.
m
[
p
]
=
append
(
s
.
conns
.
m
[
p
],
c
)
s
.
conns
.
m
[
p
]
=
append
(
s
.
conns
.
m
[
p
],
c
)
...
...
swarm_conn.go
View file @
cdade26f
...
@@ -33,6 +33,8 @@ type Conn struct {
...
@@ -33,6 +33,8 @@ type Conn struct {
sync
.
Mutex
sync
.
Mutex
m
map
[
*
Stream
]
struct
{}
m
map
[
*
Stream
]
struct
{}
}
}
stat
inet
.
Stat
}
}
// Close closes this connection.
// Close closes this connection.
...
@@ -98,7 +100,8 @@ func (c *Conn) start() {
...
@@ -98,7 +100,8 @@ func (c *Conn) start() {
}
}
c
.
swarm
.
refs
.
Add
(
1
)
c
.
swarm
.
refs
.
Add
(
1
)
go
func
()
{
go
func
()
{
s
,
err
:=
c
.
addStream
(
ts
)
stat
:=
inet
.
Stat
{
Direction
:
inet
.
DirInbound
}
s
,
err
:=
c
.
addStream
(
ts
,
stat
)
// Don't defer this. We don't want to block
// Don't defer this. We don't want to block
// swarm shutdown on the connection handler.
// swarm shutdown on the connection handler.
...
@@ -158,16 +161,22 @@ func (c *Conn) RemotePublicKey() ic.PubKey {
...
@@ -158,16 +161,22 @@ func (c *Conn) RemotePublicKey() ic.PubKey {
return
c
.
conn
.
RemotePublicKey
()
return
c
.
conn
.
RemotePublicKey
()
}
}
// Stat returns metadata pertaining to this connection
func
(
c
*
Conn
)
Stat
()
inet
.
Stat
{
return
c
.
stat
}
// NewStream returns a new Stream from this connection
// NewStream returns a new Stream from this connection
func
(
c
*
Conn
)
NewStream
()
(
inet
.
Stream
,
error
)
{
func
(
c
*
Conn
)
NewStream
()
(
inet
.
Stream
,
error
)
{
ts
,
err
:=
c
.
conn
.
OpenStream
()
ts
,
err
:=
c
.
conn
.
OpenStream
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
return
c
.
addStream
(
ts
)
stat
:=
inet
.
Stat
{
Direction
:
inet
.
DirOutbound
}
return
c
.
addStream
(
ts
,
stat
)
}
}
func
(
c
*
Conn
)
addStream
(
ts
smux
.
Stream
)
(
*
Stream
,
error
)
{
func
(
c
*
Conn
)
addStream
(
ts
smux
.
Stream
,
stat
inet
.
Stat
)
(
*
Stream
,
error
)
{
c
.
streams
.
Lock
()
c
.
streams
.
Lock
()
// Are we still online?
// Are we still online?
if
c
.
streams
.
m
==
nil
{
if
c
.
streams
.
m
==
nil
{
...
@@ -180,6 +189,7 @@ func (c *Conn) addStream(ts smux.Stream) (*Stream, error) {
...
@@ -180,6 +189,7 @@ func (c *Conn) addStream(ts smux.Stream) (*Stream, error) {
s
:=
&
Stream
{
s
:=
&
Stream
{
stream
:
ts
,
stream
:
ts
,
conn
:
c
,
conn
:
c
,
stat
:
stat
,
}
}
c
.
streams
.
m
[
s
]
=
struct
{}{}
c
.
streams
.
m
[
s
]
=
struct
{}{}
...
...
swarm_dial.go
View file @
cdade26f
...
@@ -325,7 +325,8 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
...
@@ -325,7 +325,8 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) {
"localAddr"
:
connC
.
LocalMultiaddr
(),
"localAddr"
:
connC
.
LocalMultiaddr
(),
"remoteAddr"
:
connC
.
RemoteMultiaddr
(),
"remoteAddr"
:
connC
.
RemoteMultiaddr
(),
}
}
swarmC
,
err
:=
s
.
addConn
(
connC
)
stat
:=
inet
.
Stat
{
Direction
:
inet
.
DirOutbound
}
swarmC
,
err
:=
s
.
addConn
(
connC
,
stat
)
if
err
!=
nil
{
if
err
!=
nil
{
logdial
[
"error"
]
=
err
.
Error
()
logdial
[
"error"
]
=
err
.
Error
()
connC
.
Close
()
// close the connection. didn't work out :(
connC
.
Close
()
// close the connection. didn't work out :(
...
...
swarm_listen.go
View file @
cdade26f
...
@@ -81,7 +81,8 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {
...
@@ -81,7 +81,8 @@ func (s *Swarm) AddListenAddr(a ma.Multiaddr) error {
s
.
refs
.
Add
(
1
)
s
.
refs
.
Add
(
1
)
go
func
()
{
go
func
()
{
defer
s
.
refs
.
Done
()
defer
s
.
refs
.
Done
()
_
,
err
:=
s
.
addConn
(
c
)
stat
:=
inet
.
Stat
{
Direction
:
inet
.
DirInbound
}
_
,
err
:=
s
.
addConn
(
c
,
stat
)
if
err
!=
nil
{
if
err
!=
nil
{
// Probably just means that the swarm has been closed.
// Probably just means that the swarm has been closed.
log
.
Warningf
(
"add conn failed: "
,
err
)
log
.
Warningf
(
"add conn failed: "
,
err
)
...
...
swarm_stream.go
View file @
cdade26f
...
@@ -22,6 +22,9 @@ const (
...
@@ -22,6 +22,9 @@ const (
streamReset
streamReset
)
)
// Validate Stream conforms to the go-libp2p-net Stream interface
var
_
inet
.
Stream
=
&
Stream
{}
// Stream is the stream type used by swarm. In general, you won't use this type
// Stream is the stream type used by swarm. In general, you won't use this type
// directly.
// directly.
type
Stream
struct
{
type
Stream
struct
{
...
@@ -36,6 +39,8 @@ type Stream struct {
...
@@ -36,6 +39,8 @@ type Stream struct {
notifyLk
sync
.
Mutex
notifyLk
sync
.
Mutex
protocol
atomic
.
Value
protocol
atomic
.
Value
stat
inet
.
Stat
}
}
func
(
s
*
Stream
)
String
()
string
{
func
(
s
*
Stream
)
String
()
string
{
...
@@ -165,3 +170,8 @@ func (s *Stream) SetReadDeadline(t time.Time) error {
...
@@ -165,3 +170,8 @@ func (s *Stream) SetReadDeadline(t time.Time) error {
func
(
s
*
Stream
)
SetWriteDeadline
(
t
time
.
Time
)
error
{
func
(
s
*
Stream
)
SetWriteDeadline
(
t
time
.
Time
)
error
{
return
s
.
stream
.
SetWriteDeadline
(
t
)
return
s
.
stream
.
SetWriteDeadline
(
t
)
}
}
// Stat returns metadata information for this stream.
func
(
s
*
Stream
)
Stat
()
inet
.
Stat
{
return
s
.
stat
}
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