/******************************************************************************* * @函数名称 dir_totle * @函数说明 计算文件夹的总数,多余的将其删除 * @输入参数 路径结构体指针、文件信息结构体指针 * @输出参数 无 * @返回参数 文件夹的总数 *******************************************************************************/ uint16_t dir_totle(DIR *path, FILINFO* file_info){ uint16_t dir_num = 0; static TCHAR *fn; while(1){ if(f_readdir(path,file_info) == FR_OK){ if (file_info->fname[0] == 0) break; if(file_info->fattrib == AM_DIR){ dir_num++; #if _USE_LFN fn = *file_info->lfname ? file_info->lfname : file_info->fname; #else fn = file_info->fname; #endif if(!Queue_Push(&Q_dir,fn)){ /* first pop */ msg_t *delete_file; delete_file = Queue_Pop(&Q_dir); f_unlink((const TCHAR*)delete_file->buf); memset(delete_file->buf,0,sizeof(delete_file->buf)); /* next push */ memcpy(Q_dir.m_Msg[Q_dir.rear].buf,fn,C_MAX_BUF_SIZE); Q_dir.next = Q_dir.rear; Q_dir.rear = (Q_dir.rear + 1) % Q_dir.totle_num; } } } } return dir_num; }
void* _send_buffer_monitor(void* arg) { int buf_idx = *(int*)arg; Message_t msg; Client_t* c; for (;;) { sem_wait(&g_executor.send_buffer_num_[buf_idx]); if (Queue_Pop(g_executor.send_buffer_[buf_idx], &msg) == QUEUE_OK) { if ((c = Map_GetPtr(g_executor.clients, &msg.sockfd)) == NULL) { //Clients already gone Log(LOG_NOTICE,"[SEND FAIL]Client Already Gone"); ylfree(msg.package); continue; } if (msg.trysend_cnt) { usleep(100000); } int nsend = netWrite(NULL, msg.sockfd, (char*) msg.package, msg.pkg_len); if (nsend == ERR_NETWORK_TRY_AGAIN&& ++msg.trysend_cnt < MAX_SEND_TRY) { Queue_Push(g_executor.send_buffer_[buf_idx] , &msg); sem_post(&g_executor.send_buffer_num_[buf_idx]); } else { ylfree(msg.package); } } } return NULL; }
AwaError AwaServerSession_Process(AwaServerSession * session, AwaTimeout timeout) { AwaError result = AwaError_Unspecified; if (session != NULL) { while (IPC_WaitForNotification(ServerSession_GetChannel(session), timeout) == AwaError_Success) { IPCMessage * notification; if (IPC_ReceiveNotification(ServerSession_GetChannel(session), ¬ification) == AwaError_Success) { if (!Queue_Push(session->NotificationQueue, notification)) { // Queue full? IPCMessage_Free(¬ification); } } // we have received at least 1 packet, so we no longer have any reason to wait // if there are no more in the pipeline. timeout = 0; } result = AwaError_Success; } else { LogErrorWithEnum(AwaError_SessionInvalid, "session is NULL"); } return result; }
static void _foreach_client_(const void* key, const void* data , void* args) { client_iterator_t* iterator = (client_iterator_t*)args; Queue_t clients_tobe_clear = iterator->clients_tobe_clear; time_t now_time = iterator->check_time; int pluse_interval = iterator->pluse_interval; if (now_time - ((Client_t*)data)->last_seen > pluse_interval) { int client_fd = *((int*)key); Queue_Push(clients_tobe_clear , &client_fd); } }
void ReplyMessage(int fd , int msgid , int pkg_len, uint8_t* package) { Message_t new_msg; new_msg.sockfd = fd; new_msg.trysend_cnt = 0; new_msg.msgid = msgid; new_msg.package = package; new_msg.pkg_len = pkg_len; _make_header(new_msg.package , msgid , pkg_len - MSGHEAD_SIZE); uint8_t send_buf = atomic_add_then_fetch((int*) &g_executor.cur_send_buffer_, 1) % CLIENT_SEND_QUEUES; Queue_Push(g_executor.send_buffer_[send_buf], &new_msg); sem_post(&g_executor.send_buffer_num_[send_buf]); }
int main() { system("COLOR 1A"); Queue *snake = Queue_Init(); COORD screenResolution = {WIDTH, HEIGHT}; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), screenResolution); SetConsoleTitle("TU-Snake"); COORD directions[4] = { {0, -1}, {0, 1}, {1, 0}, {-1, 0}, }; Direction direction = LEFT; unsigned int keyboardInput = 0; double snakeSpeed = SnakeSpeed; COORD food = {RandomNumber(0, WIDTH), RandomNumber(0, HEIGHT)}; int centerX = WIDTH / 2; int centerY = HEIGHT / 2; //InitSnake for (int i = 0; i < SnakeInitializeSize; i++) { COORD position = { centerX - i , centerY}; Queue_Push(snake, position); } PrintSnake(snake); while (1) { if (_kbhit() == 1) { keyboardInput = _getch(); switch (keyboardInput) { case 119 : //up if (direction != DOWN) direction = UP; break; case 115 : //down if (direction != UP) direction = DOWN; break; case 97 : //left if (direction != RIGHT) direction = LEFT; break; case 100 : //right if (direction != LEFT) direction = RIGHT; break; default: break; } } COORD snakeHead = Queue_Back(snake); COORD nextDirection = directions[direction]; COORD snakeNewHead = {snakeHead.X + nextDirection.X, snakeHead.Y + nextDirection.Y}; //check for collisions if (snakeNewHead.X < 0 || snakeNewHead.Y < 0 || snakeNewHead.X > WIDTH || snakeNewHead.Y > HEIGHT || Queue_Contains (snake, snakeNewHead)) { char text[] = "!!!Game is over!!!"; COORD startPoint = {centerX - strlen(text)/2, centerY}; GoToPosition(startPoint); system("COLOR CF"); printf("%s", text); PlaySong(); break; } //add new head to snake Queue_Push(snake, snakeNewHead); //check for grow if (snakeNewHead.X == food.X && snakeNewHead.Y == food.Y) { food.X = RandomNumber(0, WIDTH); food.Y = RandomNumber(0, HEIGHT); if (snakeSpeed - 1 > 1) snakeSpeed--; } else { Queue_Pop(snake); } PrintSnake(snake); GoToPosition(food); printf("%c", SnakeFood); if (snakeSpeed - 0.1 > 1) snakeSpeed -= 0.1; Sleep((int)snakeSpeed); ClearScreen; } system("pause > ''"); return 0; }
static void _read_query_from_client(EventLoop_t evloop , int fd , void* clientData , int mask) { AVOID_NOUSED(evloop); AVOID_NOUSED(mask); // AVOID_NOUSED(clientData); // Client_t* c = Map_GetPtr(g_executor.clients , &fd); Client_t* c = (Client_t*)clientData; int nread; char errmsg[MAX_NET_ERR_LEN]; StdMutex_Lock(c->oplock); nread = netRead(errmsg, c->fd, c->query_buffer + c->buffer_len, CLIENTS_BUFFER_LEN - c->buffer_len); if (nread == ERR_NETWORK_EXCEPTION) { //ERR HAPPEND StdMutex_Unlock(c->oplock); Log(LOG_ERROR,"Read query from client failed.reason:##%s##", errmsg); _release_client(fd, RELEASE_BY_ERROR); return; } else if (nread == 0) { //Client Close the connection Log(LOG_NOTICE,"[CLIENT_DISCONNECT][%s:%d]", c->addr, c->port); StdMutex_Unlock(c->oplock); _release_client(fd, RELEASE_BY_CLIENT); return; } c->buffer_len += nread; //try to extract as many package as possible char* iterator = c->query_buffer; while (1) { if (c->buffer_len < MSGHEAD_SIZE) { break; } if (!isValidRequest(iterator)) { //not a valid request, clear client Log(LOG_NOTICE,"invalid request received from [%s:%d], close the connection" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } Message_t new_msg; new_msg.msgid = *((int32_t*)iterator + 1); new_msg.pkg_len = *((int32_t*)iterator + 2); if (c->status == CLI_INIT && new_msg.msgid != MSG_EXCU_AUTH && new_msg.msgid != MSG_EXCU_PLUSE) { //client not login, kick out Log(LOG_NOTICE,"client [%s:%d] should get authentication first before anyother request" , c->addr , c->port); StdMutex_Unlock(c->oplock); _release_client(fd , RELEASE_BY_ERROR); return; } c->last_seen = time(NULL); if (new_msg.pkg_len + MSGHEAD_SIZE > c->buffer_len) { //not a complete package int offset = iterator - c->query_buffer; for (int pos = 0; pos != c->buffer_len; ++pos) { c->query_buffer[pos] = c->query_buffer[pos + offset]; } break; } //extract one package new_msg.sockfd = c->fd; uint8_t* pkg = ylmalloc(new_msg.pkg_len); memcpy(pkg , iterator + MSGHEAD_SIZE , new_msg.pkg_len); new_msg.package = pkg; uint8_t recv_buf = atomic_add_then_fetch((int*)&g_executor.cur_recv_buffer_ , 1)%CLIENT_RECV_QUEUES; Queue_Push(g_executor.recv_buffer_[recv_buf] , &new_msg); sem_post(&g_executor.recv_buffer_num_[recv_buf]); int msglen = new_msg.pkg_len + MSGHEAD_SIZE; c->buffer_len -= msglen; iterator += msglen; } StdMutex_Unlock(c->oplock); }
PTCar Car_Add(void) { PTCar car = Car_Create(); PTListItem pli = Queue_Push(CarList, (void *) car); pli->destroy_func = (TItem_Destroy_Func) Car_Destroy; return car; }
PTAxle Axle_Add(PTCar car) { PTAxle axle = Axle_Create(); PTListItem pli = Queue_Push(car->axles, (void *) axle); pli->destroy_func = (TItem_Destroy_Func) Axle_Destroy; return axle; }
void Car_QueuePush(void) { CarListInit(); Queue_Push(CarList, (void *)Car_Create()); }