void CMUSHclientDoc::CheckTimers () { // make sure status line is showing the right thing (after 5 seconds) if (m_pActiveCommandView || m_pActiveOutputView) { CTimeSpan ts = CTime::GetCurrentTime() - m_tStatusDisplayed; if (ts.GetTotalSeconds () > 5) ShowStatusLine (); } // end of this being the active world // flush log file to disk every 2 minutes if (m_logfile) { CTimeSpan ts = CTime::GetCurrentTime() - m_LastFlushTime; // flush every 2 minutes if (ts.GetTotalSeconds () > 120.0) { m_LastFlushTime = CTime::GetCurrentTime(); // fflush (m_logfile); fclose (m_logfile); m_logfile = fopen (m_logfile_name, "a+"); } } // end of log file open // if reconnection wanted, attempt it now ... // if they want automatic reconnection, do it if (App.m_bReconnectOnLinkFailure && !m_bDisconnectOK && m_iConnectPhase == eConnectNotConnected) { Frame.SetStatusMessage (Translate ("Reconnecting ...")); ConnectSocket(); // reconnect } if (m_bEnableTimers) { // timer has kicked in unexpectedly - ignore it if (m_CurrentPlugin) return; CheckTimerList (GetTimerMap ()); // do plugins for (PluginListIterator pit = m_PluginList.begin (); pit != m_PluginList.end (); ++pit) { m_CurrentPlugin = *pit; if (m_CurrentPlugin->m_bEnabled) CheckTimerList (GetTimerMap ()); } // end of doing each plugin m_CurrentPlugin = NULL; } } // end of CMUSHclientDoc::CheckTimers
void ChttpGet::WorkerThread() { char szCommand[1000]; char *p; int irsp = 0; ConnectSocket(); if(m_Aborting) { fclose(LOCALFILE); return; } if(m_State != HTTP_STATE_CONNECTED) { fclose(LOCALFILE); return; } sprintf(szCommand,"GET %s%s HTTP/1.1\nAccept: */*\nAccept-Encoding: deflate\nHost: %s\n\n\n",m_ProxyEnabled?"":"/",m_ProxyEnabled?m_URL:m_szDir,m_szHost); send(m_DataSock,szCommand,strlen(szCommand),0); p = GetHTTPLine(); if(strnicmp("HTTP/",p,5)==0) { char *pcode; pcode = strchr(p,' ')+1; if(!pcode) { m_State = HTTP_STATE_UNKNOWN_ERROR; fclose(LOCALFILE); return; } pcode[3] = '\0'; irsp = atoi(pcode); if(irsp == 0) { m_State = HTTP_STATE_UNKNOWN_ERROR; fclose(LOCALFILE); return; } if(irsp==200) { int idataready=0; do { p = GetHTTPLine(); if(p==NULL) { m_State = HTTP_STATE_UNKNOWN_ERROR; fclose(LOCALFILE); return; } if(*p=='\0') { idataready = 1; break; } if(strnicmp(p,"Content-Length:",strlen("Content-Length:"))==0) { char *s = strchr(p,' ')+1; p = s; if(s) { while(*s) { if(!isdigit(*s)) { *s='\0'; } s++; }; m_iBytesTotal = atoi(p); } } Sleep(1); }while(!idataready); ReadDataChannel(); return; } m_State = HTTP_STATE_FILE_NOT_FOUND; fclose(LOCALFILE); return; } else { m_State = HTTP_STATE_UNKNOWN_ERROR; fclose(LOCALFILE); return; } }
void CActiveDeterministicMasternodeManager::Init() { LOCK(cs_main); if (!fMasternodeMode) return; if (!deterministicMNManager->IsDeterministicMNsSporkActive()) return; // Check that our local network configuration is correct if (!fListen) { // listen option is probably overwritten by smth else, no good state = MASTERNODE_ERROR; strError = "Masternode must accept connections from outside. Make sure listen configuration option is not overwritten by some another parameter."; LogPrintf("CActiveDeterministicMasternodeManager::Init -- ERROR: %s\n", strError); return; } if (!GetLocalAddress(activeMasternodeInfo.service)) { state = MASTERNODE_ERROR; return; } CDeterministicMNList mnList = deterministicMNManager->GetListAtChainTip(); CDeterministicMNCPtr dmn = mnList.GetMNByOperatorKey(*activeMasternodeInfo.blsPubKeyOperator); if (!dmn) { // MN not appeared on the chain yet return; } if (!mnList.IsMNValid(dmn->proTxHash)) { if (mnList.IsMNPoSeBanned(dmn->proTxHash)) { state = MASTERNODE_POSE_BANNED; } else { state = MASTERNODE_REMOVED; } return; } mnListEntry = dmn; LogPrintf("CActiveDeterministicMasternodeManager::Init -- proTxHash=%s, proTx=%s\n", mnListEntry->proTxHash.ToString(), mnListEntry->ToString()); if (activeMasternodeInfo.service != mnListEntry->pdmnState->addr) { state = MASTERNODE_ERROR; strError = "Local address does not match the address from ProTx"; LogPrintf("CActiveDeterministicMasternodeManager::Init -- ERROR: %s", strError); return; } if (Params().NetworkIDString() != CBaseChainParams::REGTEST) { // Check socket connectivity LogPrintf("CActiveDeterministicMasternodeManager::Init -- Checking inbound connection to '%s'\n", activeMasternodeInfo.service.ToString()); SOCKET hSocket; bool fConnected = ConnectSocket(activeMasternodeInfo.service, hSocket, nConnectTimeout) && IsSelectableSocket(hSocket); CloseSocket(hSocket); if (!fConnected) { state = MASTERNODE_ERROR; strError = "Could not connect to " + activeMasternodeInfo.service.ToString(); LogPrintf("CActiveDeterministicMasternodeManager::Init -- ERROR: %s\n", strError); return; } } activeMasternodeInfo.proTxHash = mnListEntry->proTxHash; activeMasternodeInfo.outpoint = mnListEntry->collateralOutpoint; state = MASTERNODE_READY; }
XnStatus SocketInConnection::ReadThreadProcImpl() { XnStatus nRetVal = XN_STATUS_OK; XN_SOCKET_HANDLE hSocket = NULL; XnBool bCanceled = FALSE; XnUInt32 nPacketBytesRead = 0; XnUInt32 nTotalBytesRead = 0; m_nConnectionStatus = ConnectSocket(hSocket, m_strIP, m_nPort); XN_IS_STATUS_OK_LOG_ERROR("Connect socket", m_nConnectionStatus); nRetVal = xnOSSetEvent(m_hConnectEvent); XN_IS_STATUS_OK_LOG_ERROR("Set connect event", nRetVal); while (!m_bStopReadThread) { //Fill buffer with received packets nTotalBytesRead = 0; for (XnUInt32 nPacket = 0; (nPacket < BUFFER_NUM_PACKETS); nPacket++) { nPacketBytesRead = m_nMaxPacketSize; m_nConnectionStatus = ReceivePacket(hSocket, m_pBuffer + nTotalBytesRead, nPacketBytesRead, bCanceled); if (m_nConnectionStatus != XN_STATUS_OK) { m_pDataDestination->HandleDisconnection(); xnLogError(XN_MASK_LINK, "Failed to receive packet: %s", xnGetStatusString(m_nConnectionStatus)); //XN_ASSERT(FALSE); return m_nConnectionStatus; } if (bCanceled) { //Ignore packet and exit loop break; } if (nTotalBytesRead == m_nBufferSize) { xnLogError(XN_MASK_LINK, "Read thread buffer overflowed :("); XN_ASSERT(FALSE); return XN_STATUS_INTERNAL_BUFFER_TOO_SMALL; } nTotalBytesRead += nPacketBytesRead; } if (m_pDataDestination != NULL) { //Send data in buffer to its destination. //Even if at this point the read thread should be stopped, first we send all the complete packets we got. if (nTotalBytesRead > 0) { m_pDataDestination->IncomingData(m_pBuffer, nTotalBytesRead); } } } nRetVal = xnOSCloseSocket(hSocket); if (nRetVal != XN_STATUS_OK) { xnLogWarning(XN_MASK_LINK, "Failed to close input data socket :("); XN_ASSERT(FALSE); } m_nConnectionStatus = XN_STATUS_OS_NETWORK_CONNECTION_CLOSED; return XN_STATUS_OK; }
void CActiveLegacyMasternodeManager::ManageStateInitial(CConnman& connman) { if (deterministicMNManager->IsDeterministicMNsSporkActive()) return; LogPrint("masternode", "CActiveLegacyMasternodeManager::ManageStateInitial -- status = %s, type = %s, pinger enabled = %d\n", GetStatus(), GetTypeString(), fPingerEnabled); // Check that our local network configuration is correct if (!fListen) { // listen option is probably overwritten by smth else, no good nState = ACTIVE_MASTERNODE_NOT_CAPABLE; strNotCapableReason = "Masternode must accept connections from outside. Make sure listen configuration option is not overwritten by some another parameter."; LogPrintf("CActiveLegacyMasternodeManager::ManageStateInitial -- %s: %s\n", GetStateString(), strNotCapableReason); return; } // First try to find whatever local address is specified by externalip option bool fFoundLocal = GetLocal(activeMasternodeInfo.service) && CMasternode::IsValidNetAddr(activeMasternodeInfo.service); if (!fFoundLocal) { bool empty = true; // If we have some peers, let's try to find our local address from one of them connman.ForEachNodeContinueIf(CConnman::AllNodes, [&fFoundLocal, &empty](CNode* pnode) { empty = false; if (pnode->addr.IsIPv4()) fFoundLocal = GetLocal(activeMasternodeInfo.service, &pnode->addr) && CMasternode::IsValidNetAddr(activeMasternodeInfo.service); return !fFoundLocal; }); // nothing and no live connections, can't do anything for now if (empty) { nState = ACTIVE_MASTERNODE_NOT_CAPABLE; strNotCapableReason = "Can't detect valid external address. Will retry when there are some connections available."; LogPrintf("CActiveLegacyMasternodeManager::ManageStateInitial -- %s: %s\n", GetStateString(), strNotCapableReason); return; } } if (!fFoundLocal && Params().NetworkIDString() == CBaseChainParams::REGTEST) { if (Lookup("127.0.0.1", activeMasternodeInfo.service, GetListenPort(), false)) { fFoundLocal = true; } } if (!fFoundLocal) { nState = ACTIVE_MASTERNODE_NOT_CAPABLE; strNotCapableReason = "Can't detect valid external address. Please consider using the externalip configuration option if problem persists. Make sure to use IPv4 address only."; LogPrintf("CActiveLegacyMasternodeManager::ManageStateInitial -- %s: %s\n", GetStateString(), strNotCapableReason); return; } int mainnetDefaultPort = Params(CBaseChainParams::MAIN).GetDefaultPort(); if (Params().NetworkIDString() == CBaseChainParams::MAIN) { if (activeMasternodeInfo.service.GetPort() != mainnetDefaultPort) { nState = ACTIVE_MASTERNODE_NOT_CAPABLE; strNotCapableReason = strprintf("Invalid port: %u - only %d is supported on mainnet.", activeMasternodeInfo.service.GetPort(), mainnetDefaultPort); LogPrintf("CActiveLegacyMasternodeManager::ManageStateInitial -- %s: %s\n", GetStateString(), strNotCapableReason); return; } } else if (activeMasternodeInfo.service.GetPort() == mainnetDefaultPort) { nState = ACTIVE_MASTERNODE_NOT_CAPABLE; strNotCapableReason = strprintf("Invalid port: %u - %d is only supported on mainnet.", activeMasternodeInfo.service.GetPort(), mainnetDefaultPort); LogPrintf("CActiveLegacyMasternodeManager::ManageStateInitial -- %s: %s\n", GetStateString(), strNotCapableReason); return; } if (Params().NetworkIDString() != CBaseChainParams::REGTEST) { // Check socket connectivity LogPrintf("CActiveLegacyMasternodeManager::ManageStateInitial -- Checking inbound connection to '%s'\n", activeMasternodeInfo.service.ToString()); SOCKET hSocket; bool fConnected = ConnectSocket(activeMasternodeInfo.service, hSocket, nConnectTimeout) && IsSelectableSocket(hSocket); CloseSocket(hSocket); if (!fConnected) { nState = ACTIVE_MASTERNODE_NOT_CAPABLE; strNotCapableReason = "Could not connect to " + activeMasternodeInfo.service.ToString(); LogPrintf("CActiveLegacyMasternodeManager::ManageStateInitial -- %s: %s\n", GetStateString(), strNotCapableReason); return; } } // Default to REMOTE eType = MASTERNODE_REMOTE; LogPrint("masternode", "CActiveLegacyMasternodeManager::ManageStateInitial -- End status = %s, type = %s, pinger enabled = %d\n", GetStatus(), GetTypeString(), fPingerEnabled); }