本文整理匯總了Golang中github.com/ethereum/go-ethereum/rpc/codec.Codec類的典型用法代碼示例。如果您正苦於以下問題:Golang Codec類的具體用法?Golang Codec怎麽用?Golang Codec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Codec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewMinerApi
// create a new miner api instance
func NewMinerApi(ethereum *eth.Ethereum, coder codec.Codec) *minerApi {
return &minerApi{
ethereum: ethereum,
methods: MinerMapping,
codec: coder.New(nil),
}
}
示例2: NewHttpClient
// Create a new in process client
func NewHttpClient(cfg HttpConfig, c codec.Codec) *httpClient {
return &httpClient{
address: cfg.ListenAddress,
port: cfg.ListenPort,
codec: c.New(nil),
}
}
示例3: NewWeb3Api
// create a new web3 api instance
func NewWeb3Api(xeth *xeth.XEth, coder codec.Codec) *web3Api {
return &web3Api{
xeth: xeth,
methods: Web3Mapping,
codec: coder.New(nil),
}
}
示例4: NewTxPoolApi
// create a new txpool api instance
func NewTxPoolApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *txPoolApi {
return &txPoolApi{
xeth: xeth,
ethereum: eth,
methods: txpoolMapping,
codec: coder.New(nil),
}
}
示例5: NewDebugApi
// create a new debug api instance
func NewDebugApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *debugApi {
return &debugApi{
xeth: xeth,
ethereum: ethereum,
methods: DebugMapping,
codec: coder.New(nil),
}
}
示例6: NewShhApi
// create a new whisper api instance
func NewShhApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *shhApi {
return &shhApi{
xeth: xeth,
ethereum: eth,
methods: shhMapping,
codec: coder.New(nil),
}
}
示例7: NewPersonalApi
// create a new net api instance
func NewPersonalApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *personalApi {
return &personalApi{
xeth: xeth,
ethereum: eth,
methods: personalMapping,
codec: coder.New(nil),
}
}
示例8: NewNetApi
// create a new net api instance
func NewNetApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *netApi {
return &netApi{
xeth: xeth,
ethereum: eth,
methods: netMapping,
codec: coder.New(nil),
}
}
示例9: handle
func handle(id int, conn net.Conn, api shared.EthereumApi, c codec.Codec) {
codec := c.New(conn)
defer func() {
if r := recover(); r != nil {
glog.Errorf("panic: %v\n", r)
}
codec.Close()
}()
for {
requests, isBatch, err := codec.ReadRequest()
if err == io.EOF {
return
} else if err != nil {
glog.V(logger.Debug).Infof("Closed IPC Conn %06d recv err - %v\n", id, err)
return
}
if isBatch {
responses := make([]*interface{}, len(requests))
responseCount := 0
for _, req := range requests {
res, err := api.Execute(req)
if req.Id != nil {
rpcResponse := shared.NewRpcResponse(req.Id, req.Jsonrpc, res, err)
responses[responseCount] = rpcResponse
responseCount += 1
}
}
err = codec.WriteResponse(responses[:responseCount])
if err != nil {
glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
return
}
} else {
var rpcResponse interface{}
res, err := api.Execute(requests[0])
rpcResponse = shared.NewRpcResponse(requests[0].Id, requests[0].Jsonrpc, res, err)
err = codec.WriteResponse(rpcResponse)
if err != nil {
glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
return
}
}
}
}
示例10: newIpcClient
func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
c, err := net.DialUnix("unix", nil, &net.UnixAddr{cfg.Endpoint, "unix"})
if err != nil {
return nil, err
}
return &ipcClient{cfg.Endpoint, c, codec, codec.New(c)}, nil
}
示例11: NewAdminApi
// create a new admin api instance
func NewAdminApi(xeth *xeth.XEth, ethereum *eth.Ethereum, codec codec.Codec) *adminApi {
return &adminApi{
xeth: xeth,
ethereum: ethereum,
codec: codec,
coder: codec.New(nil),
}
}
示例12: newIpcClient
func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
c, err := Dial(cfg.Endpoint)
if err != nil {
return nil, err
}
return &ipcClient{cfg.Endpoint, codec, codec.New(c)}, nil
}
示例13: gethHttpHandler
func gethHttpHandler(codec codec.Codec, a shared.EthereumApi) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Limit request size to resist DoS
if req.ContentLength > maxHttpSizeReqLength {
err := fmt.Errorf("Request too large")
response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err)
httpSend(w, &response)
return
}
defer req.Body.Close()
payload, err := ioutil.ReadAll(req.Body)
if err != nil {
err := fmt.Errorf("Could not read request body")
response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err)
httpSend(w, &response)
return
}
c := codec.New(nil)
var rpcReq shared.Request
if err = c.Decode(payload, &rpcReq); err == nil {
reply, err := a.Execute(&rpcReq)
res := shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
httpSend(w, &res)
return
}
var reqBatch []shared.Request
if err = c.Decode(payload, &reqBatch); err == nil {
resBatch := make([]*interface{}, len(reqBatch))
resCount := 0
for i, rpcReq := range reqBatch {
reply, err := a.Execute(&rpcReq)
if rpcReq.Id != nil { // this leaves nil entries in the response batch for later removal
resBatch[i] = shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
resCount += 1
}
}
// make response omitting nil entries
resBatch = resBatch[:resCount]
httpSend(w, resBatch)
return
}
// invalid request
err = fmt.Errorf("Could not decode request")
res := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32600, err)
httpSend(w, res)
})
}
示例14: newIpcClient
func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
c, err := Dial(cfg.Endpoint)
if err != nil {
return nil, err
}
coder := codec.New(c)
msg := shared.Request{
Id: 0,
Method: useragent.EnableUserAgentMethod,
Jsonrpc: shared.JsonRpcVersion,
Params: []byte("[]"),
}
coder.WriteResponse(msg)
coder.Recv()
return &ipcClient{cfg.Endpoint, c, codec, coder}, nil
}
示例15: startIpc
func startIpc(cfg IpcConfig, codec codec.Codec, api shared.EthereumApi) error {
os.Remove(cfg.Endpoint) // in case it still exists from a previous run
l, err := Listen(cfg.Endpoint)
if err != nil {
return err
}
os.Chmod(cfg.Endpoint, 0600)
go func() {
for {
conn, err := l.Accept()
if err != nil {
glog.V(logger.Error).Infof("Error accepting ipc connection - %v\n", err)
continue
}
go func(conn net.Conn) {
codec := codec.New(conn)
for {
req, err := codec.ReadRequest()
if err == io.EOF {
codec.Close()
return
} else if err != nil {
glog.V(logger.Error).Infof("IPC recv err - %v\n", err)
codec.Close()
return
}
var rpcResponse interface{}
res, err := api.Execute(req)
rpcResponse = shared.NewRpcResponse(req.Id, req.Jsonrpc, res, err)
err = codec.WriteResponse(rpcResponse)
if err != nil {
glog.V(logger.Error).Infof("IPC send err - %v\n", err)
codec.Close()
return
}
}
}(conn)
}
}()
glog.V(logger.Info).Infof("IPC service started (%s)\n", cfg.Endpoint)
return nil
}