/* * Tencent is pleased to support the open source community by making Libco available. * Copyright (C) 2014 THL A29 Limited, a Tencent company. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "co_routine.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef __FreeBSD__ #include #endif using namespace std; struct task_t { stCoRoutine_t *co; int fd; struct sockaddr_in addr; }; static int SetNonBlock(int iSock) { int iFlags; iFlags = fcntl(iSock, F_GETFL, 0); iFlags |= O_NONBLOCK; iFlags |= O_NDELAY; int ret = fcntl(iSock, F_SETFL, iFlags); return ret; } static void SetAddr(const char *pszIP,const unsigned short shPort,struct sockaddr_in &addr) { bzero(&addr,sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(shPort); int nIP = 0; if( !pszIP || '\0' == *pszIP || 0 == strcmp(pszIP,"0") || 0 == strcmp(pszIP,"0.0.0.0") || 0 == strcmp(pszIP,"*") ) { nIP = htonl(INADDR_ANY); } else { nIP = inet_addr(pszIP); } addr.sin_addr.s_addr = nIP; } static int CreateTcpSocket(const unsigned short shPort = 0 ,const char *pszIP = "*" ,bool bReuse = false ) { int fd = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP); if( fd >= 0 ) { if(shPort != 0) { if(bReuse) { int nReuseAddr = 1; setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&nReuseAddr,sizeof(nReuseAddr)); } struct sockaddr_in addr ; SetAddr(pszIP,shPort,addr); int ret = bind(fd,(struct sockaddr*)&addr,sizeof(addr)); if( ret != 0) { close(fd); return -1; } } } return fd; } static void *poll_routine( void *arg ) { co_enable_hook_sys(); vector &v = *(vector*)arg; for(size_t i=0;i setRaiseFds; size_t iWaitCnt = v.size(); for(;;) { int ret = poll( pf,iWaitCnt,1000 ); printf("co %p poll wait %ld ret %d\n", co_self(),iWaitCnt,ret); for(int i=0;i<(int)iWaitCnt;i++) { printf("co %p fire fd %d revents 0x%X POLLOUT 0x%X POLLERR 0x%X POLLHUP 0x%X\n", co_self(), pf[i].fd, pf[i].revents, POLLOUT, POLLERR, POLLHUP ); setRaiseFds.insert( pf[i].fd ); } if( setRaiseFds.size() == v.size()) { break; } if( ret <= 0 ) { break; } iWaitCnt = 0; for(size_t i=0;i v; for(int i=1;i v2 = v; poll_routine( &v2 ); printf("--------------------- routine -------------------\n"); for(int i=0;i<10;i++) { stCoRoutine_t *co = 0; vector *v2 = new vector(); *v2 = v; co_create( &co,NULL,poll_routine,v2 ); printf("routine i %d\n",i); co_resume( co ); } co_eventloop( co_get_epoll_ct(),0,0 ); return 0; } //./example_poll 127.0.0.1 12365 127.0.0.1 12222 192.168.1.1 1000 192.168.1.2 1111