void KPR_mqttclient_connect(xsMachine* the) { KPR_MQTTClientRecord *self = xsGetHostData(xsThis); char *host; UInt16 port; KprMQTTClientConnectOptions options; if (xsToInteger(xsArgc) != 10) xsThrowIfFskErr(kFskErrParameterError); host = xsToString(xsArg(0)); port = xsToInteger(xsArg(1)); options.isSecure = xsToBoolean(xsArg(2)); options.keepAlive = xsToInteger(xsArg(3)); options.username = xsTest(xsArg(4)) ? xsToString(xsArg(4)) : NULL; options.password = xsTest(xsArg(5)) ? xsToString(xsArg(5)) : NULL; options.willIsRetained = false; options.willQualityOfService = 0; options.willTopic = NULL; options.willPayload = NULL; options.willPayloadLength = 0; if (xsTest(xsArg(6))) { options.willTopic = xsToString(xsArg(6)); options.willQualityOfService = xsToInteger(xsArg(7)); options.willIsRetained = xsToBoolean(xsArg(8)); if (xsTest(xsArg(9))) { if (isArrayBuffer(xsArg(9))) { options.willPayload = xsToArrayBuffer(xsArg(9)); options.willPayloadLength = xsGetArrayBufferLength(xsArg(9)); } else { options.willPayload = xsToString(xsArg(9)); options.willPayloadLength = FskStrLen(options.willPayload); } } } xsThrowIfFskErr(KprMQTTClientConnect(self->client, host, port, &options)); }
void KPR_mqttclient_connect(xsMachine* the) { KPR_MQTTClientRecord *self = xsGetHostData(xsThis); xsIntegerValue c = xsToInteger(xsArgc); char *host; UInt16 port; KprMQTTClientConnectOptions options; if (c >= 1) { host = xsToString(xsArg(0)); } else { host = "localhost"; } if (c >= 2) { port = xsToInteger(xsArg(1)); } else { port = 1883; } options.isSecure = (port == 1884); options.keepAlive = 60; options.password = NULL; options.username = NULL; options.willIsRetained = false; options.willQualityOfService = 0; options.willTopic = NULL; options.willPayload = NULL; options.willPayloadLength = 0; if (c >= 3) { xsVars(1); xsEnterSandbox(); { if (xsHas(xsArg(2), xsID("secure"))) options.isSecure = xsToBoolean(the->scratch); if (xsHas(xsArg(2), xsID("keepAlive"))) options.keepAlive = xsToInteger(the->scratch); if (xsHas(xsArg(2), xsID("username"))) options.username = xsToString(the->scratch); if (xsHas(xsArg(2), xsID("password"))) options.password = xsToString(the->scratch); if (xsHas(xsArg(2), xsID("will"))) { xsVar(0) = the->scratch; if (xsHas(xsVar(0), xsID("topic"))) options.willTopic = xsToString(the->scratch); if (xsHas(xsVar(0), xsID("qos"))) options.willQualityOfService = xsToInteger(the->scratch); if (xsHas(xsVar(0), xsID("retain"))) options.willIsRetained = xsToBoolean(the->scratch); if (xsHas(xsVar(0), xsID("data"))) { xsVar(0) = the->scratch; if (isChunk(xsVar(0))) { options.willPayload = xsGetHostData(xsVar(0)); options.willPayloadLength = xsToInteger(xsGet(xsVar(0), xsID_length)); } else { options.willPayload = xsToString(xsVar(0)); options.willPayloadLength = FskStrLen(options.willPayload); } } } } xsLeaveSandbox(); } if (options.willQualityOfService > 2 || (options.willTopic && !KprMQTTIsValidTopic(options.willTopic, false))) { xsThrowIfFskErr(kFskErrBadData); } xsThrowIfFskErr(KprMQTTClientConnect(self->client, host, port, &options)); }