Re: Help me wrap PahoMQTT
- Posted by ghaberek (admin) Apr 22, 2019
- 2721 views
xfox26 said...
Can someone give me some directions on making it x64 compatible?
Don't do this:
atom hndl = allocate(4) atom ret = peek4u(hndl)
Do this instead:
atom hndl = allocate_data( sizeof(C_POINTER) ) atom ret = peek_pointer( hndl )
Define structures like this:
ifdef BITS64 then constant MQTTClient_willOptions__struct_id = 0, -- char[4] MQTTClient_willOptions__struct_version = 4, -- int MQTTClient_willOptions__topicName = 8, -- const char* MQTTClient_willOptions__message = 16, -- const char* MQTTClient_willOptions__retained = 24, -- int MQTTClient_willOptions__qos = 28, -- int MQTTClient_willOptions__payload_len = 32, -- int MQTTClient_willOptions__payload_data = 40, -- const void* SIZEOF_MQTTClient_willOptions = 48, $ elsedef constant MQTTClient_willOptions__struct_id = 0, -- char[4] MQTTClient_willOptions__struct_version = 4, -- int MQTTClient_willOptions__topicName = 8, -- const char* MQTTClient_willOptions__message = 12, -- const char* MQTTClient_willOptions__retained = 16, -- int MQTTClient_willOptions__qos = 20, -- int MQTTClient_willOptions__payload_len = 24, -- int MQTTClient_willOptions__payload_data = 28, -- const void* SIZEOF_MQTTClient_willOptions = 32, $ end ifdef
And if you need to use them frequently, create wrapper functions to peek/poke your structure members safely:
-- peek a string pointer and return the string or "" if the ptr is NULL function peek_str( atom ptr ) ptr = peek_pointer( ptr ) if ptr = NULL then return "" end if return peek_string( ptr ) end function function peek_MQTTClient_willOptions( atom ptr ) sequence struct_id = peek({ ptr + MQTTClient_willOptions__struct_id, 4 }) atom struct_version = peek4s( ptr + MQTTClient_willOptions__struct_version ) sequence topicName = peek_str( ptr + MQTTClient_willOptions__topicName ) sequence message = peek_str( ptr + MQTTClient_willOptions__message ) atom retained = peek4s( ptr + MQTTClient_willOptions__retained ) atom qos = peek4s( ptr + MQTTClient_willOptions__qos ) atom payload_len = peek4s( ptr + MQTTClient_willOptions__payload_len ) object payload_data = peek4s( ptr + MQTTClient_willOptions__payload_data ) if payload_len != 0 and payload_data != NULL then -- actually peek the data if it's present payload_data = peek({ payload_data, payload_len }) end if return { struct_id, struct_version, topicName, message, retained, qos, payload_len, payload_data } end function
Tips and tricks:
- Invert your current ifdef's; use ifdef BITS64 and then else for 32-bit.
- Use sizeof() to determine the size of C types.
- Use allocate_data() unless you explicitly need executable memory.
- Ensure the correct peek/poke variants for your structures.
- Define separate 32/64 bit structures where necessary.
-Greg