void xplane_socket_write(xplane_socket *s, xplane_message_data *messages, int count) { udp_socket *socket = s->socket; int bufferLength = XPLANE_MESSAGE_DATA_SIZE * count + XPLANE_MESSAGE_HEADER_SIZE; unsigned char *buffer = malloc(bufferLength); *(buffer + bufferLength) = 0; unsigned char *b = buffer; memcpy(buffer, "DATA\0", 5); b += 5; for (int i = 0; i < count; i++) { xplane_message_data message = messages[i]; memcpy(b, &message.index, 4); b += 4; for (int j = 0; j < 8; j++) { float value = message.data[j]; // xplane seems to crash if it gets NaN if (value != value) { LLog("[xplane_socket_write] got NaN, ignoring\n"); value = 0; } memcpy(b, &value, sizeof(float)); b += sizeof(float); } } udp_socket_write(socket, buffer, bufferLength); }
void *_RTLENTRY _EXPFUNC realloc(void *ptr,size_t size) { size_t sz; void *ptrnew; if(!ptr) sz = 0; else if((sz=GlobalSize((HGLOBAL)ptr)) == 0) { if(ptr) LLog(("!allocated block %p -> %d",ptr,size)); return NULL; } if((ptrnew=(void*)GlobalAlloc(GPTR,size)) != NULL) { LLog(("realloc(%p,%d) = %p",ptr,size,ptrnew)); MemUsage = MemUsage - sz + size; if(!sz) MemCount++; if(ptr) { memmove(ptrnew,ptr,sz); GlobalFree(ptr); } ShowMemInfo(); } else LLog(("!realloc %p[%d] -> %p[%d] [%s]",ptr,sz,ptrnew,size,__WINError())); return ptrnew; }
void xplane_udp_data_handler_function(udp_packet *p, void *context) { xplane_socket *xplane_sock = context; xplane_data_handler handler = xplane_sock->handler; void *ctx = xplane_sock->context; xplane_message_header *header; header = (xplane_message_header *)p->data; if (strncmp(header->code, "DATA", 4) == 0) { int data_count = (p->count - XPLANE_MESSAGE_HEADER_SIZE) / XPLANE_MESSAGE_DATA_SIZE; handler((xplane_message_data *)&p->data[5], data_count, ctx); } else { LLog("[xplane_socket] discarding bogus message\n"); } }
void message_json_handler_function(yajl_val j, void *context) { state *s = context; // LLog("[message_json_handler_function] will read value type\n"); // LLog("[message_json_handler_function] value is %d\n", (unsigned int)j); // LLog("[message_json_handler_function] value type is %d\n", j->type); if (!YAJL_IS_OBJECT(j)) { LLog("[message_json_handler_function] value was not an object, bailing out\n"); return; } int num_vals = YAJL_GET_OBJECT(j)->len; for (int i = 0; i < num_vals; i++) { const char *name = YAJL_GET_OBJECT(j)->keys[i]; //LLog("[message_json_handler_function] did get name = %s\n", name); yajl_val value = YAJL_GET_OBJECT(j)->values[i]; if (YAJL_IS_INTEGER(value)) { //LLog("[message_json_handler_function] value is integer\n"); state_set_int(s, name, YAJL_GET_INTEGER(value)); } else if (YAJL_IS_DOUBLE(value)) { //LLog("[message_json_handler_function] value is double\n"); state_set_float(s, name, (float)YAJL_GET_DOUBLE(value)); } else if (YAJL_IS_TRUE(value)) { //LLog("[message_json_handler_function] value is true\n"); state_set_int(s, name, 1); } else if (YAJL_IS_FALSE(value)) { //LLog("[message_json_handler_function] value is false\n"); state_set_int(s, name, 0); } else { //LLog("[message_json_handler_function] value is complex\n"); state_set_json(s, name, value); } } }