Esempio n. 1
0
int main(int argc, char *argv[])
{
    threadpool_t *pool = threadpool_init(20, 40); // 20个线程,40工作

    int listenfd, clientfd;

    char ch;
    int port_flag = 0;
    char *port = NULL;
    while ((ch = getopt(argc, argv, "p:")) != -1)
    {
        switch (ch)
        {
            case 'p':
                port = optarg;
                printf("port is %s\n", port);
                port_flag = 1;
                break;
            case '?':
                fprintf(stderr, "Unknown command opt: %c\n", (char)optopt);
                break;
        }
    }

    if (!port_flag)
    {
        fprintf(stderr, "usage: %s <port>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* 设置信号 */
    signal(SIGPIPE, SIG_IGN);

    listenfd = tcp_server(NULL, atoi(port));
    while (1)
    {
        if ((clientfd = accept(listenfd, NULL, NULL)) < 0)
        {
            err_sys("accept");
        }
        
        /* 用线程池来处理连接 */
        int *tmp_fd = malloc(sizeof(int));
        *tmp_fd = clientfd;
        threadpool_add_job(pool, doit, (void *)tmp_fd);
    }

    // threadpool_destory(pool);
    // 这里应该接收ctrl c 信号,然后调用threadpool_destory函数
    exit(EXIT_SUCCESS);
}
Esempio n. 2
0
File: main.c Progetto: 7ym0n/hack
int main(void)
{
    struct threadpool *pool = threadpool_init(10, 1000);
    int n;
    char s[255];
    //bzero(&s,0x00);
    for(n = 1;n<9;n++){
	//itoa(n,s,4);
	printf("%d",n);
	sprintf(s,"%d",n);
	threadpool_add_job(pool, work, s);
    }

    sleep(5);
    threadpool_destroy(pool);
    return 0;
}
Esempio n. 3
0
void *File_Process(void *arg)
{
	pthread_detach(pthread_self());

    // add by andy for more info
    THREAD_INFO *tinfo = (THREAD_INFO *)arg;

    //mqid = (int)arg;
    mqid = tinfo->mqid;
    struct threadpool *pool = tinfo->pool;

	PRINTF(LEVEL_INFORM,"thread mqid = %d \n",mqid);
	//fprintf(stderr,"--->test = %d \n",test);

    //init thread share info
    MUTEX_SETUP(share_info.lock);
    share_info.dir1 = strdup(tinfo->dir1);
    share_info.dir2 = strdup(tinfo->dir2);
    share_info.app_name = strdup(tinfo->app_name);
    share_info.move_file = tinfo->move_file;
    share_info.m_addr = tinfo->m_addr;

    share_info.m_sock = Socket(AF_INET,SOCK_DGRAM,0);
	int fd;
	FILE *fp;
	struct msgbuf *buff = malloc(MAXMSG);
	int n , ret;

	mode_t fdmode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
	int wlen;

	long long pack_total;
	long long pack_saved; 
	long long pack_move; 


	char *msg = NULL;

	//char save_path[512] ={0};
	//add by andy ,fix bug tcp_echo err ,2014-12-12
	char save_path[1024] ={0};

	/**UDP 数据包结构**/
	char protocol;

	long long file_size;
	int file_name_length;
	char *file_name;
	int pack_length;
	char *pack;

again:	
	pack_saved = 0;
	pack_move = 1;
	pack_total = 10;

	//这句可能是多余的	
	memset(buff,0,MAXMSG);	


	while(1)
	{	
		if((n = msgrcv(mqid,buff,MAXMSG,pack_move,IPC_NOWAIT)) < 0)
		//if((n = msgrcv(mqid,buff,MAXMSG,pack_move,0)) < 0)
		{
			n = msgrcv(mqid,buff,MAXMSG,0,0);
			if(n < 0)
			{
				//PRINTF(LEVEL_ERROR,"thread msgrcv error :%s\n",strerror(errno));
				//WriteSysLog(LOG_PATH,log);
				goto err_end;
				continue;
			}
			pack_move = buff->mtype;
		}

		pack_move ++;

		msg = buff->mtext;

		if((pack_saved == 0)||(pack_move == 2))
		{
			if((pack_move == 2)&&(pack_saved !=0))
			{
				//PRINTF(LEVEL_ERROR,"A file is incomplete because client stop transmite by accident\n");
				// write log

				pack_saved = 0;
				if(fp != NULL)
				{
					fclose(fp);
					fp = NULL;
				}
			}

			memcpy(&file_size,msg+10,8);
			pack_total = file_size/1024;
			if(file_size%1024 != 0)
				pack_total+=1;

			memcpy(&file_name_length,msg+18,4);
			file_name = (char *)malloc(file_name_length+1);
			memcpy(file_name,msg+22,file_name_length);
			*(file_name+file_name_length) = '\0';
            // usr dir install of true path ,andy,2015-03-07
            //sprintf(save_path,RUN_PATH);
            //strcat(save_path,file_name);
            sprintf(save_path,"%s/%s/",tinfo->dir1,tinfo->app_name);
            strcat(save_path,basename(file_name));

            //memcpy(&pack_length,msg+22+file_name_length,4);

			pack = msg +22 +file_name_length +4;

			ret = CreateDir(save_path);
			if(ret < 0)
			{
				;
			}
			fp = fopen(save_path,"w+");
			if(fp == NULL)
			{
				PRINTF(LEVEL_ERROR,"Open %s error:%s",file_name,strerror(errno)); 
 				goto err_end;
			}
		}


        memcpy(&pack_length,msg+22+file_name_length,4);

        int n = fwrite(pack,sizeof(char),pack_length,fp);
		if(n != pack_length)
		{
			PRINTF(LEVEL_ERROR,"Write %s error :%s\n",file_name,strerror(errno));
			goto err_end;
		}

		pack_saved++;				/**写文件成功,记录**/

		//检查文件大小
		if(pack_total == pack_saved )
		{
            //PRINTF(LEVEL_ERROR,"Last Pack Size :%d\n",pack_length);
			fclose(fp);
			fp = NULL;

            threadpool_add_job(pool,Recv_Finish,save_path);
            //tcp_echo(save_path,strlen(save_path),t_sockfd);
            free(file_name);

			goto again;
		}
		else if(pack_total == (pack_move -1))
		{
			//PRINTF(LEVEL_ERROR,"## lost package ## File %s is incomplete\n",file_name);

			fclose(fp);

			fp = NULL;
			//remove(save_path);
            //tcp_echo(save_path,strlen(save_path),t_sockfd);
			free(file_name);
			goto again;
		}


	}

err_end:	
	free(buff);
	free(file_name);
	exit(-1);
	return NULL;
}
Esempio n. 4
0
int main(int argc, char **argv)
{
	int listener, kdpfd, nfds, n, curfds;
	socklen_t len;
	struct sockaddr_in my_addr, their_addr;
	unsigned int myport;
	struct epoll_event ev;
	struct epoll_event events[MAXEPOLLSIZE];
	struct rlimit rt;

	myport = 1234;

	pthread_t thread;
	pthread_attr_t attr;

	/* init thread pool */
	struct st_threadpool *pool = threadpool_init(10, 20);

	/* 设置每个进程允许打开的最大文件数 */
	rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;
	if (setrlimit(RLIMIT_NOFILE, &rt) == -1) 
	{
	perror("setrlimit");
	exit(1);
	}
	else 
	{
	printf("设置系统资源参数成功!\n");
	}
	/* 开启 socket 监听 */
	if ((listener = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
	{
	perror("socket 创建失败!");
	exit(1);
	}
	else
	{
	printf("socket 创建成功!\n");
	}

	/*设置socket属性,端口可以重用*/
	int opt=SO_REUSEADDR;
	setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

	setnonblocking(listener);
	bzero(&my_addr, sizeof(my_addr));
	my_addr.sin_family = PF_INET;
	my_addr.sin_port = htons(myport);
	my_addr.sin_addr.s_addr = INADDR_ANY;
	if (bind(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) 
	{
		perror("bind");
		exit(1);
	} 
	else
	{
		printf("IP 地址和端口绑定成功\n");
	}


	/* 创建 epoll 句柄,把监听 socket 加入到 epoll 集合里 */
	kdpfd = epoll_create(MAXEPOLLSIZE);
	len = sizeof(struct sockaddr_in);
	ev.events = EPOLLIN | EPOLLET;
	ev.data.fd = listener;
	if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0) 
	{
		fprintf(stderr, "epoll set insertion error: fd=%d\n", listener);
		return -1;
	}
	else
	{
		printf("监听 socket 加入 epoll 成功!\n");
	}
   
	while (1) 
	{
		/* 等待有事件发生 */
		nfds = epoll_wait(kdpfd, events, 10000, -1);
		if (nfds == -1)
		{
			perror("epoll_wait");
			break;
		}
		/* 处理所有事件 */
		for (n = 0; n < nfds; ++n)
		{
			if (events[n].data.fd == listener) 
			{
				threadpool_add_job(pool, threadpool_callback, (void*)&(events[n].data.fd));
				/*初始化属性值,均设为默认值*/
				//pthread_attr_init(&attr);
				//pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
				/*  设置线程为分离属性*/ 
				//pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

				//if(pthread_create(&thread,&attr,(void*)pthread_handle_message,(void*)&(events[n].data.fd)))
				//{
				//        perror("pthread_creat error!");
				//        exit(-1);
				//} 
			} 
		}
	}

    close(listener);
    return 0;
}
Esempio n. 5
0
int main(void)
{
    struct threadpool *pool = threadpool_init(10, 20);
    threadpool_add_job(pool, work, "1");
    threadpool_add_job(pool, work, "2");
    threadpool_add_job(pool, work, "3");
    threadpool_add_job(pool, work, "4");
    threadpool_add_job(pool, work, "5");
    threadpool_add_job(pool, work, "6");
    threadpool_add_job(pool, work, "7");
    threadpool_add_job(pool, work, "8");
    threadpool_add_job(pool, work, "9");
    threadpool_add_job(pool, work, "10");
    threadpool_add_job(pool, work, "11");
    threadpool_add_job(pool, work, "12");
    threadpool_add_job(pool, work, "13");
    threadpool_add_job(pool, work, "14");
    threadpool_add_job(pool, work, "15");
    threadpool_add_job(pool, work, "16");
    threadpool_add_job(pool, work, "17");
    threadpool_add_job(pool, work, "18");
    threadpool_add_job(pool, work, "19");
    threadpool_add_job(pool, work, "20");
    threadpool_add_job(pool, work, "21");
    threadpool_add_job(pool, work, "22");
    threadpool_add_job(pool, work, "23");
    threadpool_add_job(pool, work, "24");
    threadpool_add_job(pool, work, "25");
    threadpool_add_job(pool, work, "26");
    threadpool_add_job(pool, work, "27");
    threadpool_add_job(pool, work, "28");
    threadpool_add_job(pool, work, "29");
    threadpool_add_job(pool, work, "30");
    threadpool_add_job(pool, work, "31");
    threadpool_add_job(pool, work, "32");
    threadpool_add_job(pool, work, "33");
    threadpool_add_job(pool, work, "34");
    threadpool_add_job(pool, work, "35");
    threadpool_add_job(pool, work, "36");
    threadpool_add_job(pool, work, "37");
    threadpool_add_job(pool, work, "38");
    threadpool_add_job(pool, work, "39");
    threadpool_add_job(pool, work, "40");

    sleep(5);
    threadpool_destroy(pool);
    return 0;
}
Esempio n. 6
0
int cmd_gethost(int argc, char **argv)
{
	int i = 1;
	char *arg;
	
	struct gethost_job *pjob = NULL;

	if ( pthread_mutex_init(&(cmd_gethost_stat.lock), NULL) ){
		printf("error: mutex init \n");
		return -1;
	}
	if( NULL == (thread_pool = threadpool_init(15, 100)) ){
		printf("error: thread pool init \n");
		return -2;
	}

	wf_registe_exit_signal(gethost_exit);
	cmd_gethost_stat.start = wf_getsys_uptime(NULL);
	while(1)
	{
		if(argv[2])
			arg = argv[++i];
		else{
			memset(asc_buf, 0, sizeof(asc_buf));
			arg = fgets(asc_buf, sizeof(asc_buf), stdin);
		}
		if( !arg )
			break;
		++cmd_gethost_stat.all_cnt;
		wipe_off_CRLF_inEnd(arg);
		if(0 == strlen(arg))
			continue;
		++cmd_gethost_stat.valid_cnt;

		pjob = (struct gethost_job *)malloc(sizeof(struct gethost_job));
		if(pjob){
			pjob->id = cmd_gethost_stat.all_cnt;
			pjob->name = strdup(arg);
			if(!pjob->name){
				free(pjob);
				goto fail_done;
			}
		}
		else{
			goto fail_done;
		}
		
		
		if( threadpool_add_job(thread_pool, gethost_job, pjob, NULL) < 0)
			goto fail_done;
		else
			continue;

	fail_done:
		pthread_mutex_lock(&(cmd_gethost_stat.lock));
		++cmd_gethost_stat.fail_cnt;
		pthread_mutex_unlock(&(cmd_gethost_stat.lock));
		continue;
	}
	printf(">>>>>>>>>>>>>>>>>>>>>>> add job num: %d \n", cmd_gethost_stat.all_cnt);

	while(cmd_gethost_stat.valid_cnt != cmd_gethost_stat.ok_cnt + cmd_gethost_stat.fail_cnt)	sleep(1);
	threadpool_destroy(thread_pool);
	gethost_result();
	
	return 0;
}
Esempio n. 7
0
	int CWorldOther::SdkServerVerify(T_VECTOR_OBJECT* p, CPluto& u)
	{
#if 0
        //注释掉老代码
		if (p->size() != 4)
		{
			return -1;
		}
		string& url = VOBJECT_GET_SSTR((*p)[0]);
		int32_t nFd = VOBJECT_GET_I32((*p)[1]);
		string& strAccount = VOBJECT_GET_SSTR((*p)[2]);
		string& strPlatId = VOBJECT_GET_SSTR((*p)[3]);
		CMailBox* pmb = u.GetMailbox();
		if (NULL == pmb)
		{
			return -1;
		}

		CPluto* duplicate = new CPluto(u.GetBuff(), u.GetMaxLen());
		duplicate->SetMailbox(pmb);
		int ret = threadpool_add_job(g_threadpool, ThreadJob_SdkServerVerify, (void*)(duplicate));

		if (ret != 0)
		{
			//直接返回服务器繁忙,请稍后再试
			//printf("服务器繁忙,请稍后再试!\n");
			//std::cout << "服务器繁忙,请稍后再试!" << endl;
			LogWarning("CWorldOther::SdkServerVerify", "threadpool list is full.");
			CPluto* u2 = new CPluto;
			u2->Encode(MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK);
			(*u2)<< int32_t(-2) << nFd << strAccount << strPlatId << EndPluto;
			g_pluto_sendlist.PushPluto(u2);
			//不适合多线程发送
			//pmb->RpcCall(GetRpcUtil(), MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, -2, nFd, strAccount, strPlatId);
		}

		return 0;
#endif

        if (p->size() != 4)
        {
            return -1;
        }
        string& url = VOBJECT_GET_SSTR((*p)[0]);
        int32_t nFd = VOBJECT_GET_I32((*p)[1]);
        string& strAccount = VOBJECT_GET_SSTR((*p)[2]);
        string& strPlatId = VOBJECT_GET_SSTR((*p)[3]);
        CMailBox* pmb = u.GetMailbox();

        pluto_msgid_t msg_id = u.GetMsgId();;

        string resp = "";
        int ret = GetUrl_new(url.c_str(), resp);
        if (ret != CURLE_OK)
        {
            LogWarning("CWorldOther::SdkServerVerify", "%s ret = %d", strAccount.c_str(), ret);
            //todo:faild.
            SyncRpcCall(g_pluto_sendlist, SERVER_LOGINAPP, MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, ret, nFd, strAccount, strPlatId);            
            return 0;
        }
        Trim(resp);
        cJSON* json = cJSON_Parse(resp.c_str());
        cJSON* childJs = cJSON_GetObjectItem(json, "code");
        int rst = cJSON_GetObjectItem(json, "code")->valueint;

        if (rst == 1)
        {
            //succeed.
            if (strAccount.c_str() != cJSON_GetObjectItem(json, "msg")->valuestring)
            {
                ret = 0;
            }
            else
            {
                LogWarning("CWorldOther::SdkServerVerify", "strAccount[%s] msg[%s]", 
                    strAccount.c_str(), cJSON_GetObjectItem(json, "msg")->valuestring);
                ret = 1;
                //pmb->RpcCall(GetRpcUtil(), MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, int32_t(1), nFd, strAccount, strPlatId);
            }
        }
        else
        {
            //failed.
            LogWarning("CWorldOther::SdkServerVerify", "strAccount[%s] rst[%d]", 
                strAccount.c_str(), rst);
            ret = -1;
            //pmb->RpcCall(GetRpcUtil(), MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, -1, nFd, strAccount, strPlatId);
        }
        SyncRpcCall(g_pluto_sendlist, SERVER_LOGINAPP, MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, ret, nFd, strAccount, strPlatId);
        return 0;


	}