2022-10-11 17:36:09 +08:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2023-04-05 16:15:59 +08:00
|
|
|
"git.hpds.cc/Component/network/frame"
|
|
|
|
"github.com/quic-go/quic-go"
|
|
|
|
"time"
|
2022-10-11 17:36:09 +08:00
|
|
|
|
|
|
|
"git.hpds.cc/Component/network/auth"
|
2023-04-05 16:15:59 +08:00
|
|
|
pkgtls "git.hpds.cc/Component/network/tls"
|
2022-10-11 17:36:09 +08:00
|
|
|
)
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
// clientOptions are the options for YoMo client.
|
|
|
|
type clientOptions struct {
|
|
|
|
observeDataTags []frame.Tag
|
|
|
|
quicConfig *quic.Config
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
credential *auth.Credential
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultClientOption() *clientOptions {
|
|
|
|
defaultQuicConfig := &quic.Config{
|
|
|
|
Versions: []quic.VersionNumber{quic.VersionDraft29, quic.Version1, quic.Version2},
|
|
|
|
MaxIdleTimeout: time.Second * 40,
|
|
|
|
KeepAlivePeriod: time.Second * 20,
|
|
|
|
MaxIncomingStreams: 1000,
|
|
|
|
MaxIncomingUniStreams: 1000,
|
|
|
|
HandshakeIdleTimeout: time.Second * 3,
|
|
|
|
InitialStreamReceiveWindow: 1024 * 1024 * 2,
|
|
|
|
InitialConnectionReceiveWindow: 1024 * 1024 * 2,
|
|
|
|
TokenStore: quic.NewLRUTokenStore(10, 5),
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &clientOptions{
|
|
|
|
observeDataTags: make([]frame.Tag, 0),
|
|
|
|
quicConfig: defaultQuicConfig,
|
|
|
|
tlsConfig: pkgtls.MustCreateClientTLSConfig(),
|
|
|
|
credential: auth.NewCredential(""),
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithObserveDataTags sets data tag list for the client.
|
2023-04-05 16:15:59 +08:00
|
|
|
func WithObserveDataTags(tags ...frame.Tag) ClientOption {
|
|
|
|
return func(o *clientOptions) {
|
|
|
|
o.observeDataTags = tags
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCredential sets the client credential method (used by client).
|
|
|
|
func WithCredential(payload string) ClientOption {
|
2023-04-05 16:15:59 +08:00
|
|
|
return func(o *clientOptions) {
|
|
|
|
o.credential = auth.NewCredential(payload)
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithClientTLSConfig sets tls config for the client.
|
|
|
|
func WithClientTLSConfig(tc *tls.Config) ClientOption {
|
2023-04-05 16:15:59 +08:00
|
|
|
return func(o *clientOptions) {
|
|
|
|
if tc != nil {
|
|
|
|
o.tlsConfig = tc
|
|
|
|
}
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithClientQuicConfig sets quic config for the client.
|
|
|
|
func WithClientQuicConfig(qc *quic.Config) ClientOption {
|
2023-04-05 16:15:59 +08:00
|
|
|
return func(o *clientOptions) {
|
|
|
|
o.quicConfig = qc
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
// ClientType is equal to StreamType.
|
|
|
|
type ClientType = StreamType
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ClientTypeSource is equal to StreamTypeSource.
|
|
|
|
ClientTypeSource ClientType = StreamTypeSource
|
|
|
|
|
|
|
|
// ClientTypeUpstreamEmitter is equal to StreamTypeUpstreamEmitter.
|
|
|
|
ClientTypeUpstreamEmitter ClientType = StreamTypeUpstreamEmitter
|
|
|
|
|
|
|
|
// ClientTypeStreamFunction is equal to StreamTypeStreamFunction.
|
|
|
|
ClientTypeStreamFunction ClientType = StreamTypeStreamFunction
|
|
|
|
)
|