opened
Armagetron
username_0
I'm running a telegram bot with this library on my raspberry pi at home. When my internet connection gets lost (e.g. dsl reconnect, un-plug the pi and plug it in again) the connection to the telegram server is lost.

The LongPoll or HttpClient::makeRequest function does not run into a timeout, keeping the connection open waiting for a response never to arrive.

I implemented a timeout but my solution is not cross-platform. Maybe someone could implement a proper timeout using boost-functions?
Proper timeout
issue
created
Armagetron
username_0
Since I have been asked for the code, here is the diff.
```
diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp
index f7f4a6b..b768f5e 100644
--- a/src/net/HttpClient.cpp
+++ b/src/net/HttpClient.cpp
@@ -36,7 +36,7 @@ HttpClient& HttpClient::getInstance() {
 }
 
 string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) {
-	ssl::context context(ssl::context::sslv23);
+	ssl::context context(ssl::context::tlsv12);
 	context.set_default_verify_paths();
 
 	ssl::stream<tcp::socket> socket(_ioService, context);
@@ -51,6 +51,32 @@ string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) {
 
 	string requestText = HttpParser::getInstance().generateRequest(url, args, false);
 	write(socket, buffer(requestText.c_str(), requestText.length()));
+        
+        fd_set fileDescriptorSet;
+        struct timeval timeStruct;
+        
+        // set the timeout to 30 seconds
+        timeStruct.tv_sec = 70;
+        timeStruct.tv_usec = 0;
+        FD_ZERO(&fileDescriptorSet);
+        
+        // We'll need to get the underlying native socket for this select call, in order
+        // to add a simple timeout on the read:
+        
+        int nativeSocket = socket.lowest_layer().native();
+        
+        FD_SET(nativeSocket,&fileDescriptorSet);        
+        select(nativeSocket+1,&fileDescriptorSet,NULL,NULL,&timeStruct);
+        
+        if(!FD_ISSET(nativeSocket,&fileDescriptorSet)){ // timeout
+            
+            std::string sMsg("TIMEOUT on read client data. Client IP: ");
+            
+            sMsg.append(socket.next_layer().remote_endpoint().address().to_string());
+            _ioService.reset();
+            
+            throw std::exception();
+        }        
 
 	string response;
 	char buff[1024];
```
comment
created
rwmoekoe
username_1
i have this solved using async_read_some() and ioservice.run_one_until(timepoint)

https://github.com/username_2/tgbot-cpp/issues/160
comment
closed
reo7sp
username_2
issue