void ServerAuthModule::OnMessageVerifyClient(RawMessage& rawMessage)
{
   if (m_bIsVerifiedClient)
      throw AuthException(AuthException::authInternalError, _T("client already verified"), __FILE__, __LINE__);
   if (m_spServer == NULL)
      throw AuthException(AuthException::authInternalError, _T("server == NULL"), __FILE__, __LINE__);

   SRPAuthVerifyClientMessage verifyClientMessage;
   ConstVectorRefStream stream(rawMessage.Data());
   verifyClientMessage.Deserialize(stream);

   HighResolutionTimer timer;
   timer.Start();

   // check client; when failed, client doesn't know password
   TByteArray Ms;
   try
   {
      Ms = m_spServer->VerifyClient(verifyClientMessage.GetHc());
   }
   catch(std::runtime_error& ex)
   {
      throw AuthException(
         AuthException::authFailed, _T("couldn't verify client") + CString(ex.what()),
         __FILE__, __LINE__);
   }

   m_bIsVerifiedClient = true;

   // log
   CString cszText;
   cszText.Format(_T("verifying client took %u ms"), unsigned(timer.Elapsed()*1000.0));
   LOG_INFO(cszText, Log::Server::AuthSRP);

   // send server our hash
   SRPAuthVerifyServerMessage verifyServerMessage(Ms);
   SendMessage(verifyServerMessage);

   // get server session key
   TByteArray K = m_spServer->GetK();

   m_spServer.reset(); // no longer needed

   // set up RC4 encryption
   m_spEncryptModule.reset(new RC4::EncryptModule(K));
}
Exemple #2
0
bool RenderCore::Render()
{
	HighResolutionTimer timer;
	timer.Start();

	BeginScene(0.0f, 0.125f, 0.3f, 1.0f);	
	
	// Scene::UpdateVertexBuffer allows us to position the Scene
	if (!g_Scene->UpdateVertexBuffer(0, 0, 1280, 720)) { //  || !g_Scene2->UpdateVertexBuffer(100, 100, 640, 360)) {	
		DebugOut("Scene::UpdateVertexBuffer failed!\n");
		return false;
	}	 
	
	// Get Desktop Duplication frame
	if (g_DesktopDuplication->GetFrame()) { // && g_DesktopDuplication2->GetFrame()) {
		// Render Desktop Duplication frame onto our Scene
		g_Scene->Render(g_DesktopDuplication->GetTexture(), m_WorldMatrix, m_OrthoMatrix);
		//g_Scene2->Render(g_DesktopDuplication2->GetTexture(), m_WorldMatrix, m_OrthoMatrix);

		g_MFEncoder->WriteFrame(g_DesktopDuplication->GetTexture());
		
		g_DesktopDuplication->FinishFrame();
		//g_DesktopDuplication2->FinishFrame();
	}
	
	// limit frame rate
	double frameTime = timer.AsMilliseconds();
	while (frameTime < (MAX_FRAME_TIME)) {
		frameTime += 0.001f;
	}
	DebugOut("frame time: %f\n", frameTime);

	EndScene();

	/* const vec3f eyePosition(0.f, 0.f, 0.f);
	const vec3f lookAt(0.f, 0.f, 1.f);
	const vec3f upDir(0.f, 1.f, 0.f);

	DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixLookAtLH(   DirectX::XMLoadFloat3((const DirectX::XMFLOAT3 *)&eyePosition),
	DirectX::XMLoadFloat3((const DirectX::XMFLOAT3 *)&lookAt),
	DirectX::XMLoadFloat3((const DirectX::XMFLOAT3 *)&upDir)); */		
	
	//ZBufferState(1);

	return true;
};
void ServerAuthModule::OnMessageAuthRequest(RawMessage& rawMessage)
{
   if (m_bIsVerifiedClient)
      throw AuthException(AuthException::authInternalError, _T("client already verified"), __FILE__, __LINE__);
   if (m_spServer != NULL)
      throw AuthException(AuthException::authInternalError, _T("server == NULL"), __FILE__, __LINE__);

   AuthRequestMessage authMessage;
   ConstVectorRefStream stream(rawMessage.Data());
   authMessage.Deserialize(stream);

   // get server infos
   ATLASSERT(m_fnGetServerAuthInfo != NULL);

   TByteArray vecPasswordKey, vecSalt;
   try
   {
      m_fnGetServerAuthInfo(authMessage.GetUsername(), vecPasswordKey, vecSalt, m_iAccountId);
   }
   catch(const Exception& ex)
   {
      LOG_WARN(_T("caught exception during GetServerAuthInfo(): ") + ex.Message(), Log::Server::AuthSRP);

      throw AuthException(
         AuthException::authFailed, _T("couldn't get auth info: ") + ex.Message(),
         __FILE__, __LINE__);
   }
   catch(...)
   {
      throw AuthException(
         AuthException::authFailed, _T("couldn't get auth info"), __FILE__, __LINE__);
   }

   HighResolutionTimer timer;
   timer.Start();

   SRP::GroupParameter gp = SRP::Helper::GetPresetGroupParameter(c_uiDefaultPresetGroupParameter);
   m_spServer.reset(new SRP::Server(gp));

   // generate b
   BigInteger b = SRP::Helper::GenerateRandomBits(1024);

   BigInteger PassVerifier = SRP::Helper::ToInteger<BigInteger>(vecPasswordKey);

   // calculate B
   BigInteger B = m_spServer->GetB(PassVerifier, b);
   TByteArray Bbin;
   SRP::Helper::ToBinaryData(B, Bbin);

   // calculate session key
   TByteArray Abin = authMessage.GetA();
   BigInteger A = SRP::Helper::ToInteger<BigInteger>(Abin);

   BigInteger s = SRP::Helper::ToInteger<BigInteger>(vecSalt);

   USES_CONVERSION;
   std::string strUsername(T2CA(authMessage.GetUsername()));

   m_spServer->CalcSessionKey(A, s, strUsername, PassVerifier);

   // log
   CString cszText;
   cszText.Format(_T("calculating session key took %u ms"), unsigned(timer.Elapsed()*1000.0));
   LOG_INFO(cszText, Log::Server::AuthSRP);

   // send response
   SRPAuthResponseMessage authResponseMessage(vecSalt, Bbin);
   SendMessage(authResponseMessage);
}
 void finish() {
     _outputLoopTime(_timer.elapsed(), m_frames);
 }
 void run() {
     std::cout << "----------------------------------" << std::endl;
     std::cout << m_name << std::endl;
     _timer.restart();
 }
namespace benchmark {
    
    void _outputLoopTime(double time, int frames);
    
    class HighResolutionTimer {
    public:
        HighResolutionTimer()
                : m_startTime(takeTimeStamp()) {}
        
        void restart() {
            m_startTime = takeTimeStamp();
        }
        
        double elapsed() const {
            return double(takeTimeStamp() - m_startTime) * 1e-9;
        }
        
        std::uint64_t elapsedNanoseconds() {
            return takeTimeStamp() - m_startTime;
        }
    
    private:
        static std::uint64_t takeTimeStamp() {
            return std::uint64_t(std::chrono::high_resolution_clock::now().time_since_epoch().count());
        }
        
        std::uint64_t m_startTime;
    };
    
    HighResolutionTimer _timer;
    
    class BaseBenchmark {
    public:
        virtual void SetUp() = 0;
        
        virtual void TearDown() {}
        
        void run() {
            std::cout << "----------------------------------" << std::endl;
            std::cout << m_name << std::endl;
            _timer.restart();
        }
        
        void finish() {
            _outputLoopTime(_timer.elapsed(), m_frames);
        }
        
        void setFrames(int frames) { m_frames = frames; }
        
        void setName(const std::string& name) { m_name = name; }
        
        const std::string& name() const { return m_name; }
    
    private:
        std::string m_name{""};
        int m_frames{0};
    };
    
    std::vector<BaseBenchmark*> _benchmarks;
    std::vector<int> _frames;
    int _currentBenchmarkIndex{-1};
    
    class FrameCounterStage : public Stage {
    public:
        FrameCounterStage()
                : Stage(W, H) {}
        
        ~FrameCounterStage() {}
        
        void preRender(flash::render::RenderState& state) override {
            if (m_currentFrame++ == m_numFrames) {
                if (_currentBenchmarkIndex >= 0) {
                    _benchmarks[_currentBenchmarkIndex]->finish();
                    _benchmarks[_currentBenchmarkIndex]->TearDown();
                    clear();
                }
                ++_currentBenchmarkIndex;
                if (_currentBenchmarkIndex == _benchmarks.size()) {
                    stop();
                } else {
                    m_currentFrame = 0;
                    m_numFrames = _frames[_currentBenchmarkIndex];
                    _benchmarks[_currentBenchmarkIndex]->SetUp();
                    _benchmarks[_currentBenchmarkIndex]->run();
                }
            }
            DisplayObjectContainer::preRender(state);
        };
    
    private:
        int m_numFrames{0};
        int m_currentFrame{0};
    };
    
    FrameCounterStage* _stage;

    DisplayObject* _addNewObjectTo(DisplayObjectContainer* parent) {
        DisplayObject* object = new Shape();
        parent->addChild(object);
        return object;
    }

    DisplayObjectContainer* _addNewContainerTo(DisplayObjectContainer* parent) {
        DisplayObjectContainer* cont = new DisplayObjectContainer();
        parent->addChild(cont);
        return cont;
    }

    void _outputLoopTime(double time, int frames) {
        std::cout << "    total objects: " << _stage->treeSize() << std::endl;
        std::cout << "    loop time (frames: " << frames << "): " << time << std::endl;
        std::cout << "    frame time: " << time / frames << std::endl;
        std::cout << "    fps: " << 1 / (time / frames) << std::endl;
    }
    
    void _initBenchmarks() {
        _stage = new FrameCounterStage();
    }
    
    void _runBenchmarks() {
        _stage->start();
    }
    
    class DeepTree : public BaseBenchmark {
    public:
        void SetUp() override {
            DisplayObjectContainer* currentCont = _stage;
            for (int i = 0; i < 98; ++i) {
                currentCont = _addNewContainerTo(currentCont);
            }
            _addNewObjectTo(currentCont);
        }
    };
    
    class FlatTree : public BaseBenchmark {
    public:
        void SetUp() override {
            DisplayObjectContainer* cont = _addNewContainerTo(_stage);
            for (int i = 0; i < 200; ++i) {
                _addNewObjectTo(cont);
            }
        }
    };
    
    class NormalTree : public BaseBenchmark {
    public:
        void SetUp() override {
            std::vector<int> arr = {1, 3, 2, 1, 4, 2};
            int arrInd = 0;
    
            std::vector<DisplayObject*> objects;
            objects.reserve(160000);
                    
            DisplayObjectContainer* currentCont{nullptr};
            objects.push_back(_addNewContainerTo(_stage));
            int beginIndex = 0;
            int endIndex = 1;
            for (int i = 0; i < 17; ++i) {
                int numChildren = arr[arrInd];
                for (int j = beginIndex; j < endIndex; ++j) {
                    currentCont = static_cast<DisplayObjectContainer*>(objects[j]);
                    for (int k = 0; k < numChildren; ++k) {
                        objects.push_back(_addNewContainerTo(currentCont));
                    }
                }
                beginIndex = endIndex;
                endIndex = (int) objects.size();
                if (++arrInd == arr.size())
                    arrInd = 0;
            }
            for (int i = beginIndex; i < endIndex; ++i) {
                currentCont = static_cast<DisplayObjectContainer*>(objects[i]);
                _addNewObjectTo(currentCont);
            }
        }
    };
    
    class NormalTreeWithInvisibleChildren : public BaseBenchmark {
    public:
        void SetUp() override {
            std::vector<DisplayObject*> objects;
            objects.reserve(160000);
    
            std::vector<int> arr = {1, 3, 2, 1, 4, 2};
            int arrInd = 0;
    
            int invisIndex = 20;
    
            DisplayObjectContainer* currentCont{nullptr};
            objects.push_back(_addNewContainerTo(_stage));
            int beginIndex = 0;
            int endIndex = 1;
            for (int i = 0; i < 17; ++i) {
                int numChildren = arr[arrInd];
                for (int j = beginIndex; j < endIndex; ++j) {
                    currentCont = static_cast<DisplayObjectContainer*>(objects[j]);
                    if (j && j % invisIndex == 0) {
                        currentCont->setVisible(false);
                        ++m_numInvisible;
                    }
                    for (int k = 0; k < numChildren; ++k) {
                        objects.push_back(_addNewContainerTo(currentCont));
                    }
                }
                beginIndex = endIndex;
                endIndex = (int) objects.size();
                if (++arrInd == arr.size())
                    arrInd = 0;
            }
            for (int i = beginIndex; i < endIndex; ++i) {
                currentCont = static_cast<DisplayObjectContainer*>(objects[i]);
                if (i % invisIndex == 0) {
                    currentCont->setVisible(false);
                    ++m_numInvisible;
                }
                _addNewObjectTo(currentCont);
            }
        }
    
        void TearDown() override {
            std::cout << "    invisible objects: " << m_numInvisible << std::endl;
        }
        
    private:
        int m_numInvisible{0};
    };
}
Exemple #7
0
    while (nRand >= nRange);
    return (nRand % nMax);
}

int GetRandInt(int nMax)
{
    return GetRand(nMax);
}

uint256 GetRandHash()
{
    uint256 hash;
    RAND_bytes((unsigned char*)&hash, sizeof(hash));
    return hash;
}








inline int OutputDebugStringF(const char* pszFormat, ...)
{
    int ret = 0;
    if (fPrintToConsole)
    {
        // print to console
        va_list arg_ptr;
        va_start(arg_ptr, pszFormat);
        ret = vprintf(pszFormat, arg_ptr);
        va_end(arg_ptr);
    }
    else
    {
        // print to debug.log
        static FILE* fileout = NULL;

        if (!fileout)
        {
            boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
            fileout = fopen(pathDebug.string().c_str(), "a");
            if (fileout) setbuf(fileout, NULL); // unbuffered
        }
        if (fileout)
        {
            static bool fStartedNewLine = true;
            static boost::mutex mutexDebugLog;
            boost::mutex::scoped_lock scoped_lock(mutexDebugLog);

            // reopen the log file, if requested
            if (fReopenDebugLog) {
                fReopenDebugLog = false;
                boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
                if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
                    setbuf(fileout, NULL); // unbuffered
            }

            // Debug print useful for profiling
            if (fLogTimestamps && fStartedNewLine)
                fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
            if (pszFormat[strlen(pszFormat) - 1] == '\n')
                fStartedNewLine = true;
            else
                fStartedNewLine = false;

            va_list arg_ptr;
            va_start(arg_ptr, pszFormat);
            ret = vfprintf(fileout, pszFormat, arg_ptr);
            va_end(arg_ptr);
        }
    }

#ifdef WIN32