summaryrefslogtreecommitdiffstats
path: root/source/cServer.cpp
blob: 326bbf9ce5f45f660b8b616357a3a19ae5fc92ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// ReDucTor is an awesome guy who helped me a lot

#include "cServer.h"
#include "cClientHandle.h"
#include "cMCLogger.h"
#include "cThread.h"
#include "cEvent.h"
#include "cSleep.h"
#include "cTimer.h"
#include "cMonster.h"
#include "cSocket.h"
#include "cRoot.h"
#include "cWorld.h"
#include "cChunk.h"
#include "cPluginManager.h"
#include "cGroupManager.h"
#include "cChatColor.h"
#include "cPlayer.h"
#include "cInventory.h"
#include "cItem.h"
#include "cRecipeChecker.h"
#include "cFurnaceRecipe.h"
#include "cTracer.h"
#include "cWebAdmin.h"

#include "MersenneTwister.h"

#include "../iniFile/iniFile.h"
#include "Vector3f.h"

#include "packets/cPacket_Chat.h"

#ifndef _WIN32
#define sprintf_s(dst, size, format, ...) sprintf(dst, format, __VA_ARGS__ )
#endif

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

extern "C" {
	#include "zlib.h"
}

#ifdef SendMessage
#undef SendMessage
#endif


bool g_bWaterPhysics = false;

typedef std::list< cClientHandle* > ClientList;

struct cServer::sServerState
{
	sServerState()
		: pListenThread( 0 )
		, pTickThread( 0 )
		, bStopListenThread( false )
		, bStopTickThread( false )
	{}
	cSocket SListenClient; // socket listening for client calls

	cThread* pListenThread;	bool bStopListenThread;
	cThread* pTickThread;	bool bStopTickThread;

	ClientList Clients;

	cEvent RestartEvent;
	std::string ServerID;
};

cServer*cServer::GetServer()
{
	LOGWARN("WARNING: Using deprecated function cServer::GetServer() use cRoot::Get()->GetServer() instead!");
	return cRoot::Get()->GetServer();
}

void cServer::ServerListenThread( void *a_Args )
{
	LOG("ServerListenThread");
	cServer* self = (cServer*)a_Args;
	sServerState* m_pState = self->m_pState;
	while( !m_pState->bStopListenThread )
	{
		self->StartListenClient();
	}
}

std::string GetWSAError()
{
#ifdef _WIN32
	switch( WSAGetLastError() )
	{
	case WSANOTINITIALISED:
		return "WSANOTINITIALISED";
	case WSAENETDOWN:
		return "WSAENETDOWN";
	case WSAEFAULT:
		return "WSAEFAULT";
	case WSAENOTCONN:
		return "WSAENOTCONN";
	case WSAEINTR:
		return "WSAEINTR";
	case WSAEINPROGRESS:
		return "WSAEINPROGRESS";
	case WSAENETRESET:
		return "WSAENETRESET";
	case WSAENOTSOCK:
		return "WSAENOTSOCK";
	case WSAEOPNOTSUPP:
		return "WSAEOPNOTSUPP";
	case WSAESHUTDOWN:
		return "WSAESHUTDOWN";
	case WSAEWOULDBLOCK:
		return "WSAEWOULDBLOCK";
	case WSAEMSGSIZE:
		return "WSAEMSGSIZE";
	case WSAEINVAL:
		return "WSAEINVAL";
	case WSAECONNABORTED:
		return "WSAECONNABORTED";
	case WSAETIMEDOUT:
		return "WSAETIMEDOUT";
	case WSAECONNRESET:
		return "WSAECONNRESET";
	}
#endif
	return "No Error";
}

bool cServer::InitServer( int a_Port )
{
	if( m_bIsConnected )
	{
		LOGERROR("ERROR: Trying to initialize server while server is already running!");
		return false;
	}

	printf("/============================\\\n");
	printf("|   Minecraft Alpha Server   |\n");
	printf("|  Created by Kevin Bansberg |\n");
	printf("|           A.K.A.           |\n");
	printf("|         FakeTruth          |\n");
	printf("| Monsters by Alex Sonek     |\n");
	printf("|           A.K.A. Duralex   |\n");
	printf("\\============================/\n");
	printf("More info: WWW.MC-SERVER.ORG\n");
	printf("           WWW.AE-C.NET\n");
	printf("           WWW.RBTHINKTANK.COM\n");
	printf("email: faketruth@gmail.com\n\n");

	LOG("Starting up server.");

	if( cSocket::WSAStartup() != 0 ) // Only does anything on Windows, but whatever
	{
		LOGERROR("WSAStartup() != 0");
		return false;
	}

	m_pState->SListenClient = cSocket::CreateSocket();

	if( !m_pState->SListenClient.IsValid() )
	{
		LOGERROR("m_SListenClient==INVALID_SOCKET (%s)", cSocket::GetLastErrorString() );
		return false;
	}

    if( m_pState->SListenClient.SetReuseAddress() == -1 )
	{
        LOGERROR("setsockopt == -1");
        return false;
    }

	cSocket::SockAddr_In local;
	local.Family = cSocket::ADDRESS_FAMILY_INTERNET;
	local.Address = cSocket::INTERNET_ADDRESS_ANY;
	local.Port = (unsigned short)a_Port; // 25565

	if( m_pState->SListenClient.Bind( local ) != 0 )
	{
		LOGERROR("bind fail (%s)", cSocket::GetLastErrorString() );
		return false;
	}
	
	if( m_pState->SListenClient.Listen( 10 ) != 0)
	{
		LOGERROR("listen fail (%s)", cSocket::GetLastErrorString() );
		return false;
	}

	m_iServerPort = a_Port;
	LOG("Port %i has been bound, server is open for connections", m_iServerPort);
	m_bIsConnected = true;

	cIniFile IniFile("settings.ini");
	if( IniFile.ReadFile() )
	{
		g_bWaterPhysics = IniFile.GetValueB("Physics", "Water", false );
		
		/* Replaced below with 1.0.0 compatible ServerID generation

		std::string ServerID = IniFile.GetValue("Server", "ServerID");
		if( ServerID.empty() )
		{
			ServerID = "MCServer";
			IniFile.SetValue("Server", "ServerID", ServerID, true );
			IniFile.WriteFile();
		}
		*/
		m_pState->ServerID = "-";
		if (IniFile.GetValueB("Authentication", "Authenticate"))
		{
			MTRand mtrand1;
			unsigned int r1 = (mtrand1.randInt()%1147483647) + 1000000000;
			unsigned int r2 = (mtrand1.randInt()%1147483647) + 1000000000;
			std::ostringstream sid;
			sid << std::hex << r1;
			sid << std::hex << r2;
			std::string ServerID = sid.str();
			ServerID.resize(16, '0');
			m_pState->ServerID = ServerID;
		}
	}
	return true;
}

cServer::cServer()
	: m_pState( new sServerState )
	, m_Millisecondsf( 0 )
	, m_Milliseconds( 0 )
	, m_bIsConnected( false )
	, m_iServerPort( 0 )
	, m_bRestarting( false )
{
}

cServer::~cServer()
{
    if( m_pState->SListenClient ) m_pState->SListenClient.CloseSocket();
	m_pState->SListenClient = 0;

	m_pState->bStopListenThread = true;
	delete m_pState->pListenThread;	m_pState->pListenThread = 0;
	m_pState->bStopTickThread = true;
	delete m_pState->pTickThread;	m_pState->pTickThread = 0;

	delete m_pState;
}

// TODO - Need to modify this or something, so it broadcasts to all worlds? And move this to cWorld?
void cServer::Broadcast( const cPacket & a_Packet, cClientHandle* a_Exclude /* = 0 */ )
{
	for( ClientList::iterator itr = m_pState->Clients.begin(); itr != m_pState->Clients.end(); ++itr)
	{
		if( *itr == a_Exclude || !(*itr)->IsLoggedIn() ) continue;
		(*itr)->Send( a_Packet );
	}
}

void cServer::StartListenClient()
{
	cSocket SClient = m_pState->SListenClient.Accept();

	if( SClient.IsValid() )
	{
		char * ClientIP = SClient.GetIPString();
		if( ClientIP == 0 )
			return;

		LOG("%s connected!", ClientIP);

		cClientHandle *NewHandle = new cClientHandle( SClient );
		m_pState->Clients.push_back( NewHandle );	// TODO - lock list
	}
}

bool cServer::Tick(float a_Dt)
{
    //LOG("1. Tick %0.2f", a_Dt);
	if( a_Dt > 100.f ) a_Dt = 100.f; // Don't go over 1/10 second

	m_Millisecondsf += a_Dt;
	if( m_Millisecondsf > 1.f )
	{
		m_Milliseconds += (int)m_Millisecondsf;
		m_Millisecondsf = m_Millisecondsf - (int)m_Millisecondsf;
	}

	cRoot::Get()->TickWorlds( a_Dt ); // TODO - Maybe give all worlds their own thread?

	//World->LockClientHandle(); // TODO - Lock client list
	for( ClientList::iterator itr = m_pState->Clients.begin(); itr != m_pState->Clients.end();)
	{
		(*itr)->HandlePendingPackets();

		if( (*itr)->IsDestroyed() )
		{

			cClientHandle* RemoveMe = *itr;
			++itr;
			m_pState->Clients.remove( RemoveMe );
			delete RemoveMe;
			continue;
		}
		(*itr)->Tick(a_Dt);
		++itr;
	}
	//World->UnlockClientHandle();

	cRoot::Get()->GetPluginManager()->Tick( a_Dt );

	if( !m_bRestarting )
		return true;
	else
	{
		m_bRestarting = false;
		m_pState->RestartEvent.Set();
		LOG("<<<<>>>>SIGNALLED SEMAPHORE");
		return false;
	}
}

void ServerTickThread( void * a_Param )
{
	LOG("ServerTickThread");
	cServer *CServerObj = (cServer*)a_Param;

	cTimer Timer;

	long long msPerTick = 50;	// TODO - Put this in server config file
	long long LastTime = Timer.GetNowTime();

	bool bKeepGoing = true;
	while( bKeepGoing )
	{
		long long NowTime = Timer.GetNowTime();
		float DeltaTime = (float)(NowTime-LastTime);
		bKeepGoing = CServerObj->Tick( DeltaTime );
		long long TickTime = Timer.GetNowTime() - NowTime;
		
		if( TickTime < msPerTick )	// Stretch tick time until it's at least msPerTick
		{
			cSleep::MilliSleep( (unsigned int)( msPerTick - TickTime ) );
		}

		LastTime = NowTime;
	}

	LOG("TICK THREAD STOPPED");
}

void cServer::StartListenThread()
{
	m_pState->pListenThread = new cThread( ServerListenThread, this, "cServer::ServerListenThread" );
	m_pState->pTickThread = new cThread( ServerTickThread, this, "cServer::ServerTickThread" );
	m_pState->pListenThread->Start( true );
	m_pState->pTickThread->Start( true );
}

std::vector< std::string > StringSplit(std::string str, std::string delim)
{
	std::vector< std::string > results;
	size_t cutAt;
	while( (cutAt = str.find_first_of(delim)) != str.npos )
	{
		if(cutAt > 0)
		{
			results.push_back(str.substr(0,cutAt));
		}
		str = str.substr(cutAt+1);
	}
	if(str.length() > 0)
	{
		results.push_back(str);
	}
	return results;
}

template <class T>
bool from_string(T& t,
				 const std::string& s,
				 std::ios_base& (*f)(std::ios_base&))
{
	std::istringstream iss(s);
	return !(iss >> f >> t).fail();
}

std::string & StrToUpper(std::string& s)
{
		std::string::iterator i = s.begin();
		std::string::iterator end = s.end();

		while (i != end) {
				*i = (char)toupper(*i);
				++i;
		}
		return s;
}

int NoCaseCompare( std::string s1, std::string  s2 )
{
	return StrToUpper( s1 ).compare( StrToUpper( s2 ) );
}

bool cServer::Command( cClientHandle & a_Client, const char* a_Cmd )
{
    cPluginManager* PM = cRoot::Get()->GetPluginManager();
	if( PM->CallHook( cPluginManager::E_PLUGIN_CHAT, 2, a_Cmd, a_Client.GetPlayer() ) )
		return true;

	std::string Command( a_Cmd );
	if( Command.length() <= 0 ) return false;
	if( Command[0] != '/' ) return false;

	std::vector< std::string > split = StringSplit( Command, " " );
	if( split.size() == 0 )
		return false;

	if( split[0].compare("/coords") == 0 )
	{
		char c_Str[128];
		sprintf_s( c_Str, 128, "[X:%0.2f] [Y:%0.2f] [Z:%0.2f]", a_Client.GetPlayer()->GetPosX(), a_Client.GetPlayer()->GetPosY(), a_Client.GetPlayer()->GetPosZ() );
		a_Client.Send( cPacket_Chat( cChatColor::Green + c_Str ) );
		return true;
	}
	return false;
}

void cServer::ServerCommand( const char* a_Cmd )
{
	std::string Command( a_Cmd );
	std::vector< std::string > split = StringSplit( Command, " " );
	if( split.size() > 0 )
	{
		if( split[0].compare( "help" ) == 0 )
		{
			printf("===================ALL COMMANDS====================\n");
			printf("help      - Shows this message\n");
			printf("save-all  - Saves all loaded chunks to disk\n");
			printf("list      - Lists all players currently in server\n");
			printf("numchunks - Shows number of chunks currently loaded\n");
			printf("say       - Sends a chat message to all players\n");
			printf("restart   - Kicks all clients, and saves everything\n");
			printf("            and clears memory\n");
			printf("stop      - Saves everything and closes server\n");
			printf("===================================================\n");
			return;
		}
		if( split[0].compare( "stop" ) == 0 || split[0].compare( "restart" ) == 0 )
		{
			return;
		}
		if( split[0].compare( "save-all" ) == 0 )
		{
			cRoot::Get()->GetWorld()->SaveAllChunks();	// TODO - Force ALL worlds to save their chunks
			return;
		}
		if( split[0].compare( "list" ) == 0 )
		{
			cWorld::EntityList Entities = cRoot::Get()->GetWorld()->GetEntities();
			std::string PlayerString;
			int NumPlayers = 0;
			cRoot::Get()->GetWorld()->LockEntities();
			for( cWorld::EntityList::iterator itr = Entities.begin(); itr != Entities.end(); ++itr)
			{
				if( (*itr)->GetEntityType() != cEntity::E_PLAYER ) continue;
				PlayerString.push_back(' ');
				PlayerString += ((cPlayer*)*itr)->GetName();
				NumPlayers++;
			}
			cRoot::Get()->GetWorld()->UnlockEntities();
			printf( "Players (%i):%s\n", NumPlayers, PlayerString.c_str() );
			return;
		}
		if( split[0].compare( "numchunks" ) == 0 )
		{
			printf("Num loaded chunks: %i\n", cRoot::Get()->GetWorld()->GetNumChunks() );
			return;
		}
		if(split[0].compare("monsters") == 0 ){
			cMonster::ListMonsters();
			return;
		}
		if(split.size() > 1)
		{
			if( split[0].compare( "say" ) == 0 )
			{
				std::string Message = cChatColor::Purple + "[SERVER] " + Command.substr( Command.find_first_of("say") + 4 );
				LOG("%s", Message.c_str() );
				Broadcast( cPacket_Chat(Message) );
				return;
			}
		}
		printf("Unknown command, type 'help' for all commands.\n");
	}
	//LOG("You didn't enter anything? (%s)", a_Cmd.c_str() );
}

void cServer::SendMessage( const char* a_Message, cPlayer* a_Player /* = 0 */, bool a_bExclude /* = false */ )
{
	cPacket_Chat Chat( a_Message );
	if( a_Player && !a_bExclude )
	{
		cClientHandle* Client = a_Player->GetClientHandle();
		if( Client ) Client->Send( Chat );
		return;
	}

	Broadcast( Chat, (a_Player)?a_Player->GetClientHandle():0 );
}

void cServer::Shutdown()
{
	m_bRestarting = true;
	m_pState->RestartEvent.Wait();

	cRoot::Get()->GetWorld()->SaveAllChunks();

	//cWorld* World = cRoot::Get()->GetWorld();
	//World->LockClientHandle();	// TODO - Lock client list
	for( ClientList::iterator itr = m_pState->Clients.begin(); itr != m_pState->Clients.end(); ++itr )
	{
		delete *itr;
	}
	m_pState->Clients.clear();
	//World->UnlockClientHandle();
}


const char* cServer::GetServerID()
{
	return m_pState->ServerID.c_str();
}