Network 源码接口分析
作者:李
- 2022 年 8 月 31 日 湖南
本文字数:823 字
阅读完需:约 3 分钟
本文已参与「开源摘星计划」,欢迎正在阅读的你加入。活动链接:https://github.com/weopenprojects/WeOpen-Star
整个网络模块包含如下接口文件,一个个分析
1.network.go
// 连接处理器type ConnHandler func(conn Conn) (bool, error)
type Network interface { Dialer Listener io.Closer // 注册一个连接处理器 SetNewConnHandler(handler ConnHandler) // 断开一个连接 Disconnect(conn Conn) error // 返回连接是否关闭 Closed() bool // 返回一个本地节点id LocalPeerID() peer.ID}
复制代码
2.dialer.go
type Dialer interface { // 远程地址建立连接 Dial(ctx context.Context, remoteAddr ma.Multiaddr) (Conn, error)}复制代码
3.listener.go
type Listener interface { io.Closer //监听连接 Listen(ctx context.Context, addresses ...ma.Multiaddr) error //返回本地监听的连接 ListenAddresses() []ma.Multiaddr}
复制代码
4.conn.go
type Conn interface { io.Closer Stat //返回本地连接地址 LocalAddr() ma.Multiaddr //返回本地网络地址 LocalNetAddr() net.Addr //返回本地节点id LocalPeerID() peer.ID // 远程地址 RemoteAddr() ma.Multiaddr // 远程网络地址 RemoteNetAddr() net.Addr // 远程节点id RemotePeerID() peer.ID // 所属网络 Network() Network // 创建发送stream CreateSendStream() (SendStream, error) //接收流 AcceptReceiveStream() (ReceiveStream, error) // CreateBidirectionalStream try to open a bidirectional stream with the connection. CreateBidirectionalStream() (Stream, error) // AcceptBidirectionalStream accept a bidirectional stream with the connection. // It will block until a new bidirectional stream accepted or connection closed. AcceptBidirectionalStream() (Stream, error)}复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
李
关注
还未添加个人签名 2018.05.04 加入
还未添加个人简介









评论