void
ServerQueryThread::onDataReceived(network::TCPSocket *s, const char *data, const int len)
{
    string str;
    
    MSInfo * msi = querying_msdata[s];
    msi->touch();
    str = msi->recdata;
    str.append(data,len);
    
    if (str[0] != '\\') {
        delete msi;
        querying_msdata.erase(s);
        s->destroy();
        return; // invalid answer;
    }
    
    string lastpart;
    if (str[str.length()-1] != '\\') {
        // received incomplete
        string::size_type p = str.rfind('\\');
        msi->recdata = str.substr(p);
        str.erase(p);
    } else {
        msi->recdata = "\\";
    }
    
    StringTokenizer tknizer(str,'\\');
    
    string token = tknizer.getNextToken();
    while ( !token.empty()) {
        if ( token == "ip" ) {
            string dirip = tknizer.getNextToken();
            string port;
            if ( dirip.empty() ) { 
                msi->recdata.insert(0,"\\ip\\");
                break;
            }
            
            token = tknizer.getNextToken();
            if ( token.empty() ) {
                msi->recdata.insert(0,dirip.insert(0,"\\ip\\")+"\\");
                break;
            }

            if ( token == "port" ) {
                token = tknizer.getNextToken();
                if (token.empty()) {
                    msi->recdata.insert(0,dirip.insert(0,"\\ip\\")+"\\port\\");
                    break;
                }
                port=token;
                token = tknizer.getNextToken();
            }
            
            LOGGER.warning("Server IP received: [%s:%s]",dirip.c_str(),port.c_str());

            int iport;
            std::stringstream portstr(port);
            portstr >> iport;
            
            bool found=false;
            // check if it is already in list
            std::vector<masterserver::ServerInfo*>::iterator si;
            for ( si = serverlist->begin(); si != serverlist->end(); si++) {
                if ( ((*si)->address == dirip) && ((*si)->port == iport) ) {
                    found=true;
                    break;
                }
            }
            if (found)
                continue;
            ServerInfo * info = new ServerInfo();
            info->address = dirip;
            info->port = iport;
            info->status = ServerInfo::QUERYING;
            serverlist->push_back(info);
            not_queried.push_back(info);
            sendNextQuery(); // XXX first check it is not already in
        } else if ( token == "final") {
예제 #2
0
		void load(const std::string& file, bool utf8)
		{
			auto impl = std::make_shared<data>();

			tokenizer tknizer(file);
			while (true)
			{
				if (token::msgid != tknizer.read())
					break;
				if (token::string != tknizer.read())
					return;

				std::string msgid = std::move(tknizer.get_str());

				if (!utf8)
					msgid = nana::charset(std::move(msgid)).to_bytes(nana::unicode::utf8);

				if (token::msgstr != tknizer.read())
					return;
				if (token::string != tknizer.read())
					return;

				std::string str;

				if (utf8)
					str = nana::charset(std::move(tknizer.get_str()), nana::unicode::utf8);
				else
					str = nana::charset(std::move(tknizer.get_str()));

				std::string::size_type pos = 0;
				while (true)
				{
					pos = str.find('\\', pos);
					if (pos == str.npos)
						break;

					if (pos + 1 < str.size())
					{
						auto ch = str[pos + 1];
						switch (ch)
						{
						case '"': case '\\':
							str.erase(pos, 1);
							break;
						case 'n':
							str.erase(pos, 1);
							str[pos] = '\n';
							break;
						case 't':
							str.erase(pos, 1);
							str[pos] = '\t';
							break;
						}
						++pos;
					}
					else
						break;
				}
				impl->table[std::move(msgid)].swap(str);
			}

			//Assign all language texts to the new table.
			auto & cur_table = get_data_ptr()->table;
			auto & new_table = impl->table;
			for (auto & m : cur_table)
			{
				auto & value = new_table[m.first];
				if (value.empty())
					value = m.second;
			}

			get_data_ptr().swap(impl);
			use_eval();
		}