Exemplo n.º 1
0
//取连接  
T_Connect * get_SC_connect(int TCBno,int n,int flg)
{
int i,num;
pool *pl;
resource *rs;
pthread_t tid=pthread_self();

	if(!scpool || n<0 || n>=SCPOOLNUM) return NULL;
	pl=&scpool[n];
	if(!pl->lnk) {
		ShowLog(1,"%s:无效的连接池[%d]",__FUNCTION__,n);
		return NULL;
	}
	num=pl->resource_num;
	if(0!=pthread_mutex_lock(&pl->mut)) return NULL;
    while(1) {
	rs=pl->lnk;
	for(i=0;i<num;i++,rs++) if(rs->TCBno<0) {
		if(rs->Conn.Socket<0 || rs->cli.Errno<0) {
			switch(sc_connect(pl,rs)) {
			case -1:
			case -2:
			case -3:
				ShowLog(1,"%s:scpool[%d].%d 连接%s/%s错:err=%d,%s",
					__FUNCTION__,n,i,pl->log.HOST,pl->log.PORT,
					rs->cli.Errno, rs->cli.ErrMsg);
				pthread_mutex_unlock(&pl->mut);
				return (T_Connect *)-1;
			case 0:
				break;
			default:
				rs->cli.Errno=-1;
				ShowLog(1,"%s:scpool[%d].%d 连接%s/%s错:err=%d,%s",
					__FUNCTION__,n,i,pl->log.HOST,pl->log.PORT,
					rs->cli.Errno, rs->cli.ErrMsg);
				continue;
			} 
		} 
		rs->TCBno=TCBno;
		rs->timestamp=now_usec();
		pthread_mutex_unlock(&pl->mut);
		if(log_level) ShowLog(log_level,"%s tid=%lu,TCB:%d,pool[%d].lnk[%d]",__FUNCTION__,
			tid,TCBno,n,i);
		rs->cli.Errno=0;
		*rs->cli.ErrMsg=0;
		return &rs->Conn;
	}
	if(flg) {	//flg !=0,don't wait
		pthread_mutex_unlock(&pl->mut);
		return NULL;
	}
	if(log_level) ShowLog(log_level,"%s tid=%lu pool[%d] suspend",__FUNCTION__,pthread_self(),n);
	pthread_cond_wait(&pl->cond,&pl->mut); //没有资源,等待 
	if(log_level) ShowLog(log_level,"%s tid=%lu pool[%d] weakup",__FUNCTION__,pthread_self(),n);
    }
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
	e_int32 ret,i;
	hd_connect_t connect;
	msg_monitor_t monitor = { 0 };
	ethread_t* threads[MAX_CLIENT_SIZE];
	e_uint8 name[128]={'t','e','s','t',0};

	ret = sc_open_socket(&connect, "127.0.0.1", 6666);
	e_assert(ret>0, ret);
	ret = sc_connect(&connect);
	e_assert(ret>0, ret);
	ret = mm_init(&monitor, &connect);
	e_assert(ret>0, ret);
	mm_start(&monitor);

#if 0
	fsocket_t* fd = mm_create_socket(&monitor, name);
	if (!fd) {
		DMSG((STDOUT,"ERROR CREATE FAKE SOCKET\r\n"));
	} else{
		request_thread(fd);
	}
#else
	while (count--) {
		sprintf(name,"Thread[%d]",count);
		fsocket_t* fd = mm_create_socket(&monitor,name);
		DMSG((STDOUT,"create request thread %d\r\n",count));
		ret = createthread("request", (thread_func) &request_thread, (void*) fd,
				NULL, &threads[count]);
		if (ret <= 0) {
			DMSG((STDOUT,"createhread failed!\r\n"));
			return E_ERROR;
		}
		ret = resumethread(threads[count]);
		if (ret <= 0) {
			DMSG((STDOUT,"resumethread failed!\r\n"));
			return E_ERROR;
		}
	}
#endif
	while(count<MAX_CLIENT_SIZE-1) {
		//DMSG((STDOUT,"COUNT = %d",count));
		sleep(1);
	}

	for (i=0;i<MAX_CLIENT_SIZE;i++) {
		killthread( threads[i]);
	}
	mm_stop(&monitor);
	sc_close(&connect);
	return 0;
}