Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
2487c99a
提交
2487c99a
authored
5月 23, 2018
作者:
Łukasz Magiera
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
p2p: refactor local/remote
License: MIT Signed-off-by:
Łukasz Magiera
<
magik6k@gmail.com
>
上级
48d83be0
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
47 行增加
和
36 行删除
+47
-36
p2p.go
core/commands/p2p.go
+16
-14
listener.go
p2p/listener.go
+2
-1
local.go
p2p/local.go
+15
-11
remote.go
p2p/remote.go
+14
-10
没有找到文件。
core/commands/p2p.go
浏览文件 @
2487c99a
...
...
@@ -21,8 +21,9 @@ import (
// P2PListenerInfoOutput is output type of ls command
type
P2PListenerInfoOutput
struct
{
Protocol
string
Address
string
Protocol
string
ListenAddress
string
TargetAddress
string
}
// P2PStreamInfoOutput is output type of streams command
...
...
@@ -130,7 +131,7 @@ func forwardRemote(ctx context.Context, p *p2p.P2P, proto string, target string)
}
// TODO: return some info
_
,
err
=
p
.
NewListener
(
ctx
,
proto
,
addr
)
_
,
err
=
p
.
ForwardRemote
(
ctx
,
proto
,
addr
)
return
err
}
...
...
@@ -151,7 +152,7 @@ func forwardLocal(ctx context.Context, p *p2p.P2P, ps pstore.Peerstore, proto st
}
// TODO: return some info
_
,
err
=
p
.
Di
al
(
ctx
,
peer
,
proto
,
bindAddr
)
_
,
err
=
p
.
ForwardLoc
al
(
ctx
,
peer
,
proto
,
bindAddr
)
return
err
}
...
...
@@ -206,8 +207,9 @@ var p2pListenerLsCmd = &cmds.Command{
for
_
,
listener
:=
range
n
.
P2P
.
Listeners
.
Listeners
{
output
.
Listeners
=
append
(
output
.
Listeners
,
P2PListenerInfoOutput
{
Protocol
:
listener
.
Protocol
(),
Address
:
listener
.
Address
(),
Protocol
:
listener
.
Protocol
(),
ListenAddress
:
listener
.
ListenAddress
(),
TargetAddress
:
listener
.
TargetAddress
(),
})
}
...
...
@@ -227,10 +229,10 @@ var p2pListenerLsCmd = &cmds.Command{
w
:=
tabwriter
.
NewWriter
(
buf
,
1
,
2
,
1
,
' '
,
0
)
for
_
,
listener
:=
range
list
.
Listeners
{
if
headers
{
fmt
.
Fprintln
(
w
,
"
Address
\t
Protocol
"
)
fmt
.
Fprintln
(
w
,
"
Protocol
\t
Listen Address
\t
Target Address
"
)
}
fmt
.
Fprintf
(
w
,
"%s
\t
%s
\
n
"
,
listener
.
Address
,
listener
.
Protocol
)
fmt
.
Fprintf
(
w
,
"%s
\t
%s
\
t
%s
\n
"
,
listener
.
Protocol
,
listener
.
ListenAddress
,
listener
.
TargetAddress
)
}
w
.
Flush
()
...
...
@@ -330,7 +332,7 @@ Note that the connections originate from the ipfs daemon process.
return
}
_
,
err
=
n
.
P2P
.
NewListener
(
n
.
Context
(),
proto
,
addr
)
_
,
err
=
n
.
P2P
.
ForwardRemote
(
n
.
Context
(),
proto
,
addr
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
...
...
@@ -338,8 +340,8 @@ Note that the connections originate from the ipfs daemon process.
// Successful response.
res
.
SetOutput
(
&
P2PListenerInfoOutput
{
Protocol
:
proto
,
Address
:
addr
.
String
(),
Protocol
:
proto
,
TargetAddress
:
addr
.
String
(),
})
},
}
...
...
@@ -389,15 +391,15 @@ can transparently connect to a p2p service.
}
}
listenerInfo
,
err
:=
n
.
P2P
.
Di
al
(
n
.
Context
(),
peer
,
proto
,
bindAddr
)
listenerInfo
,
err
:=
n
.
P2P
.
ForwardLoc
al
(
n
.
Context
(),
peer
,
proto
,
bindAddr
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
output
:=
P2PListenerInfoOutput
{
Protocol
:
listenerInfo
.
Protocol
(),
Address
:
listenerInfo
.
Address
(),
Protocol
:
listenerInfo
.
Protocol
(),
ListenAddress
:
listenerInfo
.
Listen
Address
(),
}
res
.
SetOutput
(
&
output
)
...
...
p2p/listener.go
浏览文件 @
2487c99a
...
...
@@ -2,7 +2,8 @@ package p2p
type
Listener
interface
{
Protocol
()
string
Address
()
string
ListenAddress
()
string
TargetAddress
()
string
// Close closes the listener. Does not affect child streams
Close
()
error
...
...
p2p/
outbound
.go
→
p2p/
local
.go
浏览文件 @
2487c99a
...
...
@@ -12,8 +12,8 @@ import (
peer
"gx/ipfs/QmcJukH2sAFjY3HdBKq35WDzWoL3UUu2gt9wdfqZTUyM74/go-libp2p-peer"
)
//
outboundListener accepts libp2p streams and proxies them to a manet host
type
outbound
Listener
struct
{
//
localListener manet streams and proxies them to libp2p services
type
local
Listener
struct
{
ctx
context
.
Context
p2p
*
P2P
...
...
@@ -25,14 +25,14 @@ type outboundListener struct {
listener
manet
.
Listener
}
//
Di
al creates new P2P stream to a remote listener
func
(
p2p
*
P2P
)
Di
al
(
ctx
context
.
Context
,
peer
peer
.
ID
,
proto
string
,
bindAddr
ma
.
Multiaddr
)
(
Listener
,
error
)
{
//
ForwardLoc
al creates new P2P stream to a remote listener
func
(
p2p
*
P2P
)
ForwardLoc
al
(
ctx
context
.
Context
,
peer
peer
.
ID
,
proto
string
,
bindAddr
ma
.
Multiaddr
)
(
Listener
,
error
)
{
maListener
,
err
:=
manet
.
Listen
(
bindAddr
)
if
err
!=
nil
{
return
nil
,
err
}
listener
:=
&
outbound
Listener
{
listener
:=
&
local
Listener
{
ctx
:
ctx
,
p2p
:
p2p
,
...
...
@@ -50,7 +50,7 @@ func (p2p *P2P) Dial(ctx context.Context, peer peer.ID, proto string, bindAddr m
return
listener
,
nil
}
func
(
l
*
outbound
Listener
)
dial
()
(
net
.
Stream
,
error
)
{
func
(
l
*
local
Listener
)
dial
()
(
net
.
Stream
,
error
)
{
ctx
,
cancel
:=
context
.
WithTimeout
(
l
.
ctx
,
time
.
Second
*
30
)
//TODO: configurable?
defer
cancel
()
...
...
@@ -62,7 +62,7 @@ func (l *outboundListener) dial() (net.Stream, error) {
return
l
.
p2p
.
peerHost
.
NewStream
(
l
.
ctx
,
l
.
peer
,
protocol
.
ID
(
l
.
proto
))
}
func
(
l
*
outbound
Listener
)
acceptConns
()
{
func
(
l
*
local
Listener
)
acceptConns
()
{
for
{
local
,
err
:=
l
.
listener
.
Accept
()
if
err
!=
nil
{
...
...
@@ -95,16 +95,20 @@ func (l *outboundListener) acceptConns() {
}
}
func
(
l
*
outbound
Listener
)
Close
()
error
{
func
(
l
*
local
Listener
)
Close
()
error
{
l
.
listener
.
Close
()
l
.
p2p
.
Listeners
.
Deregister
(
l
.
proto
)
return
nil
}
func
(
l
*
outbound
Listener
)
Protocol
()
string
{
func
(
l
*
local
Listener
)
Protocol
()
string
{
return
l
.
proto
}
func
(
l
*
outboundListener
)
Address
()
string
{
return
"/ipfs/"
+
l
.
peer
.
String
()
func
(
l
*
localListener
)
ListenAddress
()
string
{
return
l
.
listener
.
Multiaddr
()
.
String
()
}
func
(
l
*
localListener
)
TargetAddress
()
string
{
return
"/ipfs/"
+
l
.
peer
.
Pretty
()
}
p2p/
inbound
.go
→
p2p/
remote
.go
浏览文件 @
2487c99a
...
...
@@ -9,8 +9,8 @@ import (
manet
"gx/ipfs/QmcGXGdw9BWDysPJQHxJinjGHha3eEg4vzFETre4woNwcX/go-multiaddr-net"
)
//
inbound
Listener accepts libp2p streams and proxies them to a manet host
type
inbound
Listener
struct
{
//
remote
Listener accepts libp2p streams and proxies them to a manet host
type
remote
Listener
struct
{
p2p
*
P2P
// Application proto identifier.
...
...
@@ -20,15 +20,17 @@ type inboundListener struct {
addr
ma
.
Multiaddr
}
//
NewListener
creates new p2p listener
func
(
p2p
*
P2P
)
NewListener
(
ctx
context
.
Context
,
proto
string
,
addr
ma
.
Multiaddr
)
(
Listener
,
error
)
{
listenerInfo
:=
&
inbound
Listener
{
//
ForwardRemote
creates new p2p listener
func
(
p2p
*
P2P
)
ForwardRemote
(
ctx
context
.
Context
,
proto
string
,
addr
ma
.
Multiaddr
)
(
Listener
,
error
)
{
listenerInfo
:=
&
remote
Listener
{
p2p
:
p2p
,
proto
:
proto
,
addr
:
addr
,
}
p2p
.
Listeners
.
Register
(
listenerInfo
)
p2p
.
peerHost
.
SetStreamHandler
(
protocol
.
ID
(
proto
),
func
(
remote
net
.
Stream
)
{
local
,
err
:=
manet
.
Dial
(
addr
)
if
err
!=
nil
{
...
...
@@ -55,20 +57,22 @@ func (p2p *P2P) NewListener(ctx context.Context, proto string, addr ma.Multiaddr
stream
.
startStreaming
()
})
p2p
.
Listeners
.
Register
(
listenerInfo
)
return
listenerInfo
,
nil
}
func
(
l
*
inbound
Listener
)
Protocol
()
string
{
func
(
l
*
remote
Listener
)
Protocol
()
string
{
return
l
.
proto
}
func
(
l
*
inboundListener
)
Address
()
string
{
func
(
l
*
remoteListener
)
ListenAddress
()
string
{
return
"/ipfs"
}
func
(
l
*
remoteListener
)
TargetAddress
()
string
{
return
l
.
addr
.
String
()
}
func
(
l
*
inbound
Listener
)
Close
()
error
{
func
(
l
*
remote
Listener
)
Close
()
error
{
l
.
p2p
.
peerHost
.
RemoveStreamHandler
(
protocol
.
ID
(
l
.
proto
))
l
.
p2p
.
Listeners
.
Deregister
(
l
.
proto
)
return
nil
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论