////////////////////////////////////////////////////////////////////// // CShareMonkeyData thread run void CShareMonkeyData::OnRun() { Sleep( 50 ); // Display "Please wait..." message while ( m_hInternet != NULL ) { m_pSection.Lock(); if ( BuildRequest() ) { if ( ExecuteRequest() ) { if ( DecodeResponse( m_sStatus ) ) { NotifyWindow(); m_sStatus.Empty(); } else { if ( m_hInternet == NULL ) break; NotifyWindow( (LPCTSTR)m_sStatus ); } break; } else if ( ++m_nFailures >= 3 ) { if ( m_hInternet == NULL ) break; m_sStatus = L"Failed"; NotifyWindow( (LPCTSTR)m_sStatus ); break; } else { if ( m_hInternet == NULL ) break; if ( m_hRequest ) InternetCloseHandle( m_hRequest ); m_hRequest = NULL; m_sStatus = L"Failed. Retrying..."; if ( ! NotifyWindow( (LPCTSTR)m_sStatus ) ) break; Sleep( 1000 ); } } if ( m_hRequest ) InternetCloseHandle( m_hRequest ); m_hRequest = NULL; m_sResponse.Empty(); Sleep( min( m_nDelay, 500ul ) ); } if ( m_hSession ) InternetCloseHandle( m_hSession ); m_hSession = NULL; if ( m_hInternet ) InternetCloseHandle( m_hInternet ); m_hInternet = NULL; }
void Connection::Process(bool executeAfterRead) { log_info("Process: fd=" << socket_.GetFileDescriptor()); bool newMessage = true; while (newMessage) { newMessage = false; if ((connectionState_ == READ_HEADER) && (!ReadHeader())) { connectionState_ = CLOSE_CONNECTION; break; } if ((connectionState_ == READ_REQUEST) && (!ReadRequest())) { connectionState_ = CLOSE_CONNECTION; break; } // When operating a thread pool, the main thread will read the request // but stop to transfer it to a worker thread for execution if (executeAfterRead) { if ((connectionState_ == EXECUTE_REQUEST) && (!ExecuteRequest())) { connectionState_ = CLOSE_CONNECTION; break; } } // When operating a thread pool, the main thread might need to continue // to write a long response to the socket after the connection is returned if (connectionState_ == WRITE_RESPONSE) { if (!WriteResponse()) { connectionState_ = CLOSE_CONNECTION; break; } newMessage = (connectionState_ == READ_HEADER) && (bufferLength_ > 0); } } }
// run thread void start() { const std::string END_OF_TRANSMISSION_TOKEN = "</command>"; Message readMessage; std::string result; bool end_of_transmission = false; int read_size = 0; // listen for incoming data until we receive the end-of-transmission // tokent while( !end_of_transmission ) { read_size += socket_.read_some ( boost::asio::buffer ( readMessage ), error_); result += readMessage.data(); end_of_transmission = result.find(END_OF_TRANSMISSION_TOKEN) != std::string::npos; } // find the end of the packet and resize the string to truncate // any floating garbage in the message buffer. int idx = result.find("</command>"); result = result.substr ( 0, idx+10 ); // run requested command ExecuteRequest ( result ); // send a response message back to the client. boost::asio::async_write ( socket_, boost::asio::buffer ( message_ + "\n" ), boost::bind ( &TcpConnection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); }
void CVkProto::WorkerThread(void*) { debugLogA("CVkProto::WorkerThread: entering"); m_bTerminated = m_prevError = false; m_szAccessToken = getStringA("AccessToken"); char Score[] = "friends,photos,audio,docs,video,wall,messages,offline,status,notifications,groups"; CMStringA szAccessScore(ptrA(getStringA("AccessScore"))); if (szAccessScore != Score) { setString("AccessScore", Score); delSetting("AccessToken"); m_szAccessToken = NULL; } if (m_szAccessToken != NULL) // try to receive a response from server RetrieveMyInfo(); else { // Initialize new OAuth session extern char szBlankUrl[]; AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_GET, "https://oauth.vk.com/authorize", false, &CVkProto::OnOAuthAuthorize) << INT_PARAM("client_id", VK_APP_ID) << CHAR_PARAM("scope", Score) << CHAR_PARAM("redirect_uri", szBlankUrl) << CHAR_PARAM("display", "mobile") << CHAR_PARAM("response_type", "token") << VER_API; pReq->m_bApiReq = false; pReq->bIsMainConn = true; Push(pReq); } m_hAPIConnection = NULL; while (true) { WaitForSingleObject(m_evRequestsQueue, 1000); if (m_bTerminated) break; AsyncHttpRequest *pReq; bool need_sleep = false; while (true) { { mir_cslock lck(m_csRequestsQueue); if (m_arRequestsQueue.getCount() == 0) break; pReq = m_arRequestsQueue[0]; m_arRequestsQueue.remove(0); need_sleep = (m_arRequestsQueue.getCount() > 1) && (pReq->m_bApiReq); // more than two to not gather } if (m_bTerminated) break; ExecuteRequest(pReq); if (need_sleep) { // There can be maximum 3 requests to API methods per second from a client Sleep(330); // (c) https://vk.com/dev/api_requests debugLogA("CVkProto::WorkerThread: need sleep"); } } } if (m_hAPIConnection) { debugLogA("CVkProto::WorkerThread: Netlib_CloseHandle(m_hAPIConnection) beg"); Netlib_CloseHandle(m_hAPIConnection); debugLogA("CVkProto::WorkerThread: Netlib_CloseHandle(m_hAPIConnection) end"); } m_hAPIConnection = NULL; m_hWorkerThread = 0; debugLogA("CVkProto::WorkerThread: leaving m_bTerminated = %d", m_bTerminated ? 1 : 0); }
void FatParser::ReadAsync( PGPUInt8 *buf, PGPUInt64 pos, PGPUInt32 nBytes, GenericCallbackInfo *upInfo) { DualErr derr; PGPUInt16 blockSize; pgpAssertAddrValid(buf, PGPUInt8); pgpAssertAddrValid(upInfo, GenericCallbackInfo); pgpAssertAddrValid(upInfo->callback, PGPUInt8); pgpAssertAddrValid(mFragArray, FileFrag); pgpAssert(mHost.Mounted()); pgpAssert(mNumFragsInUse > 0); pgpAssert(!mFatParserReq.isInUse); mFatParserReq.isInUse = TRUE; blockSize = mHost.GetBlockSize(); if (pos + nBytes > mBytesFile) derr = DualErr(kPGDMinorError_OOBFileRequest); // Prepare the request for execution. if (derr.IsntError()) { PGPBoolean doneWithRequest; mFatParserReq.op = kFPOP_Read; mFatParserReq.upInfo = upInfo; mFatParserReq.downInfo.callback = FatParserCallback; mFatParserReq.downInfo.refData[0] = (PGPUInt32) this; // We start out by initializing the request so that all possible // pieces of the request are marked as completed. mFatParserReq.read.readHeader = TRUE; mFatParserReq.read.readMiddle = TRUE; mFatParserReq.read.readTail = TRUE; // Now we analyze the request and determine what pieces must be // read so that the entire request can be marked as done. if (pos%blockSize > 0) { PGPUInt32 bytesInHeader; // The request has a header - that is, it doesn't begin on a // block boundary. bytesInHeader = min(nBytes, blockSize - (PGPUInt32) (pos%blockSize)); mFatParserReq.read.readHeader = FALSE; mFatParserReq.read.bufHeader = buf; mFatParserReq.read.posHeader = pos; mFatParserReq.read.nBytesHeader = bytesInHeader; buf += bytesInHeader; pos += bytesInHeader; nBytes -= bytesInHeader; } if (nBytes >= blockSize) { PGPUInt32 bytesInMiddle; // The request has a middle - that is, a section that begins and // ends on a block boundary and is more than one block in length. bytesInMiddle = nBytes - nBytes%blockSize; mFatParserReq.read.readMiddle = FALSE; mFatParserReq.read.bufMiddle = buf; mFatParserReq.read.posMiddle = pos; mFatParserReq.read.nBytesMiddle = bytesInMiddle; buf += bytesInMiddle; pos += bytesInMiddle; nBytes -= bytesInMiddle; } if (nBytes > 0) { // The request has a tail - that is, it doesn't end on a // block boundary. mFatParserReq.read.readTail = FALSE; mFatParserReq.read.bufTail = buf; mFatParserReq.read.posTail = pos; mFatParserReq.read.nBytesTail = nBytes; } // Execute the request. ExecuteRequest(&doneWithRequest); // Should always have stuff to do at this point. But if we don't, then // don't hang the computer by not calling back. if (doneWithRequest) { pgpAssert(FALSE); ScheduleAsyncCallback(); } } // Callback on error. if (derr.IsError()) { ScheduleAsyncCallback(derr); } }
void FatParser::FatParserCallbackAux() { DualErr derr; PGPBoolean doneWithRequest; PGPUInt8 *buf; PGPUInt16 blockSize; PGPUInt32 nBytes; PGPUInt64 pos; pgpAssert(mFatParserReq.isInUse); blockSize = mHost.GetBlockSize(); derr = mFatParserReq.downInfo.derr; if (derr.IsntError()) { // Before passing the request to 'ExecuteRequest' we perform some pre- // processing on the data manipulated by the piece of the request that // was just processed. switch (mFatParserReq.op) { case kFPOP_Read: if (!mFatParserReq.read.readHeader) { // We read in the header block. Copy just what the caller needs // into his buffer. mFatParserReq.read.readHeader = TRUE; buf = mFatParserReq.read.bufHeader; pos = mFatParserReq.read.posHeader; nBytes = mFatParserReq.read.nBytesHeader; pgpAssertAddrValid(buf, PGPUInt8); pgpCopyMemory(mDataBuf + pos%blockSize, buf, nBytes); } else if (!mFatParserReq.read.readMiddle) { PGPUInt32 bytesRead; // We read in a piece of the middle. Copy it into the output // buffer, and if there is still more middle left then adjust // the variables. buf = mFatParserReq.read.bufMiddle; pos = mFatParserReq.read.posMiddle; nBytes = mFatParserReq.read.nBytesMiddle; bytesRead = mFatParserReq.read.sizeCurrentRead; pgpAssertAddrValid(buf, PGPUInt8); if (nBytes - bytesRead > 0) { mFatParserReq.read.bufMiddle += bytesRead; mFatParserReq.read.posMiddle += bytesRead; mFatParserReq.read.nBytesMiddle -= bytesRead; } else { mFatParserReq.read.readMiddle = TRUE; } } else if (!mFatParserReq.read.readTail) { // We read in the tail block. Copy just what the caller needs // into his buffer. mFatParserReq.read.readTail = TRUE; buf = mFatParserReq.read.bufTail; nBytes = mFatParserReq.read.nBytesTail; pgpAssertAddrValid(buf, PGPUInt8); pgpCopyMemory(mDataBuf, buf, nBytes); } break; case kFPOP_Write: if (!mFatParserReq.write.readHeader) { // No pre-processing needed here. mFatParserReq.write.readHeader = TRUE; } else if (!mFatParserReq.write.wroteHeader) { // No pre-processing needed here. mFatParserReq.write.wroteHeader = TRUE; } else if (!mFatParserReq.write.wroteMiddle) { PGPUInt32 bytesWritten; // We wrote out a piece of the middle. Copy it into the output // buffer, and if there is still more middle left then adjust // the variables. buf = mFatParserReq.write.bufMiddle; pos = mFatParserReq.write.posMiddle; nBytes = mFatParserReq.write.nBytesMiddle; bytesWritten = mFatParserReq.write.sizeCurrentWrite; pgpAssertAddrValid(buf, PGPUInt8); if (nBytes - bytesWritten > 0) { mFatParserReq.write.bufMiddle += bytesWritten; mFatParserReq.write.posMiddle += bytesWritten; mFatParserReq.write.nBytesMiddle -= bytesWritten; } else { mFatParserReq.write.wroteMiddle = TRUE; } } else if (!mFatParserReq.write.readTail) { // No pre-processing needed here. mFatParserReq.write.readTail = TRUE; } else if (!mFatParserReq.write.wroteTail) { // No pre-processing needed here. mFatParserReq.write.wroteTail = TRUE; } break; default: pgpAssert(FALSE); break; } // Perform further processing on this request, if needed. ExecuteRequest(&doneWithRequest); } // Callback if we're done or on error. if (doneWithRequest || derr.IsError()) { ScheduleAsyncCallback(derr); } }
void PGPdisk::WriteAsync( PGPUInt8 *buf, PGPUInt64 pos, PGPUInt32 nBytes, GenericCallbackInfo *upInfo) { PGPBoolean doneWithRequest; pgpAssertAddrValid(upInfo, GenericCallbackInfo); pgpAssertAddrValid(upInfo->callback, PGPUInt8); pgpAssertAddrValid(buf, PGPUInt8); pgpAssert(Mounted()); pgpAssert(!mPGPdiskReq.isInUse); mPGPdiskReq.isInUse = TRUE; // Prepare the request for execution. mPGPdiskReq.op = kPGDOP_Write; mPGPdiskReq.upInfo = upInfo; mPGPdiskReq.downInfo.callback = PGPdiskCallback; mPGPdiskReq.downInfo.refData[0] = (PGPUInt32) this; // We start out by initializing the request so that all possible // pieces of the request are marked as completed. mPGPdiskReq.write.readHeader = TRUE; mPGPdiskReq.write.wroteHeader = TRUE; mPGPdiskReq.write.wroteMiddle = TRUE; mPGPdiskReq.write.readTail = TRUE; mPGPdiskReq.write.wroteTail = TRUE; // Now we analyze the request and determine what pieces must be // written so that the entire request can be marked as done. if (pos%kDefaultBlockSize > 0) { PGPUInt32 bytesInHeader; // The request has a header - that is, it doesn't begin on a // block boundary. bytesInHeader = min(nBytes, kDefaultBlockSize - (PGPUInt32) (pos%kDefaultBlockSize)); mPGPdiskReq.write.readHeader = FALSE; mPGPdiskReq.write.wroteHeader = FALSE; mPGPdiskReq.write.bufHeader = buf; mPGPdiskReq.write.posHeader = pos; mPGPdiskReq.write.nBytesHeader = bytesInHeader; buf += bytesInHeader; pos += bytesInHeader; nBytes -= bytesInHeader; } if (nBytes >= kDefaultBlockSize) { PGPUInt32 bytesInMiddle; // The request has a middle - that is, a section that begins and // ends on a block boundary and is more than one block in length. bytesInMiddle = nBytes - nBytes%kDefaultBlockSize; mPGPdiskReq.write.wroteMiddle = FALSE; mPGPdiskReq.write.bufMiddle = buf; mPGPdiskReq.write.posMiddle = pos; mPGPdiskReq.write.nBytesMiddle = bytesInMiddle; buf += bytesInMiddle; pos += bytesInMiddle; nBytes -= bytesInMiddle; } if (nBytes > 0) { // The request has a tail - that is, it doesn't end on a // block boundary. mPGPdiskReq.write.readTail = FALSE; mPGPdiskReq.write.wroteTail = FALSE; mPGPdiskReq.write.bufTail = buf; mPGPdiskReq.write.posTail = pos; mPGPdiskReq.write.nBytesTail = nBytes; } // Execute the request. ExecuteRequest(&doneWithRequest); // Should always have stuff to do at this point. But if we don't, then // don't hang the computer by not calling back. if (doneWithRequest) { pgpAssert(FALSE); ScheduleAsyncCallback(); } }
void PGPdisk::PGPdiskCallbackAux() { DualErr derr; PGPBoolean doneWithRequest; PGPUInt8 *buf; PGPUInt32 nBytes; PGPUInt64 pos; pgpAssert(mPGPdiskReq.isInUse); derr = mPGPdiskReq.downInfo.derr; if (derr.IsntError()) { // Before passing the request to 'ExecuteRequest' we perform some pre- // processing on the data manipulated by the piece of the request that // was just processed. switch (mPGPdiskReq.op) { case kPGDOP_Read: if (!mPGPdiskReq.read.readHeader) { // We read in the header block. Decrypt and copy just what the // caller needs into his buffer. mPGPdiskReq.read.readHeader = TRUE; buf = mPGPdiskReq.read.bufHeader; pos = mPGPdiskReq.read.posHeader; nBytes = mPGPdiskReq.read.nBytesHeader; pgpAssertAddrValid(buf, PGPUInt8); SmartCipherBlocks(pos/kDefaultBlockSize, 1, mDataBuf, mDataBuf, kCipherOp_Decrypt); pgpCopyMemory(mDataBuf + pos%kDefaultBlockSize, buf, nBytes); } else if (!mPGPdiskReq.read.readMiddle) { PGPUInt32 bytesRead; // We read in a piece of the middle. Decrypt it, copy it into // the output buffer, and if there is still more middle left // then adjust the variables. buf = mPGPdiskReq.read.bufMiddle; pos = mPGPdiskReq.read.posMiddle; nBytes = mPGPdiskReq.read.nBytesMiddle; pgpAssertAddrValid(buf, PGPUInt8); bytesRead = min(nBytes, kPGDBlocksPerOp*kDefaultBlockSize); SmartCipherBlocks(pos/kDefaultBlockSize, bytesRead/kDefaultBlockSize, mDataBuf, buf, kCipherOp_Decrypt); if (nBytes - bytesRead > 0) { mPGPdiskReq.read.bufMiddle += bytesRead; mPGPdiskReq.read.posMiddle += bytesRead; mPGPdiskReq.read.nBytesMiddle -= bytesRead; } else { mPGPdiskReq.read.readMiddle = TRUE; } } else if (!mPGPdiskReq.read.readTail) { // We read in the tail block. Decrypt and copy just what the // caller needs into his buffer. mPGPdiskReq.read.readTail = TRUE; buf = mPGPdiskReq.read.bufTail; pos = mPGPdiskReq.read.posTail; nBytes = mPGPdiskReq.read.nBytesTail; pgpAssertAddrValid(buf, PGPUInt8); SmartCipherBlocks(pos/kDefaultBlockSize, 1, mDataBuf, mDataBuf, kCipherOp_Decrypt); pgpCopyMemory(mDataBuf, buf, nBytes); } break; case kPGDOP_Write: if (!mPGPdiskReq.write.readHeader) { // No pre-processing needed here. mPGPdiskReq.write.readHeader = TRUE; } else if (!mPGPdiskReq.write.wroteHeader) { // No pre-processing needed here. mPGPdiskReq.write.wroteHeader = TRUE; } else if (!mPGPdiskReq.write.wroteMiddle) { PGPUInt32 bytesWritten; // We wrote out a piece of the middle. If there is still more // middle left to write out then adjust the variables. buf = mPGPdiskReq.write.bufMiddle; pos = mPGPdiskReq.write.posMiddle; nBytes = mPGPdiskReq.write.nBytesMiddle; pgpAssertAddrValid(buf, PGPUInt8); bytesWritten = min(nBytes, kPGDBlocksPerOp*kDefaultBlockSize); if (nBytes - bytesWritten > 0) { mPGPdiskReq.write.bufMiddle += bytesWritten; mPGPdiskReq.write.posMiddle += bytesWritten; mPGPdiskReq.write.nBytesMiddle -= bytesWritten; } else { mPGPdiskReq.write.wroteMiddle = TRUE; } } else if (!mPGPdiskReq.write.readTail) { // No pre-processing needed here. mPGPdiskReq.write.readTail = TRUE; } else if (!mPGPdiskReq.write.wroteTail) { // No pre-processing needed here. mPGPdiskReq.write.wroteTail = TRUE; } break; default: pgpAssert(FALSE); break; } // Perform further processing on this request, if needed. ExecuteRequest(&doneWithRequest); } // Callback if we're done or on error. if (doneWithRequest || derr.IsError()) { ScheduleAsyncCallback(derr); } }
void CBitprintsDownloader::OnRun() { while ( m_hInternet != NULL ) { m_pSection.Lock(); if ( m_pFiles.IsEmpty() ) { m_pSection.Unlock(); break; } m_nFileIndex = m_pFiles.RemoveHead(); m_pSection.Unlock(); m_pDlg->OnNextFile( m_nFileIndex ); if ( BuildRequest() ) { m_pDlg->OnRequesting( m_nFileIndex, m_sFileName ); if ( ExecuteRequest() ) { if ( DecodeResponse() ) { m_pDlg->OnSuccess( m_nFileIndex ); } else { if ( m_hInternet == NULL ) break; m_pDlg->OnFailure( m_nFileIndex, L"Not Found" ); } } else if ( ++m_nFailures >= 3 ) { if ( m_hInternet == NULL ) break; m_pDlg->OnFailure( m_nFileIndex, L"Aborting" ); break; } else { if ( m_hInternet == NULL ) break; if ( m_hRequest ) InternetCloseHandle( m_hRequest ); m_hRequest = NULL; m_pDlg->OnFailure( m_nFileIndex, L"Failed" ); Sleep( 1000 ); } } m_pDlg->OnFinishedFile( m_nFileIndex ); if ( m_hRequest ) InternetCloseHandle( m_hRequest ); m_hRequest = NULL; m_sResponse.Empty(); if ( m_pXML ) delete m_pXML; m_pXML = NULL; Sleep( min( m_nDelay, 500ul ) ); } if ( m_hSession != NULL && ! m_bFinished ) InternetCloseHandle( m_hSession ); m_hSession = NULL; m_bFinished = TRUE; }
//--------------------------------------------------------------------------- int TTrdItf_GTJA::GetStkAccount(char *LogType, char *Logid, char *ZJ, char *SH, char *SZ)//获得主股东账号 { char szFunid[] = "410301"; if(InitRequest(szFunid, true) != 0) return -1; KCBPCLI_SetValue(FHandle, "inputtype", LogType);//登录类型 inputtype char(1) Y 见备注 KCBPCLI_SetValue(FHandle, "inputid", Logid);//登录标识 inputid char(64) Y 见备注 int nReturnCode = ExecuteRequest(szFunid); if( nReturnCode != 0 && nReturnCode != 100 ) //nReturnCode = 0 全部返回了,=100表示还有数据 { return nReturnCode; } char szTmpbuf[20]; if( KCBPCLI_SQLMoreResults(FHandle) == 0 ) { try { bool bFlag[2] = {false, false}; while( !KCBPCLI_RsFetchRow(FHandle) ) { if( KCBPCLI_RsGetColByName( FHandle, "market", szTmpbuf ) == 0) { if(lstrcmpi(szTmpbuf, "1") == 0 && !bFlag[0]) { bFlag[0] = true; if( KCBPCLI_RsGetColByName( FHandle, "secuid", SH) != 0) return -1; } else if(lstrcmpi(szTmpbuf,this->FGTJASet.bencrypt==true ? "2":"0") == 0 && !bFlag[1]) { bFlag[1] = true; if( KCBPCLI_RsGetColByName( FHandle, "secuid", SZ) != 0) return -1; } } else return -1; if(KCBPCLI_RsGetColByName( FHandle, "fundid", ZJ ) != 0) return -1; if(KCBPCLI_RsGetColByName( FHandle, "custid", FCustid ) != 0) return -1; if(KCBPCLI_RsGetColByName( FHandle, "bankcode", FBankCode ) != 0) return -1; //ODS('M',PLUGINNAME,"银行代码:%s", FBankCode); if(bFlag[0] && bFlag[1]) break; } } __finally { KCBPCLI_SQLCloseCursor(FHandle); } } else return -1;
int CHttpClient::HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, string &strResponse) { return ExecuteRequest(_T("POST"), strUrl, strPostData, strResponse); }