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