Download this source code in text format (BigDog.cpp)
/***********************************************************
* WindowMud - Window Mud Server *
* File: BigDog.cpp *
* Usage: Game loop *
* *
* No warranty is given or implied. *
* There is no license associated with this code. *
* There are no restrictions on the use of this code. *
* This code may be used in any way the reader wishes. *
* No credit or credits are given and none are required. *
* The reader may take this code and claim they wrote it. *
************************************************************/
/***********************************************************
* Includes *
************************************************************/
#include "stdafx.h" // precompiled headers
#include "BigDog.h"
// Server
#include "Communication.h"
#include "Descriptor.h"
#include "Dnode.h"
#include "Log.h"
#include "Utility.h"
/***********************************************************
* Globals *
************************************************************/
extern CString HomeDir;
CString ErrorMsg;
bool StateConnections;
bool StateRunning;
bool StateStopping;
bool WinSockCleanUp;
/***********************************************************
* BigDog - The dog, the whole dog, and nothing but the dog *
************************************************************/
void BigDog()
{
CString GoGoGoFileName;
CFileStatus FileStatus;
CString LogBuf;
CString StopItFileName;
if (_chdir(HomeDir))
{ // Change directory failed
ErrorMsg = "BigDog - Change directory to HomeDir failed";
Utility::BlowUp();
}
// Set Go Stop, force go status
StopItFileName = CONTROL_DIR;
StopItFileName += "StopIt";
GoGoGoFileName = CONTROL_DIR;
GoGoGoFileName += "GoGoGo";
if (CFile::GetStatus(StopItFileName, FileStatus))
{ // If StopIt file exists, Rename it to GoGoGo
CFile::Rename(StopItFileName, GoGoGoFileName);
}
// Log game startup
Log::OpenLogFile();
LogBuf = "WindowMud version 0.0 has started";
Log::LogIt(LogBuf);
LogBuf = "Home directory is ";
LogBuf += HomeDir;
Log::LogIt(LogBuf);
// Initialize
StateConnections = true;
StateRunning = true;
StateStopping = false;
Communication::SockOpenPort(PORT_NBR);
Descriptor::InitDescriptor();
// Game loop
while (StateRunning)
{ // Game runs until it is stopped
Sleep(MILLI_SECONDS_TO_SLEEP);
if (!StateStopping)
{ // Game is not stopping, but should it be?
if (CFile::GetStatus(StopItFileName, FileStatus))
{ // StopIt file was found, Stop the game
StateStopping = true;
LogBuf = "Game is stopping";
Log::LogIt(LogBuf);
}
}
if (!StateStopping)
{ // Game is not stopping, check for new connections
Communication::SockCheckForNewConnections();
if (StateConnections && Dnode::GetCount() == 1)
{ // No players connected
LogBuf = "No Connections - going to sleep";
Log::LogIt(LogBuf);
StateConnections = false;
}
}
if (StateConnections)
{ // One or more players are connected
Communication::SockRecv();
}
else
{ // No connections
if (StateStopping)
{ // Game is stopping
StateRunning = false;
}
}
}
// Game has stopped so clean up
Descriptor::ClearDescriptor();
Communication::SockClosePort(PORT_NBR);
LogBuf = "WindowMud has stopped";
Log::LogIt(LogBuf);
Log::CloseLogFile();
}