static DWORD WINAPI ThreadProc( _In_ LPVOID lpParameter ) { auto me = (CompletionQueue*)lpParameter; for (;;) { CompletionPacket packet; ULONG_PTR key; auto success = GetQueuedCompletionStatus( me->iocp, (LPDWORD)&packet.byteTransferred, &key, (LPOVERLAPPED*)&packet.overlapped, INFINITE ); if (packet.overlapped == nullptr) { panic_if(!success, "Failed to deque op"); break; } packet.error = success ? 0 : GetLastError(); panic_if(key == 0, "iocpl: key is null"); ((LPFN_COMPLETION_PROC)key)(packet); } me->release(); return 0; }
bool Gui::setTheme(const char *path) { this->bg_left = this->loadThemeImage(path, "bg_left.png"); this->bg_middle = this->loadThemeImage(path, "bg_middle.png"); this->bg_right = this->loadThemeImage(path, "bg_right.png"); this->bg_submenu_left = this->loadThemeImage(path, "bg_submenu_left.png"); this->bg_submenu_middle = this->loadThemeImage(path, "bg_submenu_middle.png"); this->bg_submenu_right = this->loadThemeImage(path, "bg_submenu_right.png"); this->background = this->loadThemeImage(path, "background.png"); this->main_menu_bg = this->loadThemeImage(path, "main_menu_bg.png"); this->status_bar_bg = this->loadThemeImage(path, "status_bar.png"); this->dialogue_bg = this->loadThemeImage(path, "dialogue_box.png"); this->play = this->loadThemeImage(path, "play.png"); this->pause = this->loadThemeImage(path, "pause.png"); this->default_font = this->loadThemeFont(path, "font.ttf", 14); this->small_font = this->loadThemeFont(path, "font.ttf", 12); this->mpd_conn = mpd_connection_new(NULL, 0, 30000); panic_if (this->mpd_conn == NULL, "Out of memory"); panic_if (mpd_connection_get_error(this->mpd_conn) != MPD_ERROR_SUCCESS, "MPD conn error: %s", mpd_connection_get_error_message(this->mpd_conn)); /* Create the views */ this->status_bar = new StatusBar(); this->mv = new MainView(); this->pv = new PlayView(); this->fv = new FileView(false); this->plv = new FileView(true); return true; }
SocketStarter::SocketStarter() { auto iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); panic_if(iResult != NO_ERROR, "WSAStartup"); OsTcpSocket s; static GUID connGuid = WSAID_CONNECTEX; static GUID acceptGuid = WSAID_ACCEPTEX; DWORD numBytes; iResult = ::WSAIoctl(s.native_handle(), SIO_GET_EXTENSION_FUNCTION_POINTER, &connGuid, sizeof(connGuid), &ConnectExPtr, sizeof(ConnectExPtr), &numBytes, NULL, NULL); panic_if(iResult != NO_ERROR, "GetConnextEx"); iResult = ::WSAIoctl(s.native_handle(), SIO_GET_EXTENSION_FUNCTION_POINTER, &acceptGuid, sizeof(acceptGuid), &AcceptExPtr, sizeof(AcceptExPtr), &numBytes, NULL, NULL); panic_if(iResult != NO_ERROR, "GetAcceptEx"); WSAPROTOCOL_INFOW wsaProtocolInfo; int wsaProtocolInfoSize = sizeof(wsaProtocolInfo); iResult = getsockopt( s.native_handle(), SOL_SOCKET, SO_PROTOCOL_INFOW, reinterpret_cast<char*>(&wsaProtocolInfo), &wsaProtocolInfoSize); panic_if(iResult == SOCKET_ERROR, "SOL_SOCKET"); //syncCompletion = false; syncCompletion = (wsaProtocolInfo.dwServiceFlags1 & XP1_IFS_HANDLES) != 0; }
void ThreadPool::AssociateHandle(native_handle_t h, LPFN_COMPLETION_PROC fn) { panic_if(queue == nullptr, "ThreadPool is not initialized"); if (startSockets.syncCompletion) { auto success = SetFileCompletionNotificationModes( (HANDLE)h, FILE_SKIP_COMPLETION_PORT_ON_SUCCESS); panic_if(!success, "FILE_SKIP_COMPLETION_PORT_ON_SUCCESS"); } queue->AssociateHandle((HANDLE)h, fn); }
void ThreadPool::Post(os_async_context * o, int nBytes, LPFN_COMPLETION_PROC fn) { panic_if(queue == nullptr, "ThreadPool is not initialized"); auto success = PostQueuedCompletionStatus( queue->iocp, nBytes, (ULONG_PTR)fn, (LPOVERLAPPED)o ); panic_if(!success, "PostQueuedCompletionStatus"); }
ThreadPool::ThreadPool(int threadCount, bool enableSync) { panic_if(queue != nullptr, "ThreadPool is already initialized"); queue = new CompletionQueue(threadCount); if (enableSync == false) { startSockets.syncCompletion = false; } }
std::error_code OsTcpSocket::Accept(native_handle_t newh, endpoint_name_pair& eps, os_async_context* o) { DWORD unused; auto bRetVal = (*startSockets.AcceptExPtr)(handle_, newh, &eps, 0, sizeof(eps.local), sizeof(eps.remote), &unused, (LPOVERLAPPED)o); int status; if (bRetVal == FALSE) { status = WSAGetLastError(); panic_if(status != ERROR_IO_PENDING, "AcceptEx failed"); } else { status = 0; panic_if(true, "Strange, completed synchronously\n"); } return{ status, std::system_category() }; }
CompletionQueue(int threadCount) : iocp(CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 0)) , count(threadCount) , threadCount(threadCount) { panic_if(iocp == nullptr, "CreateIoCompletionPort"); for (int i = 0; i < threadCount; ++i) { t[i] = CreateThread( nullptr, 0, CompletionQueue::ThreadProc, this, 0, nullptr ); panic_if(t[i] == nullptr, "Failed to create thread"); } }
virtual void keyCallback(bool shift, const char *str) { panic_if(!this->cur_key, "No key selected but keyboard active???\n"); *this->cur_key = this->stringToKeycode(str); this->updateHelpMessages(); this->help->updateHelpMessage(this->cur_sel); VirtualKeyboard::kbd->deactivate(); this->cur_key = NULL; }
std::error_code OsTcpSocket::Connect(endpoint const& ep, os_async_context* o) { DWORD unused; auto ok = (*startSockets.ConnectExPtr)( handle_, (const sockaddr*)ep.addr(), (int)ep.size(), nullptr, 0, &unused, (OVERLAPPED*)o ); int status; if (ok) { status = 0; panic_if(true, "did not expect Connect to complete synchronously"); } else { status = WSAGetLastError(); panic_if(status != ERROR_IO_PENDING, "Connect failed"); } return{ status, std::system_category() }; }
bool await_suspend(coroutine_handle<> h) { this->resume = h; auto error = me->sock.Receive(buf, len, this); if (error.value() == kSynchCompletion) { return false; } if (error) { panic_if(!!error, "read failed"); } return true; }
void TimerController::disarm(TimeoutHandler *which) { /*Trying to disarm something which is not armed! */ if (which->timer_id < 0) return; panic_if(which->timer_id >= this->n_handlers, "timer_id %d is too out of bounds (max %d)\n", which->timer_id, this->n_handlers); this->handlers[which->timer_id] = NULL; which->timer_id = -1; }
static SDL_Surface *init(void) { struct SDL_Surface *screen; const SDL_VideoInfo *info; SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO); info = SDL_GetVideoInfo(); panic_if (!info, "Can't do GetVideoInfo()!\n"); screen = SDL_SetVideoMode(320, 240, info->vfmt->BitsPerPixel, SDL_HWSURFACE); panic_if(!screen, "Cannot initialize video: %s\n", SDL_GetError()); TTF_Init(); TimerController::init(TICKS_PER_MS); Gui::init(); return screen; }
void Gui::init() { Gui::gui = new Gui(); if (!Gui::gui->setTheme(ThePrefs.Theme)) { /* Set the default theme */ panic_if (!Gui::gui->setTheme("default"), "Setting default theme failed\n"); } Gui::gui->status_bar->queueMessage("Welcome to C64-network.org, the networked C64!"); Gui::gui->status_bar->queueMessage("Press Home for the menu!"); }
OsTcpSocket::OsTcpSocket() : handle_(::WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_OVERLAPPED)) { panic_if(handle_ == INVALID_SOCKET, "WsaSocketW"); int OptionValue = 1; DWORD NumberOfBytesReturned = 0; int status = 0; status = WSAIoctl( handle_, SIO_LOOPBACK_FAST_PATH, &OptionValue, sizeof(OptionValue), NULL, 0, &NumberOfBytesReturned, 0, 0); panic_if(SOCKET_ERROR == status, "SetFlags"); INT optval = 1; status = setsockopt(handle_, IPPROTO_TCP, TCP_NODELAY, (PCHAR)&optval, sizeof(INT)); panic_if(status != 0, "Failed to set TCP_NODELAY socket option"); }
void Gui::init() { Gui::gui = new Gui(); if (!Gui::gui->setTheme("default")) { /* Set the default theme */ panic_if (!Gui::gui->setTheme("default"), "Setting default theme failed\n"); } Gui::gui->status_bar->queueMessage("Press Menu key for the menu!"); /* Start in the play view */ Gui::gui->pushView(Gui::gui->pv); }
const char *ip_to_str(uint8_t *ip_in) { char *out = (char *)xmalloc(24); int ip[4]; for (int i = 0; i < 4; i++) { char tmp[3]; char *endp; tmp[0] = ip_in[i * 2]; tmp[1] = ip_in[i * 2 + 1]; tmp[2] = '\0'; ip[i] = strtoul(tmp, &endp, 16); panic_if (endp == (const char*)tmp, "Could not convert ip to str.\n"); } sprintf(out, "%d.%d.%d.%d", ip[3], ip[2], ip[1], ip[0]); return out; }
void OsTcpSocket::Bind(endpoint const& ep) { auto result = ::bind(handle_, (const sockaddr*)ep.addr(), (int)ep.size()); panic_if(result == SOCKET_ERROR, "bind failed"); }
void Frodo::ReadyToRun(void) { if (getcwd(AppDirPath, 256) == NULL) strcpy(AppDirPath, ""); this->LoadFrodorc(); if (network_server_connect) strncpy(ThePrefs.NetworkServer, network_server_connect, sizeof(ThePrefs.NetworkServer)); //Mount the floppy if passed as argument if (floppy8) { if (ext_matches_list(floppy8, game_exts)) { strncpy(ThePrefs.DrivePath[0], floppy8, sizeof(ThePrefs.DrivePath[0])); if (ext_matches_list(floppy8, prg_exts)) { char *tmp_filename; FILE *src, *dst; tmp_filename = (char *)xmalloc(strlen(TMP_ROOT_PATH) + 4); sprintf(tmp_filename, "%s/ab", TMP_ROOT_PATH); /* Clean temp dir first (we only want one file) */ unlink(tmp_filename); src = fopen(ThePrefs.DrivePath[0], "r"); if (src != NULL) { snprintf(ThePrefs.DrivePath[0], sizeof(ThePrefs.DrivePath[0]), "%s", TMP_ROOT_PATH); /* Special handling of .prg: Copy to TMP_PATH and * load that as a dir */ dst = fopen(tmp_filename, "w"); if (dst) { Uint8 buf[1024]; size_t v; do { v = fread(buf, 1, sizeof(buf), src); fwrite(buf, 1, v, dst); } while (v > 0); fclose(dst); } fclose(src); } free(tmp_filename); } } } panic_if (!init_graphics(), "Can't initialize graphics!\n"); // Create and start C64 TheC64 = new C64; DataStore::ds = new DataStore(); TimerController::init(); Gui::init(); load_rom_files(); TheC64->Run(); delete TheC64; }
void AssociateHandle(HANDLE ioHandle, LPFN_COMPLETION_PROC fn) { auto result = CreateIoCompletionPort(ioHandle, iocp, (ULONG_PTR)fn, 0); panic_if(result != iocp, "association handle failed"); }
void OsTcpSocket::Listen() { auto iResult = ::listen(handle_, 100); panic_if(iResult == SOCKET_ERROR, "listen failed with error"); }
Connection await_resume() { panic_if(!!this->err, "Connect failed"); return std::move(conn); }