int
__cdecl
main(
    int argc,
    char *argv[]
    )
{
    HANDLE WritingThread;
    DWORD ThreadId;
    ULARGE_INTEGER FileSize;
    ULARGE_INTEGER InitialFileSize;
    BOOL Success;
    DWORD Status;
    DWORD StartTime, EndTime;
    SYSTEM_INFO SystemInfo;
    HANDLE BufferedHandle;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s SourceFile DestinationFile\n", argv[0]);
        exit(1);
    }

    //
    //confirm we are running on Windows NT 3.5 or greater, if not, display notice and
    //terminate.  Completion ports are only supported on Win32 & Win32s.  Creating a
    //Completion port with no handle specified is only supported on NT 3.51, so we need
    //to know what we're running on.  Note, Win32s does not support console apps, thats
    //why we exit here if we are not on Windows NT.
    //
    //
    //ver.dwOSVersionInfoSize needs to be set before calling GetVersionInfoEx()
    //
    ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    //
    //Failure here could mean several things 1. On an NT system,
    //it indicates NT version 3.1 because GetVersionEx() is only
    //implemented on NT 3.5.  2. On Windows 3.1 system, it means
    //either Win32s version 1.1 or 1.0 is installed.
    //
    Success = GetVersionEx((LPOSVERSIONINFO) &ver);

    if ( (!Success) ||                                   //GetVersionEx() failed - see above.
         (ver.dwPlatformId != VER_PLATFORM_WIN32_NT) )   //GetVersionEx() succeeded but we are not on NT.
      {
       MessageBox(NULL,
                  "This sample application can only be run on Windows NT. 3.5 or greater\n"
                  "This application will now terminate.",
                  "UnBufCp2",
                  MB_OK           |
                  MB_ICONSTOP     |
                  MB_SETFOREGROUND );
       exit( 1 );
      }

    //
    // Get the system's page size.
    //
    GetSystemInfo(&SystemInfo);
    PageSize = SystemInfo.dwPageSize;

    //
    // Open the source file and create the destination file.
    // Use FILE_FLAG_NO_BUFFERING to avoid polluting the
    // system cache with two copies of the same data.
    //

    SourceFile = CreateFile(argv[1],
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_READ,
                            NULL,
                            OPEN_EXISTING,
                            FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
                            NULL);
    if (SourceFile == INVALID_HANDLE_VALUE) {
        fprintf(stderr, "failed to open %s, error %d\n", argv[1], GetLastError());
        exit(1);
    }
    FileSize.LowPart = GetFileSize(SourceFile, &FileSize.HighPart);
    if ((FileSize.LowPart == 0xffffffff) && (GetLastError() != NO_ERROR)) {
        fprintf(stderr, "GetFileSize failed, error %d\n", GetLastError());
        exit(1);
    }

    DestFile = CreateFile(argv[2],
                          GENERIC_READ | GENERIC_WRITE,
                          FILE_SHARE_READ | FILE_SHARE_WRITE,
                          NULL,
                          CREATE_ALWAYS,
                          FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
                          SourceFile);
    if (DestFile == INVALID_HANDLE_VALUE) {
        fprintf(stderr, "failed to open %s, error %d\n", argv[2], GetLastError());
        exit(1);
    }

    //
    // Extend the destination file so that the filesystem does not
    // turn our asynchronous writes into synchronous ones.
    //
    InitialFileSize.QuadPart = (FileSize.QuadPart + PageSize - 1) & ~((DWORD_PTR)(PageSize - 1));
    Status = SetFilePointer(DestFile,
                            InitialFileSize.LowPart,
                            (PLONG)&InitialFileSize.HighPart,
                            FILE_BEGIN);
    if ((Status == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR)) {
        fprintf(stderr, "SetFilePointer failed, error %d\n", GetLastError());
        exit(1);
    }
    Success = SetEndOfFile(DestFile);
    if (!Success) {
        fprintf(stderr, "SetEndOfFile failed, error %d\n", GetLastError());
        exit(1);
    }

    //
    //In NT 3.51 it is not necessary to specify the FileHandle parameter
    //of CreateIoCompletionPort()--It is legal to specify the FileHandle
    //as INVALID_HANDLE_VALUE.  However, for NT 3.5 an overlapped file
    //handle is needed.
    //
    //We know already that we are running on NT, or else we wouldn't have
    //gotten this far, so lets see what version we are running on.
    //
    if (ver.dwMajorVersion == 3 && ver.dwMinorVersion == 50) 
        {
          //
          //we're running on NT 3.5 - Completion Ports exists
          //
          ReadPort = CreateIoCompletionPort(SourceFile,        //file handle to associate with I/O completion port
                                            NULL,              //optional handle to existing I/O completion port
                                            (DWORD_PTR)SourceFile, //completion key
                                            1);                //# of threads allowed to execute concurrently

          if (ReadPort == NULL) {
            fprintf(stderr, "failed to create ReadPort, error %d\n", GetLastError());
            exit(1);
          }
        }

    else   
      //
      //We are running on NT 3.51 or greater.
      //
      //Create the I/O Completion Port.
      //
        {
          ReadPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, //file handle to associate with I/O completion port
                                            NULL,                 //optional handle to existing I/O completion port
                                            (DWORD_PTR)SourceFile,    //completion key
                                            1);                   //# of threads allowed to execute concurrently




         //
         //If we need to, aka we're running on NT 3.51, let's associate a file handle with the
         //completion port.
         //
         ReadPort = CreateIoCompletionPort(SourceFile,
                                           ReadPort,
                                           (DWORD_PTR)SourceFile,  //should be the previously specified key.
                                           1);

         if (ReadPort == NULL)
          {
            fprintf(stderr,
                    "failed to create ReadPort, error %d\n",
                    GetLastError());

            exit(1);

          }
        }

    WritePort = CreateIoCompletionPort(DestFile,
                                       NULL,
                                       (DWORD_PTR)DestFile,
                                       1);
    if (WritePort == NULL) {
        fprintf(stderr, "failed to create WritePort, error %d\n", GetLastError());
        exit(1);
    }

    //
    // Start the writing thread
    //
    WritingThread = CreateThread(NULL,
                                 0,
                                 (LPTHREAD_START_ROUTINE)WriteLoop,
                                 &FileSize,
                                 0,
                                 &ThreadId);
    if (WritingThread == NULL) {
        fprintf(stderr, "failed to create write thread, error %d\n", GetLastError());
        exit(1);
    }
    CloseHandle(WritingThread);

    StartTime = GetTickCount();

    //
    // Start the reads
    //
    ReadLoop(FileSize);

    EndTime = GetTickCount();

    //
    // We need another handle to the destination file that is
    // opened without FILE_FLAG_NO_BUFFERING. This allows us to set
    // the end-of-file marker to a position that is not sector-aligned.
    //
    BufferedHandle = CreateFile(argv[2],
                                GENERIC_WRITE,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL);
    if (BufferedHandle == INVALID_HANDLE_VALUE) {
        fprintf(stderr,
                "failed to open buffered handle to %s, error %d\n",
                argv[2],
                GetLastError());
        exit(1);
    }

    //
    // Set the destination's file size to the size of the
    // source file, in case the size of the source file was
    // not a multiple of the page size.
    //
    Status = SetFilePointer(BufferedHandle,
                            FileSize.LowPart,
                            (PLONG)&FileSize.HighPart,
                            FILE_BEGIN);
    if ((Status == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR)) {
        fprintf(stderr, "final SetFilePointer failed, error %d\n", GetLastError());
        exit(1);
    }
    Success = SetEndOfFile(BufferedHandle);
    if (!Success) {
        fprintf(stderr, "SetEndOfFile failed, error %d\n", GetLastError());
        exit(1);
    }
    CloseHandle(BufferedHandle);
    CloseHandle(SourceFile);
    CloseHandle(DestFile);

    printf("\n\n%d bytes copied in %.3f seconds\n",
           FileSize.LowPart,
           (float)(EndTime-StartTime)/1000.0);
    printf("%.2f MB/sec\n",
           ((LONGLONG)FileSize.QuadPart/(1024.0*1024.0)) / (((float)(EndTime-StartTime)) / 1000.0));

    return(0);

}
Example #2
0
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	QTextCodec *codec = QTextCodec::codecForName("ISO 8859-7");
	QTextCodec::setCodecForCStrings(codec);
	QTextCodec::setCodecForLocale(codec);

	bool ok;
	QString tmpStr;

	QString iniFile = "./GiDT.ini";

	qDebug() << "Main Thread id: " << QThread::currentThreadId();

	MainSignalHandler slotHandler;


	QObject::connect(&slotHandler, SIGNAL(MainQuit()), &a, SLOT(quit()),Qt::DirectConnection);


	/***************** CONFIGURATION *****************/
	//read config file
	QSettings settings(iniFile, QSettings::IniFormat);
	glb_core.inbox = settings.value("Folders/inbox").toString();
	glb_core.hbbox = settings.value("Folders/hbbox").toString();
	glb_core.logbox = settings.value("Folders/logbox").toString();

	logger = new Logger(glb_core.logbox);

	logger->AddToLog("Program started.");

    //QSqlDatabase maindbHandler = QSqlDatabase::addDatabase("QSQLITE");	//create db object

	/***************** MODULE MANAGER *****************/

	//Start module manager
	modManager = new ModuleManager(&a, iniFile);
	QObject::connect(modManager, SIGNAL(Log(QString)), logger, SLOT(AddLog(QString)), Qt::DirectConnection);
	QObject::connect(modManager, SIGNAL(Output(QString)), logger, SLOT(AddLog(QString)), Qt::DirectConnection);

	/***************** DATABASE *****************/
	//Connect to MySQL database
	glb_core.dbName = settings.value("Database/dbName").toString();
	glb_core.dbServer = settings.value("Database/dbServer").toString();
	glb_core.dbUser = settings.value("Database/dbUser").toString();
	glb_core.dbPassword = settings.value("Database/dbPassword").toString();

	db = new KnxDB(glb_core.dbUser, glb_core.dbPassword, glb_core.dbName, glb_core.dbServer);
	QObject::connect(modManager, SIGNAL(Output(QString)), db, SLOT(Input(QString)), Qt::DirectConnection);		//MOD -> DB
	QObject::connect(db, SIGNAL(OutputMod(QString)),modManager, SLOT(Input(QString)), Qt::DirectConnection);	//DB -> MOD
	QObject::connect(db, SIGNAL(Log(QString)), logger, SLOT(AddLog(QString)), Qt::DirectConnection);
	if (!db->Connect()) {
		qDebug() << "WTF?!?";	//if it can't connect to db then exit
		abort();
	}
	

    /***************** KNX/IP GATEWAYS *****************/
    //Create a new knx/ip client
    int size = settings.beginReadArray("KnxGateways");
    qDebug() << "Found " << size << " gateways.";
    if (size > 0) {
        for (int i = 0; i < size; ++i) {
            settings.setArrayIndex(i);
            qDebug() << "KnxGateways ini[" << i << "]: " << settings.value("iniFile").toString();
            gw[i] = new KnxGateway(settings.value("iniFile").toString());
        }
        settings.endArray();
    }
    //gw = new KnxGateway(settings.value("KnxGateways/iniFile").toString());
    //Use only first gateway
    QObject::connect(gw[0], SIGNAL(Output(QString)), db, SLOT(Input(QString)),Qt::DirectConnection);		//GW -> DB
    QObject::connect(db, SIGNAL(OutputKnx(QString)), gw[0], SLOT(Input(QString)), Qt::DirectConnection);	//DB -> GW
    QObject::connect(gw[0], SIGNAL(Log(QString)), logger, SLOT(AddLog(QString)), Qt::DirectConnection);
    QObject::connect(&slotHandler, SIGNAL(AllQuit()), gw[0], SLOT(AllQuit()),Qt::DirectConnection);


//	QThread gatewayThread;
//	gw[0]->moveToThread(&gatewayThread);
//	QObject::connect(&gatewayThread, SIGNAL(started()),gw[0], SLOT(UdpThread()), Qt::DirectConnection);
//	gatewayThread.start();


//	/***************** UDP SERVER *****************/
//	QString udpServerIp = settings.value("UdpServer/listenIp").toString();
//	quint16 udpServerPort = settings.value("UdpServer/listenPort").toInt();

//	udpServer = new UdpServer(udpServerIp, udpServerPort);
//	QObject::connect(udpServer, SIGNAL(Output(QString)), db, SLOT(Input(QString)), Qt::DirectConnection);		//UDP -> DB
//	QObject::connect(db, SIGNAL(OutputUdp(QString)), udpServer, SLOT(Input(QString)), Qt::DirectConnection);	//UDP <- DB
//	QObject::connect(udpServer, SIGNAL(Log(QString)), logger, SLOT(AddLog(QString)), Qt::DirectConnection);
//	QObject::connect(&slotHandler, SIGNAL(AllQuit()), udpServer, SLOT(AllQuit()),Qt::DirectConnection);
//	//QObject::connect(udpServer, SIGNAL(Output(QString, QString)), modManager, SLOT(Input(QString)), Qt::DirectConnection);


//	QThread udpServerThread;
//	udpServer->moveToThread(&udpServerThread);
//	QObject::connect(&udpServerThread, SIGNAL(started()),udpServer, SLOT(OnUdpData()), Qt::DirectConnection);
//	udpServerThread.start();







//	/***************** CONSOLE *****************/

    ConsoleCapture * console = new ConsoleCapture();
    QObject::connect(console, SIGNAL(Log(QString)), logger, SLOT(AddLog(QString)), Qt::DirectConnection);
    QObject::connect(&slotHandler, SIGNAL(AllQuit()), console, SLOT(AllQuit()),Qt::DirectConnection);
    QObject::connect(console, SIGNAL(ConsoleMsg(QString)), &slotHandler, SLOT(ConsoleMsg(QString)),Qt::DirectConnection);

    QThread captureThread;
    console->moveToThread(&captureThread);
    QObject::connect(&captureThread, SIGNAL(started()),console, SLOT(ReadLoop()), Qt::DirectConnection);
    // go!
    captureThread.start();

    //Start ModuleManager
    if (modManager->Begin() < 0) {
        logger->AddToLog("Could not start Module Manager.");
        return(-1);
    }
    QThread modThread;

    modManager->moveToThread(&modThread);
    QObject::connect(&modThread, SIGNAL(started()),modManager, SLOT(ManagerThread()), Qt::DirectConnection);
    modThread.start();

	qDebug() << "[Main] Functioning with timestamp: " << QDateTime::currentDateTime().toTime_t();
	//QTimer::singleShot(0, console, SLOT(ReadLoop()));
	
	return a.exec();
}