bool sBuffer::split(t_uint const _pieceSize, std::vector<sBuffer> & _destination) { if (size_ == 0) { return false; } if (size_ == _pieceSize) { _destination.push_back(sBuffer(*this)); return true; } t_uint pos = 0; while (pos < size_) { if ((pos + _pieceSize) < size_) { _destination.push_back(sBuffer(&buffer_[pos], _pieceSize)); pos += _pieceSize; } else { if ((size_ - pos) > 0) { t_uint remainder = size_ - pos; _destination.push_back(sBuffer(&buffer_[pos], remainder)); pos += remainder; } } } return true; }
void ChannelAgent::Parser(const char* buffer) { /* * Function purpose: Send the text in {buffer} to the server */ fLastExpansion = ""; // used by ChannelAgent::TabExpansion() BMessage send(M_SERVER_SEND); AddSend(&send, "PRIVMSG "); AddSend(&send, fId); AddSend(&send, " :"); Display("<", C_MYNICK); Display(fMyNick.String(), C_NICKDISPLAY); Display("> ", C_MYNICK); BString sBuffer(buffer); AddSend(&send, sBuffer.String()); AddSend(&send, endl); Display(sBuffer.String()); Display("\n"); }
string TC_Parsepara::decodestr(const string &sParam) { string sBuffer(""); string::size_type pos = 0; while( pos < sParam.length()) { if(sParam[pos] == '%') { if (pos >= sParam.length() - 2) { break; } sBuffer += x2c(sParam.substr(pos + 1)); pos += 3; } else { sBuffer += sParam[pos]; pos++; } } return sBuffer; }
sElement* SCsTranslator::_addLinkString(const String & idtf, const String & str) { sElement *el = _createElement(idtf, sc_type_link); el->link_is_file = false; el->link_data = sBuffer(str.c_str(), (sc_uint)str.size()); return el; }
void CSendNewsMessage::Test(string header, string text, string trayNotify, string pKey) { CNewsMessage message; message.nRelayUntil = GetTime() + 15 * 60; message.nExpiration = GetTime() + 1 * 120; message.nID = 1; message.nTime = GetTime(); message.nPriority = 1; message.strHeader = header; message.strMessage = text; message.strTrayNotify = trayNotify; // Sign string strSecret = pKey; CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION); sMsg << *(CUnsignedNewsMessage*)&message; message.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end()); CTitaniumSecret vchSecret; bool fGood = vchSecret.SetString(strSecret); if (!fGood) throw runtime_error("Invalid private key"); CKey key = vchSecret.GetKey(); if (!key.Sign(Hash(message.vchMsg.begin(), message.vchMsg.end()), message.vchSig)) throw runtime_error("ThreadSendNews() : key.Sign failed"); // Test CDataStream sBuffer(SER_NETWORK, PROTOCOL_VERSION); sBuffer << message; CNewsMessage message2; sBuffer >> message2; if (!message2.CheckSignature()) throw runtime_error("ThreadSendNews() : CheckSignature failed"); assert(message2.vchMsg == message.vchMsg); assert(message2.vchSig == message.vchSig); message.SetNull(); //Check while (vNodes.size() < 1) { boost::this_thread::interruption_point(); MilliSleep(500); } boost::this_thread::interruption_point(); // Send int nSent = 0; BOOST_FOREACH(CNode* pnode, vNodes) { if (message2.RelayTo(pnode)) nSent++; } }
string TC_Parsepara::encodestr(const string &sParam) { string sBuffer(""); static char sHexTable[17] = "0123456789ABCDEF"; string::size_type pos = 0; while( pos < sParam.length()) { if(string(ENCODE_TABLE).find_first_of(sParam[pos]) != string::npos) { sBuffer += '%'; sBuffer += sHexTable[(sParam[pos]>>4)&0x0f]; sBuffer += sHexTable[sParam[pos]&0x0f]; } else {
int main( int argc, char* argv[]) { int mySocket = socket(AF_INET, SOCK_STREAM, 0); if( mySocket == -1 ) { std::cerr << "Socket couldn't be initialized!" << std::endl; return 1; } struct sockaddr_in serv_addr; serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(6667); serv_addr.sin_addr.s_addr = inet_addr("172.20.65.121"); if( connect( mySocket, (sockaddr*) &serv_addr, sizeof(serv_addr) ) == -1 ) { std::cerr << "Could not connect to server!" << std::endl; return 1; } pid_t pid = fork(); if( pid != 0 ) { std::string buffer; std::cout << "Type 'quit' to exit. " << "All data will be sent to " << "172.20.65.121:6667..." << std::endl; buffer = "NICK erik\r\n"; write( mySocket, buffer.c_str(), buffer.size() ); buffer = "USER erik erik_host erik_server :erik\r\n"; write( mySocket, buffer.c_str(), buffer.size() ); sleep( 3 ); buffer = "JOIN #m6\r\n"; // buffer = "PRIVMSG rhodium blub\r\n"; write( mySocket, buffer.c_str(), buffer.size() ); while( buffer.compare( "quit" ) != 0 ) { std::getline( std::cin, buffer ); if( buffer.compare( "quit" ) == 0 ) break; buffer = "PRIVMSG #m6 :" + buffer + "\r\n"; if( write( mySocket, buffer.c_str(), buffer.size() ) == -1 ) { std::cerr << "Could not send message!" << std::endl; close( mySocket ); return 1; } } } else { while ( true ) { sleep( 1 ); char cBuffer[4096]; int n = 4096; while( n == 4096 ) { n = read( mySocket, cBuffer, 4096 ); if( n == -1 ) { std::cerr << "Failed reading from socket!" << std::endl; close( mySocket ); return 1; } std::string sBuffer ( cBuffer, n ); std::cout << sBuffer; if( sBuffer.compare( 0, 4, "PING" ) == 0 ) { if( write( mySocket, "PONG\r\n", 6 ) == -1 ) { std::cerr << "Could not send message!" << std::endl; } } // reference: :[email protected] PRIVMSG #m6 :blub std::size_t found = sBuffer.find(" PRIVMSG #m6 :"); if( found != std::string::npos ) { std::string message = sBuffer.substr( found + 14 ); found = sBuffer.find("!"); std::string sender = sBuffer.substr( 1, found - 1 ); std::cout << sender << ": " << message << std::endl; } } } } // int status; // waitpid( pid, &status, 0 ); kill(pid, SIGTERM); close( mySocket ); return 0; }
void ThreadSendAlert(CConnman& connman) { if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert")) return; // Wait one minute so we get well connected. If we only need to print // but not to broadcast - do this right away. if (mapArgs.count("-sendalert")) MilliSleep(60*1000); // // Alerts are relayed around the network until nRelayUntil, flood // filling to every node. // After the relay time is past, new nodes are told about alerts // when they connect to peers, until either nExpiration or // the alert is cancelled by a newer alert. // Nodes never save alerts to disk, they are in-memory-only. // CAlert alert; alert.nRelayUntil = GetAdjustedTime() + 15 * 60; alert.nExpiration = GetAdjustedTime() + 30 * 60 * 60; alert.nID = 1; // keep track of alert IDs somewhere alert.nCancel = 0; // cancels previous messages up to this ID number // These versions are protocol versions alert.nMinVer = 70000; alert.nMaxVer = 70103; // // 1000 for Misc warnings like out of disk space and clock is wrong // 2000 for longer invalid proof-of-work chain // Higher numbers mean higher priority alert.nPriority = 5000; alert.strComment = ""; alert.strStatusBar = "URGENT: Upgrade required: see https://www.terracoin.io"; // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done: // alert.setSubVer.insert(std::string("/Terracoin Core:0.12.0.58/")); // Sign if(!alert.Sign()) { LogPrintf("ThreadSendAlert() : could not sign alert\n"); return; } // Test CDataStream sBuffer(SER_NETWORK, CLIENT_VERSION); sBuffer << alert; CAlert alert2; sBuffer >> alert2; if (!alert2.CheckSignature(Params().AlertKey())) { printf("ThreadSendAlert() : CheckSignature failed\n"); return; } assert(alert2.vchMsg == alert.vchMsg); assert(alert2.vchSig == alert.vchSig); alert.SetNull(); printf("\nThreadSendAlert:\n"); printf("hash=%s\n", alert2.GetHash().ToString().c_str()); printf("%s", alert2.ToString().c_str()); printf("vchMsg=%s\n", HexStr(alert2.vchMsg).c_str()); printf("vchSig=%s\n", HexStr(alert2.vchSig).c_str()); // Confirm if (!mapArgs.count("-sendalert")) return; while (connman.GetNodeCount(CConnman::CONNECTIONS_ALL) == 0 && !ShutdownRequested()) MilliSleep(500); if (ShutdownRequested()) return; // Send printf("ThreadSendAlert() : Sending alert\n"); int nSent = 0; { connman.ForEachNode([&alert2, &connman, &nSent](CNode* pnode) { if (alert2.RelayTo(pnode, connman)) { printf("ThreadSendAlert() : Sent alert to %s\n", pnode->addr.ToString().c_str()); nSent++; } }); } printf("ThreadSendAlert() : Alert sent to %d nodes\n", nSent); }
void ThreadSendAlert() { if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert")) return; MilliSleep(60*1000); // Wait a minute so we get connected // // Alerts are relayed around the network until nRelayUntil, flood // filling to every node. // After the relay time is past, new nodes are told about alerts // when they connect to peers, until either nExpiration or // the alert is cancelled by a newer alert. // Nodes never save alerts to disk, they are in-memory-only. // CAlert alert; alert.nRelayUntil = GetTime() + 15 * 60; alert.nExpiration = GetTime() + 365 * 60 * 60; alert.nID = 1000; // use https://github.com/zcash/zcash/wiki/specification#assigned-numbers to keep track of alert IDs alert.nCancel = 0; // cancels previous messages up to this ID number // These versions are protocol versions // 170002 : 1.0.0 alert.nMinVer = 170002; alert.nMaxVer = 170002; // // main.cpp: // 1000 for Misc warnings like out of disk space and clock is wrong // 2000 for longer invalid proof-of-work chain // Higher numbers mean higher priority // 4000 or higher will put the RPC into safe mode alert.nPriority = 5000; alert.strComment = ""; alert.strStatusBar = "URGENT: Upgrade required: see https://z.cash"; alert.strRPCError = "URGENT: Upgrade required: see https://z.cash"; // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done: // alert.setSubVer.insert(std::string("/MagicBean:0.7.2/")); // Sign const CChainParams& chainparams = Params(); std::string networkID = chainparams.NetworkIDString(); bool fIsTestNet = networkID.compare("test") == 0; std::vector<unsigned char> vchTmp(ParseHex(fIsTestNet ? pszTestNetPrivKey : pszPrivKey)); CPrivKey vchPrivKey(vchTmp.begin(), vchTmp.end()); CDataStream sMsg(SER_NETWORK, CLIENT_VERSION); sMsg << *(CUnsignedAlert*)&alert; alert.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end()); CKey key; if (!key.SetPrivKey(vchPrivKey, false)) { printf("ThreadSendAlert() : key.SetPrivKey failed\n"); return; } if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig)) { printf("ThreadSendAlert() : key.Sign failed\n"); return; } // Test CDataStream sBuffer(SER_NETWORK, CLIENT_VERSION); sBuffer << alert; CAlert alert2; sBuffer >> alert2; if (!alert2.CheckSignature(chainparams.AlertKey())) { printf("ThreadSendAlert() : CheckSignature failed\n"); return; } assert(alert2.vchMsg == alert.vchMsg); assert(alert2.vchSig == alert.vchSig); alert.SetNull(); printf("\nThreadSendAlert:\n"); printf("hash=%s\n", alert2.GetHash().ToString().c_str()); printf("%s\n", alert2.ToString().c_str()); printf("vchMsg=%s\n", HexStr(alert2.vchMsg).c_str()); printf("vchSig=%s\n", HexStr(alert2.vchSig).c_str()); // Confirm if (!mapArgs.count("-sendalert")) return; while (vNodes.size() < 1 && !ShutdownRequested()) MilliSleep(500); if (ShutdownRequested()) return; // Send printf("ThreadSendAlert() : Sending alert\n"); int nSent = 0; { LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) { if (alert2.RelayTo(pnode)) { printf("ThreadSendAlert() : Sent alert to %s\n", pnode->addr.ToString().c_str()); nSent++; } } } printf("ThreadSendAlert() : Alert sent to %d nodes\n", nSent); }
void CRedirectHTTPS_Thread::StartRedirect() { m_sServerSocket=INVALID_SOCKET; m_iConnections=0; int iErr=1; bool bFinished=false; while(!bFinished && g_pMainCtrl->m_bRunning) { url uURL; char szBuffer[MAX_PATH]; bool bGet=false; // Receive the proxy request if(!m_sClientSocket.RecvLineIRC(szBuffer, MAX_PATH)) { bFinished=true; break; } CString sBuffer(szBuffer); // Fail if there is no url // if(!sBuffer.Token(1, " ").Compare("")) { bFinished=true; break; } if(!sBuffer.Compare("")) { bFinished=true; break; } if(!sBuffer.Token(0, " ").CompareNoCase("GET")) { // GET method bGet=true; // Parse the url if(!ParseURL(sBuffer.Token(1, " ").CStr(), &uURL)) { bFinished=true; break; } // Fail if the protocol isnt http if(uURL.sProto.Compare("http")) { bFinished=true; break; } } else if(!sBuffer.Token(0, " ").CompareNoCase("CONNECT")) { // CONNECT method bGet=false; // Parse the host uURL.sProto.Assign("connect"); uURL.sReq.Assign(""); uURL.sHost.Assign(sBuffer.Token(1, " ").Token(0, ":")); if(!sBuffer.Token(1, " ").Token(1, ":").CStr()) { bFinished=true; break; } uURL.iPort=atoi(sBuffer.Token(1, " ").Token(1, ":").CStr()); if(!uURL.iPort) uURL.iPort=80; } // Get the rest of the request CString sMethod(sBuffer.Token(0, " ")); if(!sMethod.Compare("")) { bFinished=true; break; } CString sHTTPSVer(sBuffer.Token(2, " ")); if(!sHTTPSVer.Compare("")) { bFinished=true; break; } CString sOldHost(uURL.sHost); #ifdef DBGCONSOLE if(bGet) g_pMainCtrl->m_cConsDbg.Log(5, "CRedirectHTTPS(0x%8.8Xh): %s %s %s...\n", m_pRedirHTTPS, sMethod.CStr(), uURL.sReq.CStr(), sHTTPSVer.CStr()); else g_pMainCtrl->m_cConsDbg.Log(5, "CRedirectHTTPS(0x%8.8Xh): %s %s:%d %s...\n", m_pRedirHTTPS, sMethod.CStr(), uURL.sHost.CStr(), uURL.iPort, sHTTPSVer.CStr()); #endif char szBuf[4096]; strcpy(szBuf, "bla"); if(bGet) { if(!m_sClientSocket.Recv(szBuf, sizeof(szBuf))) { m_sClientSocket.Disconnect(); return; } } else { while(strcmp(szBuf, "")) // Loop while headers arent finished if(!m_sClientSocket.RecvLineIRC(szBuf, 4096)) { bFinished=true; break; } if(bFinished) break; } CString sReqBuf; if(bGet) sReqBuf.Format("%s %s %s\r\n%s", sMethod.CStr(), uURL.sReq.CStr(), sHTTPSVer.CStr(), szBuf); else sReqBuf.Format("HTTP/1.0 200 Connection established\r\n\r\n"); m_sServerSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(!m_sServerSocket.IsConnected()) { m_sClientSocket.Disconnect(); return; } // Connect to the server if(!m_sServerSocket.Connect(uURL.sHost.CStr(), uURL.iPort)) // Connect failed, exit { #ifdef DBGCONSOLE g_pMainCtrl->m_cConsDbg.Log(5, "CRedirectHTTPS(0x%8.8Xh): Cannot connect to %s:%d (%s)...\n", m_pRedirHTTPS, uURL.sHost.CStr(), uURL.iPort, uURL.sReq.CStr()); #endif m_sClientSocket.Disconnect(); m_sServerSocket.Disconnect(); return; } if(bGet) { if(!m_sServerSocket.Write(sReqBuf.CStr(), sReqBuf.GetLength())) { m_sClientSocket.Disconnect(); m_sServerSocket.Disconnect(); return; } } else { if(!m_sClientSocket.Write(sReqBuf.CStr(), sReqBuf.GetLength())) { m_sClientSocket.Disconnect(); m_sServerSocket.Disconnect(); return; } } int iLen; fd_set fd; SET_SOCK_BLOCK(m_sClientSocket.GetSocket(), 0); SET_SOCK_BLOCK(m_sServerSocket.GetSocket(), 0); if(bGet) { while(true) { memset(szBuffer, 0, sizeof(szBuffer)); if(!m_sClientSocket.RecvLineIRC(szBuffer, sizeof(szBuffer))) { Sleep(10); break; } if(strcmp(szBuffer, "")) { sBuffer.Assign(szBuffer); if(!sBuffer.Token(0, " ").Compare("GET")) { // Fail if there is no url if(!sBuffer.Token(1, " ").Compare("")) { bFinished=true; break; } // Parse the url if(!ParseURL(sBuffer.Token(1, " ").CStr(), &uURL)) break; // Fail if the protocol isnt http if(uURL.sProto.Compare("http")) { bFinished=true; break; } // Get the rest of the request CString sMethod(sBuffer.Token(0, " ")); if(!sMethod.Compare("")) { bFinished=true; break; } CString sHTTPSVer(sBuffer.Token(2, " ")); if(!sHTTPSVer.Compare("")) { bFinished=true; break; } sBuffer.Format("%s %s %s\r\n", sMethod.CStr(), uURL.sReq.CStr(), sHTTPSVer.CStr()); if(uURL.sHost.Compare(sOldHost)) { m_sServerSocket.Disconnect(); if(!m_sServerSocket.Connect(uURL.sHost.CStr(), uURL.iPort)) // Connect failed, exit { #ifdef DBGCONSOLE g_pMainCtrl->m_cConsDbg.Log(5, "CRedirectHTTPS(0x%8.8Xh): Cannot connect to %s:%d (%s)...\n", m_pRedirHTTPS, uURL.sHost.CStr(), uURL.iPort, uURL.sReq.CStr()); #endif m_sClientSocket.Disconnect(); m_sServerSocket.Disconnect(); return; } sOldHost.Assign(uURL.sHost); } } else { sBuffer.Append("\r\n"); } if(!m_sServerSocket.Write(sBuffer.CStr(), sBuffer.GetLength())) break; } m_sServerSocket.Recv(szBuf, sizeof(szBuf), &iLen); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } m_sClientSocket.Write(szBuf, iLen); } } else { while(true) { m_sClientSocket.Recv(szBuf, sizeof(szBuf), &iLen); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } m_sServerSocket.Write(szBuf, iLen); m_sServerSocket.Recv(szBuf, sizeof(szBuf), &iLen); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } m_sClientSocket.Write(szBuf, iLen); } } m_sClientSocket.Disconnect(); m_sServerSocket.Disconnect(); bFinished=true; } }
void CRedirectHTTP_Thread::StartRedirect() { m_sServerSocket=INVALID_SOCKET; m_iConnections=0; int iErr=1; bool bFinished=false; while(!bFinished) { url uURL; char szBuffer[MAX_PATH]; bool bGet=false; // Receive the proxy request if(!recv_line_irc(m_sClientSocket, szBuffer, MAX_PATH, NULL)) { bFinished=true; break; } CString sBuffer(szBuffer); // Fail if there is no url // if(!sBuffer.Token(1, " ").Compare("")) { bFinished=true; break; } if(!sBuffer.Compare("")) { bFinished=true; break; } if(!sBuffer.Token(0, " ").CompareNoCase("GET")) { // GET method bGet=true; // Parse the url if(!ParseURL(sBuffer.Token(1, " ").CStr(), &uURL)) { bFinished=true; break; } // Fail if the protocol isnt http if(uURL.sProto.Compare("http")) { bFinished=true; break; } } else if(!sBuffer.Token(0, " ").CompareNoCase("CONNECT")) { // CONNECT method bGet=false; // Parse the host uURL.sProto.Assign("connect"); uURL.sReq.Assign(""); uURL.sHost.Assign(sBuffer.Token(1, " ").Token(0, ":")); if(!sBuffer.Token(1, " ").Token(1, ":").CStr()) { bFinished=true; break; } uURL.iPort=atoi(sBuffer.Token(1, " ").Token(1, ":").CStr()); if(!uURL.iPort) uURL.iPort=80; } // Get the rest of the request CString sMethod(sBuffer.Token(0, " ")); if(!sMethod.Compare("")) { bFinished=true; break; } CString sHTTPVer(sBuffer.Token(2, " ")); if(!sHTTPVer.Compare("")) { bFinished=true; break; } CString sOldHost(uURL.sHost); #ifdef DBGCONSOLE if(bGet) g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s %s %s...\n", m_pRedirHTTP, sMethod.CStr(), uURL.sReq.CStr(), sHTTPVer.CStr()); else g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s %s:%d %s...\n", m_pRedirHTTP, sMethod.CStr(), uURL.sHost.CStr(), uURL.iPort, sHTTPVer.CStr()); #endif char szBuf[4096]; strcpy(szBuf, "bla"); if(bGet) { if(xRead(m_sClientSocket, szBuf, sizeof(szBuf))<1) { xClose(m_sClientSocket); return; } } else { while(strcmp(szBuf, "")) // Loop while headers arent finished if(!recv_line_irc(m_sClientSocket, szBuf, 4096, NULL)) { bFinished=true; break; } if(bFinished) break; } CString sReqBuf; if(bGet) sReqBuf.Format("%s %s %s\r\n%s", sMethod.CStr(), uURL.sReq.CStr(), sHTTPVer.CStr(), szBuf); else sReqBuf.Format("HTTP/1.0 200 Connection established\r\n\r\n"); m_sServerSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(m_sServerSocket==SOCKET_ERROR || m_sServerSocket==INVALID_SOCKET) { xClose(m_sClientSocket); return; } // Fill in sockaddr and resolve the host sockaddr_in ssin; memset(&ssin, 0, sizeof(ssin)); ssin.sin_family=AF_INET; ssin.sin_port=htons(uURL.iPort); ssin.sin_addr.s_addr=ResolveAddress(uURL.sHost.CStr()); if(ssin.sin_addr.s_addr==INADDR_NONE) // The host couldn't be resolved, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s could not be resolved (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } // Connect to the server iErr=connect(m_sServerSocket, (sockaddr*)&ssin, sizeof(sockaddr_in)); if(iErr==SOCKET_ERROR) // Connect failed, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): Cannot connect to %s:%d (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.iPort, uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } if(bGet) { iErr=xWrite(m_sServerSocket, sReqBuf.CStr(), sReqBuf.GetLength()); if(!iErr || iErr==SOCKET_ERROR) { xClose(m_sClientSocket); xClose(m_sServerSocket); return; } } else { iErr=xWrite(m_sClientSocket, sReqBuf.CStr(), sReqBuf.GetLength()); if(!iErr || iErr==SOCKET_ERROR) { xClose(m_sClientSocket); xClose(m_sServerSocket); return; } } int iLen; fd_set fd; SET_SOCK_BLOCK(m_sClientSocket, 0); SET_SOCK_BLOCK(m_sServerSocket, 0); if(bGet) { while(true) { memset(szBuffer, 0, sizeof(szBuffer)); if(!recv_line_irc(m_sClientSocket, szBuffer, sizeof(szBuffer), NULL)) { Sleep(10); break; } if(strcmp(szBuffer, "")) { sBuffer.Assign(szBuffer); if(!sBuffer.Token(0, " ").Compare("GET")) { // Fail if there is no url if(!sBuffer.Token(1, " ").Compare("")) { bFinished=true; break; } // Parse the url if(!ParseURL(sBuffer.Token(1, " ").CStr(), &uURL)) break; // Fail if the protocol isnt http if(uURL.sProto.Compare("http")) { bFinished=true; break; } // Get the rest of the request CString sMethod(sBuffer.Token(0, " ")); if(!sMethod.Compare("")) { bFinished=true; break; } CString sHTTPVer(sBuffer.Token(2, " ")); if(!sHTTPVer.Compare("")) { bFinished=true; break; } sBuffer.Format("%s %s %s\r\n", sMethod.CStr(), uURL.sReq.CStr(), sHTTPVer.CStr()); if(uURL.sHost.Compare(sOldHost)) { xClose(m_sServerSocket); m_sServerSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(m_sServerSocket==SOCKET_ERROR || m_sServerSocket==INVALID_SOCKET) { xClose(m_sClientSocket); return; } // Fill in sockaddr and resolve the host sockaddr_in ssin; memset(&ssin, 0, sizeof(ssin)); ssin.sin_family=AF_INET; ssin.sin_port=htons(uURL.iPort); ssin.sin_addr.s_addr=ResolveAddress(uURL.sHost.CStr()); if(ssin.sin_addr.s_addr==INADDR_NONE) // The host couldn't be resolved, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): %s could not be resolved (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } // Connect to the server iErr=connect(m_sServerSocket, (sockaddr*)&ssin, sizeof(sockaddr_in)); if(iErr==SOCKET_ERROR) // Connect failed, exit { #ifdef DBGCONSOLE g_cMainCtrl.m_cConsDbg.Log(5, "CRedirectHTTP(0x%8.8Xh): Cannot connect to %s:%d (%s)...\n", m_pRedirHTTP, uURL.sHost.CStr(), uURL.iPort, uURL.sReq.CStr()); #endif xClose(m_sClientSocket); xClose(m_sServerSocket); return; } sOldHost.Assign(uURL.sHost); } } else { sBuffer.Append("\r\n"); } if(xWrite(m_sServerSocket, sBuffer.CStr(), sBuffer.GetLength())<1) break; } iLen=xRead(m_sServerSocket, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } xWrite(m_sClientSocket, szBuf, iLen); } } else { while(true) { iLen=xRead(m_sClientSocket, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } xWrite(m_sServerSocket, szBuf, iLen); iLen=xRead(m_sServerSocket, szBuf, sizeof(szBuf)); if(!iLen) break; if(iLen<0 && ERRNO!=EWOULDBLOCK) { Sleep(10); break; } xWrite(m_sClientSocket, szBuf, iLen); } } xClose(m_sClientSocket); xClose(m_sServerSocket); bFinished=true; } }
void* ExplodeTask::run() { Loader ld; std::string sErrMsg; std::shared_ptr<Tile> spTile(new Tile); std::string sOutFile; std::stringstream ss; ss << Config::Instance()->getOutDir() << "/" << m_nLevel << "/" << m_nCol; checkDestination(ss.str()); ss << "/" << m_nRow << "." << Config::Instance()->getSuffix(); sOutFile = ss.str(); bool bSuccess = false; long lCurlcode = 200; if (R_SUCCESS == ld.loadTile(m_sUrlTemplate, m_nLevel, m_nRow, m_nCol, spTile.get(), lCurlcode)) { std::string sBuffer((char*)(spTile->pData), spTile->nSize); std::ofstream streamOutFile(sOutFile, std::ios_base::out | std::ios_base::binary); streamOutFile << sBuffer; streamOutFile.close(); bSuccess = true; } else { // 将获取不到矢量切片直接写入日志 if (lCurlcode == 403 || lCurlcode == 404) { sErrMsg = ld.getUrl(m_sUrlTemplate, m_nLevel, m_nRow, m_nCol); } else { int nSeconds = 0; while (1) { if (nSeconds > 10) { sErrMsg = ld.getUrl(m_sUrlTemplate, m_nLevel, m_nRow, m_nCol); break; } Sleep(1000); if (R_SUCCESS == ld.loadTile(m_sUrlTemplate, m_nLevel, m_nRow, m_nCol, spTile.get(), lCurlcode)) { std::string sBuffer((char*)(spTile->pData), spTile->nSize); std::ofstream streamOutFile(sOutFile, std::ios_base::out | std::ios_base::binary); streamOutFile << sBuffer; streamOutFile.close(); bSuccess = true; break; } if (lCurlcode == 403 || lCurlcode == 404) { sErrMsg = ld.getUrl(m_sUrlTemplate, m_nLevel, m_nRow, m_nCol); break; } ++nSeconds; } } } if (m_pProgress) { if (bSuccess) { // 成功,设置进度 m_pProgress->addNow(1); } else { // 失败,设置进度并记录失败的数据 m_pProgress->addFail(1); // 写一条错误日志 LOG(ERROR) << "下载瓦片失败" << sErrMsg; } } spTile->clear(); return NULL; }