以太坊系列之十九 对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的更多相关文章

  1. 添加本地jar到Maven库

    转自:http://dk05408.iteye.com/blog/2170986 上传: mvn install:install-file -Dfile=D:/workspace/p2p_server ...

  2. 以太坊系列之六: p2p模块--以太坊源码学习

    p2p模块 p2p模块对外暴露了Server关键结构,帮助上层管理复杂的p2p网路,使其集中于Protocol的实现,只关注于数据的传输. Server使用discover模块,在指定的UDP端口管理 ...

随机推荐

  1. Office 2019 2016 安装破解教程

    声明:工具由蓝点网提供支持,密钥为本人收集内容,非转载部分 GVLKs for Office 2019     Product GVLK Office Professional Plus 2019  ...

  2. 微信小程序之巧妙的封装

    巧妙的封装 暴露一个访问地址xapp.config.js module.exports = { api_host: `https://a.squmo.com/yizu` } 继续引入,加暴露api.c ...

  3. SecureCRT乱码问题解决方法

    环境:SecureCRT登陆REDHAT5.3 LINUX系统 问题:vi编辑器编辑文件时文件中的内容中文显示乱码,但是直接使用linux系统terminal打开此文件时中文显示正常,确诊问题出现在客 ...

  4. spring mvc helloworld 和表单功能、页面重定向

    Spring MVC Hello World 例子 这里有个很好的教程:https://www.cnblogs.com/wormday/p/8435617.html 下面的例子说明了如何使用 Spri ...

  5. python's sixteenth day for me 员工信息表

    import os user_dic = { 'username':None, 'password':None, 'login':True } flag = False name_list = ['i ...

  6. 利用JS 在网页上获取并显示当前日期 星期

    下边的HTML代码,可以取出日期与星期 <html><body><h1><script language=JavaScript>var d, s = & ...

  7. Python中的闭包与迭代器

    前面内容补充 函数名分应用(第一类对象) 函数名的命名规范与变量命名是一样的函数名其实就是变量名 函数名可以作为列表中的元素进行存储 例如: def func1(): pass def func2() ...

  8. 基于JNI,JAVA 调用 C++入门

    1.步骤一览 2.步骤详情 2.1. 编写Java类 native package com.ibugs.jni; /** * * @author i_bugs * */ public class CP ...

  9. 服务器发送邮件出现Could not connect to SMTP host错误 解决办法

    服务器发送邮件出现Could not connect to SMTP host错误 解决办法 功夫不负有心人,最后了解到,除了google的smtp服务器收到请求“smtp”会接受,其他服务器比如qq ...

  10. Collision Detection

    [Collision Detection] Collision Detection是Rigidbody中的一个属性.所以显然Collision Detection指定的类型只在Rigidbody之间才 ...