-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
107 lines (90 loc) Β· 2.16 KB
/
client.go
File metadata and controls
107 lines (90 loc) Β· 2.16 KB
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
package eventale
import (
"context"
"fmt"
"log/slog"
"net"
"os"
"time"
eventalepb "github.com/nohns/eventale/gen/v1"
"github.com/nohns/eventale/internal/connection"
"github.com/nohns/eventale/internal/frame"
"github.com/nohns/eventale/internal/wire"
"google.golang.org/protobuf/proto"
)
var _networkTimeout = 30 * time.Second
type frameDecoder interface {
Decode(ctx context.Context) (*frame.Frame, error)
}
type Client struct {
conn net.Conn
frmer frameDecoder
}
func WithContext(ctx context.Context) dialOpt {
return dialOptFunc(func(opts *dialOpts) {
if ctx == nil {
return
}
opts.ctx = ctx
})
}
func Dial(address string, options ...dialOpt) (*Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), _networkTimeout)
defer cancel()
opts := dialOpts{
ctx: ctx,
}
for _, opt := range options {
opt.apply(&opts)
}
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, err
}
c := &connection.Conn{
NetConn: conn,
Logger: slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})),
}
// Send hello and receive server hello
fmt.Print("send client hello\n")
frm, err := frame.Make(frame.FrameKindClientHello, frame.WithProto(&eventalepb.WireClientHello{
ClientVersion: &eventalepb.SemanticVersion{
Major: 0,
Minor: 0,
Patch: 1,
},
}))
if err != nil {
return nil, err
}
if err := c.Send(context.Background(), frm); err != nil {
return nil, err
}
frm, err = frame.NewDecoder(conn).Decode()
if err != nil {
return nil, fmt.Errorf("client dial: %v", err)
}
if frm.Kind != frame.FrameKindServerHello {
return nil, fmt.Errorf("unexpected frame kind %d after client hello", frm.Kind)
}
var srvhello eventalepb.WireServerHello
if err := proto.Unmarshal(frm.Payload, &srvhello); err != nil {
return nil, fmt.Errorf("client dial: %v", err)
}
fmt.Printf("recv server hello - %s\n", wire.SemVerStr(srvhello.ServerVersion))
return &Client{
conn: conn,
}, nil
}
type dialOpts struct {
ctx context.Context
}
type dialOpt interface {
apply(opts *dialOpts)
}
type dialOptFunc func(opts *dialOpts)
func (f dialOptFunc) apply(opts *dialOpts) {
f(opts)
}