MCPcopy Create free account
hub / github.com/Tencent/libco / poll_routine

Function poll_routine

example_poll.cpp:109–179  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

107}
108
109static void *poll_routine( void *arg )
110{
111 co_enable_hook_sys();
112
113 vector<task_t> &v = *(vector<task_t>*)arg;
114 for(size_t i=0;i<v.size();i++)
115 {
116 int fd = CreateTcpSocket();
117 SetNonBlock( fd );
118 v[i].fd = fd;
119
120 int ret = connect(fd,(struct sockaddr*)&v[i].addr,sizeof( v[i].addr ));
121 printf("co %p connect i %ld ret %d errno %d (%s)\n",
122 co_self(),i,ret,errno,strerror(errno));
123 }
124 struct pollfd *pf = (struct pollfd*)calloc( 1,sizeof(struct pollfd) * v.size() );
125
126 for(size_t i=0;i<v.size();i++)
127 {
128 pf[i].fd = v[i].fd;
129 pf[i].events = ( POLLOUT | POLLERR | POLLHUP );
130 }
131 set<int> setRaiseFds;
132 size_t iWaitCnt = v.size();
133 for(;;)
134 {
135 int ret = poll( pf,iWaitCnt,1000 );
136 printf("co %p poll wait %ld ret %d\n",
137 co_self(),iWaitCnt,ret);
138 for(int i=0;i<ret;i++)
139 {
140 printf("co %p fire fd %d revents 0x%X POLLOUT 0x%X POLLERR 0x%X POLLHUP 0x%X\n",
141 co_self(),
142 pf[i].fd,
143 pf[i].revents,
144 POLLOUT,
145 POLLERR,
146 POLLHUP
147 );
148 setRaiseFds.insert( pf[i].fd );
149 }
150 if( setRaiseFds.size() == v.size())
151 {
152 break;
153 }
154 if( ret <= 0 )
155 {
156 break;
157 }
158
159 iWaitCnt = 0;
160 for(size_t i=0;i<v.size();i++)
161 {
162 if( setRaiseFds.find( v[i].fd ) == setRaiseFds.end() )
163 {
164 pf[ iWaitCnt ].fd = v[i].fd;
165 pf[ iWaitCnt ].events = ( POLLOUT | POLLERR | POLLHUP );
166 ++iWaitCnt;

Callers 1

mainFunction · 0.85

Calls 7

co_enable_hook_sysFunction · 0.85
connectFunction · 0.85
co_selfFunction · 0.85
pollFunction · 0.85
closeFunction · 0.85
CreateTcpSocketFunction · 0.70
SetNonBlockFunction · 0.70

Tested by

no test coverage detected