예제 #1
0
파일: cconnp.c 프로젝트: petercloud/RFS
CSOCKET_CNODE *cconnp_reserve(CCONNP *cconnp)
{
    CSOCKET_CNODE *csocket_cnode;
    int            sockfd;

    if(EC_TRUE == cqueue_is_empty(CCONNP_IDLE_CONN_QUEUE(cconnp)))
    {
        CQUEUE_DATA *cqueue_data;
     
        /*if no idle, new one*/
        if(csocket_connect(CCONNP_SRV_IPADDR(cconnp), CCONNP_SRV_PORT(cconnp), CSOCKET_IS_NONBLOCK_MODE, &sockfd))
        {
            dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: connect server %s:%ld failed\n",
                                CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
            return (NULL_PTR);
        }

        csocket_cnode = csocket_cnode_new(CCONNP_SRV_TCID(cconnp), sockfd, CSOCKET_TYPE_TCP, CCONNP_SRV_IPADDR(cconnp), CCONNP_SRV_PORT(cconnp));
        if(NULL_PTR == csocket_cnode)
        {
            dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: new csocket_cnode for socket %d to server %s:%ld failed\n",
                            sockfd, CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
            csocket_close(sockfd);
            return (NULL_PTR);
        }

        cqueue_data = cqueue_push(CCONNP_IDLE_CONN_QUEUE(cconnp), (void *)csocket_cnode);
        if(NULL_PTR == cqueue_data)
        {
            dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: push socket %d to server %s:%ld failed\n",
                            sockfd, CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
            csocket_cnode_free(csocket_cnode);
            return (NULL_PTR);
        }
     
        CSOCKET_CNODE_WORK_NODE(csocket_cnode)    = (void *)cqueue_data;
        CSOCKET_CNODE_WORK_OWNER(csocket_cnode)   = (void *)cconnp;
        CSOCKET_CNODE_WORK_RELEASE(csocket_cnode) = (CSOCKET_CNODE_WORK_REL)cconnp_erase;;
        CSOCKET_CNODE_WORK_STATUS(csocket_cnode)  = CSOCKET_CNODE_WORK_STATUS_IDLE;

        dbg_log(SEC_0154_CCONNP, 9)(LOGSTDOUT, "[DEBUG] cconnp_reserve: create and push sockfd %d to server %s:%ld done\n",
                        CSOCKET_CNODE_SOCKFD(csocket_cnode),
                        CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));      
    }

    /*reserve one idle*/
    csocket_cnode = cqueue_pop(CCONNP_IDLE_CONN_QUEUE(cconnp));
    if(NULL_PTR == csocket_cnode)
    {
        dbg_log(SEC_0154_CCONNP, 0)(LOGSTDOUT, "error:cconnp_reserve: server %s:%ld has no idle conn\n",
                        CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));
        return (NULL_PTR);
    }

    if(EC_TRUE == CSOCKET_CNODE_WORK_PUSHED(csocket_cnode))
    {
        /*when csocket_cnode was released and pushed to connp, RD event was set. here need to clear it*/
        cepoll_del_event(task_brd_default_get_cepoll(), CSOCKET_CNODE_SOCKFD(csocket_cnode), CEPOLL_RD_EVENT);
        CSOCKET_CNODE_WORK_PUSHED(csocket_cnode) = EC_FALSE;
    }
 
    CSOCKET_CNODE_WORK_NODE(csocket_cnode)    = NULL_PTR;
    CSOCKET_CNODE_WORK_OWNER(csocket_cnode)   = (void *)cconnp;
    CSOCKET_CNODE_WORK_RELEASE(csocket_cnode) = (CSOCKET_CNODE_WORK_REL)cconnp_release;
    CSOCKET_CNODE_WORK_STATUS(csocket_cnode)  = CSOCKET_CNODE_WORK_STATUS_NONE;

    dbg_log(SEC_0154_CCONNP, 9)(LOGSTDOUT, "[DEBUG] cconnp_reserve: pop sockfd %d from server %s:%ld done\n",
                    CSOCKET_CNODE_SOCKFD(csocket_cnode),
                    CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp));  
    return (csocket_cnode);
}
예제 #2
0
파일: main.c 프로젝트: wgm/cerb2-cparser
int main(int argc, char ** argv)
{
  int bytes = 0;
  CSOCKET* sock=NULL;
  CLOG_INFO* log=NULL;
  CSTRING* buf = NULL;

  // open the logfile
  log = clog_open("./test_csocket.log", CTRACE, NULL, 0);

  if(3>argc) {
    clog(log, CFATAL, "USAGE: %s host user password", argv[0]);
    clog_close(log);
    return 1;
  }
  
  // create the new socket  
  sock = csocket_new(log);

  // initialize the socket
  csocket_init(log, sock, argv[1], 110);

  // connect
  csocket_connect(log, sock);

  // read/write
  buf = cstring_new(1024);
  bytes=csocket_read(log, sock, buf->string, 1024);
  fprintf(stdout, "%s", buf->string);

  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  cstring_strcat_imp(buf, "USER ", 5);
  cstring_strcat_imp(buf, argv[2], strlen(argv[2]));  
  cstring_strcat_imp(buf, "\r\n", 2);
  csocket_write(log, sock, buf->string, buf->length);
  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  bytes=csocket_read(log, sock, buf->string, 1024);
  fprintf(stdout, "%s", buf->string);

  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  cstring_strcat_imp(buf, "PASS ", 5);
  cstring_strcat_imp(buf, argv[3], strlen(argv[3]));
  cstring_strcat_imp(buf, "\r\n", 2);
  csocket_write(log, sock, buf->string, buf->length);
  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  bytes=csocket_read(log, sock, buf->string, 1024);
  fprintf(stdout, "%s", buf->string);

  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  cstring_strcat_imp(buf, "STAT\r\n", 6);
  csocket_write(log, sock, buf->string, buf->length);
  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  bytes=csocket_read(log, sock, buf->string, 1024);
  fprintf(stdout, "%s", buf->string);

  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  cstring_strcat_imp(buf, "QUIT\r\n", 6);
  csocket_write(log, sock, buf->string, buf->length);
  buf->length=0;
  memset(buf->string, 0, buf->memsize);
  bytes=csocket_read(log, sock, buf->string, 1024);
  fprintf(stdout, "%s", buf->string);

  cstring_free(&buf);

  // close the connection
  csocket_close(log, sock);

  // free the socket
  csocket_free(log, sock);

  // close the log
  clog_close(log);
  
  return 0;
}