-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
commonOptions.go
840 lines (696 loc) · 23.8 KB
/
commonOptions.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
package options
import (
"context"
"fmt"
"net"
"time"
dtlsServer "github.com/plgd-dev/go-coap/v3/dtls/server"
"github.com/plgd-dev/go-coap/v3/message/pool"
"github.com/plgd-dev/go-coap/v3/mux"
"github.com/plgd-dev/go-coap/v3/net/blockwise"
"github.com/plgd-dev/go-coap/v3/net/client"
"github.com/plgd-dev/go-coap/v3/net/monitor/inactivity"
"github.com/plgd-dev/go-coap/v3/net/responsewriter"
"github.com/plgd-dev/go-coap/v3/options/config"
"github.com/plgd-dev/go-coap/v3/pkg/runner/periodic"
tcpClient "github.com/plgd-dev/go-coap/v3/tcp/client"
tcpServer "github.com/plgd-dev/go-coap/v3/tcp/server"
udpClient "github.com/plgd-dev/go-coap/v3/udp/client"
udpServer "github.com/plgd-dev/go-coap/v3/udp/server"
)
type ErrorFunc = config.ErrorFunc
type Handler interface {
tcpClient.HandlerFunc | udpClient.HandlerFunc
}
// HandlerFuncOpt handler function option.
type HandlerFuncOpt[H Handler] struct {
h H
}
func panicForInvalidHandlerFunc(t, exp any) {
panic(fmt.Errorf("invalid HandlerFunc type %T, expected %T", t, exp))
}
func (o HandlerFuncOpt[H]) TCPServerApply(cfg *tcpServer.Config) {
switch v := any(o.h).(type) {
case tcpClient.HandlerFunc:
cfg.Handler = v
default:
var exp tcpClient.HandlerFunc
panicForInvalidHandlerFunc(v, exp)
}
}
func (o HandlerFuncOpt[H]) TCPClientApply(cfg *tcpClient.Config) {
switch v := any(o.h).(type) {
case tcpClient.HandlerFunc:
cfg.Handler = v
default:
var exp tcpClient.HandlerFunc
panicForInvalidHandlerFunc(v, exp)
}
}
func (o HandlerFuncOpt[H]) UDPServerApply(cfg *udpServer.Config) {
switch v := any(o.h).(type) {
case udpClient.HandlerFunc:
cfg.Handler = v
default:
var exp udpClient.HandlerFunc
panicForInvalidHandlerFunc(v, exp)
}
}
func (o HandlerFuncOpt[H]) DTLSServerApply(cfg *dtlsServer.Config) {
switch v := any(o.h).(type) {
case udpClient.HandlerFunc:
cfg.Handler = v
default:
var exp udpClient.HandlerFunc
panicForInvalidHandlerFunc(v, exp)
}
}
func (o HandlerFuncOpt[H]) UDPClientApply(cfg *udpClient.Config) {
switch v := any(o.h).(type) {
case udpClient.HandlerFunc:
cfg.Handler = v
default:
var t udpClient.HandlerFunc
panicForInvalidHandlerFunc(v, t)
}
}
// WithHandlerFunc set handle for handling request's.
func WithHandlerFunc[H Handler](h H) HandlerFuncOpt[H] {
return HandlerFuncOpt[H]{h: h}
}
// HandlerFuncOpt handler function option.
type MuxHandlerOpt struct {
m mux.Handler
}
func (o MuxHandlerOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.Handler = mux.ToHandler[*tcpClient.Conn](o.m)
}
func (o MuxHandlerOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.Handler = mux.ToHandler[*tcpClient.Conn](o.m)
}
func (o MuxHandlerOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.Handler = mux.ToHandler[*udpClient.Conn](o.m)
}
func (o MuxHandlerOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.Handler = mux.ToHandler[*udpClient.Conn](o.m)
}
func (o MuxHandlerOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.Handler = mux.ToHandler[*udpClient.Conn](o.m)
}
// WithMux sets multiplexer for handle requests.
func WithMux(m mux.Handler) MuxHandlerOpt {
return MuxHandlerOpt{
m: m,
}
}
// ContextOpt handler function option.
type ContextOpt struct {
ctx context.Context
}
func (o ContextOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.Ctx = o.ctx
}
func (o ContextOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.Ctx = o.ctx
}
func (o ContextOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.Ctx = o.ctx
}
func (o ContextOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.Ctx = o.ctx
}
func (o ContextOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.Ctx = o.ctx
}
// WithContext sets parent context of server.
func WithContext(ctx context.Context) ContextOpt {
return ContextOpt{ctx: ctx}
}
// MaxMessageSizeOpt handler function option.
type MaxMessageSizeOpt struct {
maxMessageSize uint32
}
func (o MaxMessageSizeOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.MaxMessageSize = o.maxMessageSize
}
func (o MaxMessageSizeOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.MaxMessageSize = o.maxMessageSize
}
func (o MaxMessageSizeOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.MaxMessageSize = o.maxMessageSize
}
func (o MaxMessageSizeOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.MaxMessageSize = o.maxMessageSize
}
func (o MaxMessageSizeOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.MaxMessageSize = o.maxMessageSize
}
// WithMaxMessageSize limit size of processed message.
func WithMaxMessageSize(maxMessageSize uint32) MaxMessageSizeOpt {
return MaxMessageSizeOpt{maxMessageSize: maxMessageSize}
}
// ErrorsOpt errors option.
type ErrorsOpt struct {
errors ErrorFunc
}
func (o ErrorsOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.Errors = o.errors
}
func (o ErrorsOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.Errors = o.errors
}
func (o ErrorsOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.Errors = o.errors
}
func (o ErrorsOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.Errors = o.errors
}
func (o ErrorsOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.Errors = o.errors
}
// WithErrors set function for logging error.
func WithErrors(errors ErrorFunc) ErrorsOpt {
return ErrorsOpt{errors: errors}
}
// ProcessReceivedMessageOpt gopool option.
type ProcessReceivedMessageOpt[C responsewriter.Client] struct {
ProcessReceivedMessageFunc config.ProcessReceivedMessageFunc[C]
}
func panicForInvalidProcessReceivedMessageFunc(t, exp any) {
panic(fmt.Errorf("invalid ProcessReceivedMessageFunc type %T, expected %T", t, exp))
}
func (o ProcessReceivedMessageOpt[C]) TCPServerApply(cfg *tcpServer.Config) {
switch v := any(o.ProcessReceivedMessageFunc).(type) {
case config.ProcessReceivedMessageFunc[*tcpClient.Conn]:
cfg.ProcessReceivedMessage = v
default:
var t config.ProcessReceivedMessageFunc[*tcpClient.Conn]
panicForInvalidProcessReceivedMessageFunc(v, t)
}
}
func (o ProcessReceivedMessageOpt[C]) TCPClientApply(cfg *tcpClient.Config) {
switch v := any(o.ProcessReceivedMessageFunc).(type) {
case config.ProcessReceivedMessageFunc[*tcpClient.Conn]:
cfg.ProcessReceivedMessage = v
default:
var t config.ProcessReceivedMessageFunc[*tcpClient.Conn]
panicForInvalidProcessReceivedMessageFunc(v, t)
}
}
func (o ProcessReceivedMessageOpt[C]) UDPServerApply(cfg *udpServer.Config) {
switch v := any(o.ProcessReceivedMessageFunc).(type) {
case config.ProcessReceivedMessageFunc[*udpClient.Conn]:
cfg.ProcessReceivedMessage = v
default:
var t config.ProcessReceivedMessageFunc[*udpClient.Conn]
panicForInvalidProcessReceivedMessageFunc(v, t)
}
}
func (o ProcessReceivedMessageOpt[C]) DTLSServerApply(cfg *dtlsServer.Config) {
switch v := any(o.ProcessReceivedMessageFunc).(type) {
case config.ProcessReceivedMessageFunc[*udpClient.Conn]:
cfg.ProcessReceivedMessage = v
default:
var t config.ProcessReceivedMessageFunc[*udpClient.Conn]
panicForInvalidProcessReceivedMessageFunc(v, t)
}
}
func (o ProcessReceivedMessageOpt[C]) UDPClientApply(cfg *udpClient.Config) {
switch v := any(o.ProcessReceivedMessageFunc).(type) {
case config.ProcessReceivedMessageFunc[*udpClient.Conn]:
cfg.ProcessReceivedMessage = v
default:
var t config.ProcessReceivedMessageFunc[*udpClient.Conn]
panicForInvalidProcessReceivedMessageFunc(v, t)
}
}
func WithProcessReceivedMessageFunc[C responsewriter.Client](processReceivedMessageFunc config.ProcessReceivedMessageFunc[C]) ProcessReceivedMessageOpt[C] {
return ProcessReceivedMessageOpt[C]{ProcessReceivedMessageFunc: processReceivedMessageFunc}
}
type (
UDPOnInactive = func(cc *udpClient.Conn)
TCPOnInactive = func(cc *tcpClient.Conn)
)
type OnInactiveFunc interface {
UDPOnInactive | TCPOnInactive
}
func panicForInvalidOnInactiveFunc(t, exp any) {
panic(fmt.Errorf("invalid OnInactiveFunc type %T, expected %T", t, exp))
}
// KeepAliveOpt keepalive option.
type KeepAliveOpt[C OnInactiveFunc] struct {
timeout time.Duration
onInactive C
maxRetries uint32
}
func (o KeepAliveOpt[C]) toTCPCreateInactivityMonitor(onInactive TCPOnInactive) func() tcpClient.InactivityMonitor {
return func() tcpClient.InactivityMonitor {
keepalive := inactivity.NewKeepAlive(o.maxRetries, onInactive, func(cc *tcpClient.Conn, receivePong func()) (func(), error) {
return cc.AsyncPing(receivePong)
})
return inactivity.New(o.timeout/time.Duration(o.maxRetries+1), keepalive.OnInactive)
}
}
func (o KeepAliveOpt[C]) toUDPCreateInactivityMonitor(onInactive UDPOnInactive) func() udpClient.InactivityMonitor {
return func() udpClient.InactivityMonitor {
keepalive := inactivity.NewKeepAlive(o.maxRetries, onInactive, func(cc *udpClient.Conn, receivePong func()) (func(), error) {
return cc.AsyncPing(receivePong)
})
return inactivity.New(o.timeout/time.Duration(o.maxRetries+1), keepalive.OnInactive)
}
}
func (o KeepAliveOpt[C]) TCPServerApply(cfg *tcpServer.Config) {
switch onInactive := any(o.onInactive).(type) {
case TCPOnInactive:
cfg.CreateInactivityMonitor = o.toTCPCreateInactivityMonitor(onInactive)
default:
var t TCPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o KeepAliveOpt[C]) TCPClientApply(cfg *tcpClient.Config) {
switch onInactive := any(o.onInactive).(type) {
case TCPOnInactive:
cfg.CreateInactivityMonitor = o.toTCPCreateInactivityMonitor(onInactive)
default:
var t TCPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o KeepAliveOpt[C]) UDPServerApply(cfg *udpServer.Config) {
switch onInactive := any(o.onInactive).(type) {
case UDPOnInactive:
cfg.CreateInactivityMonitor = o.toUDPCreateInactivityMonitor(onInactive)
default:
var t UDPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o KeepAliveOpt[C]) DTLSServerApply(cfg *dtlsServer.Config) {
switch onInactive := any(o.onInactive).(type) {
case UDPOnInactive:
cfg.CreateInactivityMonitor = o.toUDPCreateInactivityMonitor(onInactive)
default:
var t UDPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o KeepAliveOpt[C]) UDPClientApply(cfg *udpClient.Config) {
switch onInactive := any(o.onInactive).(type) {
case UDPOnInactive:
cfg.CreateInactivityMonitor = o.toUDPCreateInactivityMonitor(onInactive)
default:
var t UDPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
// WithKeepAlive monitoring's client connection's.
func WithKeepAlive[C OnInactiveFunc](maxRetries uint32, timeout time.Duration, onInactive C) KeepAliveOpt[C] {
return KeepAliveOpt[C]{
maxRetries: maxRetries,
timeout: timeout,
onInactive: onInactive,
}
}
// InactivityMonitorOpt notifies when a connection was inactive for a given duration.
type InactivityMonitorOpt[C OnInactiveFunc] struct {
duration time.Duration
onInactive C
}
func (o InactivityMonitorOpt[C]) toTCPCreateInactivityMonitor(onInactive TCPOnInactive) func() tcpClient.InactivityMonitor {
return func() tcpClient.InactivityMonitor {
return inactivity.New(o.duration, onInactive)
}
}
func (o InactivityMonitorOpt[C]) toUDPCreateInactivityMonitor(onInactive UDPOnInactive) func() udpClient.InactivityMonitor {
return func() udpClient.InactivityMonitor {
return inactivity.New(o.duration, onInactive)
}
}
func (o InactivityMonitorOpt[C]) TCPServerApply(cfg *tcpServer.Config) {
switch onInactive := any(o.onInactive).(type) {
case TCPOnInactive:
cfg.CreateInactivityMonitor = o.toTCPCreateInactivityMonitor(onInactive)
default:
var t TCPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o InactivityMonitorOpt[C]) TCPClientApply(cfg *tcpClient.Config) {
switch onInactive := any(o.onInactive).(type) {
case TCPOnInactive:
cfg.CreateInactivityMonitor = o.toTCPCreateInactivityMonitor(onInactive)
default:
var t TCPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o InactivityMonitorOpt[C]) UDPServerApply(cfg *udpServer.Config) {
switch onInactive := any(o.onInactive).(type) {
case UDPOnInactive:
cfg.CreateInactivityMonitor = o.toUDPCreateInactivityMonitor(onInactive)
default:
var t UDPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o InactivityMonitorOpt[C]) DTLSServerApply(cfg *dtlsServer.Config) {
switch onInactive := any(o.onInactive).(type) {
case UDPOnInactive:
cfg.CreateInactivityMonitor = o.toUDPCreateInactivityMonitor(onInactive)
default:
var t UDPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
func (o InactivityMonitorOpt[C]) UDPClientApply(cfg *udpClient.Config) {
switch onInactive := any(o.onInactive).(type) {
case UDPOnInactive:
cfg.CreateInactivityMonitor = o.toUDPCreateInactivityMonitor(onInactive)
default:
var t UDPOnInactive
panicForInvalidOnInactiveFunc(onInactive, t)
}
}
// WithInactivityMonitor set deadline's for read operations over client connection.
func WithInactivityMonitor[C OnInactiveFunc](duration time.Duration, onInactive C) InactivityMonitorOpt[C] {
return InactivityMonitorOpt[C]{
duration: duration,
onInactive: onInactive,
}
}
// NetOpt network option.
type NetOpt struct {
net string
}
func (o NetOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.Net = o.net
}
func (o NetOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.Net = o.net
}
// WithNetwork define's tcp version (udp4, udp6, tcp) for client.
func WithNetwork(net string) NetOpt {
return NetOpt{net: net}
}
// PeriodicRunnerOpt function which is executed in every ticks
type PeriodicRunnerOpt struct {
periodicRunner periodic.Func
}
func (o PeriodicRunnerOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.PeriodicRunner = o.periodicRunner
}
func (o PeriodicRunnerOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.PeriodicRunner = o.periodicRunner
}
func (o PeriodicRunnerOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.PeriodicRunner = o.periodicRunner
}
func (o PeriodicRunnerOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.PeriodicRunner = o.periodicRunner
}
func (o PeriodicRunnerOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.PeriodicRunner = o.periodicRunner
}
// WithPeriodicRunner set function which is executed in every ticks.
func WithPeriodicRunner(periodicRunner periodic.Func) PeriodicRunnerOpt {
return PeriodicRunnerOpt{periodicRunner: periodicRunner}
}
// BlockwiseOpt network option.
type BlockwiseOpt struct {
transferTimeout time.Duration
enable bool
szx blockwise.SZX
}
func (o BlockwiseOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.BlockwiseEnable = o.enable
cfg.BlockwiseSZX = o.szx
cfg.BlockwiseTransferTimeout = o.transferTimeout
}
func (o BlockwiseOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.BlockwiseEnable = o.enable
cfg.BlockwiseSZX = o.szx
cfg.BlockwiseTransferTimeout = o.transferTimeout
}
func (o BlockwiseOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.BlockwiseEnable = o.enable
cfg.BlockwiseSZX = o.szx
cfg.BlockwiseTransferTimeout = o.transferTimeout
}
func (o BlockwiseOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.BlockwiseEnable = o.enable
cfg.BlockwiseSZX = o.szx
cfg.BlockwiseTransferTimeout = o.transferTimeout
}
func (o BlockwiseOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.BlockwiseEnable = o.enable
cfg.BlockwiseSZX = o.szx
cfg.BlockwiseTransferTimeout = o.transferTimeout
}
// WithBlockwise configure's blockwise transfer.
func WithBlockwise(enable bool, szx blockwise.SZX, transferTimeout time.Duration) BlockwiseOpt {
return BlockwiseOpt{
enable: enable,
szx: szx,
transferTimeout: transferTimeout,
}
}
type OnNewConnFunc interface {
tcpServer.OnNewConnFunc | udpServer.OnNewConnFunc
}
// OnNewConnOpt network option.
type OnNewConnOpt[F OnNewConnFunc] struct {
f F
}
func panicForInvalidOnNewConnFunc(t, exp any) {
panic(fmt.Errorf("invalid OnNewConnFunc type %T, expected %T", t, exp))
}
func (o OnNewConnOpt[F]) UDPServerApply(cfg *udpServer.Config) {
switch v := any(o.f).(type) {
case udpServer.OnNewConnFunc:
cfg.OnNewConn = v
default:
var exp udpServer.OnNewConnFunc
panicForInvalidOnNewConnFunc(v, exp)
}
}
func (o OnNewConnOpt[F]) DTLSServerApply(cfg *dtlsServer.Config) {
switch v := any(o.f).(type) {
case udpServer.OnNewConnFunc:
cfg.OnNewConn = v
default:
var exp udpServer.OnNewConnFunc
panicForInvalidOnNewConnFunc(v, exp)
}
}
func (o OnNewConnOpt[F]) TCPServerApply(cfg *tcpServer.Config) {
switch v := any(o.f).(type) {
case tcpServer.OnNewConnFunc:
cfg.OnNewConn = v
default:
var exp tcpServer.OnNewConnFunc
panicForInvalidOnNewConnFunc(v, exp)
}
}
// WithOnNewConn server's notify about new client connection.
func WithOnNewConn[F OnNewConnFunc](onNewConn F) OnNewConnOpt[F] {
return OnNewConnOpt[F]{
f: onNewConn,
}
}
// WithRequestMonitor
type WithRequestMonitorFunc interface {
tcpClient.RequestMonitorFunc | udpClient.RequestMonitorFunc
}
// WithRequestMonitorOpt network option.
type WithRequestMonitorOpt[F WithRequestMonitorFunc] struct {
f F
}
func panicForInvalidWithRequestMonitorFunc(t, exp any) {
panic(fmt.Errorf("invalid WithRequestMonitorFunc type %T, expected %T", t, exp))
}
func (o WithRequestMonitorOpt[F]) UDPServerApply(cfg *udpServer.Config) {
switch v := any(o.f).(type) {
case udpClient.RequestMonitorFunc:
cfg.RequestMonitor = v
default:
var exp udpClient.RequestMonitorFunc
panicForInvalidWithRequestMonitorFunc(v, exp)
}
}
func (o WithRequestMonitorOpt[F]) DTLSServerApply(cfg *dtlsServer.Config) {
switch v := any(o.f).(type) {
case udpClient.RequestMonitorFunc:
cfg.RequestMonitor = v
default:
var exp udpClient.RequestMonitorFunc
panicForInvalidWithRequestMonitorFunc(v, exp)
}
}
func (o WithRequestMonitorOpt[F]) TCPServerApply(cfg *tcpServer.Config) {
switch v := any(o.f).(type) {
case tcpClient.RequestMonitorFunc:
cfg.RequestMonitor = v
default:
var exp tcpClient.RequestMonitorFunc
panicForInvalidWithRequestMonitorFunc(v, exp)
}
}
// WithRequestMonitor enables request monitoring for the connection.
// It is called for each CoAP message received from the peer before it is processed.
// If it returns an error, the connection is closed.
// If it returns true, the message is dropped.
func WithRequestMonitor[F WithRequestMonitorFunc](requestMonitor F) WithRequestMonitorOpt[F] {
return WithRequestMonitorOpt[F]{
f: requestMonitor,
}
}
// CloseSocketOpt close socket option.
type CloseSocketOpt struct{}
func (o CloseSocketOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.CloseSocket = true
}
func (o CloseSocketOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.CloseSocket = true
}
// WithCloseSocket closes socket at the close connection.
func WithCloseSocket() CloseSocketOpt {
return CloseSocketOpt{}
}
// DialerOpt dialer option.
type DialerOpt struct {
dialer *net.Dialer
}
func (o DialerOpt) UDPClientApply(cfg *udpClient.Config) {
if o.dialer != nil {
cfg.Dialer = o.dialer
}
}
func (o DialerOpt) TCPClientApply(cfg *tcpClient.Config) {
if o.dialer != nil {
cfg.Dialer = o.dialer
}
}
// WithDialer set dialer for dial.
func WithDialer(dialer *net.Dialer) DialerOpt {
return DialerOpt{
dialer: dialer,
}
}
// ConnectionCacheOpt network option.
type MessagePoolOpt struct {
messagePool *pool.Pool
}
func (o MessagePoolOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.MessagePool = o.messagePool
}
func (o MessagePoolOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.MessagePool = o.messagePool
}
func (o MessagePoolOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.MessagePool = o.messagePool
}
func (o MessagePoolOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.MessagePool = o.messagePool
}
func (o MessagePoolOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.MessagePool = o.messagePool
}
// WithMessagePool configure's message pool for acquire/releasing coap messages
func WithMessagePool(messagePool *pool.Pool) MessagePoolOpt {
return MessagePoolOpt{
messagePool: messagePool,
}
}
// GetTokenOpt token option.
type GetTokenOpt struct {
getToken client.GetTokenFunc
}
func (o GetTokenOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.GetToken = o.getToken
}
func (o GetTokenOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.GetToken = o.getToken
}
func (o GetTokenOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.GetToken = o.getToken
}
func (o GetTokenOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.GetToken = o.getToken
}
func (o GetTokenOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.GetToken = o.getToken
}
// WithGetToken set function for generating tokens.
func WithGetToken(getToken client.GetTokenFunc) GetTokenOpt {
return GetTokenOpt{getToken: getToken}
}
// LimitClientParallelRequestOpt limit's number of parallel requests from client.
type LimitClientParallelRequestOpt struct {
limitClientParallelRequests int64
}
func (o LimitClientParallelRequestOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.LimitClientParallelRequests = o.limitClientParallelRequests
}
func (o LimitClientParallelRequestOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.LimitClientParallelRequests = o.limitClientParallelRequests
}
func (o LimitClientParallelRequestOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.LimitClientParallelRequests = o.limitClientParallelRequests
}
func (o LimitClientParallelRequestOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.LimitClientParallelRequests = o.limitClientParallelRequests
}
func (o LimitClientParallelRequestOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.LimitClientParallelRequests = o.limitClientParallelRequests
}
// WithLimitClientParallelRequestOpt limits number of parallel requests from client. (default: 1)
func WithLimitClientParallelRequest(limitClientParallelRequests int64) LimitClientParallelRequestOpt {
return LimitClientParallelRequestOpt{limitClientParallelRequests: limitClientParallelRequests}
}
// LimitClientEndpointParallelRequestOpt limit's number of parallel requests to endpoint by client.
type LimitClientEndpointParallelRequestOpt struct {
limitClientEndpointParallelRequests int64
}
func (o LimitClientEndpointParallelRequestOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.LimitClientEndpointParallelRequests = o.limitClientEndpointParallelRequests
}
func (o LimitClientEndpointParallelRequestOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.LimitClientEndpointParallelRequests = o.limitClientEndpointParallelRequests
}
func (o LimitClientEndpointParallelRequestOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.LimitClientEndpointParallelRequests = o.limitClientEndpointParallelRequests
}
func (o LimitClientEndpointParallelRequestOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.LimitClientEndpointParallelRequests = o.limitClientEndpointParallelRequests
}
func (o LimitClientEndpointParallelRequestOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.LimitClientEndpointParallelRequests = o.limitClientEndpointParallelRequests
}
// WithLimitClientEndpointParallelRequest limits number of parallel requests to endpoint from client. (default: 1)
func WithLimitClientEndpointParallelRequest(limitClientEndpointParallelRequests int64) LimitClientEndpointParallelRequestOpt {
return LimitClientEndpointParallelRequestOpt{limitClientEndpointParallelRequests: limitClientEndpointParallelRequests}
}
// ReceivedMessageQueueSizeOpt limit's message queue size for received messages.
type ReceivedMessageQueueSizeOpt struct {
receivedMessageQueueSize int
}
func (o ReceivedMessageQueueSizeOpt) TCPServerApply(cfg *tcpServer.Config) {
cfg.ReceivedMessageQueueSize = o.receivedMessageQueueSize
}
func (o ReceivedMessageQueueSizeOpt) TCPClientApply(cfg *tcpClient.Config) {
cfg.ReceivedMessageQueueSize = o.receivedMessageQueueSize
}
func (o ReceivedMessageQueueSizeOpt) UDPServerApply(cfg *udpServer.Config) {
cfg.ReceivedMessageQueueSize = o.receivedMessageQueueSize
}
func (o ReceivedMessageQueueSizeOpt) DTLSServerApply(cfg *dtlsServer.Config) {
cfg.ReceivedMessageQueueSize = o.receivedMessageQueueSize
}
func (o ReceivedMessageQueueSizeOpt) UDPClientApply(cfg *udpClient.Config) {
cfg.ReceivedMessageQueueSize = o.receivedMessageQueueSize
}
// WithReceivedMessageQueueSize limit's message queue size for received messages. (default: 16)
func WithReceivedMessageQueueSize(receivedMessageQueueSize int) ReceivedMessageQueueSizeOpt {
return ReceivedMessageQueueSizeOpt{receivedMessageQueueSize: receivedMessageQueueSize}
}