-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathSocket.mqh
235 lines (202 loc) · 5.66 KB
/
Socket.mqh
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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @file
* Implements class for socket connection.
*/
/**
* Socket class.
*/
class Socket {
protected:
// Target machine address. E.g., "localhost" or "127.0.0.1"
string address;
// Target machine port. E.g., 8080.
int port;
// Stores default timeout used for socket connection.
int timeout;
// Stores default number of retries used for socket connection.
int num_retries;
// Socket identifier.
int socket;
// Whether connection has been estabilished using TLS handshake.
bool is_tls;
public:
/**
* Constructor.
*/
Socket() : is_tls(false), socket(INVALID_HANDLE) {}
/**
* Destructor.
*/
~Socket() {
if (socket != INVALID_HANDLE) {
#ifdef __MQL5__
SocketClose(socket);
#endif
}
}
/**
* Makes a socket connection to the target machine.
*/
bool Connect(const string _address, const int _port = 0, const int _timeout = 3000, int _num_retries = 5) {
#ifdef __MQL5__
timeout = _timeout;
num_retries = _num_retries;
if (socket != INVALID_HANDLE) {
// Socket already connected?
if (IsConnected() && _address == address && port == _port) {
// Already connected to given address and port. Nothing to do.
return true;
} else {
SocketClose(socket);
socket = INVALID_HANDLE;
}
} else {
// Invalid socket, creating new one.
socket = SocketCreate();
int last_error = GetLastError();
if (last_error == 4014) {
Alert("Cannot create socket: ", "SocketCreate() is not allowed for call");
} else if (socket == -1) {
Alert("Cannot create socket!");
}
if (last_error != 0) {
// Someting bad happened and socket cannot be created.
Alert("Error ", last_error, " happened while tried to connect to ", _address, ":", IntegerToString(_port));
return false;
}
}
// Trying to connect to the given address and port.
while (_num_retries-- > 0) {
if (SocketConnect(socket, _address, _port, _timeout)) {
break;
}
}
if (!IsConnected()) {
// Cannot connect. Giving up.
return false;
}
// Checking if connection is over TLS.
string subject, issuer, serial, thumbprint;
datetime expiration;
is_tls = SocketTlsCertificate(socket, subject, issuer, serial, thumbprint, expiration);
return true;
#else
return false;
#endif;
}
/**
* Checks whether socket is still connected to the target machine.
*/
bool IsConnected() const {
#ifdef __MQL5__
return SocketIsConnected(socket);
#else
return false;
#endif
}
/**
* Ensures socket connection is still active. Returns false if connection cannot be reestabilished.
*/
bool EnsureConnected() {
if (!IsConnected()) {
if (!Connect(address, port, timeout, num_retries)) {
return false;
}
}
return true;
}
/**
* Checks whether there is any data be read.
*/
bool HasData() {
#ifdef __MQL5__
return ::SocketIsReadable(socket) > 0;
#else
return false;
#endif
}
/**
* Sends string through the socket.
*/
bool Send(const string text) {
unsigned char _buffer[];
int _buffer_length = StringToCharArray(text, _buffer, 0, WHOLE_ARRAY, CP_UTF8);
return Send(_buffer, _buffer_length);
}
/**
* Sends bytes through the socket.
*/
bool Send(const unsigned char& _buffer[], unsigned int _buffer_length) {
if (!EnsureConnected()) {
return false;
}
#ifdef __MQL5__
if (is_tls) {
return SocketTlsSend(socket, _buffer, _buffer_length) != -1;
} else {
return SocketSend(socket, _buffer, _buffer_length) != -1;
}
#else
return false;
#endif;
}
/**
* Reads string from the socket. Awaits given miliseconds before giving up.
*/
string ReadString(int _timeout_ms = 1000) {
#ifdef __MQL5__
if (!EnsureConnected()) {
return NULL;
}
string text = "";
unsigned int _data_length;
unsigned char _buffer[];
while ((_data_length = ::SocketIsReadable(socket)) > 0) {
if (!Read(_buffer, _data_length, _timeout_ms)) {
return "";
}
text += CharArrayToString(_buffer, 0, WHOLE_ARRAY, CP_UTF8);
}
return text;
#else
return "";
#endif
}
/**
* Reads bytes from the socket. Awaits given miliseconds before giving up.
*/
bool Read(unsigned char& _buffer[], unsigned int _buffer_max_length, unsigned int _timeout_ms = 1000) {
if (!EnsureConnected()) {
return false;
}
#ifdef __MQL5__
if (is_tls) {
return SocketTlsRead(socket, _buffer, _buffer_max_length) != -1;
} else {
return SocketRead(socket, _buffer, _buffer_max_length, _timeout_ms) != -1;
}
#else
return false;
#endif
}
};