Ejemplo n.º 1
0
void Interface::CommandThreadSend()
{
	try
	{
		while (!wakeup && !endThread)
			this_thread::sleep_for(chrono::milliseconds(10));

		while (!endThread)
		{
			static_cs.StartSession();

			for (auto it = static_cmdlist.begin(); (it != static_cmdlist.end() || !dynamic_cmdlist.empty()) && !endThread;)
			{
				if (it != static_cmdlist.end())
				{
					const auto& next_list = *it;

					for (auto it = next_list.begin(); it != next_list.end() && !endThread; ++it)
					{
						auto cmd = Interface::Evaluate(*it);

						if (!cmd.second.empty())
						{
							CommandParsed stream = API::Translate(cmd);

							for (auto it = stream.begin(); it != stream.end() && !endThread; ++it)
								while (!endThread && !pipeServer->Send(&(*it)[0]))
								{
									DWORD error = GetLastError();

									if (error == ERROR_BROKEN_PIPE || error == ERROR_NO_DATA)
										endThread = true;
								}
						}
					}

					++it;
				}

				dynamic_cs.StartSession();

				for (; !dynamic_cmdlist.empty() && !endThread; natives.erase(dynamic_cmdlist.front().first), dynamic_cmdlist.pop_front())
				{
					dynamic_cs.EndSession();

					const auto& dynamic = dynamic_cmdlist.front();

					auto cmd = Interface::Evaluate(dynamic.first);

					if (!cmd.second.empty())
					{
						CommandParsed stream = API::Translate(cmd, dynamic.second);

						for (auto it = stream.begin(); it != stream.end() && !endThread; ++it)
							while (!endThread && !pipeServer->Send(&(*it)[0]))
							{
								DWORD error = GetLastError();

								if (error == ERROR_BROKEN_PIPE || error == ERROR_NO_DATA)
									endThread = true;
							}
					}

					dynamic_cs.StartSession();
				}

				dynamic_cs.EndSession();

				this_thread::sleep_for(chrono::milliseconds(1));
			}

			static_cs.EndSession();
		}
	}
	catch (exception& e)
	{
		try
		{
			VaultException& vaulterror = dynamic_cast<VaultException&>(e);
			vaulterror.Message();
		}
		catch (bad_cast&)
		{
			VaultException vaulterror(e.what());
			vaulterror.Message();
		}

#ifdef VAULTMP_DEBUG
		debug.print("Send thread is going to terminate (ERROR)");
#endif
	}

	if (wakeup)
	{
		unsigned char buffer[PIPE_LENGTH];
		buffer[0] = PIPE_ERROR_CLOSE;
		pipeServer->Send(buffer);
	}

	endThread = true;
}
DWORD WINAPI Interface::CommandThreadSend( LPVOID data )
{
    try
    {
        while (!wakeup && !endThread)
            Sleep(10);

		while ( !endThread )
		{
			CommandList::iterator it;
			CommandList::iterator insertAt = cmdlist.end();

			if ( !tmplist.empty() )
			{
				cs.StartSession();
				cmdlist.splice( cmdlist.begin(), tmplist );
				cs.EndSession();
			}

			for ( it = cmdlist.begin(); it != cmdlist.end() && !endThread; )
			{
				if ( !tmplist.empty() )
				{
					cs.StartSession();

                    unsigned int count = tmplist.size();

					if ( insertAt != cmdlist.end() )
					{
						CommandList::iterator insertAt_tmp = insertAt;
						++insertAt_tmp;
						cmdlist.splice( insertAt_tmp, tmplist );
					}

					else
					{
						CommandList::iterator it_tmp = it;
						++it_tmp;
						cmdlist.splice( it_tmp, tmplist );
					}

					CommandList::iterator it_tmp = it;
					advance( it_tmp, count );

					insertAt = it_tmp;

					cs.EndSession();
				}

                map<string, string>::iterator al = alias.find(it->first->first);
                string name = (al != alias.end() ? al->second : it->first->first);
                string def = defs.find(name)->second;
                ParamContainer& param = it->first->second;
                vector<signed int>& data = it->second;

                if (data[2] != 0)
                {
                    data[2]--;
                    ++it;
                    continue;
                }

                multimap<string, string> cmd = Interface::Evaluate(name, def, param);

                if (cmd.size() != 0)
                {
                    signed int key = data[3];

                    CommandParsed stream = API::Translate(cmd, key);

                    if (stream.size() != 0)
                    {
                        CommandParsed::iterator it2;

                        for (it2 = stream.begin(); it2 != stream.end() && !endThread; ++it2)
                        {
                            char* content = *it2;
                            pipeServer->Send(content);
                        }

                        for (it2 = stream.begin(); it2 != stream.end(); ++it2)
                        {
                            char* content = *it2;
                            delete[] content;
                        }
                    }
                }

                if (data[0] == 0)
                {
                    FreeContainer(param);
                    natives.erase(it->first);
                    it = cmdlist.erase(it);
                }
                else
                    ++it;
            }
        }
    }
    catch (std::exception& e)
    {
        try
        {
            VaultException& vaulterror = dynamic_cast<VaultException&>(e);
            vaulterror.Message();
        }
        catch (std::bad_cast& no_vaulterror)
        {
            VaultException vaulterror(e.what());
            vaulterror.Message();
        }

#ifdef VAULTMP_DEBUG
        if (debug != NULL)
            debug->Print("Send thread is going to terminate (ERROR)", true);
#endif

        return ((DWORD) data);
    }

    return ((DWORD) data);
}
Ejemplo n.º 3
0
void Interface::CommandThreadSend()
{
	try
	{
		while (!wakeup && !endThread)
			this_thread::sleep_for(chrono::milliseconds(10));

		while (!endThread)
		{
			StaticCommandList::iterator it;

			static_cs.StartSession();

			for (it = static_cmdlist.begin(); (it != static_cmdlist.end() || !dynamic_cmdlist.empty()) && !endThread;)
			{
				if (it != static_cmdlist.end())
				{
					vector<Native::iterator>::iterator it2;
					vector<Native::iterator>& next_list = *it;

					for (it2 = next_list.begin(); it2 != next_list.end() && !endThread; ++it2)
					{
						vector<string> cmd = Interface::Evaluate(*it2);

						if (!cmd.empty())
						{
							CommandParsed stream = API::Translate(cmd);
							CommandParsed::iterator it;

							for (it = stream.begin(); it != stream.end() && !endThread; ++it)
								pipeServer->Send(it->get());
						}
					}

					++it;
				}

				dynamic_cs.StartSession();

				for (; !dynamic_cmdlist.empty() && !endThread; natives.erase(dynamic_cmdlist.front().first), dynamic_cmdlist.pop_front())
				{
					dynamic_cs.EndSession();

					auto dynamic = dynamic_cmdlist.front();

					vector<string> cmd = Interface::Evaluate(dynamic.first);

					if (!cmd.empty())
					{
						CommandParsed stream = API::Translate(cmd, dynamic.second);
						CommandParsed::iterator it;

						for (it = stream.begin(); it != stream.end() && !endThread; ++it)
							pipeServer->Send(it->get());
					}

					dynamic_cs.StartSession();
				}

				dynamic_cs.EndSession();

				this_thread::sleep_for(chrono::milliseconds(1));
			}

			static_cs.EndSession();
		}
	}
	catch (std::exception& e)
	{
		try
		{
			VaultException& vaulterror = dynamic_cast<VaultException&>(e);
			vaulterror.Message();
		}
		catch (std::bad_cast& no_vaulterror)
		{
			VaultException vaulterror(e.what());
			vaulterror.Message();
		}

#ifdef VAULTMP_DEBUG

		if (debug)
			debug->Print("Send thread is going to terminate (ERROR)", true);

#endif
	}

	if (wakeup)
	{
		unsigned char buffer[PIPE_LENGTH];
		buffer[0] = PIPE_ERROR_CLOSE;
		pipeServer->Send(buffer);
	}

	endThread = true;
}