Exemple #1
0
	void iocp_thread()
	{
		DWORD bytes_transferred;
		OVER *over;
		ULONG key;

		while(true) {
			bytes_transferred = 0;
			BOOL completed = GetQueuedCompletionStatus(hIOCP,&bytes_transferred,&key,(OVERLAPPED**)&over,timeout);

			if(key == KEY_STOP) {
				if(log_level) std::cerr << "KEY_STOP" << std::endl;
				break;
			}

			DWORD last_error = completed ? 0 : HandleLastError("GetQueuedCompletionStatus");
			if(log_level) fprintf(stderr,"completed[%i] bytes_transferred[%i] over.operation[%i]\n",completed,bytes_transferred,over ? over->operation : 0);			
			
			if((!completed && last_error == WAIT_TIMEOUT)) {
				fprintf(stderr,"WAIT_TIMEOUT\n");				
			}

			if(timeout.check()) {
				int i = CancelIo(hCom);
				fprintf(stderr,"CancelIo = %i\n",i);
				continue;
			}
			

			if(!completed || !over) {
				continue;
			}

			if(over->operation == OP_READ) {
				if(completed) {
					long packet_found = data_received(read_buf,bytes_transferred);
					if(!packet_found) {
						Read(read_buf,1);
					}
				}
			}

			if(over->operation == OP_WRITE) {
				if(data_sent(bytes_transferred,system::error_code(last_error,system::system_category())) == 0) {
					Read(read_buf,1);
				}
			}						
		}

		if(log_level) std::cerr << "iocp_thread stopped" << std::endl;
	}