static void end_test(grpc_end2end_test_fixture *f) {
  shutdown_server(f);
  shutdown_client(f);

  grpc_completion_queue_shutdown(f->server_cq);
  drain_cq(f->server_cq);
  grpc_completion_queue_destroy(f->server_cq);
  grpc_completion_queue_shutdown(f->client_cq);
  drain_cq(f->client_cq);
  grpc_completion_queue_destroy(f->client_cq);
}
Exemple #2
0
void *setup_client(struct list_loop *ls, void *data, struct sockaddr *addr, int fd)
{
  struct mul_client *ci;
  struct katcp_dispatch *d;

  ci = create_client(ls);
  if(ci == NULL){
    return NULL;
  }

  ci->c_dispatch = clone_katcp(d);
  if(ci->c_dispatch == NULL){
    shutdown_client(ci);
    return NULL;
  }

  set_multi_katcp(ci->c_dispatch, ci);
  exchange_katcp(ci->c_dispatch, fd);
  on_connect_katcp(ci->c_dispatch);

  return ci;
}
Exemple #3
0
int run_client(struct list_loop *ls, struct task_loop *tl, int mask)
{
  struct mul_client *ci;
  int update, idle, result;
#if 0
  struct task_loop *tx;
#endif
  struct link_loop *lk;
  struct mul_msg *mm;
  
  ci = user_loop(ls, tl);
  if(ci == NULL){
    stop_loop(ls, tl);
    return 0;
  }

  ci->c_task = tl;

  idle = LO_STOP_MASK | LO_WAIT_MASK;

#ifdef DEBUG
  fprintf(stderr, "client[%p]: mask 0x%02x\n", tl, mask);
#endif

  update = idle;
  if(mask & LO_STOP_MASK){
    /* WARNING: may have to remove fd to prevent almost harmless but ugly double close */
    shutdown_client(ci);
    return 0;
  }

  /* handle read case */
  update |= LO_READ_MASK;
  if(mask & LO_READ_MASK){
    result = read_katcp(ci->c_dispatch);
#ifdef DEBUG
    fprintf(stderr, "client: read code is %d\n", result);
#endif
    if(result < 0){
      stop_loop(ls, tl);
      return 0;
    }
    if(result > 0){
      update &= ~(LO_READ_MASK); /* on EOF stop reading */
    }
  }

  /* handle work function */
  result = lookup_katcp(ci->c_dispatch);
  if(result < 0){
    stop_loop(ls, tl);
    return 0;
  }

  if(result > 0){
    /* WARNING: statement triggers dispatch if no request pending (new) or if something has arrived (with pending request) */
    if((ci->c_waiting == 0) || (mask & LO_WAIT_MASK)){
#ifdef DEBUG
      fprintf(stderr, "client[%p]: calling dispatch (queue=%d)\n", tl, ci->c_queue);
#endif
      result = call_katcp(ci->c_dispatch);
      if(result == KATCP_RESULT_RESUME){
        ci->c_waiting = (ci->c_queue > 0) ? 1 : 0;
      } else {
        ci->c_waiting = 0;
        if(ci->c_queue > 0){ /* WARNING: this should actually be an abort, as the dispatch routines have broken the queue */
          fprintf(stderr, "client[%p]: warning: finished with %d outstanding requests\n", tl, ci->c_queue);
          log_message_katcp(ci->c_dispatch, KATCP_LEVEL_ERROR, NULL, "client function has %d outstand requests", ci->c_queue);
        }
        ci->c_queue = 0;
      }
    }
  }

  if(mask & LO_WAIT_MASK){
    if(!(ci->c_waiting)){ /* no dispatch function busy, so we handle the informs and ditch everything else */
#ifdef DEBUG
      fprintf(stderr, "client[%p]: idle, handling queue\n", tl);
#endif
      while((lk = receive_link_loop(ls, tl)) != NULL){
#ifdef DEBUG
        fprintf(stderr, "client[%p]: received event type %d\n", tl, lk->k_type);
#endif
        if((lk->k_type == ci->c_overall->o_type_msg) && (lk->k_data != NULL)){
          mm = lk->k_data;
          if(arg_inform_msg(mm)){ /* inform messages get sent out */
            dispatch_from_msg(ci->c_dispatch, mm);
          }
        }
        discard_link_loop(ls, lk);
      } 
    }
  }

  /* handle write case */
  if(mask & LO_WRITE_MASK){
    if(write_katcp(ci->c_dispatch) < 0){
      stop_loop(ls, tl);
      return 0;
    }
  }

  if(flushing_katcp(ci->c_dispatch)){
    /* WARNING: could disable reads and running if flush buffer too large */
    update |= LO_WRITE_MASK;
  }

  if(update == idle){
    /* if we are only interested in stopping then stop */
    stop_loop(ls, tl);
  }

  set_mask_loop(ls, tl, update, 0);

  if(exited_katcp(ci->c_dispatch) != KATCP_EXIT_NOTYET){
    /* TODO: make it stop */
  }

  return 0;
}
static gboolean
handle_command (BlockTxClient *client, int command)
{
    gboolean ret = FALSE;
    int rsp;

    switch (command) {
    case BLOCK_CLIENT_CMD_TRANSFER:
        /* Ignore TRANSFER command if client has been shutdown. */
        if (client->recv_state == RECV_STATE_DONE) {
            seaf_debug ("Client was shutdown, ignore transfer command.\n");
            break;
        }

        if (transfer_next_block (client) < 0) {
            rsp = client->info->result;
            pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp));

            shutdown_client (client);

            client->break_loop = FALSE;
        }
        break;
    case BLOCK_CLIENT_CMD_CANCEL:
        if (client->recv_state == RECV_STATE_DONE) {
            seaf_debug ("Client was shutdown, ignore cancel command.\n");
            break;
        }

        seaf_debug ("Canceled command received.\n");
        client->info->result = BLOCK_CLIENT_CANCELED;

        if (client->info->transfer_once) {
            shutdown_client (client);
            ret = TRUE;
        } else {
            rsp = client->info->result;
            pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp));

            shutdown_client (client);

            client->break_loop = FALSE;

            ret = FALSE;
        }

        break;
    case BLOCK_CLIENT_CMD_END:
        client->info->result = BLOCK_CLIENT_ENDED;

        rsp = client->info->result;
        pipewrite (client->info->done_pipe[1], &rsp, sizeof(rsp));

        /* Don't need to shutdown_client() if it's already called. */
        if (client->recv_state != RECV_STATE_DONE)
            shutdown_client (client);

        client->break_loop = FALSE;

        ret = TRUE;
        break;
    }

    return ret;
}
Exemple #5
0
void process_client_req(int fd)
{
  HIME_req req;
#if DBG
  dbg("svr--> process_client_req %d\n", fd);
#endif
  int rn = myread(fd, &req, sizeof(req));

  if (rn <= 0) {
    shutdown_client(fd);
    return;
  }
  if (hime_clients[fd].type == Connection_type_tcp) {
    __hime_enc_mem((u_char *)&req, sizeof(req), &srv_ip_port.passwd, &hime_clients[fd].seed);
  }
  to_hime_endian_4(&req.req_no);
  to_hime_endian_4(&req.client_win);
  to_hime_endian_4(&req.flag);
  to_hime_endian_2(&req.spot_location.x);
  to_hime_endian_2(&req.spot_location.y);

//  dbg("spot %d %d\n", req.spot_location.x, req.spot_location.y);

  ClientState *cs = NULL;

  if (current_CS && req.client_win == current_CS->client_win) {
    cs = current_CS;
  } else {
    int idx = fd;
    cs = hime_clients[fd].cs;

    int new_cli = 0;
    if (!cs) {
      cs = hime_clients[idx].cs = tzmalloc(ClientState, 1);
      new_cli = 1;
    }

    cs->client_win = req.client_win;
    cs->b_hime_protocol = TRUE;
    cs->input_style = InputStyleOverSpot;

    if (hime_init_im_enabled && ((hime_single_state && !is_init_im_enabled) || (!hime_single_state && new_cli))) {
      dbg("new_cli default_input_method:%d\n", default_input_method);
      is_init_im_enabled = TRUE;
      current_CS = cs;
      save_CS_temp_to_current();
      init_state_chinese(cs);
    }
  }

  if (!cs)
    p_err("bad cs\n");

  if (req.req_no != HIME_req_message) {
    cs->spot_location.x = req.spot_location.x;
    cs->spot_location.y = req.spot_location.y;
  }

  gboolean status;
  HIME_reply reply;
  bzero(&reply, sizeof(reply));

  switch (req.req_no) {
    case HIME_req_key_press:
    case HIME_req_key_release:
      current_CS = cs;
      save_CS_temp_to_current();

#if DBG && 0
      {
        char tt[128];

        if (req.keyeve.key < 127) {
          sprintf(tt,"'%c'", req.keyeve.key);
        } else {
          strcpy(tt, XKeysymToString(req.keyeve.key));
        }

        dbg_time("HIME_key_press  %x %s\n", cs, tt);
      }
#endif
      to_hime_endian_4(&req.keyeve.key);
      to_hime_endian_4(&req.keyeve.state);

//	  dbg("serv key eve %x %x predit:%d\n",req.keyeve.key, req.keyeve.state, cs->use_preedit);

#if DBG
	  char *typ;
      typ="press";
#endif
#if 0
      if (req.req_no==HIME_req_key_press)
        status = Process2KeyPress(req.keyeve.key, req.keyeve.state);
      else {
        status = Process2KeyRelease(req.keyeve.key, req.keyeve.state);
#else
      if (req.req_no==HIME_req_key_press)
        status = ProcessKeyPress(req.keyeve.key, req.keyeve.state);
      else {
        status = ProcessKeyRelease(req.keyeve.key, req.keyeve.state);
#endif

#if DBG
        typ="rele";
#endif
      }

      if (status)
        reply.flag |= HIME_reply_key_processed;
#if DBG
      dbg("%s srv flag:%x status:%d len:%d %x %c\n",typ, reply.flag, status, output_bufferN, req.keyeve.key,req.keyeve.key & 0x7f);
#endif
      int datalen;
      datalen = reply.datalen =
        output_bufferN ? output_bufferN + 1 : 0; // include '\0'
      to_hime_endian_4(&reply.flag);
      to_hime_endian_4(&reply.datalen);
      write_enc(fd, &reply, sizeof(reply));

//      dbg("server reply.flag %x\n", reply.flag);

      if (output_bufferN) {
        write_enc(fd, output_buffer, datalen);
        clear_output_buffer();
      }

      break;
    case HIME_req_focus_in:
#if DBG
      dbg_time("HIME_req_focus_in  %x %d %d\n",cs, cs->spot_location.x, cs->spot_location.y);
#endif
//      current_CS = cs;
      hime_FocusIn(cs);
      break;
    case HIME_req_focus_out:
#if DBG
      dbg_time("HIME_req_focus_out  %x\n", cs);
#endif
      hime_FocusOut(cs);
      break;
    case HIME_req_focus_out2:
      {
#if DBG
      dbg_time("HIME_req_focus_out2  %x\n", cs);
#endif
      if (hime_FocusOut(cs))
        flush_edit_buffer();

      HIME_reply reply;
      bzero(&reply, sizeof(reply));

      int datalen = reply.datalen =
        output_bufferN ? output_bufferN + 1 : 0; // include '\0'
      to_hime_endian_4(&reply.flag);
      to_hime_endian_4(&reply.datalen);
      write_enc(fd, &reply, sizeof(reply));

//      dbg("server reply.flag %x\n", reply.flag);

      if (output_bufferN) {
        write_enc(fd, output_buffer, datalen);
        clear_output_buffer();
      }
      }
      break;
    case HIME_req_set_cursor_location:
#if DBG
      dbg_time("set_cursor_location %x %d %d\n", cs,
         cs->spot_location.x, cs->spot_location.y);
#endif
      update_in_win_pos();
      break;
    case HIME_req_set_flags:
//      dbg("HIME_req_set_flags\n");
      if (BITON(req.flag, FLAG_HIME_client_handle_raise_window)) {
#if DBG
        dbg("********* raise * window\n");
#endif
        if (!hime_pop_up_win)
          cs->b_raise_window = TRUE;
      }

	  if (req.flag & FLAG_HIME_client_handle_use_preedit)
        cs->use_preedit = TRUE;

      int rflags;
      rflags = 0;
      if (hime_pop_up_win)
        rflags = FLAG_HIME_srv_ret_status_use_pop_up;

      write_enc(fd, &rflags, sizeof(rflags));
      break;
    case HIME_req_get_preedit:
      {
#if DBG
      dbg("svr HIME_req_get_preedit %x\n", cs);
#endif
      char str[HIME_PREEDIT_MAX_STR];
      HIME_PREEDIT_ATTR attr[HIME_PREEDIT_ATTR_MAX_N];
      int cursor, sub_comp_len;
      int attrN = hime_get_preedit(cs, str, attr, &cursor, &sub_comp_len);
      if (hime_edit_display&(HIME_EDIT_DISPLAY_BOTH|HIME_EDIT_DISPLAY_OVER_THE_SPOT))
        cursor=0;
      if (hime_edit_display&HIME_EDIT_DISPLAY_OVER_THE_SPOT) {
        attrN=0;
        str[0]=0;
      }
      int len = strlen(str)+1; // including \0
      write_enc(fd, &len, sizeof(len));
      write_enc(fd, str, len);
//      dbg("attrN:%d\n", attrN);
      write_enc(fd, &attrN, sizeof(attrN));
      if (attrN > 0)
        write_enc(fd, attr, sizeof(HIME_PREEDIT_ATTR)*attrN);
        write_enc(fd, &cursor, sizeof(cursor));
        write_enc(fd, &sub_comp_len, sizeof(sub_comp_len));
//      dbg("uuuuuuuuuuuuuuuuu len:%d %d cursor:%d\n", len, attrN, cursor);
      }
      break;
    case HIME_req_reset:
      hime_reset();
      break;
    case HIME_req_message:
      {
//        dbg("HIME_req_message\n");
        short len=0;
        int rn = myread(fd, &len, sizeof(len));
        if (rn <= 0) {
cli_down:
          shutdown_client(fd);
          return;
        }

        // only unix socket, no decrypt
        char buf[512];
        // message should include '\0'
        if (len > 0 && len < sizeof(buf)) {
          if (myread(fd, buf, len)<=0)
            goto cli_down;
          message_cb(buf);
        }
      }
      break;
    default:
      dbg_time("Invalid request %x from:", req.req_no);
      struct sockaddr_in addr;
      socklen_t len=sizeof(addr);
      bzero(&addr, sizeof(addr));

      if (!getpeername(fd, (struct sockaddr *)&addr, &len)) {
        dbg("%s\n", inet_ntoa(addr.sin_addr));
      } else {
        perror("getpeername\n");
      }
      shutdown_client(fd);
      break;
  }
}