Example #1
0
void pc_client_destroy(pc_client_t *client) {
  pc_client_state state;

  uv_mutex_lock(&client->state_mutex);
  state = client->state;
  uv_mutex_unlock(&client->state_mutex);

  if(PC_ST_INITED == client->state) {
    goto finally;
  }

  if(PC_ST_CLOSED == client->state) {
    goto finally;
  }

  pc_client_disconnect(client);
  pc_client_join(client);

finally:
  pc__client_clear(client);

  if(client->uv_loop) {
    uv_loop_delete(client->uv_loop);
    client->uv_loop = NULL;
  }
  pc_jsonp_free(client);
}
Example #2
0
int main() {
  // create a client instance.
  pc_client_t *client = pc_client_new();

  struct sockaddr_in address;

  memset(&address, 0, sizeof(struct sockaddr_in));
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  address.sin_addr.s_addr = inet_addr(ip);

  // add some event callback.
  pc_add_listener(client, "onHey", on_hey);
  pc_add_listener(client, PC_EVENT_DISCONNECT, on_close);

  // try to connect to server.
  if(pc_client_connect(client, &address)) {
    printf("fail to connect server.\n");
    pc_client_destroy(client);
    return 1;
  }

  do_notify(client);

  // main thread has nothing to do and wait until child thread return.
  pc_client_join(client);

  // release the client
  pc_client_destroy(client);

  return 0;
}
Example #3
0
void pc_client_destroy(pc_client_t *client) {
  if(PC_ST_INITED == client->state) {
    pc__client_clear(client);
    goto finally;
  }

  if(PC_ST_CLOSED == client->state) {
    pc__client_clear(client);
    goto finally;
  }

  // 1. asyn worker thread
  // 2. wait work thread exit
  // 3. free client
  uv_async_send(client->close_async);

  pc_client_join(client);

  if(PC_ST_CLOSED != client->state) {
    pc_client_stop(client);
    // wait uv_loop_t stop
    sleep(1);
    pc__client_clear(client);
  }

finally:
  if(client->uv_loop) {
    uv_loop_delete(client->uv_loop);
    client->uv_loop = NULL;
  }
  free(client);
}
Example #4
0
void Login::doLogin()
{
    const char *ip = GATE_HOST;
    int port = GATE_PORT;

    pc_client_t *client = pc_client_new();

    struct sockaddr_in address;

    memset(&address, 0, sizeof(struct sockaddr_in));
    address.sin_family = AF_INET;
    address.sin_port = htons(port);
    address.sin_addr.s_addr = inet_addr(ip);

    // try to connect to server.
    if(pc_client_connect(client, &address)) {
        CCLOG("fail to connect server.\n");
        pc_client_destroy(client);
        return ;
    }

    // add some event callback.
    pc_add_listener(client, PC_EVENT_DISCONNECT, on_close);

    const char *route = "gate.gateHandler.queryEntry";
    json_t *msg = json_object();
    json_t *str = json_string(username.c_str());
    json_object_set(msg, "uid", str);
    // decref for json object
    json_decref(str);

    pc_request_t *request = pc_request_new();
    void (*on_request_gate_cb)(pc_request_t *req, int status, json_t *resp) = &Login::requstGateCallback;
    pc_request(client, request, route, msg, on_request_gate_cb);

    // main thread has nothing to do and wait until child thread return.
    pc_client_join(client);

    // release the client
    pc_client_destroy(client);
}
Example #5
0
int main() {
  pc_client_t *client = pc_client_new();

  struct sockaddr_in address;

  memset(&address, 0, sizeof(struct sockaddr_in));
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  address.sin_addr.s_addr = inet_addr(ip);

  pc_add_listener(client, "onHey", on_hey);
  pc_add_listener(client, PC_EVENT_DISCONNECT, on_close);

  if(pc_client_connect(client, &address)) {
    printf("fail to connect server.\n");
    goto error;
  }

  int i;
  for(i=0; i<count; i++) {
    do_notify(client);
  }

  pc_client_join(client);

  //sleep(3);

  printf("before main return.\n");

  pc_client_destroy(client);

  return 0;

error:
  pc_client_destroy(client);
  return 1;
}