Skip to content

Possible segmentation fault on GetRequest? #55

Description

@neobits

Hello! I'm currently working on a app that needs to access the internet.
I'm using the addon first to "ping" a server to see if there's network connection available. This is done in a separate thread.
But one in a while, the app terminates with a segmentation fault. I done a backtrace using gdb, but no result other than 1 stack was found. Maybe I'm doing some errors, so I would like to know if these calls can be handled in a separate thread.

Here is the coude:

ConnectionHandler.h

class ConnectionHandler : public ofThread
{
public:
	ConnectionHandler();

	void setup(ConfigurationManager *config);
	void threadedFunction();
	bool pingServer();
	bool isConnected();

private:
	ConfigurationManager	*config;
	std::map<std::string, std::string> headers;
	ofxHTTP::ClientSessionSettings sessionSettings;
	bool connected;
	long sleepTime;
};

ConnectionHandler.cpp

#include "ConnectionHandler.h"

ConnectionHandler::ConnectionHandler()
{

}
//--------------------------------------------------------------------------
void ConnectionHandler::setup(ConfigurationManager *config) 
{
	startThread(false);
	setThreadName("ConnectionHandlerThread");
	this->config = config;
	sleepTime = config->systemConfig.getCheckConnectionTime() * 1000;
	sessionSettings.setKeepAliveTimeout(Poco::Timespan::SECONDS * 10);
}
//--------------------------------------------------------------------------
bool ConnectionHandler::isConnected() 
{
	return connected;
}
//--------------------------------------------------------------------------
void ConnectionHandler::threadedFunction()
{
	while (isThreadRunning())
	{
		// Try ping and check response
		connected = pingServer();

		ofLogNotice("ConnectionHandler::threadedFunction")
			<< "Ping " << config->serverConfig.pbDNSServer
			<< " result " << std::boolalpha << connected;

		sleep(sleepTime);
	}
}
//--------------------------------------------------------------------------
bool ConnectionHandler::pingServer()
{
	bool success = false;
	//ResponseInfo info;
	try { // Do the query!
		string uri = "https://"; // Using a secure scheme
		uri.append(config->serverConfig.pbDNSServer); // Append server address
		ofx::HTTP::GetRequest request(uri);	// Create request
		ofxHTTP::Client client; // Create client
		ofx::HTTP::Context ctx;	// Create context
		ctx.setClientSessionSettings(sessionSettings);
		request.setContentType("application/x-www-form-urlencoded");
		
		// Execute the request within the given context.
		auto response = client.execute(ctx, request);

		if (response->getStatus() == Poco::Net::HTTPResponse::HTTP_OK) {
			//info.success = true;
			success = true;
		}
		ofLogNotice("ConnectionHandler::pingServer")
			<< "HTTP Status Code = " << response->getStatus();
	}
	catch (const Poco::Exception& exc) {
		ofLogError("ConnectionHandler::pingServer") << "Poco: " << exc.displayText();
	}
	catch (const std::exception& exc) {
		ofLogError("ConnectionHandler::pingServer") << exc.what();
	}
	return success;
}

Thank you very much for your help! I'm testing this for days but cannot resolve it :S

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions