Esempio n. 1
0
void MDCWindow::actionOpen_ROM()
{
    // Get the filepath of a file to open from the user
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open ROM"), "", tr("SNES ROM images (*.smc *.sfc);; All files (*.*)"));

    // If the file was selected, open it
    if(fileName.compare(""))
    {
        // Creae a file object from the filename
        QFile openedFile(fileName);

        // Catch a failed attempt to open the file in read-only mode
        if(!openedFile.open(QIODevice::ReadOnly))
        {
            QMessageBox::critical(this, "Failed to open ROM!", "The file \"" + fileName + "\" could not be opened!");
            return;
        }

        // Read contents of the file into a byte array
        QByteArray tempFileData(openedFile.readAll());
        openedFile.close();

        // Validate the file size
        QString errorMSG(validateROM(tempFileData));
        if(errorMSG.compare(""))
        {
            QMessageBox::critical(this, "Failed to open ROM!", errorMSG);
            return;
        }

        // Enable all disable controls
        ui->actionSave_ROM->setEnabled(true);
        ui->actionLoad_Level->setEnabled(true);
        ui->SaveFileButton->setEnabled(true);
        ui->OpenLevelButton->setEnabled(true);
        ui->IncLevelButton->setEnabled(true);
        ui->LevelLabel->setText("0");

        // Set globals to the opened data
        file = tempFileData;
        header = file.size() % 0x100000;
        setWindowTitle(QFileInfo(openedFile).fileName().append(" - My Dream Course"));

        // Load level 0 as the default level
        currentLevel = 0;
        loadLevel(currentLevel);

        // Draw the screen after level 0 has been loaded
        drawScreen();
    }
}
Esempio n. 2
0
int VideoPreviewSocketProcessor::run()
{
	// ReadHeaders from socket.
	vector<char> buff(8192);
	::Sleep(1);
	
	int size = recv(sock, buff.data(), buff.size(), 0);
	if (size == SOCKET_ERROR)
	{
		int error = ::WSAGetLastError();
		while (error == WSAEWOULDBLOCK)
		{
			::Sleep(1);
			size = recv(sock, buff.data(), buff.size(), 0);
			error = 0;
			if (size == SOCKET_ERROR)
				error =  ::WSAGetLastError();
		}
	}
	if (size == SOCKET_ERROR)
		return 0;
		
	string header = buff.data();
	header = header.substr(0, static_cast<size_t>(size));
	
	VideoPreview::getInstance()->AddLogInfo("Header = " + header);
	parseHeader(header);
	size_t BlockSize = SETTING(INT_PREVIEW_SERVER_SPEED) * 1024;
	if (BlockSize == 0)
		BlockSize = 500 * 1024;
		
	int64_t realFileDataLength = VideoPreview::getInstance()->GetFilePreviewSize();
	int64_t startValue = 0;
	int64_t endValue =  realFileDataLength - 1;
	
	HeaderMapIter iter = headers.find("Range");
	if (iter != headers.end())
	{
		string parsebytesValue = iter->second;
		// bytes=1465141248-
		if (parsebytesValue.substr(0, 6).compare("bytes=") == 0)
		{
			string realbytesRange = parsebytesValue.substr(6);
			size_t lastIndex = realbytesRange.find('-');
			if (lastIndex != string::npos)
			{
				string startValueStr = realbytesRange.substr(0, lastIndex);
				startValue = Util::toInt64(startValueStr);
				if (startValue < 0 || startValue > realFileDataLength - 1)
					startValue = 0;
				size_t realbytesRangeLen = realbytesRange.length();
				if (realbytesRangeLen > lastIndex + 1)
				{
					string endValueStr = realbytesRange.substr(lastIndex + 1);
					if (!endValueStr.empty())
					{
						endValue = Util::toInt64(endValueStr);
						if (endValue < 0 || endValue > realFileDataLength - 1)
							endValue = realFileDataLength - 1;
					}
				}
			}
		}
	}
	
	long dataLength = endValue - startValue + 1;
	
	// dcdebug("VideoPreviewSocketProcessor Ranger: %s - %s\n", Util::toString(startValue).c_str(), Util::toString(endValue).c_str());
	string clientName;
	iter = headers.find("User-Agent");
	if (iter != headers.end())
		clientName = iter->second;
		
	_snprintf(buff.data(), buff.size(), "Client %s asks range " I64_FMT " - " I64_FMT, clientName.c_str(), startValue, endValue);
	string loginfo = buff.data();
	VideoPreview::getInstance()->AddLogInfo(loginfo);
	
	
	// Keep_Alive?
	//DWORD optval = TRUE;
	//int optlen = sizeof(optval);
	//int test = 0;
	//test = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&optval, optlen);
	
	// Accept new socket thread
	if (VideoPreview::getInstance() != NULL && endValue > 0)
	{
		const std::string content_type = "application/avi";
		
		std::string data = "HTTP/1.1 200 OK\nContent-type: ";
		data += content_type;
		data += "\nContent-Encoding: 8bit";
		if (VideoPreview::getInstance()->IsPreviewFileExists() && BOOLSETTING(INT_PREVIEW_USE_VIDEO_SCROLL))
		{
			data += "\nAccept-Ranges: bytes";
			data += "\nContent-Length:";
			data += Util::toString(dataLength);
			data += "\nContent-Range: bytes ";
			data += Util::toString(startValue);
			data += '-';
			data += Util::toString(endValue);
			data += '/';
			data += Util::toString(realFileDataLength);
		}
		data += "\nContent-Disposition: attachment; filename=";
		data += Util::getFileName(VideoPreview::getInstance()->GetFilePreviewName());
		data += "\n\n";
		
		int64_t filePosition = startValue;
		
		if (SOCKET_ERROR != ::send(sock, data.c_str(), static_cast<int>(data.size()), 0))
		{
			bool dataAsks = false;
			while (!isServerDie)
			{
				if (filePosition >= endValue)
				{
					_snprintf(buff.data(), buff.size(), "Finished sending for client %s (" I64_FMT " - " I64_FMT ")", clientName.c_str(), startValue, endValue);
					loginfo = buff.data();
					VideoPreview::getInstance()->AddLogInfo(loginfo);
					break;
				}
				if (VideoPreview::getInstance()->CanUseFile())
				{
					try
					{
						unique_ptr<SharedFileStream> openedFile(new SharedFileStream(VideoPreview::getInstance()->GetFilePreviewTempName(),  File::READ, File::OPEN | File::SHARED | File::CREATE | File::NO_CACHE_HINT));
						if (openedFile.get() != NULL)
						{
							if (VideoPreview::getInstance()->IsAvailableData(filePosition, BlockSize))
							{
								if (dataAsks)
								{
									_snprintf(buff.data(), buff.size(), "Data was downloaded for client %s (" I64_FMT " - " I64_FMT ")", clientName.c_str(), startValue, endValue);
									loginfo = buff.data();
									VideoPreview::getInstance()->AddLogInfo(loginfo);
								}
								dataAsks = false;
								openedFile->setPos(filePosition);
								size_t readSize = BlockSize;
								unique_ptr<uint8_t[]> data(new uint8_t[readSize]);
								readSize = openedFile->read((void*)data.get(), readSize);
								openedFile.reset();
								filePosition += readSize;
								if (readSize == 0)
								{
									_snprintf(buff.data(), buff.size(), "Finished sending for client %s (" I64_FMT " - " I64_FMT ")", clientName.c_str(), startValue, endValue);
									loginfo = buff.data();
									VideoPreview::getInstance()->AddLogInfo(loginfo);
									break;
								}
								
								// dcdebug("VideoPreviewSocketProcessor SENDING Ranger: %s - %s\n", Util::toString(filePosition).c_str(), Util::toString(readSize).c_str());
								_snprintf(buff.data(), buff.size(), "Sending " I64_FMT " - %u for client %s (" I64_FMT " - " I64_FMT ")", filePosition, unsigned(readSize), clientName.c_str(), startValue, endValue);
								loginfo = buff.data();
								VideoPreview::getInstance()->AddLogInfo(loginfo);
								
								int result = ::send(sock, (const char*)data.get(), static_cast<int>(readSize), 0);
								if (result == SOCKET_ERROR)
								{
									int error = ::WSAGetLastError();
									while (error == WSAEWOULDBLOCK)
									{
										::Sleep(1);
										result = ::send(sock, (const char*)data.get(), static_cast<int>(readSize), 0);
										error = 0;
										if (result == SOCKET_ERROR)
											error =  ::WSAGetLastError();
									}
								}
								if (SOCKET_ERROR == result)
									break;
								::Sleep(1000);
							}
							else
							{
								openedFile.reset();
								if (!dataAsks)
								{
									VideoPreview::getInstance()->SetDownloadSegment(filePosition, BlockSize);
									dataAsks = true;
								}
								::Sleep(1000);
							}
						}
						else
						{
							::Sleep(1);
						}
						
					}
					catch (Exception& e)
					{
						LogManager::message("[VideoPreviewSocketProcessor] Error open SharedFileStream for file = [" + VideoPreview::getInstance()->GetFilePreviewTempName() + "] Error =" + e.getError());
					}
				}
				else
				{
					::Sleep(1000);
				}
			}
		}
	}
	
	::closesocket(sock);
	inProcess = false;
	
	_snprintf(buff.data(), buff.size(), "SOCKET Closed by Client %s range " I64_FMT " - " I64_FMT, clientName.c_str(), startValue, endValue);
	loginfo = buff.data();
	VideoPreview::getInstance()->AddLogInfo(loginfo);
	return 0;
}