Exemple #1
0
int pc_client_connect3(pc_client_t *client, struct sockaddr_in *addr) {
  pc_connect_t *conn_req = pc_connect_req_new(addr);

  if(client->enable_reconnect){
    memcpy(&client->addr, addr, sizeof(struct sockaddr_in));
  }

  if(conn_req == NULL) {
    fprintf(stderr, "Fail to malloc pc_connect_t.\n");
    goto error;
  }

  if(pc_connect(client, conn_req, NULL, pc__client_connect3_cb)) {
    fprintf(stderr, "Fail to connect to server.\n");
    goto error;
  }

  uv_thread_create(&client->worker, pc__worker, client);

  return 0;

error:
  if(conn_req) pc_connect_req_destroy(conn_req);
  return -1;
}
Exemple #2
0
int pc_client_connect(pc_client_t *client, struct sockaddr_in *addr) {
  pc_connect_t *conn_req = pc_connect_req_new(addr);

  if(conn_req == NULL) {
    LOGD( "Fail to malloc pc_connect_t.\n");
    goto error;
  }

  if(pc_connect(client, conn_req, NULL, pc__client_connected_cb)) {
    LOGD( "Fail to connect to server.\n");
    goto error;
  }

  // 1. start work thread
  // 2. wait connect result

  uv_thread_create(&client->worker, pc__worker, client);

  // TODO should set a timeout?
  pc__cond_wait(client, 0);

  pc_connect_req_destroy(conn_req);

  if(PC_ST_WORKING != client->state) {
    return -1;
  }

  return 0;
error:
  if(conn_req) pc_connect_req_destroy(conn_req);
  return -1;
}
Exemple #3
0
int pc_client_connect2(pc_client_t *client, pc_connect_t *conn_req, pc_connect_cb cb){
  if(client->enable_reconnect){
    memcpy(&client->addr, conn_req->address, sizeof(struct sockaddr_in));
  }

  if(pc_connect(client, conn_req, NULL, cb)){
    // When failed, user should be responsible of reclaiming conn_req's memory
	// When succeeded, user should be responsible of reclaiming con_req's memory in cb
	// This API do not hold any assumption of conn_req(How it resides in memory)
    fprintf(stderr,"Fail to connect server.\n");
	return -1;
  }
  
  uv_thread_create(&client->worker, pc__worker, client);
  return 0;
}
Exemple #4
0
void pc__client_reconnect_timer_cb(uv_timer_t* timer, int status) {
  /* unused */
  (void)status;
  pc_client_t* client = (pc_client_t*)timer->data;
  pc_connect_t* conn_req = pc_connect_req_new(&client->addr);
  if (!conn_req) {
    fprintf(stderr, "out of memory");
    pc_client_stop(client);
  }

  if(pc_connect(client, conn_req, NULL, pc__client_reconnected_cb)) {
    fprintf(stderr, "Fail to connect to server.\n");
    pc_connect_req_destroy(conn_req);
    client->reconnecting = 0;
    pc_client_stop(client);
  }
}