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
|
package mt4
import (
"context"
"fmt"
"net"
"time"
"go.popov.link/metatrader4/internal/conn"
"go.popov.link/metatrader4/internal/proto"
)
// Client provides access to a MetaTrader4 server.
type Client struct {
addr string
port int
dialTimeout time.Duration
readTimeout time.Duration
writeTimeout time.Duration
autoClose bool
dialer net.Dialer
c *conn.Conn
}
// Option configures the Client.
type Option func(*Client)
// WithDialTimeout sets timeout for establishing connections.
func WithDialTimeout(d time.Duration) Option { return func(c *Client) { c.dialTimeout = d } }
// WithReadTimeout sets timeout for reading responses.
func WithReadTimeout(d time.Duration) Option { return func(c *Client) { c.readTimeout = d } }
// WithWriteTimeout sets timeout for writing requests.
func WithWriteTimeout(d time.Duration) Option { return func(c *Client) { c.writeTimeout = d } }
// WithAutoClose enables or disables automatic connection close after Execute.
func WithAutoClose(b bool) Option { return func(c *Client) { c.autoClose = b } }
// NewClient creates a new Client with optional configuration.
func NewClient(addr string, port int, opts ...Option) *Client {
cl := &Client{
addr: addr,
port: port,
dialTimeout: 5 * time.Second,
readTimeout: 5 * time.Second,
writeTimeout: 5 * time.Second,
autoClose: true,
}
for _, o := range opts {
o(cl)
}
return cl
}
// Connect establishes connection to the MT4 server if not already connected.
func (c *Client) Connect(ctx context.Context) error {
if c.c != nil {
return nil
}
address := fmt.Sprintf("%s:%d", c.addr, c.port)
cn, err := conn.Dial(ctx, address, c.dialTimeout)
if err != nil {
return err
}
c.c = cn
return nil
}
// Close closes underlying connection.
func (c *Client) Close() error {
if c.c == nil {
return nil
}
err := c.c.Close()
c.c = nil
return err
}
// Execute sends command with params to the server and returns decoded response.
func (c *Client) Execute(ctx context.Context, command string, params map[string]string) (string, error) {
if err := c.Connect(ctx); err != nil {
return "", fmt.Errorf("connect: %w", err)
}
encoded, err := proto.EncodeParams(params)
if err != nil {
if c.autoClose {
c.Close()
}
return "", err
}
req := proto.BuildRequest(command, encoded, c.autoClose)
if err := c.c.Send(ctx, req, c.writeTimeout); err != nil {
if c.autoClose {
c.Close()
}
return "", fmt.Errorf("send: %w", err)
}
respBytes, err := c.c.Receive(ctx, c.readTimeout)
if c.autoClose {
c.Close()
}
if err != nil {
return "", fmt.Errorf("receive: %w", err)
}
resp, err := proto.DecodeResponse(string(respBytes))
if err != nil {
return "", err
}
return resp, nil
}
|