FskErr KprSocketConnect(KprSocket self, const char *host, int port) { FskErr err = kFskErrNone; long flags = 0; if (self->mode != kKprSocketModeUndefined) return kFskErrBadState; self->host = FskStrDoCopy(host); bailIfNULL(self->host); self->port = port; self->mode = kKprSocketModeTCP; if (self->secure) { FskSSLOption ssl; flags |= kConnectFlagsSSLConnection; FskNetSSLOptionInitialize(self->host, self->port, flags, kFskNetSocketDefaultPriority, &ssl); ssl.protocolVersion = self->tlsProtocolVersion; ssl.applicationProtocols = self->applicationProtocols; bailIfError(FskNetConnectToSecureHost(&ssl, self->certs, KprSocketOnConnect, self)); } else { bailIfError(FskNetConnectToHost((char *) self->host, self->port, false, KprSocketOnConnect, self, flags, NULL, "KprSocketTCP")); } bail: if (err) { KprSocketCleanup(self); } return err; }
FskErr KprSoundPlay(KprSound self) { FskErr err = kFskErrNone; FskSndChannel soundChannel = gSoundChannel; float volume; UInt32 c; KprSoundFrame frame; bailIfNULL(soundChannel); bailIfError(KprSoundLoad(self)); FskTimeCallbackRemove(gSoundTimer); FskSndChannelSetMoreCallback(soundChannel, NULL, NULL); FskSndChannelGetVolume(soundChannel, &volume); FskSndChannelStop(soundChannel); bailIfError(FskSndChannelSetFormat(soundChannel, kFskAudioFormatUndefined, self->audioFormat, self->numChannels, self->sampleRate, self->formatInfo, self->formatInfoSize)); c = self->frameCount; frame = &self->frames[0]; while (c) { bailIfError(FskSndChannelEnqueue(soundChannel, self->data + frame->frameOffset, frame->frameSize * frame->count, frame->count, frame->samplesPerFrame, NULL, NULL)); c--; frame++; } self->playing = true; FskSndChannelSetVolume(soundChannel, volume); FskSndChannelSetMoreCallback(soundChannel, KprSoundPlayMore, self); bailIfError(FskSndChannelStart(soundChannel, 0)); bail: return err; }
static FskErr KprWebSocketEndpointConnectCallback(FskSocket skt, void *refCon) { FskErr err = kFskErrNone; KprWebSocketEndpoint self = refCon; FskDebugStr("CONNECT: callback was called\n"); if (!skt || 0 == skt->ipaddrRemote) { bailIfError(kFskErrSocketNotConnected); } bailIfError(KprWebSocketEndpointSetupSocketReader(self, skt, kKprWebSocketEndpoint_onHeader)); self->doMask = true; self->needNonMaskedFrame = true; if (self->cancelConnection) { KprWebSocketEndpointDisconnect(self); } else { bailIfError(KprWebSocketEndpointUpgradeConnection(self)); } bail: if (err) { CALLBACK(errorCallback)(self, err, "cannot start handshake", self->refcon); } return err; }
FskErr KprWebSocketServerListen(KprWebSocketServer self, int port, char *interfaceName) { FskErr err = kFskErrNone; if (!self->server) { KprSocketServer server = NULL; bailIfError(KprSocketServerNew(&server, self)); server->acceptCallback = KprWebSocketServerAcceptNewConnection; server->interfaceDroppedCallback = KprWebSocketServerInterfaceDropped; server->debugName = "WebSocket Server"; self->server = server; } bailIfError(KprSocketServerListen(self->server, port, interfaceName)); if (self->launchCallback) { self->launchCallback(self, self->refCon); } bail: if (err && self->errorCallback) { self->errorCallback(self, err, "failed to launch", self->refCon); } return err; }
FskErr KprCoAPServerSessionNew(KprCoAPServer server, KprCoAPMessage request, KprCoAPEndpoint endpoint, KprCoAPServerSession *it) { FskErr err = kFskErrNone; KprCoAPServerSession self = NULL; KprCoAPMessageOptionRecord *option; bailIfError(KprMemPtrNewClear(sizeof(KprCoAPServerSessionRecord), &self)); bailIfError(KprRetainableNew(&self->retainable)); self->server = server; self->sessionId = KprCoAPServerNextSessionId(server); self->endpoint = KprRetain(endpoint); self->request = KprRetain(request); self->autoAck = true; self->confiramableRequest = KprCoAPMessageIsConfirmable(request); KprCoAPEndpointGetExpireTime(endpoint, &self->expireAt); option = KprCoAPMessageFindOption(request, kKprCoAPMessageOptionObserve); self->observeRequested = (option != NULL && option->value.uint == kKprCoAPMessageObserveRegister); bailIfError(KprCoAPMessageBuildUri(self->request)); *it = self; bail: if (err) { KprCoAPServerSessionDispose(self); } return err; }
FskErr KprWebSocketEndpointConnect(KprWebSocketEndpoint self, char *url, char *origin) { FskErr err = kFskErrNone; self->url = FskStrDoCopy(url); bailIfNULL(self->url); bailIfError(FskStrParseUrl(self->url, &self->parts)); if (!KprWebSocketEndpointCheckURL(self)) { bailIfError(kFskErrInvalidParameter); } self->isSecure = (FskStrCompare(self->parts->scheme, "wss") == 0); if (origin) { self->origin = FskStrDoCopy(origin); bailIfNULL(self->origin); } self->state = kKprWebSocketStateConnecting; INVOKE_AFTER1(KprWebSocketEndpointStartConnect, self); bail: return err; }
static FskErr KprWebSocketServerRequestNew(KprWebSocketServerRequest *it, KprWebSocketServer server, FskSocket skt, const char *interfaceName, int ip) { FskErr err = kFskErrNone; KprWebSocketServerRequest request = NULL; bailIfError(FskMemPtrNewClear(sizeof(KprWebSocketServerRequestRecord), &request)); request->server = server; request->skt = skt; skt = NULL; request->interfaceName = FskStrDoCopy(interfaceName); bailIfNULL(request->interfaceName); request->ip = ip; FskNetSocketGetRemoteAddress(request->skt, (UInt32 *)&request->requesterAddress, &request->requesterPort); FskNetSocketMakeNonblocking(request->skt); bailIfError(FskHeaderStructNew(&request->requestHeaders)); bailIfError(FskHeaderStructNew(&request->responseHeaders)); request->out.size = 512; bailIfError(FskMemPtrNew(request->out.size, &request->out.buffer)); FskThreadAddDataHandler(&request->dataHandler, (FskThreadDataSource)request->skt, (FskThreadDataReadyCallback)KprWebSocketServerRequestDoRead, true, false, request); // request->state = kHTTPNewSession; *it = request; bail: if (err) { KprWebSocketServerRequestDispose(request); FskNetSocketClose(skt); } return err; }
static FskErr KprSocketNewFromFskSocket(FskSocket skt, KprSocketFlags flags, KprSocket *it) { FskErr err = kFskErrNone; KprSocket self = NULL; UInt32 ipaddr; char ipaddrStr[22]; bailIfError(FskMemPtrNewClear(sizeof(KprSocketRecord), &self)); bailIfError(FskNetSocketGetLocalAddress(skt, &ipaddr, &self->port)); FskNetIPandPortToString(ipaddr, 0, ipaddrStr); self->host = FskStrDoCopy(ipaddrStr); bailIfNULL(self->host); self->mode = kKprSocketModeTCP; self->proto = kKprSocketProtoTCP; self->secure = (Boolean)(flags & kKprSocketFlagsSecure); bailIfError(KprSocketOnConnect(skt, self)); *it = self; FskListAppend(&gSockets, self); bail: if (err) { KprSocketDispose(self); } return err; }
static void KprCoAPClientResolverResolved(FskResolver rr) { FskErr err = kFskErrNone; KprCoAPClientResolver self = (KprCoAPClientResolver)rr->ref; KprCoAPMessageQueue entry; self->resolver = NULL; bailIfError(rr->err); self->ipaddr = rr->resolvedIP; FskTimeGetNow(&self->resolvedAt); entry = self->waiting; while (entry) { KprCoAPMessageQueue next = entry->next; KprCoAPMessage message = entry->message; bailIfError(KprCoAPClientStartRequest(self->client, self->ipaddr, self->port, message)); entry = next; } bail: if (err) { if (self->constructed) { KprCoAPClientReportError(self->client, err, "dns resolve error"); } else { self->err = err; } } }
static void DNSSD_API KprZeroconfPlatformResolveCallBack(DNSServiceRef resolveRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *fullname, const char *hostname, uint16_t port, uint16_t txtLen, const unsigned char *txtRecord, void *context) { FskErr err = kFskErrNone; KprZeroconfBrowser self = context; KprZeroconfPlatformBrowser browser = self->platform; KprZeroconfPlatformService resolver = KprZeroconfPlatformServiceFind(browser->services, resolveRef); if (!resolver || (errorCode != kDNSServiceErr_NoError)) { FskInstrumentedItemPrintfDebug(browser, "KprZeroconfPlatformResolveCallBack returned %d\n", errorCode); } else { DNSServiceErrorType error; DNSServiceRef serviceRef; KprZeroconfPlatformService service = NULL; FskInstrumentedItemPrintfDebug(browser, "RESOLVE: %s %s is at %s:%d", self->serviceType, resolver->name, hostname, ntohs(port)); error = DNSServiceGetAddrInfo(&serviceRef, 0, interfaceIndex, kDNSServiceProtocol_IPv4, hostname, KprZeroconfPlatformGetAddrInfoCallBack, self); if (error != kDNSServiceErr_NoError) { bailIfError(kFskErrNetworkErr); } bailIfError(KprZeroconfPlatformServiceNew(&service, resolver->owner, serviceRef, resolver->name, ntohs(port))); if (txtLen > 1) { bailIfError(FskMemPtrNewClear(txtLen, &service->txt)); FskStrNCopy(service->txt, (const char*)txtRecord, txtLen); } FskListAppend(&browser->services, service); FskListRemove(&browser->services, resolver); KprZeroconfPlatformServiceDispose(resolver); } bail: return; }
void KPR_mqttclient_unsubscribe(xsMachine* the) { KPR_MQTTClientRecord *self = xsGetHostData(xsThis); FskErr err = kFskErrNone; xsIntegerValue c = xsToInteger(xsArgc); char **topics = NULL; int i, count; UInt16 token; if (c == 0) xsThrowIfFskErr(kFskErrParameterError); count = c; bailIfError(FskMemPtrNew(sizeof(char*) * count, &topics)); for (i = 0; i < count; i++) { topics[i] = xsToString(xsArg(i)); } bailIfError(KprMQTTClientUnsubscribeTopics(self->client, topics, count, &token)); xsResult = xsInteger(token); bail: FskMemPtrDispose(topics); xsThrowIfFskErr(err); }
FskErr KprWebSocketEndpointClose(KprWebSocketEndpoint self, UInt16 code, char *reason) { FskErr err = kFskErrNone; FskDebugStr("CLOSE: (%d), %s\n", code, reason); self->closeCode = code; if (reason) { FskMemPtrDispose(self->closeReason); self->closeReason = FskStrDoCopy(reason); } switch (self->state) { case kKprWebSocketStateConnecting: bailIfError(KprWebSocketEndpointCancelConnection(self)); break; case kKprWebSocketStateOpen: bailIfError(KprWebSocketEndpointStartClosingHandshake(self, code, reason)); break; case kKprWebSocketStateClosing: case kKprWebSocketStateClosed: default: break; } bail: return err; }
FskErr KprCoAPClientResolverNew(KprCoAPClientResolver *it, KprCoAPClient client, const char *host, UInt16 port, KprCoAPMessage message) { FskErr err = kFskErrNone; KprCoAPClientResolver self = NULL; bailIfError(KprMemPtrNewClear(sizeof(KprCoAPClientResolverRecord), &self)); self->host = FskStrDoCopy(host); bailIfNULL(self->host); bailIfError(KprCoAPMessageQueueAppend(&self->waiting, message)); self->client = client; self->port = port; bailIfError(FskNetHostnameResolveQTAsync((char *) host, 0, KprCoAPClientResolverResolved, self, &self->resolver)); bailIfError(self->err); self->constructed = true; *it = self; bail: if (err) { KprCoAPClientResolverDispose(self); } return err; }
static FskErr KprSocketListenerNew(KprSocket owner, int ip, int port, KprSocketListener *it) { KprSocketListener self = NULL; FskErr err; FskSocket skt = NULL; bailIfError(KprMemPtrNewClear(sizeof(KprSocketListenerRecord), &self)); bailIfError(FskNetSocketNewTCP(&skt, true, "KprSocketListener")); FskNetSocketReuseAddress(skt); FskNetSocketMakeNonblocking(skt); bailIfError(FskNetSocketBind(skt, ip, port)); bailIfError(FskNetSocketGetLocalAddress(skt, NULL, &port)); self->owner = owner; self->ip = ip; self->port = port; self->socket = skt; skt = NULL; FskListAppend(&owner->listeners, self); if (it) *it = self; bail: FskNetSocketClose(skt); if (err) { KprSocketListenerDispose(self); } return err; }
FskErr KprSocketAccept(KprSocket self, KprSocket *it) { FskErr err = kFskErrNone; FskSocket skt = NULL; KprSocket socket = NULL; KprSocketFlags flags = 0; if (self->currentListener == NULL) return kFskErrBadSocket; bailIfError(KprSocketListenerAccept(self->currentListener, &skt)); if (self->secure) flags |= kKprSocketFlagsSecure; bailIfError(KprSocketNewFromFskSocket(skt, flags, &socket)); skt = NULL; socket->server = self; if (self->secure) { bailIfError(KprSocketStartTSL(socket)); } else { if (self->callbacks.accept) { self->callbacks.accept(self, socket, self->refcon); } } *it = socket; socket = NULL; bail: FskNetSocketClose(skt); KprSocketDispose(socket); return err; }
FskErr KprDBStatementNew(KprDBStatement* it, KprDB db, const char* text, char* bindFormat, char* rowFormat) { FskErr err = kFskErrNone; KprDBStatement self = NULL; SInt32 count, size, i; bailIfError(KprMemPtrNewClear(sizeof(KprDBStatementRecord), it)); self = *it; FskInstrumentedItemNew(self, NULL, &KprDBStatementInstrumentation); bailIfError(sqlite3_prepare_v2(db->data, text, FskStrLen(text), &self->data, NULL)); count = sqlite3_bind_parameter_count(self->data); if (count && !bindFormat) BAIL(kFskErrInvalidParameter); if (!count && bindFormat) BAIL(kFskErrInvalidParameter); if (count) { if ((SInt32)FskStrLen(bindFormat) != count) BAIL(kFskErrInvalidParameter); self->bindFormat = FskStrDoCopy(bindFormat); bailIfNULL(self->bindFormat); } count = sqlite3_column_count(self->data); if (count && !rowFormat) BAIL(kFskErrInvalidParameter); if (!count && rowFormat) BAIL(kFskErrInvalidParameter); if (count) { if ((SInt32)FskStrLen(rowFormat) != count) BAIL(kFskErrInvalidParameter); self->rowFormat = FskStrDoCopy(rowFormat); bailIfNULL(self->rowFormat); bailIfError(KprMemPtrNewClear(count * sizeof(char*), &self->keys)); for (size = 0, i = 0; rowFormat[i]; i++) { self->keys[i] = sqlite3_column_name(self->data, i); switch (rowFormat[i]) { case 'b': size += sizeof(void*) + sizeof(UInt32); break; case 'd': size += sizeof(double); break; case 'i': size += sizeof(SInt32); break; case 'I': size += sizeof(FskInt64); break; case 'r': size += sizeof(SInt32); break; case 't': size += sizeof(char*); break; default: BAIL(kFskErrInvalidParameter); break; } } bailIfError(KprMemPtrNewClear(size, &self->values)); } bail: if (err) KprDBStatementDispose(self); return err; }
static FskErr KprWebSocketEndpointSetupSocketReader(KprWebSocketEndpoint self, FskSocket skt, int initialState) { FskErr err = kFskErrNone; KprSocketReaderState states[] = { { kKprWebSocketEndpoint_onHeader, (KprSocketReaderCallback) KprWebSocketEndpointReadHeader }, { kKprWebSocketEndpoint_onFrameOpcode, (KprSocketReaderCallback) KprWebSocketEndpointReadFrameOpcode }, { kKprWebSocketEndpoint_onFrameLength, (KprSocketReaderCallback) KprWebSocketEndpointReadFrameLength }, { kKprWebSocketEndpoint_onFrameLength16, (KprSocketReaderCallback) KprWebSocketEndpointReadFrameLength16 }, { kKprWebSocketEndpoint_onFrameLength64, (KprSocketReaderCallback) KprWebSocketEndpointReadFrameLength64 }, { kKprWebSocketEndpoint_onFrameMaskData, (KprSocketReaderCallback) KprWebSocketEndpointReadFrameMaskData }, { kKprWebSocketEndpoint_onFrameMessage, (KprSocketReaderCallback) KprWebSocketEndpointReadFrameMessage }, }; UInt32 stateCount = sizeof(states) / sizeof(KprSocketReaderState); KprSocketReader reader; KprSocketWriter writer; bailIfError(KprSocketReaderNew(&reader, skt, states, stateCount, self)); bailIfError(KprSocketWriterNew(&writer, skt, self)); FskNetSocketReceiveBufferSetSize(skt, kSocketBufferSize); reader->errorCallback = KprWebSocket_onReadError; KprSocketReaderSetState(reader, initialState); writer->errorCallback = KprWebSocket_onWriteError; self->socket = skt; self->reader = reader; self->writer = writer; bail: if (err) { KprSocketReaderDispose(reader); KprSocketWriterDispose(writer); } return err; }
FskErr KprURLMerge(char* base, char* reference, char** result) { FskErr err = kFskErrNone; KprURLPartsRecord parts; KprURLPartsRecord baseParts; char* path = NULL; UInt32 length; KprURLSplit(reference, &parts); if (!parts.scheme) { KprURLSplit(base, &baseParts); parts.scheme = baseParts.scheme; parts.schemeLength = baseParts.schemeLength; if (!parts.authority) { parts.authority = baseParts.authority; parts.authorityLength = baseParts.authorityLength; if (!parts.pathLength) { if (baseParts.authority && !baseParts.pathLength) { bailIfError(FskMemPtrNew(1, &path)); FskMemMove(path, "/", 1); parts.path = path; parts.pathLength = 1; } else { parts.path = baseParts.path; parts.pathLength = baseParts.pathLength; } if (!parts.query) { parts.query = baseParts.query; parts.queryLength = baseParts.queryLength; } } else if (*parts.path != '/') { if (baseParts.authority && !baseParts.pathLength) { bailIfError(FskMemPtrNew(1 + parts.pathLength, &path)); FskMemMove(path, "/", 1); FskMemMove(path + 1, parts.path, parts.pathLength); parts.path = path; parts.pathLength++; } else if (baseParts.name) { length = baseParts.name - baseParts.path; bailIfError(FskMemPtrNew(length + parts.pathLength, &path)); FskMemMove(path, baseParts.path, length); FskMemMove(path + length, parts.path, parts.pathLength); parts.path = path; parts.pathLength += length; } } } } bailIfError(KprURLJoin(&parts, result)); bail: FskMemPtrDispose(path); return err; }
FskErr KprSoundLoadSpoolerRead(FskMediaSpooler spooler, FskInt64 position, UInt32 bytesToRead, void **buffer, UInt32 *bytesRead) { FskErr err = kFskErrNone; KprSoundLoadSpoolerRefcon refcon = spooler->refcon; bailIfError(FskFileSetPosition(refcon->fref, &position)); bailIfError(FskFileRead(refcon->fref, (bytesToRead > sizeof(refcon->buffer)) ? sizeof(refcon->buffer) : bytesToRead, refcon->buffer, bytesRead)); *buffer = refcon->buffer; bail: return err; }
void KPR_MQTTClient(xsMachine* the) { FskErr err; KPR_MQTTClientRecord *self = NULL; xsIntegerValue c = xsToInteger(xsArgc); KprMQTTClient client = NULL; char clientIdentifier[kKprMQTTClientIdentifierMaxLength + 1]; Boolean cleanSession = true; if (c >= 1) { char *arg = xsToString(xsArg(0)); UInt32 len = FskStrLen(arg); if (len < 1 || len > kKprMQTTClientIdentifierMaxLength) xsThrowIfFskErr(kFskErrBadData); FskStrCopy(clientIdentifier, arg); } else { char hex[9]; FskStrNumToHex(FskRandom(), hex, 8); FskStrCopy(clientIdentifier, kKprMQTTClientIdentifierPrefix); FskStrCat(clientIdentifier, hex); } if (c >= 2) { cleanSession = xsToBoolean(xsArg(1)); } bailIfError(FskMemPtrNewClear(sizeof(KPR_MQTTClientRecord), &self)); bailIfError(KprMQTTClientNew(&client, clientIdentifier, cleanSession, self)); client->connectCallback = KPR_mqttclient_onConnect; client->subscribeCallback = KPR_mqttclient_onSubscribe; client->unsubscribeCallback = KPR_mqttclient_onUnsubscribe; client->publishCallback = KPR_mqttclient_onPublish; client->messageCallback = KPR_mqttclient_onMessage; client->disconnectCallback = KPR_mqttclient_onDisconnect; client->errorCallback = KPR_mqttclient_onError; self->client = client; self->the = the; self->slot = xsThis; self->code = the->code; xsSetHostData(self->slot, self); // xsCall1(xsGet(xsGlobal, xsID_Object), xsID_seal, self->slot); xsRemember(self->slot); bail: if (err) { KprMQTTClientDispose(client); FskMemPtrDispose(self); xsThrowIfFskErr(err); } }
FskErr fxParseXMLStopTag(KprCodeParser* parser) { FskErr err = kFskErrNone; bailIfError(KprCodeParserColorAt(parser, 1, parser->offset)); bailIfError(fxParseXMLName(parser)); bailIfError(fxParseXMLSpace(parser, NULL)); if (parser->character != '>') return kFskErrBadData; KprCodeParserAdvance(parser); bail: return err; }
FskErr KprApplicationNew(KprApplication* it, char* url, char* id, Boolean breakOnStart, Boolean breakOnExceptions) { KprCoordinatesRecord coordinates = { kprLeftRight, kprTopBottom, 0, 0, 0, 0, 0, 0 }; xsAllocation allocation = { 2 * 1024 * 1024, 1024 * 1024, 64 * 1024, 8 * 1024, 2048, 16000, 1993 }; FskErr err = kFskErrNone; KprApplication self; bailIfError(FskMemPtrNewClear(sizeof(KprApplicationRecord), it)); self = *it; FskInstrumentedItemNew(self, NULL, &KprApplicationInstrumentation); self->dispatch = &KprApplicationDispatchRecord; self->flags = kprContainer | kprClip | kprVisible; KprContentInitialize((KprContent)self, &coordinates, NULL, NULL); bailIfError(KprURLMerge(gShell->url, url, &self->url)); if (id) { self->id = FskStrDoCopy(id); bailIfNULL(self->id); } self->the = xsAliasMachine(&allocation, gShell->root, self->url, self); if (!self->the) BAIL(kFskErrMemFull); FskInstrumentedItemSendMessageNormal(self, kprInstrumentedContentCreateMachine, self); xsBeginHost(self->the); xsResult = xsNewHostFunction(KPR_include, 1); xsSet(xsResult, xsID("uri"), xsString(self->url)); xsNewHostProperty(xsGlobal, xsID("include"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsResult = xsNewHostFunction(KPR_require, 1); xsSet(xsResult, xsID("uri"), xsString(self->url)); xsNewHostProperty(xsGlobal, xsID("require"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("application"))); self->slot = xsResult; xsSetHostData(xsResult, self); (void)xsCall1(xsGet(xsGlobal, xsID("Object")), xsID("seal"), xsResult); xsNewHostProperty(xsGlobal, xsID("application"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsNewHostProperty(xsGlobal, xsID("shell"), xsNull, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); if (breakOnStart) xsDebugger(); if (breakOnExceptions) (void)xsCall1(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("setBreakOnException"), xsBoolean(breakOnExceptions)); (void)xsCall1(xsGlobal, xsID("include"), xsString(self->url)); xsEndHost(self->the); KprContentChainPrepend(&gShell->applicationChain, self, 0, NULL); bail: return err; }
FskErr KprMQTTClientUnsubscribeTopic(KprMQTTClient self, char *topic) { FskErr err; KprMQTTMessage message = NULL; bailIfError(KprMQTTClientCreateUnsubscribeMessage(self, topic, &message)); bailIfError(KprMQTTClientSendMessage(self, &message)); bail: KprMQTTMessageDispose(message); return err; }
FskErr fxParseXMLStartTag(KprCodeParser* parser) { FskErr err = kFskErrNone; int length; bailIfError(KprCodeParserColorAt(parser, 1, parser->offset)); bailIfError(fxParseXMLName(parser)); bailIfError(fxParseXMLSpace(parser, &length)); while ((parser->character) && (parser->character != '/') && (parser->character != '>')) { if (!length) return kFskErrBadData; bailIfError(fxParseXMLName(parser)); bailIfError(fxParseXMLSpace(parser, NULL)); if (parser->character != '=') return kFskErrBadData; KprCodeParserAdvance(parser); bailIfError(fxParseXMLSpace(parser, NULL)); bailIfError(fxParseXMLValue(parser)); bailIfError(fxParseXMLSpace(parser, &length)); } if (parser->character == '/') KprCodeParserAdvance(parser); if (parser->character != '>') return kFskErrBadData; KprCodeParserAdvance(parser); bail: return err; }
FskErr KprLibraryServerStart(KprLibraryServer self, char* value) { FskErr err = kFskErrNone; SInt32 port = 8080; if (self->server) return err; if (value) port = FskStrToNum(value); bailIfError(FskHTTPServerCreate(port, NULL, &self->server, self, false)); bailIfError(FskHTTPServerSetCallbacks(self->server, &self->vectors)); FskHTTPServerStart(self->server); bail: return err; }
FskErr KprDBNew(KprDB* it, char* path, Boolean rw) { FskErr err = kFskErrNone; KprDB self = NULL; if (!gDBCount) bailIfError(sqlite3_initialize()); bailIfError(KprMemPtrNewClear(sizeof(KprDBRecord), it)); self = *it; FskInstrumentedItemNew(self, NULL, &KprDBInstrumentation); bailIfError(sqlite3_open_v2(path, &self->data, rw ? SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE : SQLITE_OPEN_READONLY, NULL)); gDBCount++; bail: return err; }
FskErr KprMQTTClientPublish(KprMQTTClient self, char *topic, void *payload, UInt32 payloadLength, UInt8 qos, Boolean retain, UInt16 *token) { FskErr err; KprMQTTMessage message = NULL; bailIfError(KprMQTTClientCreatePublishMessage(self, topic, payload, payloadLength, qos, retain, &message)); *token = message->messageId; bailIfError(KprMQTTClientSendMessage(self, &message)); bail: KprMQTTMessageDispose(message); return err; }
FskErr KprSocketBind(KprSocket self, int port, const char *iface) { FskErr err = kFskErrNone; KprSocketListener listener = NULL; FskNetInterfaceRecord *ifc = NULL; if (self->port != 0) return kFskErrBadState; if (iface) { ifc = FskNetInterfaceFindByName((char *) iface); if (ifc == NULL) return kFskErrNotFound; bailIfError(KprSocketListenerNew(self, ifc->ip, port, &listener)); if (port == 0) { port = KprSocketListenerGetPort(listener); } } else { int i, numI; numI = FskNetInterfaceEnumerate(); for (i = 0; i < numI; i++) { int ip, status; err = FskNetInterfaceDescribe(i, &ifc); if (err) continue; ip = ifc->ip; status = ifc->status; FskNetInterfaceDescriptionDispose(ifc); ifc = NULL; if (status) { bailIfError(KprSocketListenerNew(self, ip, port, &listener)); if (port == 0) { port = KprSocketListenerGetPort(listener); } } } } self->port = port; bail: FskNetInterfaceDescriptionDispose(ifc); if (err) { KprSocketCleanup(self); } return err; }
void KPR_mqttclient_publish(xsMachine* the) { KPR_MQTTClientRecord *self = xsGetHostData(xsThis); FskErr err = kFskErrNone; char *topic; void *payload = NULL; UInt32 payloadLength = 0; UInt8 qos; Boolean retain; UInt16 token; if (xsToInteger(xsArgc) != 4) xsThrowIfFskErr(kFskErrParameterError); topic = xsToString(xsArg(0)); if (xsTest(xsArg(1))) { if (isArrayBuffer(xsArg(1))) { payload = xsToArrayBuffer(xsArg(1)); payloadLength = xsGetArrayBufferLength(xsArg(1)); } else { payload = xsToString(xsArg(1)); payloadLength = FskStrLen(payload); } } qos = xsToInteger(xsArg(2)); retain = xsToBoolean(xsArg(3)); bailIfError(KprMQTTClientPublish(self->client, topic, payload, payloadLength, qos, retain, &token)); xsResult = xsInteger(token); bail: xsThrowIfFskErr(err); }
FskErr KprAuthorityReverse(char* id, char** di) { FskErr err = kFskErrNone; char *p, *q; SInt32 c = FskStrLen(id), i; bailIfError(FskMemPtrNew(c + 1, di)); p = id + c; q = *di; i = 0; while (c) { c--; p--; if (*p == '.') { p++; FskMemCopy(q, p, i); q += i; *q++ = '.'; p--; i = 0; } else i++; } FskMemCopy(q, p, i); q += i; *q = 0; bail: return err; }