以太坊系列之十九 对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. 分布式锁之二:zookeeper分布式锁2

    示例: package com.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.zoo ...

  2. AngularJS:template

    ylbtech-AngularJS: 1.返回顶部 1.   2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9 ...

  3. kali下apche配置多网站

    设置文件地址为 :/etc/apache2/sites-available/default 添加: <VirtualHost *:80>       ServerName www.php. ...

  4. Py修行路 python基础 (二十三)模块与包

    一.模块 1)定义: 模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 2)为何要用模块: 退出python解释器然后重新进入,那之前定义的函数或者变量都将丢失,因 ...

  5. numpy的一些用法

    安装numpy windows安装pip即可,具体方法参考pip官网 http://pip-cn.readthedocs.io/en/latest/installing.html 安装方法:pip i ...

  6. Storm集群部署及单词技术

    1. 集群部署的基本流程 集群部署的流程:下载安装包.解压安装包.修改配置文件.分发安装包.启动集群 注意: 所有的集群上都需要配置hosts vi  /etc/hosts 192.168.239.1 ...

  7. thread.Join(); 让主线程等待自己完成

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. sql 在存储过程中使用事务(转)

    本来想自己写一下,后来发现这个写的比我理解的要好,所以直接拽过来了,链接地址:https://www.cnblogs.com/RascallySnake/archive/2010/05/17/1737 ...

  9. jsp Ajax请求(返回xml数据类型)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  10. 在Python中操作谷歌浏览器

    在Python中使用谷歌浏览器,注意以下几点: 1.下载安装的谷歌浏览器Chrome和驱动chromedriver.exe要版本一致. 2.驱动chromedriver.exe要放在Chrome浏览器 ...