-
Notifications
You must be signed in to change notification settings - Fork 13
/
connection_options.go
183 lines (159 loc) · 5.66 KB
/
connection_options.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2024 Blink Labs Software
//
// 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 ouroboros
import (
"log/slog"
"net"
"github.com/blinklabs-io/gouroboros/protocol/blockfetch"
"github.com/blinklabs-io/gouroboros/protocol/chainsync"
"github.com/blinklabs-io/gouroboros/protocol/keepalive"
"github.com/blinklabs-io/gouroboros/protocol/localstatequery"
"github.com/blinklabs-io/gouroboros/protocol/localtxmonitor"
"github.com/blinklabs-io/gouroboros/protocol/localtxsubmission"
"github.com/blinklabs-io/gouroboros/protocol/peersharing"
"github.com/blinklabs-io/gouroboros/protocol/txsubmission"
)
// ConnectionOptionFunc is a type that represents functions that modify the Connection config
type ConnectionOptionFunc func(*Connection)
// WithConnection specifies an existing connection to use. If none is provided, the Dial() function can be
// used to create one later
func WithConnection(conn net.Conn) ConnectionOptionFunc {
return func(c *Connection) {
c.conn = conn
}
}
// WithNetwork specifies the network
func WithNetwork(network Network) ConnectionOptionFunc {
return func(c *Connection) {
c.networkMagic = network.NetworkMagic
}
}
// WithNetworkMagic specifies the network magic value
func WithNetworkMagic(networkMagic uint32) ConnectionOptionFunc {
return func(c *Connection) {
c.networkMagic = networkMagic
}
}
// WithErrorChan specifies the error channel to use. If none is provided, one will be created
func WithErrorChan(errorChan chan error) ConnectionOptionFunc {
return func(c *Connection) {
c.errorChan = errorChan
}
}
// WithServer specifies whether to act as a server
func WithServer(server bool) ConnectionOptionFunc {
return func(c *Connection) {
c.server = server
}
}
// WithLogger specifies the slog.Logger to use. This is empty by default
func WithLogger(logger *slog.Logger) ConnectionOptionFunc {
return func(c *Connection) {
c.logger = logger
}
}
// WithNodeToNode specifies whether to use the node-to-node protocol. The default is to use node-to-client
func WithNodeToNode(nodeToNode bool) ConnectionOptionFunc {
return func(c *Connection) {
c.useNodeToNodeProto = nodeToNode
}
}
// WithKeepAlives specifies whether to use keep-alives. This is disabled by default
func WithKeepAlive(keepAlive bool) ConnectionOptionFunc {
return func(c *Connection) {
c.sendKeepAlives = keepAlive
}
}
// WithDelayMuxerStart specifies whether to delay the muxer start. This is useful if you need to take some
// custom actions before the muxer starts processing messages, generally when acting as a server
func WithDelayMuxerStart(delayMuxerStart bool) ConnectionOptionFunc {
return func(c *Connection) {
c.delayMuxerStart = delayMuxerStart
}
}
// WithDelayProtocolStart specifies whether to delay the start of the relevant mini-protocols. This is useful
// if you are maintaining lots of connections and want to reduce resource overhead by only starting particular
// protocols
func WithDelayProtocolStart(delayProtocolStart bool) ConnectionOptionFunc {
return func(c *Connection) {
c.delayProtocolStart = delayProtocolStart
}
}
// WithFullDuplex specifies whether to enable full-duplex mode when acting as a client
func WithFullDuplex(fullDuplex bool) ConnectionOptionFunc {
return func(c *Connection) {
c.fullDuplex = fullDuplex
}
}
// WithPeerSharing specifies whether to enable peer sharing. This affects both the protocol handshake and
// whether the PeerSharing protocol is enabled
func WithPeerSharing(peerSharing bool) ConnectionOptionFunc {
return func(c *Connection) {
c.peerSharingEnabled = peerSharing
}
}
// WithBlockFetchConfig specifies BlockFetch protocol config
func WithBlockFetchConfig(cfg blockfetch.Config) ConnectionOptionFunc {
return func(c *Connection) {
c.blockFetchConfig = &cfg
}
}
// WithChainSyncConfig secifies ChainSync protocol config
func WithChainSyncConfig(cfg chainsync.Config) ConnectionOptionFunc {
return func(c *Connection) {
c.chainSyncConfig = &cfg
}
}
// WithKeepAliveConfig specifies KeepAlive protocol config
func WithKeepAliveConfig(cfg keepalive.Config) ConnectionOptionFunc {
return func(c *Connection) {
c.keepAliveConfig = &cfg
}
}
// WithLocalStateQueryConfig specifies LocalStateQuery protocol config
func WithLocalStateQueryConfig(
cfg localstatequery.Config,
) ConnectionOptionFunc {
return func(c *Connection) {
c.localStateQueryConfig = &cfg
}
}
// WithLocalTxMonitorConfig specifies LocalTxMonitor protocol config
func WithLocalTxMonitorConfig(
cfg localtxmonitor.Config,
) ConnectionOptionFunc {
return func(c *Connection) {
c.localTxMonitorConfig = &cfg
}
}
// WithLocalTxSubmissionConfig specifies LocalTxSubmission protocol config
func WithLocalTxSubmissionConfig(
cfg localtxsubmission.Config,
) ConnectionOptionFunc {
return func(c *Connection) {
c.localTxSubmissionConfig = &cfg
}
}
// WithPeerSharingConfig specifies PeerSharing protocol config
func WithPeerSharingConfig(cfg peersharing.Config) ConnectionOptionFunc {
return func(c *Connection) {
c.peerSharingConfig = &cfg
}
}
// WithTxSubmissionConfig specifies TxSubmission protocol config
func WithTxSubmissionConfig(cfg txsubmission.Config) ConnectionOptionFunc {
return func(c *Connection) {
c.txSubmissionConfig = &cfg
}
}