EC_BOOL cconnp_release(CCONNP *cconnp, CSOCKET_CNODE *csocket_cnode) { CQUEUE_DATA *cqueue_data; CTMV *ctmv; ASSERT(CSOCKET_CNODE_WORK_STATUS_NONE == CSOCKET_CNODE_WORK_STATUS(csocket_cnode)); ctmv = task_brd_default_get_daytime(); CSOCKET_CNODE_WORK_EXPIRED_MSEC(csocket_cnode) = CTMV_NSEC(ctmv) * 1000 + CTMV_MSEC(ctmv) + CCONNP_EXPIRED_MSEC(cconnp); 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_release: push sockfd %d to server %s:%ld failed\n", CSOCKET_CNODE_SOCKFD(csocket_cnode), CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp)); return (EC_FALSE); } 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; CSOCKET_CNODE_WORK_PUSHED(csocket_cnode) = EC_TRUE; /*when idle, client should never receive data from server. if RD event happen, connection must be broken*/ cepoll_set_event(task_brd_default_get_cepoll(), CSOCKET_CNODE_SOCKFD(csocket_cnode), CEPOLL_RD_EVENT, (CEPOLL_EVENT_HANDLER)__csocket_cnode_detect_broken, /*which return false to trigger shutdown*/ (void *)csocket_cnode); cepoll_set_shutdown(task_brd_default_get_cepoll(), CSOCKET_CNODE_SOCKFD(csocket_cnode), (CEPOLL_EVENT_HANDLER)csocket_cnode_force_close, /*trigger cconnp_erase and then close socket*/ (void *)csocket_cnode); cepoll_set_timeout(task_brd_default_get_cepoll(), CSOCKET_CNODE_SOCKFD(csocket_cnode), (uint32_t)CONN_TIMEOUT_NSEC, (CEPOLL_EVENT_HANDLER)csocket_cnode_force_close, (void *)csocket_cnode); dbg_log(SEC_0154_CCONNP, 9)(LOGSTDOUT, "[DEBUG] cconnp_release: push sockfd %d to server %s:%ld done\n", CSOCKET_CNODE_SOCKFD(csocket_cnode), CCONNP_SRV_IPADDR_STR(cconnp), CCONNP_SRV_PORT(cconnp)); return (EC_TRUE); }
static int test_cqueue(void) { test_start("cqueue"); cqueue_t q; cqueue_init(&q, 62); for (int k = 0; k < 100; k++) { for (int i = 0; i < 60; i++) { cqueue_push(&q, (cqueue_item_t) i); } for (int i = 0; i < 60; i++) { int item = (int) cqueue_pop(&q); test_assert(item == i); } } cqueue_free(&q); return test_success(); }
int main() { int ch=1,dt; uint16_t i,sz; uint8_t data; #ifndef WIN32 system("clear"); #else system("cls"); #endif if(cqueue_init(&cqn,dir,MAX)!=STATUS_SUCCESS) { printf("\n Error in Initializing "); exit(1); } while(ch!= 0) { printf("\n "); printf("\n 1. Circular Queue INSERT "); printf("\n 2. Circular Queue DELETE "); printf("\n 3. Circular Queue DISPLAY"); printf("\n 4. Circular Queue Move"); printf("\n 5. Circular Queue Move Into Queue(Excess)"); printf("\n 6. Circular Queue Move Into Queue(Correct)"); printf("\n 0. EXIT\n"); printf("\nEnter your choice : "); scanf("%d",&ch); ch = 0; // Intentional switch(ch) { case 1: printf("\n Enter the Integer Data to be inserted: "); scanf("%d",&dt); data = dt & 0x0FF; dt = cqueue_push(&cqn,data); if(dt!=STATUS_SUCCESS) { printf("\n Could not Insert data Due to %d status", dt); } break; case 2: if(cqueue_empty(&cqn)==STATUS_SUCCESS) printf("\nqueue is empty\n"); else { if(cqueue_pop(&cqn,&data)==STATUS_SUCCESS) printf("\n the deleted element is: 0x%02X",data); else printf("\n Error in Receiving Data \n"); } break; case 3: if(cqueue_empty(&cqn)==STATUS_SUCCESS) printf("\nqueue is empty"); else { for(i=0;i<cqn.max;i++)prn[i]=0; if(cqueue_copydata(&cqn,prn,&sz,cqn.max)==STATUS_SUCCESS) { for(i=0;i<sz;i++) printf("\n (%2d)= 0x%02X",i+1,prn[i]); } } break; case 4: if(cqueue_empty(&cqn)==STATUS_SUCCESS) printf("\nqueue is empty"); else { for(i=0;i<cqn.max;i++)prn[i]=0; if(cqueue_movedata(&cqn,prn,&sz,cqn.max)==STATUS_SUCCESS) { printf("\n Size Copied: %d",sz); for(i=0;i<sz;i++) printf("\n (%2d)= 0x%02X",i+1,prn[i]); } } break; case 5: printf("\n The following Data would be copied into Queue:"); printf("\n uint8_t temp[5]={0x11,0x22,0x33,0x44,0x55};"); printf("\nStatus would be 3 as only 4 bytes can be inserted"); dt = cqueue_movetoqueue(&cqn,temp,&sz,cqn.max); printf("\n Data Copied: %d",sz); printf("\n Status Returned: %d",dt); break; case 6: printf("\n The following Data would be copied into Queue:"); printf("\n uint8_t temp[5]={0x11,0x22,0x33,0x44,0x55};"); printf("\nStatus would be 0 as only 4 bytes to insert"); dt = cqueue_movetoqueue(&cqn,temp,&sz,cqn.max-1); printf("\n Data Copied: %d",sz); printf("\n Status Returned: %d",dt); break; } } getchar(); return 0; }
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); }
int main(void) { cq = cqueue_new(1024 * 80, 1000, QF_NOTIFY | QF_LOCK | QF_SHM); printCqueue(cq); pid_t pid; int z, worker_num = 5; for (z = 0; z < worker_num; z++) { if ((pid = fork()) < 0) { printf("fork"); exit(1); } else if (pid > 0) { printf("created child process success. [pid = %d]", (int)pid); continue; } else { //child process int recvn = 0; struct item_bz tm_bz; char fname[56]; char mbuff[256]; FILE *fp; sprintf(fname, "/root/src/log_%d.txt", z); fp = fopen(fname, "a+"); while (1) { if (cqueue_wait(cq) > 0) { if (cqueue_pop(cq, &tm_bz, sizeof(struct item_bz)) < 0) continue; recvn++; printf("worker[%d] recv: [buf=%s] [rand=%d]", z, tm_bz.buf, tm_bz.a); sprintf(mbuff, "worker[%d] recv: [buf=%s] [rand=%d]\n", z, tm_bz.buf, tm_bz.a); //write log to file[i]; fputs(mbuff, fp); } } printf("worker[%d] finish: [recvn=%d]", z, recvn); fclose(fp); exit(0); } } label: sleep(1); int sendn = 0; int inum = 10000; while (inum > 0) { struct item_bz ibz = {"--||||||||mmmmmm$$########", rand() % 11, &ibz}; if (cqueue_push(cq, &ibz, sizeof (struct item_bz)) == 0) { cqueue_notify(cq); sendn++; inum--; } } printCqueue(cq); printf("master send finish: [num=%d] [sendn=%d]", inum, sendn); int status; for (z = 0; z < worker_num; z++) { wait(&status); } cqueue_free(cq); return 0; }