forked from chenzhijie/go-web3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.go
45 lines (37 loc) · 711 Bytes
/
ipc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package transport
import (
"encoding/json"
"net"
)
func newIPC(addr string) (Transport, error) {
conn, err := net.Dial("unix", addr)
if err != nil {
return nil, err
}
codec := &ipcCodec{
buf: json.RawMessage{},
conn: conn,
dec: json.NewDecoder(conn),
}
return newStream(codec)
}
type ipcCodec struct {
buf json.RawMessage
conn net.Conn
dec *json.Decoder
}
func (i *ipcCodec) Close() error {
return i.conn.Close()
}
func (i *ipcCodec) Read(b []byte) ([]byte, error) {
i.buf = i.buf[:0]
if err := i.dec.Decode(&i.buf); err != nil {
return nil, err
}
b = append(b, i.buf...)
return b, nil
}
func (i *ipcCodec) Write(b []byte) error {
_, err := i.conn.Write(b)
return err
}