From 1253124e1807bfb36da5e173a1f6607dd07f5de4 Mon Sep 17 00:00:00 2001 From: Sergey Lukin Date: Thu, 2 Jun 2022 03:35:03 +0300 Subject: [PATCH] feat: add timeout option --- README.md | 25 +++++++++++++++++-------- src/mql4-http.mqh | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8e1cf2f..e538e62 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -Overview --------- +## Overview This is a simple MQL4 wrapper that uses Windows native wininet.dll and shell32.dll libraries and is supported by MT4 build 600 or newer. @@ -11,8 +10,7 @@ Following features are currently supported: - open HTTP(S) URI in default system's web browser -How to use ----------- +## How to use `mql4-http` is using system's dll libraries so make sure you enable DLL imports in MT4 before running it (Tools -> Options -> Expert Advisors -> Allow DLL imports) @@ -30,6 +28,19 @@ int start () { } ``` +In order to set timeout, use second argument (only "`n`s" format is supported): + +```c +#include + +int start () { + + httpGET("http://example.com/long-running-task.php", "60s"); + + return(0); +} +``` + ```c #include @@ -43,8 +54,7 @@ int start () { You can also [watch this video demonstration](http://screencast.com/t/UVMAlgCjJ2) -Credits -------- +## Credits This library was based on following great resources: @@ -52,8 +62,7 @@ This library was based on following great resources: - EasyXML parser: http://www.mql5.com/code/1998 -License -------- +## License This is free and unencumbered software released into the public domain. diff --git a/src/mql4-http.mqh b/src/mql4-http.mqh index 2cb4068..7745130 100644 --- a/src/mql4-http.mqh +++ b/src/mql4-http.mqh @@ -30,6 +30,12 @@ int InternetOpenW( string sProxyBypass="", int lFlags=0 ); +int InternetSetOptionW( + int hInternet, + int dwOption, + int & lpBuffer[], + int dwBufferLength +); int InternetOpenUrlW( int hInternetSession, string sUrl, @@ -52,6 +58,7 @@ int InternetCloseHandle( #define INTERNET_FLAG_RELOAD 0x80000000 #define INTERNET_FLAG_NO_CACHE_WRITE 0x04000000 #define INTERNET_FLAG_PRAGMA_NOCACHE 0x00000100 +#define INTERNET_OPTION_CONNECT_TIMEOUT 2 int hSession_IEType; int hSession_Direct; @@ -82,9 +89,19 @@ int hSession(bool Direct) } } -string httpGET(string strUrl) +string httpGET(string strUrl, string timeoutInSeconds = "") { int handler = hSession(false); + if (timeoutInSeconds != "") + { + int timeout=StrToInteger(timeoutInSeconds) * 1000; + int option[] = {0}; + option[0] = timeout; + InternetSetOptionW(handler, + INTERNET_OPTION_CONNECT_TIMEOUT, + option, 4); + } + int response = InternetOpenUrlW(handler, strUrl, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE |