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端口管理 ...
随机推荐
- java中输入3个数,从大到小的输出。。。。
总结:我暂时不能理解,C语言时讲过,java里就不理解了 package com.a; import java.sql.Date; import java.util.Scanner; //输入三个数, ...
- JVM之运行时数据区
Java虚拟机运行时数据区包括PC寄存器.Java虚拟机栈.Java堆.方法区.本地方法栈.运行时常量池六个部分. 1. PC寄存器 PC寄存器(又叫程序计数器,Program Counter Reg ...
- Velodyne 线性激光雷达数据合成
坐标系旋转 如果想用字母表示角度,有两个方法: 1. 用三角函数sind(θ4).cosd(θ4).tand(θ4).atand(θ4)进行表示,注意:θ4在输入时是角度,只是没有度数特有的符号(° ...
- Windows10 官方原版镜像下载途径 Label:win10解决方案
https://www.microsoft.com/en-gb/software-download/windows10ISO 设置浏览标签为手机以避免跳转,下载即可 或者手机打开该网址,获取下载链接 ...
- MMU段式映射(VA -> PA)过程分析
MMU:内存管理单元. CPU寻址的方式: 未使用MMU:CPU发出地址(PA) 直接内存寻址(SDRAM or DDRx). 使用MMU :CPU发出地址(VA) MMU接收CPU发来的地址 经过 ...
- window下配置Solr6.5以及IK Analyzer分词配置
一.安装准备及各软件使用版本说明: 1.下载jdk,我下载的版本是jdk-8u121-windows-x64.exe,下载地址: http://www.oracle.com/technetwork/j ...
- windows下配置非安装版的MySQL5.6
Installing MySQL on Microsoft Windows Using a noinstall Zip Archive,在Windows上使用非安装压缩包安装MySQL.安装步骤如下: ...
- 循序渐进Python3(十一) --4-- web之jQuery
jQuery jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设 ...
- Oracle11gr2_ADG管理之switchover实战
. 环境 db_primary db_stanby 备注 db版本 11.2.0.4.0 11.2.0.4.0 os版本 centos 6.4 centos 6.4 db_unique_name ne ...
- 【知识碎片】CSS 篇
1.CSS达到截取效果 地方卡机了会计师的立法及 => 地方卡机了... max-width: 400px; overflow: hidden; white-space: nowrap; t ...