forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
controlmsgs.go
105 lines (87 loc) 路 2.91 KB
/
controlmsgs.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2019 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wire
import (
"io"
"time"
perunio "perun.network/go-perun/pkg/io"
)
func init() {
RegisterDecoder(Ping, func(r io.Reader) (Msg, error) { var m PingMsg; return &m, m.Decode(r) })
RegisterDecoder(Pong, func(r io.Reader) (Msg, error) { var m PongMsg; return &m, m.Decode(r) })
RegisterDecoder(Shutdown, func(r io.Reader) (Msg, error) { var m ShutdownMsg; return &m, m.Decode(r) })
}
// Since ping and pong messages are essentially the same, this is a common
// implementation for both.
type pingPongMsg struct {
Created time.Time
}
func (m pingPongMsg) Encode(writer io.Writer) error {
return perunio.Encode(writer, m.Created)
}
func (m *pingPongMsg) Decode(reader io.Reader) error {
return perunio.Decode(reader, &m.Created)
}
func newPingPongMsg() pingPongMsg {
// do not use `time.Now()` directly because it contains monotonic clock
// data specific to the current process which breaks, e.g.,
// `reflect.DeepEqual`, cf. "Marshal/Unmarshal functions are asymmetrical"
// https://github.com/golang/go/issues/19502
return pingPongMsg{Created: time.Unix(0, time.Now().UnixNano())}
}
// PingMsg is a ping request.
// It contains the time at which it was sent, so that the recipient can also
// measure the time it took to transmit the ping request.
type PingMsg struct {
pingPongMsg
}
// Type returns Ping.
func (m *PingMsg) Type() Type {
return Ping
}
// NewPingMsg creates a new Ping message.
func NewPingMsg() *PingMsg {
return &PingMsg{newPingPongMsg()}
}
// PongMsg is the response to a ping message.
// It contains the time at which it was sent, so that the recipient knows how
// long the ping request took to be transmitted, and how quickly the response
// was sent.
type PongMsg struct {
pingPongMsg
}
// Type returns Pong.
func (m *PongMsg) Type() Type {
return Pong
}
// NewPongMsg creates a new Pong message.
func NewPongMsg() *PongMsg {
return &PongMsg{newPingPongMsg()}
}
// ShutdownMsg is sent when orderly shutting down a connection.
type ShutdownMsg struct {
Reason string
}
// Encode implements msg.Encode.
func (m *ShutdownMsg) Encode(w io.Writer) error {
return perunio.Encode(w, m.Reason)
}
// Decode implements msg.Decode.
func (m *ShutdownMsg) Decode(r io.Reader) error {
return perunio.Decode(r, &m.Reason)
}
// Type implements msg.Type.
func (m *ShutdownMsg) Type() Type {
return Shutdown
}