Ejemplo n.º 1
0
THREAD_PROC client_writer(void *arg)
{
    Client *C = (Client*) arg;
    EZS *E = C->E;
    char *msg;
    int rc;
    int no = C->no;

    THREAD_DETACH;
    THREAD_OKCANCEL;
    C->state++;
    tot_wt++;

    if (debug>1) fprintf(stderr, "new client_writer\n");

    while (C->state) {
       msg = get_client_msg(C);
       if (msg) {
          rc = ezs_write(E, msg, strlen(msg));
          if (debug) printf("[%d] sent %d bytes: %s\n", C->no, rc, msg);
          if (rc<=0) {
              report_err(E, "write");
              if (ezs_geterror(E, NULL)==EZS_ERR_NO_CONNECTION) break;
          }
       } else sleep(1);
    }

    if (C->state==0) {
       ezs_disconnect(E);
       free_client(C);
    } else C->state = 0;

    tot_wt--;
}
Ejemplo n.º 2
0
void channel_handle_client_read(connector_t pconn, int event)
{
    //由于和客户端只有一次交互,不用一直读取
    if (connector_read(pconn, event) > 0)
    {
        char *val = buffer_get_read(pconn->preadbuf);
        message_t pmsg = (message_t)malloc(sizeof(message));
        memset(pmsg, 0, sizeof(pmsg));
        size_t len1 = get_client_msg(val, pmsg);

        if (len1 == 0)
        {
            print_log(LOG_TYPE_ERROR, "Read Client Msg Error %s", val);
            free(pmsg);
            return;
        }

        char data[20] = {0};
        memcpy(data, pmsg->uid, pmsg->len);
        buffer_read(pconn->preadbuf, len1, TRUE);

        memcpy(pconn->uid, data, pmsg->len);

        int len2 = sizeof(connector_t);
        ht_insert(pconn->pworker->pht, data, (pmsg->len)+1, pconn, len2+1);

        context_t pcontext = (context_t)malloc(sizeof(context));
        memset(pcontext, 0, sizeof(context));
        memcpy(pcontext->data, data, pmsg->len);
        list_push_tail(pconn->pworker->plist, pcontext);

        //print_log(LOG_TYPE_DEBUG, "Hash key %s, Len %d", pcontext->data, pmsg->len);

        char cmd[REDIS_CMD_LEN] = {'\0'};
        get_request_str(data, cmd);

        int len = strlen(cmd);

        if (pconn->pworker->redis->state == CONN_STATE_RUN)
        {
            buffer_write(pconn->pworker->redis->pwritebuf, cmd, len);
            connector_write(pconn->pworker->redis);
        }
        else
        {
            print_log(LOG_TYPE_ERROR, "Redis not run");
            list_pop_head(pconn->pworker->plist);
            ht_remove(pconn->pworker->pht, data, (pmsg->len)+1);
            pconn->pworker->neterr_count++;
        }

        free(pmsg);
    }
}