forked from oliverpool/argo
/
rpc.go
81 lines (66 loc) · 1.95 KB
/
rpc.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Package rpc handles the rpc communication with the aria2 dameon
package rpc
import (
"encoding/json"
"net"
"github.com/oliverpool/argo"
)
// Request represents a JSON-RPC request sent by a client.
type Request struct {
// JSON-RPC protocol.
Version string `json:"jsonrpc"`
// A String containing the name of the method to be invoked.
Method string `json:"method"`
// Object to pass as request parameter to the method.
Params interface{} `json:"params"`
// The request id. This can be of any type. It is used to match the
// response with the request that it is replying to.
ID string `json:"id"`
}
// Response represents a JSON-RPC response to a request
type Response struct {
Result json.RawMessage `json:"result"`
ID string `json:"id"`
Error argo.ResponseError
}
// Poster allows to perform Requests
type Poster interface {
Post(Request) (Response, error)
Close() error
}
// Notification from aria2
type Notification struct {
// JSON-RPC protocol.
Version string `json:"jsonrpc"`
// A String containing the name of the method to be invoked.
Method string `json:"method"`
// Object to pass as request parameter to the method.
Params []struct {
GID argo.GID `json:"gid"` // GID of the download
} `json:"params"`
}
// Identifier returns the rpc method
func (r Notification) Identifier() string {
return r.Method
}
// GID gathers the GID of the notification
func (r Notification) GID() []argo.GID {
gid := make([]argo.GID, len(r.Params))
for i, g := range r.Params {
gid[i] = g.GID
}
return gid
}
func isClosedNetworkConnectionError(err error) bool {
if e, ok := err.(*net.OpError); ok {
return "use of closed network connection" == e.Err.Error()
}
return false
}
// ConvertClosedNetworkConnectionError converts a TCP "closed network connection" error to a argo.ErrConnIsClosed
func ConvertClosedNetworkConnectionError(err error) error {
if isClosedNetworkConnectionError(err) {
return argo.ErrConnIsClosed
}
return err
}