Пример #1
0
void FakeGPS()
{
    TcpListenSocket listen;
    TcpServer connection;
    Packet data;
    string source;
    if(!listen.InitializeSocket(PORT)) 
    {
        cout << "Unable to initialize listen socket on port " << PORT << endl;
        return;
    }
    cout << "Initialized TCP Listen Socket\n";
    cout << "Waiting for a connection.... ";
    bool quitFlag = false;
    while( listen.AwaitConnection(connection) && !quitFlag) 
    {
        cout << "Connection Made!\n";
        std::stringstream str;
        Time t;
        while(!quitFlag)
        {
            // Clear string.
            str.clear();
            str.str(std::string());
            // Set position data and message type.
            str << "$GPGLL,4916.45,N,12311.12,W,";
            // Get the current time.
            t.SetCurrentTime();
            str << t.mHour << t.mMinute << "." << t.mSecond;
            str << ",A";
            // Now calculate the checksum.
            unsigned char checkSum = 0;
            // Don't include the '$' character, but go to end.
            for(unsigned int i = 1; i < (unsigned int)str.str().size(); i++)
            {
                if(i == 1) 
                {
                    checkSum = str.str().c_str()[i];
                }
                else
                {
                    checkSum ^= str.str().c_str()[i];
                }
            }
            // Now add the checksum to the string.
            str << "*" << std::hex << std::uppercase << (int) checkSum;
            // Print to screen.
            std::cout << str.str() << std::endl;
            // Now send the data.
            if(connection.Send(str.str().c_str(), (unsigned int)str.str().length()) <= 0)
            {
                // Failed to send, connection must be closed.
                break;
            }
            // Check for exit by user.
            if(GetChar() == 27)
            {
                quitFlag = true;
            }
            SleepMs(250);
        }

        cout << "Connection Closed.\n";
        connection.Shutdown();
    }
}