-
Notifications
You must be signed in to change notification settings - Fork 12
/
socket_server.mq5
120 lines (94 loc) · 3.42 KB
/
socket_server.mq5
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
// ###################################################################
// Based on the awesome example from: https://www.mql5.com/en/blogs/post/706665
// ###################################################################
#property strict
#include <socket-library-mt4-mt5.mqh>
// Server socket
ServerSocket * glbServerSocket;
// Array of current clients
ClientSocket * glbClients[];
// Watch for need to create timer;
bool glbCreatedTimer = false;
// --------------------------------------------------------------------
// Initialisation - set up server socket
// --------------------------------------------------------------------
void OnInit()
{
// Create the server socket
glbServerSocket = new ServerSocket(5000, false);
if (glbServerSocket.Created()) {
Print("Server socket created");
// Note: this can fail if MT4/5 starts up
// with the EA already attached to a chart. Therefore,
// we repeat in OnTick()
glbCreatedTimer = EventSetMillisecondTimer(100);
} else {
Print("Server socket FAILED - is the port already in use?");
}
}
// --------------------------------------------------------------------
// Termination - free server socket and any clients
// --------------------------------------------------------------------
void OnDeinit(const int reason)
{
glbCreatedTimer = false;
// Delete all clients currently connected
for (int i = 0; i < ArraySize(glbClients); i++) {
delete glbClients[i];
}
// Free the server socket
delete glbServerSocket;
Print("Server socket terminated");
}
// --------------------------------------------------------------------
// Timer - accept new connections, and handle incoming data from clients
// --------------------------------------------------------------------
void OnTimer()
{
string recvMsg = "No data recived";
// Keep accepting any pending connections until Accept() returns NULL
ClientSocket * pNewClient = NULL;
do {
pNewClient = glbServerSocket.Accept();
if (pNewClient != NULL) {
int sz = ArraySize(glbClients);
ArrayResize(glbClients, sz + 1);
glbClients[sz] = pNewClient;
Print("Connection recived!");
pNewClient.Send("Connected to the MT5 Server!\r\n");
}
} while (pNewClient != NULL);
// Read incoming data from all current clients, watching for
// any which now appear to be dead
int ctClients = ArraySize(glbClients);
for (int i = ctClients - 1; i >= 0; i--) {
ClientSocket * pClient = glbClients[i];
//pNewClient.Send("Hellowwwwww\r\n");
// Keep reading CRLF-terminated lines of input from the client
// until we run out of data
string strCommand;
do {
strCommand = pClient.Receive();
if (strCommand != "") Print(strCommand);
// Free the server socket
if (strCommand == "q"){
delete glbServerSocket;
Print("Server socket terminated");
}
} while (strCommand != "");
if (!pClient.IsSocketConnected()) {
// Client is dead. Remove from array
delete pClient;
for (int j = i + 1; j < ctClients; j++) {
glbClients[j - 1] = glbClients[j];
}
ctClients--;
ArrayResize(glbClients, ctClients);
}
}
}
// Use OnTick() to watch for failure to create the timer in OnInit()
void OnTick()
{
if (!glbCreatedTimer) glbCreatedTimer = EventSetMillisecondTimer(100);
}