コード例 #1
0
ファイル: mqtt.c プロジェクト: dsmrd/dsmrd
static void mqtt_misc(void* userdata) {
    mqtt_t inst = (mqtt_t) userdata;
    int rval;
    int fd;

    if (inst->connected == 0) {
        inst->reconnect_timer++;
        inst->reconnect_timer %= 20;
        if (inst->reconnect_timer == 0) {
            rval = mosquitto_reconnect(inst->mosq);
            if (rval != MOSQ_ERR_SUCCESS) {
                error("Cannot reconnect mosquitto: %s", mosquitto_strerror(rval));
            } else {
                fd = mosquitto_socket(inst->mosq);
                assert(fd != 0);

                rval = dispatch_register(inst->dispatch, fd, mqtt_read, mqtt_write, NULL, mqtt_close, inst);
                assert(rval == 0);
            }
        }
    }

    rval = mosquitto_loop_misc(inst->mosq);
    switch (rval) {
    case MOSQ_ERR_SUCCESS:
        break;
    case MOSQ_ERR_NO_CONN:
        //debug("mosquitto_loop_misc: No connection");
        break;
    case MOSQ_ERR_INVAL:
    default:
        error("mosquitto_loop_misc: %s", mosquitto_strerror(rval));
        break;
    }
}
コード例 #2
0
ファイル: file.c プロジェクト: castaway/glknew
frefid_t glk_fileref_create_by_name(glui32 usage, char *name, glui32 rock) {
  struct glk_fileref_struct *fileref;
  char *name_copy;

  fileref = malloc(sizeof(*fileref));
  /* According to the prototype in dispatch.c, name does not become an
     owned pointer, so we should probably copy it. */
  name_copy = malloc(strlen(name) + 1);
  strcpy(name_copy, name);
  fileref->name = name_copy;
  fileref->rock = rock;
  
  if (dispatch_register) {
    fileref->dispatch_rock = dispatch_register(fileref, gidisp_Class_Fileref);
  }

  return fileref;
}
コード例 #3
0
ファイル: stream.c プロジェクト: castaway/glknew
strid_t glk_stream_open_base(glui32 rock, glui32 fmode, glui32 type, struct glk_stream_struct_vtable *vtable) {
  struct glk_stream_struct *stream;

  stream = malloc(sizeof(struct glk_stream_struct));
  if (!stream) {
    return stream;
  }
  
  stream->rock = rock;
  stream->fmode = fmode;
  stream->type = type;
  stream->vtable = vtable;
  stream->readcount = 0;
  stream->writecount = 0;

  stream->did_dispatch_register = 0;
  if (dispatch_register) {
    stream->dispatch_rock = dispatch_register((void *)stream, gidisp_Class_Stream);
    stream->did_dispatch_register = 1;
  }
  
  if (!first_stream) {
    printf("DEBUG: New stream %p is first_stream\n", stream);
    first_stream = stream;
    stream->next = NULL;
  } else {
    printf("DEBUG: New stream %p isn't first_stream\n", stream);
    strid_t prev_stream = first_stream;
    printf("DEBUG: Starting at prev_stream = %p\n", prev_stream);
    while (prev_stream->next != NULL) {
      prev_stream = prev_stream->next;
      printf("DEBUG: prev_stream=%p\n", prev_stream);
    }
    printf("DEBUG: prev_stream=%p\n", prev_stream);
    stream->next = prev_stream->next;
    prev_stream->next = stream;
  }

  return stream;
}
コード例 #4
0
ファイル: mqtt.c プロジェクト: dsmrd/dsmrd
int mqtt_open(mqtt_t inst, dispatch_t d, const char* name, const char* host, int port, int keepalive) {
    int rval = -1;
    int fd;

    inst->mosq = mosquitto_new(name, true, inst);
    if (inst->mosq == NULL) {
        error("Cannot create mqtt");
    } else {
        inst->dispatch = d;

        mosquitto_log_callback_set(inst->mosq, on_log);
        mosquitto_connect_callback_set(inst->mosq, on_connect);
        mosquitto_disconnect_callback_set(inst->mosq, on_disconnect);
        mosquitto_publish_callback_set(inst->mosq, on_publish);
        mosquitto_message_callback_set(inst->mosq, on_message);
        mosquitto_subscribe_callback_set(inst->mosq, on_subscribe);
        mosquitto_unsubscribe_callback_set(inst->mosq, on_unsubscribe);

        inst->timer = dispatch_create_timer(d, 2000000, mqtt_misc, inst);
        if (inst->timer == NULL) {
            error("Cannot create timer");
        }

        rval = mosquitto_connect(inst->mosq, host, port, keepalive);
        if (rval != MOSQ_ERR_SUCCESS) {
            error("Cannot connect mqtt");
        } else {
            fd = mosquitto_socket(inst->mosq);

            rval = dispatch_register(d, fd, mqtt_read, mqtt_write, NULL, mqtt_close, inst);
            if (rval != 0) {
                error("Cannot register mqtt");
            }
        }
    }

    return rval;
}