p2p_server
以太坊系列之十九 对p2p模块server的理解
type transport interface {
// The two handshakes.
doEncHandshake(prv *ecdsa.PrivateKey, dialDest *discover.Node) (discover.NodeID, error)
doProtoHandshake(our *protoHandshake) (*protoHandshake, error)
// The MsgReadWriter can only be used after the encryption
// handshake has completed. The code uses conn.id to track this
// by setting it to a non-nil value after the encryption handshake.
MsgReadWriter
// transports must provide Close because we use MsgPipe in some of
// the tests. Closing the actual network connection doesn't do
// anything in those tests because NsgPipe doesn't use it.
close(err error)
}
type conn struct {
fd net.Conn
transport //指向真正的transport实现,rlpx
flags connFlag
cont chan error // The run loop uses cont to signal errors to setupConn.
id discover.NodeID // valid after the encryption handshake
caps []Cap // valid after the protocol handshake
name string // valid after the protocol handshake
}
// Server manages all peer connections.
type Server struct {
// Config fields may not be modified while the server is running.
Config
// Hooks for testing. These are useful because we can inhibit
// the whole protocol stack.
newTransport func(net.Conn) transport
newPeerHook func(*Peer)
lock sync.Mutex // protects running
running bool
ntab discoverTable
listener net.Listener
ourHandshake *protoHandshake
lastLookup time.Time
DiscV5 *discv5.Network
// These are for Peers, PeerCount (and nothing else).
peerOp chan peerOpFunc
peerOpDone chan struct{}
quit chan struct{}
addstatic chan *discover.Node
removestatic chan *discover.Node
posthandshake chan *conn
addpeer chan *conn
delpeer chan peerDrop
loopWG sync.WaitGroup // loop, listenLoop
}
Server是暴露给上层使用的接口,配置完毕以后,直接调用Start就可以了,其中Start做了非常复杂的工作.主要是启动监听端口,等待连接.这里会创建多个goroutine,完毕以后进入run,run实际上就是一个汇聚层,其他的goroutine都回把得到的结果通过channel形式汇报给主goroutine,也就是run,由run集中处理,避免并发冲突.
另外run的另一个重要功能就是不断的调用dialstate,来创建连接,维护与结点的连接.也就是scheduleTasks.
创建Server,对于用户来说主要是指定一个newPeerHook,这样当有了新的结点以后,就可以和这些结点进行通信,通信的格式是Msg,这个应该是用户最关心的.
newTransport 是和结点建立连接(tcp或者udp)以后进行协商密钥,协议握手的地方,实际上是newRLPX.也就是rlpx来负责.
这里先说说Config,这里的PrivateKey必须指定,否则会出错.
p2p_server的更多相关文章
- 添加本地jar到Maven库
转自:http://dk05408.iteye.com/blog/2170986 上传: mvn install:install-file -Dfile=D:/workspace/p2p_server ...
- 以太坊系列之六: p2p模块--以太坊源码学习
p2p模块 p2p模块对外暴露了Server关键结构,帮助上层管理复杂的p2p网路,使其集中于Protocol的实现,只关注于数据的传输. Server使用discover模块,在指定的UDP端口管理 ...
随机推荐
- 使用QuartZ.net来做定时计划任务 ; 值不能为 null。 参数名: assemblyString
1. 当异常的时候, 发现需要的类名, 没有取到, 然后就发生异常了 2. 分析: 业务层调用数据层, 数据层去掉配置的时候, 发现配置文件中根本就没有配置, 这个时候使用反射来取, 肯定是拿不到的, ...
- (转)Docker - 创建 Docker overlay network (containers 通信)
原文链接: http://www.cnblogs.com/AlanWalkOn/p/6101875.html --- 创建基于Key-Value的Docker overlay network. 这样运 ...
- 第四章 Javac编译原理
4.1 Javac是什么 是一种编译器,将JAVA源代码(.java文件)语言先转化成JVM能够识别的一种语言(.class文件),然后由JVM将JVM语言再转化成当前机器可以识别的机器语言. 4.2 ...
- AJAX的流程是什么?
客户端产生js的事件 创建XMLHttpRequest对象 对XMLHttpRequest进行配置 通过AJAX引擎发送异步请求 服务器端接受请求并且处理请求,返回html或者xml内容 XML调用一 ...
- windows提权辅助工具koadic
项目地址:https://github.com/zerosum0x0/koadic ┌─[root@sch01ar]─[/sch01ar] └──╼ #git clone https://github ...
- XML注释与Description标签及Java:注解(Annotation)的关系
NET中的规范标准注释(一) -- XML注释标签讲解 一.摘要 .Net允许开发人员在源代码中插入XML注释,这在多人协作开发的时候显得特别有用. C#解析器可以把代码文件中的这些XML标记提取出来 ...
- python与桶排序
问题提出: 将以下数据: 6, 8, 2, 3, 4, 0, 9, 1, 5,1 按从小到达排列. 桶排序原理: 桶排序也叫计数排序,简单来说,就是将数据集里面所有元素按顺序列举出来,然后统计元素出现 ...
- 执行CRUD总结
- OSGI 模块化
推荐教程:https://course.tianmaying.com/osgi-toturial+osgi-concept#15
- sendCloud群发邮件一点总结
1.群发时,若发送的邮件为html页面,则不能用[普通发送]然后foreach循环: 若是单纯的文本,则可以用普通发送,否则,第一封邮件成功后,后面的都是html乱码. 2.若要用html模板发送,可 ...