Пример #1
0
int pc__handshake_req(pc_client_t *client) {
  json_t *handshake_opts = client->handshake_opts;
  json_t *body = json_object();

  if(body == NULL) {
    fprintf(stderr, "Fail to create json_t for handshake request.\n");
    return -1;
  }

  // compose handshake package
  json_t *sys = json_object();
  if(sys == NULL) {
    fprintf(stderr, "Fail to create json_t for handshake request.\n");
    goto error;
  }

  json_object_set(body, "sys", sys);

  json_object_set(sys, "version", json_string(PC_VERSION));

  if(handshake_opts) {
    json_t *heartbeat = json_object_get(handshake_opts, "heartbeat");
    if(heartbeat) {
      json_object_set(sys, "heartbeat", heartbeat);
    }

    json_t *user = json_object_get(handshake_opts, "user");
    if(heartbeat) {
      json_object_set(body, "user", user);
    }
  }

  const char *data = json_dumps(body, JSON_COMPACT);
  if(data == NULL) {
    fprintf(stderr, "Fail to compose Pomelo handshake package.\n");
    goto error;
  }

  pc_buf_t buf = pc_pkg_encode(PC_PKG_HANDSHAKE, data, strlen(data));
  if(pc__binary_write(client, buf.base, buf.len, pc__handshake_req_cb)) {
    fprintf(stderr, "Fail to send handshake request.\n");
    goto error;
  }

  json_decref(body);

  return 0;
error:
  if(body) json_decref(body);
  return -1;
}
Пример #2
0
int pc__handshake_ack(pc_client_t *client) {
  pc_buf_t buf = pc_pkg_encode(PC_PKG_HANDSHAKE_ACK, NULL, 0);
  if(buf.len == -1) {
    fprintf(stderr, "Fail to encode handshake ack package.\n");
    goto error;
  }

  if(pc__binary_write(client, buf.base, buf.len, pc__handshake_ack_cb)) {
    fprintf(stderr, "Fail to send handshake ack.\n");
    goto error;
  }

  return 0;

error:
  if(buf.len != -1) free(buf.base);
  return -1;
}
Пример #3
0
int pc__heartbeat_req(pc_client_t *client) {
  if(PC_ST_WORKING != client->state) {
    return -1;
  }

  pc_buf_t buf = pc_pkg_encode(PC_PKG_HEARBEAT, NULL, 0);
  if(buf.len == -1) {
    fprintf(stderr, "Fail to encode heartbeat package.\n");
    goto error;
  }

  if(pc__binary_write(client, buf.base, buf.len, pc__heartbeat_req_cb)) {
    fprintf(stderr, "Fail to send heartbeat request.\n");
    goto error;
  }

  return 0;

error:
  if(buf.len != -1) free(buf.base);
  return -1;
}