Skip to content
Snippets Groups Projects
server.cpp 126 KiB
Newer Older
Perttu Ahola's avatar
Perttu Ahola committed
/*
Minetest-c55
Perttu Ahola's avatar
Perttu Ahola committed
Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
Perttu Ahola's avatar
Perttu Ahola committed

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
Perttu Ahola's avatar
Perttu Ahola committed
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
Perttu Ahola's avatar
Perttu Ahola committed

You should have received a copy of the GNU Lesser General Public License along
Perttu Ahola's avatar
Perttu Ahola committed
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

Perttu Ahola's avatar
Perttu Ahola committed
#include "server.h"
#include <iostream>
Perttu Ahola's avatar
Perttu Ahola committed
#include "clientserver.h"
#include "map.h"
#include "jmutexautolock.h"
#include "main.h"
#include "constants.h"
Perttu Ahola's avatar
Perttu Ahola committed
#include "config.h"
Perttu Ahola's avatar
Perttu Ahola committed
#include "mapblock.h"
#include "settings.h"
#include "profiler.h"
Perttu Ahola's avatar
Perttu Ahola committed
#include "script.h"
#include "scriptapi.h"
Perttu Ahola's avatar
Perttu Ahola committed
#include "nodedef.h"
#include "craftdef.h"
#include "content_mapnode.h"
#include "content_nodemeta.h"
#include "content_sao.h"
#include "mods.h"
#include "tool.h"
#include "sound.h" // dummySoundManager
#include "event_manager.h"
#include "hex.h"
#include "util/string.h"
#include "util/pointedthing.h"
Perttu Ahola's avatar
Perttu Ahola committed
#include "util/mathconstants.h"
#include "rollback.h"

#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
Perttu Ahola's avatar
Perttu Ahola committed

#define BLOCK_EMERGE_FLAG_FROMDISK (1<<0)

class MapEditEventIgnorer
{
public:
	MapEditEventIgnorer(bool *flag):
		m_flag(flag)
	{
		if(*m_flag == false)
			*m_flag = true;
		else
			m_flag = NULL;
	}

	~MapEditEventIgnorer()
	{
		if(m_flag)
		{
			assert(*m_flag);
			*m_flag = false;
		}
	}
	
private:
	bool *m_flag;
};

class MapEditEventAreaIgnorer
{
public:
	MapEditEventAreaIgnorer(VoxelArea *ignorevariable, const VoxelArea &a):
		m_ignorevariable(ignorevariable)
	{
		if(m_ignorevariable->getVolume() == 0)
			*m_ignorevariable = a;
		else
			m_ignorevariable = NULL;
	}

	~MapEditEventAreaIgnorer()
	{
		if(m_ignorevariable)
		{
			assert(m_ignorevariable->getVolume() != 0);
			*m_ignorevariable = VoxelArea();
		}
	}
	
private:
	VoxelArea *m_ignorevariable;
};

Perttu Ahola's avatar
Perttu Ahola committed
void * ServerThread::Thread()
{
	ThreadStarted();

Perttu Ahola's avatar
Perttu Ahola committed
	DSTACK(__FUNCTION_NAME);

Perttu Ahola's avatar
Perttu Ahola committed
	while(getRun())
	{
		try{
			//TimeTaker timer("AsyncRunStep() + Receive()");

			{
				//TimeTaker timer("AsyncRunStep()");
				m_server->AsyncRunStep();
			}
Perttu Ahola's avatar
Perttu Ahola committed
		
			//infostream<<"Running m_server->Receive()"<<std::endl;
Perttu Ahola's avatar
Perttu Ahola committed
			m_server->Receive();
		}
		catch(con::NoIncomingDataException &e)
		{
		}
Perttu Ahola's avatar
Perttu Ahola committed
		catch(con::PeerNotFoundException &e)
		{
			infostream<<"Server: PeerNotFoundException"<<std::endl;
Perttu Ahola's avatar
Perttu Ahola committed
		}
		catch(con::ConnectionBindFailed &e)
		{
			m_server->setAsyncFatalError(e.what());
		}
		catch(LuaError &e)
		{
			m_server->setAsyncFatalError(e.what());
		}
Perttu Ahola's avatar
Perttu Ahola committed
	}
	
	END_DEBUG_EXCEPTION_HANDLER(errorstream)
Perttu Ahola's avatar
Perttu Ahola committed

	return NULL;
}

void * EmergeThread::Thread()
{
	ThreadStarted();

Perttu Ahola's avatar
Perttu Ahola committed
	DSTACK(__FUNCTION_NAME);

	bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");

	v3s16 last_tried_pos(-32768,-32768,-32768); // For error output
Perttu Ahola's avatar
Perttu Ahola committed
	/*
		Get block info from queue, emerge them and send them
		to clients.

		After queue is empty, exit.
	*/
	while(getRun())
Perttu Ahola's avatar
Perttu Ahola committed
		QueuedBlockEmerge *qptr = m_server->m_emerge_queue.pop();
		if(qptr == NULL)
			break;
		
		SharedPtr<QueuedBlockEmerge> q(qptr);

		v3s16 &p = q->pos;
		//infostream<<"EmergeThread::Thread(): running"<<std::endl;
Perttu Ahola's avatar
Perttu Ahola committed

		//TimeTaker timer("block emerge");
Perttu Ahola's avatar
Perttu Ahola committed
		
Loading
Loading full blame...