forked from fuyao-w/papillon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
77 lines (66 loc) · 1.62 KB
/
transport.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
package papillon
import (
"bufio"
"encoding/json"
"net"
"time"
)
func (c rpcType) String() string {
switch c {
case RpcVoteRequest:
return "VoteRequest"
case RpcAppendEntryPipeline:
return "AppendEntryPipeline"
case RpcAppendEntry:
return "AppendEntry"
case RpcInstallSnapshot:
return "InstallSnapshot"
case RpcFastTimeout:
return "FastTimeout"
default:
return unknown
}
}
const (
RpcVoteRequest rpcType = iota + 1
RpcAppendEntry
RpcAppendEntryPipeline
RpcInstallSnapshot
RpcFastTimeout
)
const (
rpcMaxPipeline = 128
// DefaultTimeoutScale is the default TimeoutScale in a NetworkTransport.
DefaultTimeoutScale = 256 * 1024 // 256KB
)
// NetLayer 网络层抽象
type NetLayer interface {
net.Listener
// Dial is used to create a new outgoing connection
Dial(peer ServerAddr, timeout time.Duration) (net.Conn, error)
}
type (
WithPeers interface {
Connect(addr ServerAddr, rpc RpcInterface)
Disconnect(addr ServerAddr)
DisconnectAll()
}
fastPath func(cb *RPC) bool
PackageParser interface {
Encode(writer *bufio.Writer, rpcType rpcType, data []byte) (err error)
Decode(reader *bufio.Reader) (rpcType, []byte, error)
}
RpcConvert interface {
Deserialization(data []byte, i interface{}) error
Serialization(i interface{}) (bytes []byte, err error)
}
// JsonRpcHandler 提供 json 的序列化能力
JsonRpcHandler struct{}
)
var defaultCmdConverter = new(JsonRpcHandler)
func (j *JsonRpcHandler) Deserialization(data []byte, i interface{}) error {
return json.Unmarshal(data, i)
}
func (j *JsonRpcHandler) Serialization(i interface{}) (bytes []byte, err error) {
return json.Marshal(i)
}