Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible segmentation fault on GetRequest? #55

Open
neobits opened this issue Mar 16, 2020 · 0 comments
Open

Possible segmentation fault on GetRequest? #55

neobits opened this issue Mar 16, 2020 · 0 comments

Comments

@neobits
Copy link

neobits commented Mar 16, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant