Skip to content

Commit

Permalink
Support DialOption
Browse files Browse the repository at this point in the history
  • Loading branch information
yutopp committed Jul 19, 2023
1 parent 2ae4e86 commit 5ff026e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,51 @@
package rtmp

import (
"context"
"net"

"github.com/pkg/errors"
)

func Dial(protocol, addr string, config *ConnConfig) (*ClientConn, error) {
return DialWithDialer(&net.Dialer{}, protocol, addr, config)
type dialOptions struct {
dialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
}

func DialWithDialer(dialer *net.Dialer, protocol, addr string, config *ConnConfig) (*ClientConn, error) {
func WithContextDialer(dialFunc func(context.Context, string, string) (net.Conn, error)) DialOption {
return func(o *dialOptions) {
o.dialFunc = dialFunc
}
}

type DialOption func(*dialOptions)

func Dial(protocol, addr string, config *ConnConfig, opts ...DialOption) (*ClientConn, error) {
opt := &dialOptions{
dialFunc: func(ctx context.Context, network, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, addr)
},
}
for _, o := range opts {
o(opt)
}

if protocol != "rtmp" {
return nil, errors.Errorf("Unknown protocol: %s", protocol)
}

rwc, err := dialer.Dial("tcp", addr)
// TODO: support ctx
rwc, err := opt.dialFunc(context.Background(), "tcp", addr)
if err != nil {
return nil, err
}

return newClientConnWithSetup(rwc, config)
}

func DialWithDialer(dialer *net.Dialer, protocol, addr string, config *ConnConfig) (*ClientConn, error) {
return Dial(protocol, addr, config, WithContextDialer(dialer.DialContext))
}

func makeValidAddr(addr string) (string, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
Expand Down

0 comments on commit 5ff026e

Please sign in to comment.