ipc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "encoding/json"
  19. "net"
  20. )
  21. // CreateIPCListener creates an listener, on Unix platforms this is a unix socket, on Windows this is a named pipe
  22. func CreateIPCListener(endpoint string) (net.Listener, error) {
  23. return ipcListen(endpoint)
  24. }
  25. // ipcClient represent an IPC RPC client. It will connect to a given endpoint and tries to communicate with a node using
  26. // JSON serialization.
  27. type ipcClient struct {
  28. endpoint string
  29. conn net.Conn
  30. out *json.Encoder
  31. in *json.Decoder
  32. }
  33. // NewIPCClient create a new IPC client that will connect on the given endpoint. Messages are JSON encoded and encoded.
  34. // On Unix it assumes the endpoint is the full path to a unix socket, and Windows the endpoint is an identifier for a
  35. // named pipe.
  36. func NewIPCClient(endpoint string) (Client, error) {
  37. conn, err := newIPCConnection(endpoint)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &ipcClient{endpoint: endpoint, conn: conn, in: json.NewDecoder(conn), out: json.NewEncoder(conn)}, nil
  42. }
  43. // Send will serialize the given message and send it to the server.
  44. // When sending the message fails it will try to reconnect once and send the message again.
  45. func (client *ipcClient) Send(msg interface{}) error {
  46. if err := client.out.Encode(msg); err == nil {
  47. return nil
  48. }
  49. // retry once
  50. client.conn.Close()
  51. conn, err := newIPCConnection(client.endpoint)
  52. if err != nil {
  53. return err
  54. }
  55. client.conn = conn
  56. client.in = json.NewDecoder(conn)
  57. client.out = json.NewEncoder(conn)
  58. return client.out.Encode(msg)
  59. }
  60. // Recv will read a message from the connection and tries to parse it. It assumes the received message is JSON encoded.
  61. func (client *ipcClient) Recv(msg interface{}) error {
  62. return client.in.Decode(&msg)
  63. }
  64. // Close will close the underlying IPC connection
  65. func (client *ipcClient) Close() {
  66. client.conn.Close()
  67. }
  68. // SupportedModules will return the collection of offered RPC modules.
  69. func (client *ipcClient) SupportedModules() (map[string]string, error) {
  70. return SupportedModules(client)
  71. }