示例#1
0
文件: mread.c 项目: sshic/skynet
int
mread_poll(struct mread_pool * self , int timeout) {
	self->skip = 0;
	if (self->active >= 0) {
		struct socket * s = &self->sockets[self->active];
		if (s->status == SOCKET_READ) {
			return self->active;
		}
	}
	if (self->closed > 0 ) {
		return _report_closed(self);
	}
	if (self->queue_head >= self->queue_len) {
		if (_read_queue(self, timeout) == -1) {
			self->active = -1;
			return -1;
		}
	}
	for (;;) {
		struct socket * s = _read_one(self);
		if (s == NULL) {
			self->active = -1;
			return -1;
		}
		if (s == LISTENSOCKET) {
			struct sockaddr_in remote_addr;
			socklen_t len = sizeof(struct sockaddr_in);
			int client_fd = accept(self->listen_fd , (struct sockaddr *)&remote_addr ,  &len);
			if (client_fd >= 0) {
//				printf("MREAD connect %s:%u (fd=%d)\n",inet_ntoa(remote_addr.sin_addr),ntohs(remote_addr.sin_port), client_fd);
				struct socket * s = _add_client(self, client_fd);
				if (s) {
					self->active = -1;
					return s - self->sockets;
				}
			}
		} else {
			int index = s - self->sockets;
			assert(index >=0 && index < self->max_connection);
			self->active = index;
			s->status = SOCKET_POLLIN;
			return index;
		}
	}
}
示例#2
0
文件: mread.c 项目: kyelin/skynet
int
mread_poll(struct mread_pool * self , int timeout) {
	self->skip = 0;
	if (self->active >= 0) {
		struct socket * s = &self->sockets[self->active];
		if (s->status == SOCKET_READ) {
			return self->active;
		}
	}
	if (self->closed > 0 ) {
		return _report_closed(self);
	}
	if (self->queue_head >= self->queue_len) {
		if (_read_queue(self, timeout) == -1) {
			self->active = -1;
			return -1;
		}
	}
/*
#if defined(_WIN32)
	for(;;){//accept socket
		int n = WSAWaitForMultipleEvents(1, self->events,
                                     0, timeout, 0);
		WSANETWORKEVENTS  ne;
		if (n == WAIT_FAILED) {     
            	break;
       		 }
        	if (n == WAIT_TIMEOUT) {
            	break;
       		 }

        	n -= WSA_WAIT_EVENT_0;
		if (WSAEnumNetworkEvents(self->listen_fd, self->events[n], &ne) == -1) {
            		break;
       		 }
	        if (ne.lNetworkEvents & FD_ACCEPT) {
		   
        	    SOCKET fd = accept(self->listen_fd, NULL, NULL);
		    struct socket * s = _add_client(self, fd);
		    if (CreateIoCompletionPort((HANDLE)fd,self->iocp,s, 0) == NULL) {
        		 break;
    		    }	 
			if (CreateIoCompletionPort((HANDLE)fd,self->iocp,1, 0) == NULL) {
			printf("add connection GetLastError is : %d  %d \n",GetLastError(),fd);
        		break;
       			 }
        		DWORD   flags = 0;       
    			DWORD   recvBytes =0;  
        		ZeroMemory(&s->ol,sizeof(OVERLAPPED));
    			if (WSARecv(fd, NULL,0, &recvBytes, &flags, &(s->ol), NULL) == SOCKET_ERROR)  
   			{  
   				DWORD error; 
				error = GetLastError();
				printf("GetLastError is : %d , recvBytes = %d \n",error,recvBytes);
       		
    			}		
        	   continue;
       		}
	}
#endif
*/
	for (;;) {
		struct socket * s = _read_one(self);
		if (s == NULL) {
			self->active = -1;
			return -1;
		}
		if (s == LISTENSOCKET) {
			struct sockaddr_in remote_addr;
			socklen_t len = sizeof(struct sockaddr_in);
			int client_fd = accept(self->listen_fd , (struct sockaddr *)&remote_addr ,  &len);
			if (client_fd >= 0) {
//				printf("MREAD connect %s:%u (fd=%d)\n",inet_ntoa(remote_addr.sin_addr),ntohs(remote_addr.sin_port), client_fd);
				struct socket * s = _add_client(self, client_fd);
				if (s) {
					self->active = -1;
					return s - self->sockets;
				}
			}
		} else {
			int index = s - self->sockets;
			assert(index >=0 && index < self->max_connection);
			self->active = index;
			s->status = SOCKET_POLLIN;
			return index;
		}
	}
}