当前位置: 首页 > news >正文

go-ethereum之rpc

简介

主要用于DeApp与node之间的交互

结构

1
n
1
n
use
«interface»
ServerCodec
Server
serviceRegistry
service
callback
Client
jsonCodec

Client:服务端接收到一个连接后创建Client来处理请求,表示一个到服务端的连接

type Client struct {idgen    func() ID // for subscriptionsisHTTP   bool      // connection type: http, ws or ipcservices *serviceRegistryidCounter atomic.Uint32// This function, if non-nil, is called when the connection is lost.reconnectFunc reconnectFunc// config fieldsbatchItemLimit       intbatchResponseMaxSize int// writeConn is used for writing to the connection on the caller's goroutine. It should// only be accessed outside of dispatch, with the write lock held. The write lock is// taken by sending on reqInit and released by sending on reqSent.writeConn jsonWriter// for dispatchclose       chan struct{}closing     chan struct{}    // closed when client is quittingdidClose    chan struct{}    // closed when client quitsreconnected chan ServerCodec // where write/reconnect sends the new connectionreadOp      chan readOp      // read messagesreadErr     chan error       // errors from readreqInit     chan *requestOp  // register response IDs, takes write lockreqSent     chan error       // signals write completion, releases write lockreqTimeout  chan *requestOp  // removes response IDs when call timeout expires
}

客户端创建Client使用newClient,服务端接收连接创建表示连接的客户端使用initClient。即客户端主要使用下面几个方法

  • DialOptions
  • DialHTTPWithClient(http)
  • DialInProc(inproc进程内)
  • DialIPC(进程间)
  • DialWebsocketWithDialer或者DialWebsocket(websocket)
func newClient(initctx context.Context, cfg *clientConfig, connect reconnectFunc) (*Client, error) {conn, err := connect(initctx)if err != nil {return nil, err}c := initClient(conn, new(serviceRegistry), cfg)c.reconnectFunc = connectreturn c, nil
}func initClient(conn ServerCodec, services *serviceRegistry, cfg *clientConfig) *Client {_, isHTTP := conn.(*httpConn)c := &Client{isHTTP:               isHTTP,services:             services,idgen:                cfg.idgen,batchItemLimit:       cfg.batchItemLimit,batchResponseMaxSize: cfg.batchResponseLimit,writeConn:            conn,close:                make(chan struct{}),closing:              make(chan struct{}),didClose:             make(chan struct{}),reconnected:          make(chan ServerCodec),readOp:               make(chan readOp),readErr:              make(chan error),reqInit:              make(chan *requestOp),reqSent:              make(chan error, 1),reqTimeout:           make(chan *requestOp),}// Set defaults.if c.idgen == nil {c.idgen = randomIDGenerator()}// Launch the main loop.if !isHTTP {go c.dispatch(conn)}return c
}

Server:rpc服务器

type Server struct {services serviceRegistryidgen    func() IDmutex              sync.Mutexcodecs             map[ServerCodec]struct{}run                atomic.BoolbatchItemLimit     intbatchResponseLimit inthttpBodyLimit      intwsReadLimit        int64
}

serviceRegistry:用于管理服务器对外提供的服务

type serviceRegistry struct {mu       sync.Mutexservices map[string]service
}

service:对外提供的一个服务

type service struct {name          string               // name for servicecallbacks     map[string]*callback // registered handlerssubscriptions map[string]*callback // available subscriptions/notifications
}

callback:服务中的一个方法

type callback struct {fn          reflect.Value  // the functionrcvr        reflect.Value  // receiver object of method, set if fn is methodargTypes    []reflect.Type // input argument typeshasCtx      bool           // method's first argument is a context (not included in argTypes)errPos      int            // err return idx, of -1 when method cannot return errorisSubscribe bool           // true if this is a subscription callback
}

ServerCodec :网络编解码接口

type ServerCodec interface {peerInfo() PeerInforeadBatch() (msgs []*jsonrpcMessage, isBatch bool, err error)close()jsonWriter
}
http://www.dtcms.com/a/589447.html

相关文章:

  • 开源模型登顶?Kimi K2 Thinking 实测解析:它真能超越 GPT-5 吗?
  • 积分交易网站开发学院网站整改及建设情况报告
  • 影刀RPA实战:一键生成视频号销售日报,告别手工统计,效率提升10倍![特殊字符]
  • C语言算法:时间与空间复杂度分析
  • 最新选题-基于Hadopp和Spark的国漫推荐系统
  • Rust 练习册 :构建自然语言数学计算器
  • 中专旅游管理专业职业发展指南:从入门到精通的成长路径
  • 视频网站 建设绿化公司网站建设
  • 【Chrono】Cargo.toml 配置文件深度分析
  • 基于深度学习的车载视角路面病害检测系统【python源码+Pyqt5界面+数据集+训练代码】
  • 前端计算精度解决方案:big.js库
  • 珠海网站制作推广公司哪家好王野天个人简介
  • 微前端架构:JavaScript 隔离方案全解析(含 CSS 隔离)概要
  • 敏感性分析(Sensitivity Analysis)在机器学习中的应用详解
  • 北京怀柔做网站管理运营的公司最大的源码分享平台
  • 计算机网络自顶向下方法44——网络层 ICMP:因特网控制报文协议 网络控制与管理协议 管理信息库 NETCONF、YANG
  • Java面向对象实验:类的设计、构造方法重载与图形面积计算
  • 网站有哪些备案青海企业网站建设开发
  • 网站制作公司怎么找定制微信软件
  • autocad2025下载安装教程
  • 在页面上写C#(我的Blazor学习一)
  • 洛阳免费网站建设合肥建筑公司
  • 空间矢量PWM(SVPWM)实战:从原理到MATLAB仿真,优化逆变器输出谐波
  • 基于MATLAB的图像融合拼接GUI系统设计
  • 【Nginx优化】性能调优与安全配置
  • 海淘网站入口又拍 wordpress
  • 抖音审核机制、降权、养号、橱窗要求
  • 网站的页脚近期新闻消息
  • 1.python基础:基本概述
  • 常德百竞seo洛阳seo