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.htmlv) 1.1
http://rapidshare.com/files/140959091/DSWP.zip.htmlChange 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:
#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.