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

parsing works sometimes and crashes others #752

Closed
AdamMiltonBarker opened this issue Sep 21, 2017 · 9 comments
Closed

parsing works sometimes and crashes others #752

AdamMiltonBarker opened this issue Sep 21, 2017 · 9 comments

Comments

@AdamMiltonBarker
Copy link

Hi guys I am using this with paho c, both installed using VCPKG and used in a C++ console app. I attempting to convert message->payload to json so I can access the elements of the response. This works sometimes but in others I get errors such as:

"parse error - unexpected 's'; expected end of input"

Exactly the same response is work and not working randomly. This is the code which is the on message arrived callback from paho:

int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
	int i;
	const char* payloadptr;

	payloadptr = (const char*)message->payload;
	std::cout << payloadptr;
	std::vector<std::string> v = explode("/", topicName);
	std::string s(payloadptr);
	std::cout << s;
	json data = json::parse(s+'\n');
	std::cout << data["Command"];

	if (v[4]=="Commands") {

	}
	 
	MQTTAsync_freeMessage(&message);
	MQTTAsync_free(topicName);
	return 1;
}

Example response:

{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}

@gregmarr
Copy link
Contributor

It wouldn't affect the crash issue, but you don't need the \n at the end.

@gregmarr
Copy link
Contributor

Do you have the std::cout output from when it works and when it doesn't? It's likely a difference in the string.

@AdamMiltonBarker
Copy link
Author

AdamMiltonBarker commented Sep 21, 2017

Hi thanks for reply, I added that as I thought it would help :) I will remove it, I will try get that for you now, but the strings being sent are exactly the same, I have an interface that sends the above string on click of a button and that is all it is receiving at the moment, I have a feeling that is to do with the pointer not being completely populated by the time it gets to json::parse ?, I have posted in Paho also. Not sure whether it is on this side or Paho.

eclipse/paho.mqtt.c#337

@AdamMiltonBarker
Copy link
Author

AdamMiltonBarker commented Sep 21, 2017

OK so this is happening, which explains the e error. This is 100% not sent with the message:

{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}et.c{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}et.c

One thing I notice is that I have FFMPEG running on the same console. The output from that sometimes get split up in between the print outs of the response, and I doubt that would be it but I checked and there is nothing with et.c in it, but checked anyway to see if it was conflicting, so somewhere it appears that text is getting added to the response. In addition the interface with the button also receives its own publishes and this is totally fine every time, looks like something in my code or an issue with Paho, cannot see anything doing it from the code above though.

@AdamMiltonBarker
Copy link
Author

:O It is coming from the output of FFMPEG I disabled it and it works everytime, no idea how that is possible.

@AdamMiltonBarker
Copy link
Author

AdamMiltonBarker commented Sep 21, 2017

Anyway this appears to be issue between Paho and FFMPEG, thanks for the time to help.

I have removed the output of FFMPEG but it is still crashing, there is some sort of conflict between paho and FFMPEG, FFMPEG is activated in a pipe in this program.

If I disable this, the error does not happen:

pPipe = _popen(" ffmpeg -loglevel quiet -loop 1 -f image2pipe -i - -framerate 25 -vcodec mpeg4 -f mpegts -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 http://127.0.0.1:XXXX", "wb");

UPDATE: No it has just happened with it disabled, this is getting annoying lol

@AdamMiltonBarker
Copy link
Author

New output:

{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}"Capture"{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}X#�╖º3êêêê─╧û F�{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}X#�╖º3êêêê─╧û F�

@AdamMiltonBarker
Copy link
Author

{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}etOut.êêêêc�å(��{"Sensor":"CCTV","SensorID":130,"Command":"Capture","CommandValue":"Face"}etOut.êêêêc�å(��

@AdamMiltonBarker
Copy link
Author

Solved with:

int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
	int i;
	char* payloadptr;

	payloadptr = (char*)message->payload;
	std::cout << payloadptr;
	std::string s = "";
	for (i = 0; i<message->payloadlen; i++)
	{
		s = s + *payloadptr++;
	}
	s = s + '\n';

	std::vector<std::string> v = explode("/", topicName);
	std::cout << s;
	json data = json::parse(s);
	std::cout << data["Command"];

	if (v[4]=="Commands") {

	}
	 
	MQTTAsync_freeMessage(&message);
	MQTTAsync_free(topicName);
	return 1;
}

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

2 participants