static void ftw_subscriber_async_recv_thread(void *arg)
{
    LStrHandle msg_sent_to_lv;
    struct ftw_socket *self;
    void *recv_buf;
    MgErr lv_err;
    int socket_err;
    int rc;

    ftw_assert(arg);

    /*  Create local pointer to arguments and notify launching process this thread is constructed. */
    self = ((struct ftw_socket *) arg);
    nn_sem_post(&self->async_recv_ready);

    lv_err = mgNoErr;
    socket_err = 0;

    /*  This broker relays messages from the nanomsg socket into the LabVIEW incoming message queue. */
    while (!lv_err && !socket_err) {

        rc = nn_recv(self->id, &recv_buf, NN_MSG, 0);

        if (rc < 0) {
            socket_err = ((errno == ETIMEDOUT || errno == EAGAIN) ? 0 : errno);
            continue;
        }

        msg_sent_to_lv = (LStrHandle)DSNewHandle(rc);
        lv_err = ftw_support_buffer_to_LStrHandle(&msg_sent_to_lv, recv_buf, rc);
        if (lv_err)
            continue;

        /*  On the LabVIEW side, the handler is a an event registration, meaning
            LabVIEW will not effectively apply backpressure to the Publisher socket.
            For this reason, care must be taken that the Subscriber keep up with
            the Publisher, yet this consideration has long-existed for the LabVIEW
            developer (since, LV-native primitives will likewise cause a memory leak,
            when Event Registrations are allowed to grow without bound). */
        lv_err = PostLVUserEvent(self->incoming_msg_notifier_event, &msg_sent_to_lv);
        if (lv_err)
            continue;

        lv_err = DSDisposeHandle (msg_sent_to_lv);
        rc = nn_freemsg(recv_buf);
        if (rc) {
            socket_err = errno;
            continue;
        }
    }

    return;
}
FTW_PRIVATE_SUPPORT void ftw_libuv_callback_alloc (uv_handle_t *handle, size_t suggested_size, uv_buf_t* buf)
{
    LStrHandle lv_mem;

    lv_mem = (LStrHandle) DSNewHandle(sizeof (int32) + suggested_size * sizeof (uChar));

    if (lv_mem) {
        LHStrPtr (lv_mem)->cnt = (int32) suggested_size;
        *buf = uv_buf_init(LHStrBuf(lv_mem), suggested_size);
    }
    else {
        *buf = uv_buf_init(NULL, 0);
    }
    return;
}
void LVlinear_print_function(const char * message){
	LVUserEventRef *usrEv = loggingUsrEv;
	if (usrEv != nullptr && message != nullptr){
		// Filter out the progress messages (.....)
		if (strcmp(message, ".") != 0){
			// Move the string to a handle
			size_t length = strlen(message);
			LStrHandle lvmsg = (LStrHandle)DSNewHandle(sizeof(int32_t) + length);
			MoveBlock(message, (*lvmsg)->str, length);
			(*lvmsg)->cnt = static_cast<int32>(length);

			// Post the string to the user event
			PostLVUserEvent(*usrEv, &lvmsg);
		}
	}
}
void LVException::populateErrorCluster(lvError * err) {
	std::string debugInfo;
	err->code = m_code;
	err->status = true;

	if (m_debug){
		debugInfo = "<<DEBUGINFO: File=";
		debugInfo += m_file;
		debugInfo += " Line=";
		debugInfo += std::to_string(m_line);
		debugInfo += ">> ";
		m_messageLength += debugInfo.size();
	}

	// Check if the existing string handle is valid.
	if (DSCheckHandle(err->source) == mgNoErr) {
		// Handle valid: Check if it needs to be resized
		if ((m_messageLength + sizeof(int32_t)) > DSGetHandleSize(err->source)) {
			DSSetHandleSize(err->source, sizeof(int32_t) + m_messageLength);
		}
	}
	else {
		// Handle invalid: Create a new string handle
		err->source = (LStrHandle)DSNewHandle(sizeof(int32_t) + m_messageLength);
	}
	if (m_debug) {
		// Move the string data (with debug info)
		debugInfo += this->what();
		MoveBlock(debugInfo.c_str(), (*err->source)->str, m_messageLength);
	}
	else {
		// Move the string data (without debug info)
		MoveBlock(this->what(), (*err->source)->str, m_messageLength);
	}

	// Set the string length variable
	(*err->source)->cnt = static_cast<int32>(m_messageLength);
}
#include "cphidgettextled.h"
#include "cphidgetweightsensor.h"

#include "csocket.h"

#ifdef COMPILE_PHIDGETS_LABVIEW

#include "phidget_labview.h"

LV_CFHANDLE_0(, Attach, lvNothing)
LV_CFHANDLE_0(, Detach, lvNothing)
LV_CFHANDLE_0(, ServerConnect, lvNothing)
LV_CFHANDLE_0(, ServerDisconnect, lvNothing)
LV_CFHANDLE_BODY(, Error, lvError, void *userPtr, int val1, const char *val2)
	data->val1 = val1;
    data->val2=(LStrHandle)DSNewHandle(sizeof(int32)+255*sizeof(char));
	memset(LStrBuf(*data->val2),'\0',255);
	snprintf((char*)LStrBuf(*data->val2),255,"%s",val2);
	LStrLen(*data->val2)=strlen(val2);

    ret = PostLVUserEvent(ev, data);

	DSDisposeHandle(data->val2);
    DSDisposePtr(data);
	return EPHIDGET_OK;
}

LV_CFHANDLE_2(Accelerometer, AccelerationChange, lvIndexedDouble, int, double)

LV_CFHANDLE_2(AdvancedServo, PositionChange, lvIndexedDouble, int, double)
LV_CFHANDLE_2(AdvancedServo, VelocityChange, lvIndexedDouble, int, double)