Dedicated Server Watchdog Program

Author Topic: Dedicated Server Watchdog Program  (Read 17805 times)

I've recently been having a dedicated server up and running , allowing my sister and friends to play on the server while I was away. Unfortunately the server intermittently crashes for seemingly unknown reasons, resulting in me having to reload the dedicated.bet file. Getting tired of having to chug downstairs when the server crashes after we've been playing (usually with massive events or on the castle build i've been working on)and seeing a couple posts in the 'Help' section of the forums on the matter of the dedicated server crashing, I decided to create a program to monitor the server for me.

The program is simply named 'Dedicated Server Watchdog Program'. Simply enough, the program checks every 30 seconds to make sure that the server is running, and if it's not, it will reload it and check to make sure it reloaded successfully, then go back to idling.

Because this is built for a very specific purpose it has some specific requirements. First and foremost.. Blockland MUST be installed in: C:\Program Files\Blockland. on my main computer Blockland is installed in C:\Blockland... This will not work!
I will try to address this issue in any later builds of the program, but for now... Have blockland in that folder.

On top of this, DSWP needs to be ran from the same folder in order for Blockland to load accordingly.

Another side note:
You cannot run another instance of blockland at the same time on the same computer because of the way the check works. All other instances of Blockland.exe MUST BE CLOSED if you expect this to relaunch the server for you. Otherwise if the server crashes it will find the handle to the other instance and think that IT is the dedicated server.

Please feel free to ask and questions or give comments, I have included the source code for those of you who wish to learn about how it works. Otherwise, Because of the limitations put onto the forums to prevent uploading certain files, I have uploaded the program to Rapidshare which can be downloaded here:
 
v) 1.0
http://rapidshare.com/files/139901086/DSWP.zip.html


v) 1.1
http://rapidshare.com/files/140959091/DSWP.zip.html

Change Log:
 - Cleaned up the code
 - Added a "Freeze checker"
 - Continued Testing with Ubuntu.

Up-coming:
 - Dynamic File location
 - Crash log
 - Crash date stamps
 - GUI
 - Binary edition (Possibly *nix)
 - Mac edition (don't hold your breath)

Updated Source:
Code: [Select]
#include <windows.h>  // these are our preprocessors. Our API is defined here
#include <iostream>

using namespace std; //include the namespace lib. includes c functions (cout, cin)

STARTUPINFO          si = { sizeof(si) };   //This is a pointer to our "Start Up info" typedef
PROCESS_INFORMATION  pi;                    //This is a pointer to our process information (this is where we can grab the pid)
char                 szExe[] = "C:\\Program Files\\Blockland\\dedicated.bat";
HWND hwnd;
HANDLE phandle; //This is a typedef for the OpenProcess function
DWORD pid;   //this will store the PID of blockland.
DWORD result;
void info(); // Just moving the jibberish out of the way

int main(){
info(); //spits out our info and waits for the user
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
if(hwnd){
GetWindowThreadProcessId(hwnd, &pid);
cout << endl << "the dedicated server is running under the PID:" << pid << endl;
cout << "Running Back checks..." << endl;
}
while(TRUE){
cout << "\nChecking if the server is up..." << endl;
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
SendMessageTimeout(hwnd,WM_NULL,0,0,SMTO_BLOCK | SMTO_ABORTIFHUNG, 5000, &result);
if(!hwnd){
cout << "It appears that Blockland isn't up. Re-Launching...";
///*
CreateProcess(0, szExe, 0, 0, TRUE, 0, 0, 0, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
Sleep(2500);
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
if(!hwnd){
MessageBox(0,"Unable to Re-Launch your dedicated server. Please make sure that you put this file into your BLOCKLAND folder",0,MB_OK);
break;
}
}
cout << endl << "Making sure it's not frozen..." << endl;
if(result){
cout << "It appears that Blockland is frozen running. Re-Launching...";
///*
CreateProcess(0, szExe, 0, 0, TRUE, 0, 0, 0, &si, &pi);
WaitForSingleObject(pi.hProcess, INFINITE);
Sleep(2500);
hwnd=FindWindow(0,"C:\\Program Files\\Blockland\\Blockland.exe");
if(!hwnd){
MessageBox(0,"Unable to Re-Launch your dedicated server. Please make sure that you put this file into your BLOCKLAND folder",0,MB_OK);
break;
}
else{
GetWindowThreadProcessId(hwnd, &pid);
phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
cout << "\n your dedicated server has been succefully relaunched." << endl;
}
}
else{
cout << endl << "Check passed, everything is fine" << endl;
cout << "Currently the PID of the hooked blockland server is:" << pid << endl;
//*/

}
Sleep(30000);
}
CloseHandle(phandle);
return 0;
}

void info(){
cout << "Dedicated Server Watchdog Program" << endl;
cout << " Created for Blockland V9" << endl;
cout << " 8/24/08 - Version 1.0 " << endl;
cout << "By: Poent" << endl;
cout << "\n\nThis launcher was designed to work with Blockland stored in:\n C:\\Program Files\\Blockland" << endl;
cout << "You will be able to change this is later versions, \nbut until then... leave it there." << endl;
cout << "\n\n By default, this program will check every 30 seconds \nto make sure that blockland is running." << endl;
cout << "If for any reason the dedicated server is \nnot found, it will relaunch 'dedicated.bat' and verify \nthat it successfully relaunched." << endl;
cout << "\n\n The watchdog program is now ready to launch. \nOnce you're ready to begin, Simply hit 'Enter'..." << endl;
cin.get();
}



The program seems to work great for me. While crashes dont happen too often, when they do happen it has been very useful to have this guy running. It works well and loads the server.

known issues:
Under windows vista, Computer that require Blockland to run under "administrative mode" must disable UAC, the program cannot continue and will fail if UAC is enabled.

The program will not load saved maps or bricks. unless there is a switch in Blockland that will allow the dedicated server to load a specified map, i don't know any way of allowing it to do so either.
« Last Edit: August 29, 2008, 12:06:04 AM by Poent »

I'd recomend serverdoc.  Google it.  It works well for hosting bl servers.

I'd be lying if I said my solution was better, this looks like an excellent program. Hopefully this will still be of use to someone if only for a reference to use for their interest in  C++. I'll be sure to check out serverdoc as well.

well i like this too! it works well, i needed this ;)

Can  you make a mac compatible version?

this is great my server keeps crashing and this is gonna help
 :cookie: for you

well i like this too! it works well, i needed this ;)

Unfortunately I've only worked with Win32 API so far. I'll try to look into it though.


Can  you make a mac compatible version?
you dont need one, there is a SuperMacro program on the apple website that does a WAYY better job.

OK i have tried making a ded server but it doesnt work. How would i make one on destruct instead of bedroom? And also am i suppose to leave the new window that appears up?

OK i have tried making a ded server but it doesnt work. How would i make one on destruct instead of bedroom? And also am i suppose to leave the new window that appears up?

You'll need to modify the server preferences.cs file to load a separate map. I haven't tested this, but you may also be able to simply modify the batch file and change the -bedroom switch to whatever map you want to load. And yes, you need to leave the window that it spawns open. It not only watches the program but will keep you informed on what's going on. At some point this will Log time and date, and give you an "uptime" and last crash data.

Status Update:
Currently the program isn't checking for when the program freezes. I had this happen yesterday and the watchdog program failed to reload the server until windows decided to end the task on its own, while it took about 30 minutes for windows to do this... it did eventually restart. I'm working on trying to find a way to monitor the process by either the PID or some kind of inline hook that would give the program a little more specific information about the process, so it can monitor it a bit better.

I was also doing some research on the mac compatibility and I have yet to find any comparable API to do what we need it to do. I'll keep looking but I haven't found much.

you dont need one, there is a SuperMacro program on the apple website that does a WAYY better job.

I searched for a little bit on google and the apple web page for this Program and was unable to find anything. I did find A macro tool named "AutoPilot", but it seems to have a pretty steep learning curve.

You'll need to modify the server preferences.cs file to load a separate map.


or go into admin menu and click change map[/obvious]

or go into admin menu and click change map[/obvious]
Indeed...

Otherwise the batch file will handle the loading for the dedicated server it looks like.

@ether:
If you edit your batch file to say... -
start blockland.exe -dedicated -map destruct

Then it will load the destruct map.

Very nice idea. Would this really qualify as an Add-on, though?

How about in a future version, when the server crashes, copy the console.log to another folder, with a date and time in the filename, to try to find out what caused the crash?

I'm not sure if it qualifies as an add-on or not but this seemed like the place to put it.

I do intend to include all of those features into later versions, minus trying to find out what causes the crash. I've spent some time already looking for the cause of it But i haven't found anything. I don't know enough about how the game works to be able to diagnose anything very specific either way.