ipc.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package comms
  17. import (
  18. "fmt"
  19. "math/rand"
  20. "net"
  21. "encoding/json"
  22. "github.com/ethereum/go-ethereum/rpc/codec"
  23. "github.com/ethereum/go-ethereum/rpc/shared"
  24. )
  25. type IpcConfig struct {
  26. Endpoint string
  27. }
  28. type ipcClient struct {
  29. endpoint string
  30. c net.Conn
  31. codec codec.Codec
  32. coder codec.ApiCoder
  33. }
  34. func (self *ipcClient) Close() {
  35. self.coder.Close()
  36. }
  37. func (self *ipcClient) Send(req interface{}) error {
  38. var err error
  39. if r, ok := req.(*shared.Request); ok {
  40. if err = self.coder.WriteResponse(r); err != nil {
  41. if _, ok := err.(*net.OpError); ok { // connection lost, retry once
  42. if err = self.reconnect(); err == nil {
  43. err = self.coder.WriteResponse(r)
  44. }
  45. }
  46. }
  47. return err
  48. }
  49. return fmt.Errorf("Invalid request (%T)", req)
  50. }
  51. func (self *ipcClient) Recv() (interface{}, error) {
  52. res, err := self.coder.ReadResponse()
  53. if err != nil {
  54. return nil, err
  55. }
  56. if r, ok := res.(shared.SuccessResponse); ok {
  57. return r.Result, nil
  58. }
  59. if r, ok := res.(shared.ErrorResponse); ok {
  60. return r.Error, nil
  61. }
  62. return res, err
  63. }
  64. func (self *ipcClient) SupportedModules() (map[string]string, error) {
  65. req := shared.Request{
  66. Id: 1,
  67. Jsonrpc: "2.0",
  68. Method: "modules",
  69. }
  70. if err := self.coder.WriteResponse(req); err != nil {
  71. return nil, err
  72. }
  73. res, err := self.coder.ReadResponse()
  74. if err != nil {
  75. return nil, err
  76. }
  77. if sucRes, ok := res.(shared.SuccessResponse); ok {
  78. data, _ := json.Marshal(sucRes.Result)
  79. modules := make(map[string]string)
  80. err = json.Unmarshal(data, &modules)
  81. if err == nil {
  82. return modules, nil
  83. }
  84. }
  85. return nil, fmt.Errorf("Invalid response")
  86. }
  87. // Create a new IPC client, UNIX domain socket on posix, named pipe on Windows
  88. func NewIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
  89. return newIpcClient(cfg, codec)
  90. }
  91. // Start IPC server
  92. func StartIpc(cfg IpcConfig, codec codec.Codec, offeredApi shared.EthereumApi) error {
  93. return startIpc(cfg, codec, offeredApi)
  94. }
  95. func newIpcConnId() int {
  96. return rand.Int() % 1000000
  97. }