void TimerCheck(void) { unsigned int now = GetTimeInMillis(); while (timers && (int)(timers->expires - now) <= 0) DoTimer(timers, now, &timers); }
void ClientSession::OnTick() { if (!mLogon) return; /// 주기적으로 이동 또는 채팅 보냄 if (rand() % 5 == 0) { /// 20% 확률로 채팅 ChatBroadcastRequest sendPkt; sendPkt.mPlayerId = mPlayerId; sprintf_s(sendPkt.mChat, " TEST CHAT: HELLO~~ This is Player %d =====", mPlayerId); SendPacket(sendPkt); } else { /// 80% 확률로 random walking MoveRequest sendPkt; sendPkt.mPlayerId = mPlayerId; sendPkt.mPosX = mPosX + rand() % 100 - 50; sendPkt.mPosY = mPosY + rand() % 60 - 30; SendPacket(sendPkt); } /// 1초마다 반복 DoTimer(1000, &ClientSession::OnTick); }
void ClientSession::HandleReadPayload(const boost::system::error_code& error) { if (error) { std::cout << error.message() << std::endl; return; } int pktLen = static_cast<int>(mReadBuf[1]) << 8 | mReadBuf[0]; int pktType = static_cast<int>(mReadBuf[3]) << 8 | mReadBuf[2]; switch (pktType) { case PKT_SC_LOGIN: { LoginResult pktResult; memcpy_s(&pktResult, pktLen, (void*)&mReadBuf, pktLen); mLogon = true; printf_s("LOGIN OK: ID[%d] Name[%s] POS[%.4f, %.4f]\n", pktResult.mPlayerId, pktResult.mName, pktResult.mPosX, pktResult.mPosY); /// 적당히 기다린 후에 타이머 돌리기 DoTimer(rand() % 1000, &ClientSession::OnTick); } break; case PKT_SC_CHAT: { ChatBroadcastResult pktResult; memcpy_s(&pktResult, pktLen, (void*)&mReadBuf, pktLen); printf_s("CHAT from [%s]: %s\n", pktResult.mName, pktResult.mChat); } break; case PKT_SC_MOVE: { MoveBroadcastResult pktResult; memcpy_s(&pktResult, pktLen, (void*)&mReadBuf, pktLen); /// 자신일 경우만 업데이트 if (mPlayerId == pktResult.mPlayerId) { mPosX = pktResult.mPosX; mPosY = pktResult.mPosY; printf_s(" Player [%d] move to (%.2f, %.2f) \n", pktResult.mPlayerId, pktResult.mPosX, pktResult.mPosY); } } break; } PostReadHeader(); }
/* get samples ---------------------------------------------------------------- */ void SPC_update(unsigned char *buf) { // APU_LOOP int c, ic; #if 1 for (c = 0; c < 2048000 / 32 / RATE; c ++) { for (ic = 0; ic < 32; ic ++) APU_EXECUTE1(); IAPU.TimerErrorCounter ++; DoTimer(); } #else for (APU.Cycles = 0; APU.Cycles < 204800 / RATE; APU.Cycles ++) { APU_EXECUTE1(); ++ IAPU.TimerErrorCounter; if ((IAPU.TimerErrorCounter & 31) == 0) DoTimer(); APURegisters.PC = IAPU.PC - IAPU.RAM; S9xAPUPackStatus(); } #endif S9xMixSamples ((unsigned char *)buf, samples_per_mix); }
int TimerForce(TimerPtr timer) { int rc = -1; TimerPtr *prev; for (prev = &timers; *prev; prev = &(*prev)->next) { if (*prev == timer) { DoTimer(timer, GetTimeInMillis(), prev); rc = 0; break; } } return rc; }
/*------------------------------------------- WM_TIMER message ---------------------------------------------*/ void OnTimerTimer(HWND hwnd) { PTIMERSTRUCT pitem, pDo; DWORD tick; int nCopyData; char s[20]; wchar_t ws[20]; if(!m_pTimerRun) return; tick = GetTickCount(); pDo = NULL; pitem = m_pTimerRun; while(pitem) { if(pitem->bDisp) { int remaining = pitem->interval - (tick - pitem->tickonstart)/1000 - 1; if(remaining >= 0) { if(pitem->nDispType == 0) nCopyData = COPYDATA_DISP1; else if(pitem->nDispType == 2) nCopyData = COPYDATA_USTR0 + pitem->nUserStr; else nCopyData = COPYDATA_CAT1; wsprintf(s, "[%02d:%02d]", remaining/60, remaining%60); MultiByteToWideChar(CP_ACP, 0, s, -1, ws, 19); SendStringToOtherW(g_hwndClock, hwnd, ws, nCopyData); } } if(pDo == NULL && tick - pitem->tickonstart > pitem->interval * 1000) pDo = pitem; pitem = pitem->next; } if(pDo) DoTimer(hwnd, pDo); }
/* get samples ---------------------------------------------------------------- */ void SPC_update(unsigned char *buf) { // APU_LOOP int c, ic, oc; BCOLOR(255, 0, 0); /* VP : rewrote this loop, it was completely wrong in original spcxmms-0.2.1 */ for (c = 0; c < 2048000 / RATE; ) { oc = c; for (ic = c + 256; c < ic; ) { int cycles; cycles = S9xAPUCycleLengths [*IAPU.PC]; (*S9xApuOpcodes[*IAPU.PC]) (); //APU_EXECUTE1(); if (IAPU.Slowdown > 0) { /* VP : in slowdown mode, SPC executes 2^SPC_slowdown_cycle_shift times slower */ c += cycles << SPC_slowdown_cycle_shift; IAPU.Slowdown --; } else { /* VP : no slowdown, normal speed of the SPC */ c += cycles; } } /* VP : Execute timer required number of times */ for (ic = 0; ic < (c / 32) - (oc / 32) ; ic++) { IAPU.TimerErrorCounter ++; DoTimer(); } } BCOLOR(255, 255, 0); S9xMixSamples ((unsigned char *)buf, samples_per_mix); BCOLOR(0, 0, 0); }
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case 1: DoStart(hWnd); break; case 2: DoStop(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_TIMER: DoTimer(hWnd); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); DoPaint(hdc); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
LRESULT CALLBACK FloatToolbarWndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { PFLT_WND FltInfo; /* Get the window context */ FltInfo = (PFLT_WND)GetWindowLongPtr(hwnd, GWLP_USERDATA); if (FltInfo == NULL && Message != WM_CREATE) { goto HandleDefaultMessage; } switch(Message) { case WM_CREATE: { FltInfo = (PFLT_WND)(((LPCREATESTRUCT)lParam)->lpCreateParams); /*FIXME: read this from registry */ // FltInfo->bShow = TRUE; SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)FltInfo); FltInfo->bOpaque = FALSE; SetWindowLongPtr(hwnd, GWL_EXSTYLE, GetWindowLongPtr(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED); /* set the tranclucency to 60% */ FltInfo->Transparancy = 60; SetLayeredWindowAttributes(hwnd, 0, (255 * FltInfo->Transparancy) / 100, LWA_ALPHA); } break; case WM_TIMER: { DoTimer(FltInfo, wParam); } break; case WM_NCMOUSEMOVE: case WM_MOUSEMOVE: { if (FltInfo->bOpaque == FALSE) { RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); FltInfo->bOpaque = TRUE; //MessageBox(NULL, _T("in"), _T("Hit test"), MB_OK | MB_ICONEXCLAMATION); /* timer to check if cursor is in toolbar coords */ SetTimer(hwnd, ID_TIMER1, 200, NULL); /* timer to fade in the toolbars */ SetTimer(hwnd, ID_TIMER2, 50, NULL); } } break; case WM_CLOSE: ShowHideWindow(FltInfo->hSelf); break; case WM_COMMAND: { if (LOWORD(wParam) == IDCANCEL) ShowHideWindow(FltInfo->hSelf); switch(LOWORD(wParam)) { case ID_NEW: MessageBox(hwnd, _T("Kapow!"), _T("Hit test"), MB_OK | MB_ICONEXCLAMATION); break; case ID_CLONESTAMP: case ID_COLORPICKER: case ID_ECLIPSE: case ID_ECLIPSESEL: case ID_ERASER: case ID_FREEFORM: case ID_LASOO: case ID_LINE: case ID_MAGICWAND: case ID_MOVE: case ID_MOVESEL: case ID_PAINTBRUSH: case ID_PAINTBUCKET: case ID_PENCIL: case ID_RECOLORING: case ID_RECTANGLE: case ID_ROUNDRECT: case ID_TEXT: case ID_ZOOM: /*SendMessage(Info->hSelf, LOWORD(wParam), wParam, lParam);*/ break; } } break; case WM_NCACTIVATE: /* FIXME: needs fully implementing */ return DefWindowProc(hwnd, Message, TRUE, lParam); break; case WM_DESTROY: SetWindowLongPtr(hwnd, GWLP_USERDATA, 0); break; default: HandleDefaultMessage: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; }
void ReadSocketInfo(void) { int SelectResult; struct timeval TimeOut; struct DccUser *dccptr; struct PortInfo *tempport; fd_set readfds, writefds; time_t curr_ts; time_t last_ts = current_ts; spill[0] = '\0'; for (;;) { /* * DoTimer() will be called from this code, and so we need curr_ts * here. And we also need time setup here -kre */ curr_ts = current_ts = time(NULL); #ifdef HIGHTRAFFIC_MODE if (HTM) { int htm_count; if (last_ts != curr_ts) { /* * So DoTimer() doesn't get called a billion times when HTM is * turned off */ last_ts = curr_ts; /* * DoTimer() won't do anything other than update * Network->CheckRecvB since we are in HTM */ DoTimer(curr_ts); } /* if (last_ts != curr_ts) */ /* Check if HTM should be turned off */ if ((curr_ts - HTM_ts) >= HTM_TIMEOUT) { float currload; currload = ((float) (Network->RecvB - Network->CheckRecvB) / (float) 1024) / (float) HTM_INTERVAL; if (currload >= (float) ReceiveLoad) { HTM_ts = curr_ts; putlog(LOG1, "Continuing high-traffic mode (%0.2f K/s)", currload); SendUmode(OPERUMODE_Y, "*** Continuing high-traffic mode (%0.2f K/s)", currload); } else { HTM = HTM_ts = 0; putlog(LOG1, "Resuming standard traffic operations (%0.2f K/s)", currload); SendUmode(OPERUMODE_Y, "*** Resuming standard traffic operations (%0.2f K/s)", currload); } } for (htm_count = 0; htm_count < HTM_RATIO; htm_count++) if (!ReadHub()) { read_socket_done = 1; return; } } /* if (HTM) */ else #endif /* HIGHTRAFFIC_MODE */ if (last_ts != curr_ts) { if (curr_ts > (last_ts + 1)) { time_t delta; /* * we skipped at least 1 time interval - make up for it * by calling DoTimer() how ever many intervals we missed * (ie: if the TS went from 5 to 8, call DoTimer() twice * since we missed 6 and 7) */ for (delta = (last_ts + 1); delta < curr_ts; delta++) DoTimer(delta); } last_ts = curr_ts; DoTimer(curr_ts); } FD_ZERO(&readfds); FD_ZERO(&writefds); TimeOut.tv_sec = 0L; TimeOut.tv_usec = 20000L; if (currenthub && (HubSock != NOSOCKET)) { if (IsHubConnect(currenthub)) FD_SET(HubSock, &writefds); else FD_SET(HubSock, &readfds); } for (dccptr = connections; dccptr; dccptr = dccptr->next) { if (IsDccConnect(dccptr)) FD_SET(dccptr->socket, &writefds); else { FD_SET(dccptr->socket, &readfds); if (dccptr->authfd != NOSOCKET) { FD_SET(dccptr->authfd, &readfds); if (dccptr->flags & SOCK_WRID) FD_SET(dccptr->authfd, &writefds); } } } for (tempport = PortList; tempport; tempport = tempport->next) if (tempport->socket != NOSOCKET) FD_SET(tempport->socket, &readfds); SelectResult = select(FD_SETSIZE, &readfds, &writefds, NULL, &TimeOut); if (SelectResult > 0) { /* something happened */ if (currenthub && (HubSock != NOSOCKET)) { /* This is for info coming from the hub server */ if (IsHubConnect(currenthub) && FD_ISSET(HubSock, &writefds)) { if (!CompleteHubConnection(currenthub)) { close(HubSock); currenthub->sockfd = HubSock = NOSOCKET; } } else if (FD_ISSET(HubSock, &readfds)) { if (!ReadHub()) { read_socket_done = 1; return; /* something bad happened */ } } } /* if (currenthub && (HubSock != NOSOCKET)) */ for (tempport = PortList; tempport; tempport = tempport->next) { if (tempport->socket == NOSOCKET) continue; if (FD_ISSET(tempport->socket, &readfds)) { /* * a client has connected on one of our listening ports * (P: lines) - accept their connection, and perform * ident etc */ ConnectClient(tempport); } } /* this is for info coming from a dcc/telnet/tcm client */ for (dccptr = connections; dccptr != NULL; dccptr = dccnext) { assert(dccptr->socket != NOSOCKET); dccnext = dccptr->next; if (IsDccConnect(dccptr) && FD_ISSET(dccptr->socket, &writefds)) { if (!GreetDccUser(dccptr)) DeleteDccClient(dccptr); continue; } if (dccptr->authfd >= 0) { if (FD_ISSET(dccptr->authfd, &writefds) && (dccptr->flags & SOCK_WRID)) writeauth(dccptr); /* send ident request */ if (FD_ISSET(dccptr->authfd, &readfds)) readauth(dccptr); /* read ident reply */ } if (dccptr->flags & SOCK_NEEDID) continue; if (FD_ISSET(dccptr->socket, &readfds)) { /* read and parse any data from the socket */ if (!ReadSock(dccptr)) { CloseConnection(dccptr); continue; } } /* if (FD_ISSET(dccptr->socket, &readfds)) */ } /* for (dccptr = connections; dccptr; dccptr = dccptr->next) */ dccnext = NULL; /* XXX */ } /* if (SelectResult > 0) */ else { /* Also check whether is errno set at all.. -kre */ if ((SelectResult == (-1)) && errno && (errno != EINTR)) { #ifdef DEBUGMODE fprintf(stderr, "Connection closed: %s\n", strerror(errno)); #endif putlog(LOG1, "Lost connection to %s:%d (%s)", currenthub->hostname, currenthub->port, strerror(errno)); return; } } } /* for (;;) */ } /* ReadSocketInfo() */
void dispatch(void) { int ret, i; struct timeval *wt, waittime; int timeout; unsigned int now; fd_set dump_rfds; while (1) { /* this can do some work queue job */ /* check timer list */ wt = NULL; if (timers) { now = GetTimeInMillis(); timeout = timers->expires - now; if (timeout > 0 && timeout > timers->delta + 250) { /* time has rewound. reset the timers. */ CheckAllTimers(); } if (timers) { timeout = timers->expires - now; if (timeout < 0) timeout = 0; waittime.tv_sec = timeout / MILLI_PER_SECOND; waittime.tv_usec = (timeout % MILLI_PER_SECOND) * (1000000 / MILLI_PER_SECOND); wt = &waittime; } } /* prepare read fd set */ FD_ZERO(&dump_rfds); //memcpy(&dump_rfds, &rfds, sizeof(rfds)); for (i = 0; i < MAX_EVENT_SET; i++) { if (!gInputEventSet[i]) continue; if (maxfds < gInputEventSet[i]->fd) maxfds = gInputEventSet[i]->fd; FD_SET(gInputEventSet[i]->fd, &dump_rfds); } ret = select(maxfds+1, &dump_rfds, NULL, NULL, wt); if (ret <= 0) { /* An error or timeout occurred */ if (ret < 0) { return; } if (timers) { int expired = 0; now = GetTimeInMillis(); if ((int)(timers->expires - now) <= 0) expired = 1; while (timers && (int)(timers->expires - now) <= 0) DoTimer(timers, now, &timers); } } else { handleEvent(&dump_rfds, ret); if (timers) { now = GetTimeInMillis(); while (timers && (int)(timers->expires - now) <= 0) DoTimer(timers, now, &timers); } } } }