[Solved] MCServer Questions: Running as Service / Running in VS
#1
Hello all,

I have two issues/questions that I am hoping someone may be able to assist me with.
  1. 1 I am trying to get MCServer to run as a Windows Service. I am using a wrapper available from Source Forge. The MCServer appears to run, starts logging, but then immediately terminates. Doing a bit of troubleshooting I can see that the member variable, m_bStop is being set to true shutting down the server. I am cloning the source code from gitHub and compiling with Visual Studio 2013 Express. If someone can shed some light on the behavior and perhaps suggest a work around, I would appreciate it.

  2. I would like to execute the MCServer from within Visual Studio. However, when attempting to execute the server, I receive an error related to, "Unable 5o start program", a path associated with "ALL_BUILD' and a message that says, "The system cannot find the path specified." If someone can tell me first is it possible to execute the server under VS and if so what do I need to do? Admittedly, my VS skills are weak.
Thank you.
Reply
Thanks given by:
#2
Hello, and welcome to the forum.

What wrapper are you using, the Java Service Wrapper - http://sourceforge.net/projects/wrapper/ ? I don't know much about it, but I would expect that it manipulates the input stream handles somehow, which makes MCS believe that its input stream has closed, which is a signal for the server to shut down gracefully. I'd recommend using the original Microsoft tool, srvany.exe, as the wrapper; unfortunately finding a proper download is troublesome these days. Or perhaps look through the Java Service Wrapper's documentation if it says anything about the input stream, and perhaps a way to make it not close the stream.

As for running from VS, you're really close. Just open the solution in VS, get the Solution Explorer window (by default docked to the left), find the MCServer project there, right-click it and select "Set as startup project". As it is, there's no way for CMake to set the startup project while it generates the solution file, and VS defaults to the first project in the solution, which unfortunately is CMake's internal housekeeping project, so it cannot be run directly.
The next step is to set the Working directory, so that the server can find its stuff when it starts up. Right-click on the project once again, select Properties, and in the dialog, navigate to Configuration Properties -> Debugging in the tree on the left side, then change Working directory to "..\MCServer" (without the dblquotes). Note that if you switch to a different configuration (release / profile / ...), you'll need to do this again, or you can simply select All configurations in the Configuration combo at the top of the dialog before changing the value.
That's it, now you should be able to run without any problems.

I recommend one more thing - set up MS Symbol servers in order to improve the debugging accuracy. In VS, use the Tools -> Options menu, navigate the left tree to Debugging -> Symbols, make sure that Microsoft Symbol Servers is checked in the list and that the cache directory under the list is valid, select the "All modules, unless excluded" radio button and confirm the changes in the dialog. The first debugging session that you start afterwards will take quite some time to start, don't worry, it's a one-off action, VS is downloading public symbols for all the MS system DLLs that MCServer uses from MS servers, so that you get complete stack traces. It took my computer some three minutes to display the MCServer console and another three minutes for the server to start; YMMV. With the new settings, whenever you hit a breakpoint or a crash, the callstack window will have the most accurate callstack available, instead of only guessing through half the entries.
Reply
Thanks given by: TealFrog
#3
I have added a GitHub issue to make it more easy to start MCS as a service:
https://github.com/mc-server/MCServer/issues/1360
Reply
Thanks given by: TealFrog
#4
The wrapper I'm using is actually off of GitHub, I took a look at the Java one earlier on Source Forge and forgot that I used the one off of GitHub instead. (my mistake).

GitHub winsw,
http://repo.jenkins-ci.org/releases/com/...nsw/winsw/

I know of the srvany.exe utility and that is great suggestion. As you mentioned it is quite old. I want to say it was part of NT 4.0 resource kit and/or possibly Win2K resource kit. I probably have it some where.

The GitHub, WinSw works for executables and Java. The C# source is available and I'm compiling it, so I can manipulate it as well.

I'll try the suggestions and look into it a bit further.

A huge thank you for both posts.
Reply
Thanks given by:
#5
The suggestions worked.

I was able to get the MCServer server to run under MSVS by changing the setting related to the startup project. I also successfully added the debugging symbols. This was a great suggestion. I am used to configuring symbols under WinDbg, but it never dawned on me to do this under MSVS, but then again most of my development has either been done by either using notepad or vi as an IDE.

As far as using the wrapper to run the MCServer server as a service, the wrapper does close stdin. I added some additional C# code to keep the stream open. The MCServer server runs and I'm able to connect using the Minecraft client; however, once connected and inside the MCServer Minecraft world the world crashes with an uncaught exception and the error mentioning an invalid parameter being passed to a function. The error appears to occur with the deletion of a pointer as part of a linked list (STL) of world blocks or similar. Unless this is a bug being introduced by the wrapper, my next step will be to look at the MCServer code to have it run without the stdin console. I was sort of hoping to avoid this.

For what it is worth the error is occurring in,
Code:
void cSetChunkData::RemoveInvalidBlockEntities(void)
at
Code:
delete *itr;
.

I'm not a huge fan of Java (though it does have its place) and I really think that MCServer has some huge advantages being written in C++. I'm hoping that MCServer works out as I'm not crazy about the alternatives.

Thanks.
Reply
Thanks given by:
#6
Thanks for the good news.

The iterator problem is known and a fix is pending in the pull requests, should be available later today.
Reply
Thanks given by:
#7
Fix merged.
Reply
Thanks given by:
#8
Awesome! Using the most recent MCServer merged source there is now no more crashing. The MCServer server runs as a service using the winsw project.

Hopefully, I can follow up with some details later to help others.

Thanks again and taking the time to educate me a bit.
Reply
Thanks given by:
#9
For anyone who wishes to replicate the process and run MCServer as a service using the same method, this is how I accomplished it. This is a high level overview of the process with the required information. I am making the assumption that the person doing this is either comfortable with the steps, i.e. using GitHub, using Microsoft Visual Studio, modifying and compiling source code or is willing to seek out the information and do some research on any unfamiliar areas. Having knowledge about compiling source code, using Visual Studio, GitHub and working with Windows Services is very helpful. Anyone is capable of doing this if they have the tenacity to seek out the information on the Internet. To cover all of the details related to every requirement and individual step would probably be better suited for a Wiki article or other "how to" article.

Do the following:
  • Clone or download as a zip file the following winsw service wrapper from GitHub,
    https://github.com/kohsuke/winsw
  • Read the README.markdown file. This can easily be done on the GitHub site by clicking on the file name README.markdown.
  • If not installed, download and install Microsoft Visual Studio. I used Microsoft Visual Studo 2013 Express for Windows Desktop.
  • Open the solution file, winsw.sln (a Microsoft Visual Studio Solution file) in Microsoft Visual Studio and compile the solution to verify that everything is working correctly.
  • If the project compiles successfully with zero errors, then edit the main.cs file in MSVS or apply the following patch to the main.cs file.

    Code:
    --- C:\Users\Administrator\Documents\GitHub\winsw\Backup\Main.cs
    +++ C:\Users\Administrator\Documents\GitHub\winsw\Main.cs
    @@ -216,10 +216,18 @@

                 StartProcess(process, startarguments, descriptor.Executable);

    +            StreamWriter w = process.StandardInput;
    +            w.AutoFlush = true;
    +            Console.SetOut(w);
    +
    +            var handle = process.Handle;
    +            Kernel32.SetStdHandle(-10, handle); // set stdin
    +
                 // send stdout and stderr to its respective output file.
                 HandleLogfiles();

    -            process.StandardInput.Close(); // nothing for you to read!
    +        
    +            //process.StandardInput.Close(); // nothing for you to read! This line could also just be deleted.
             }

             protected override void OnShutdown()

  • If editing the main.cs file add the above lines prefixed with '+' (addition) symbol and delete or change the line above that is prefixed with a '-' (subtraction) symbol. In all honesty, this is a hack and there are better ways of accomplishing the same thing, but this works.
  • Once the changes have been made recompile the project.
  • Follow the directions for installing and using the service wrapper contained in the README.markdown.
The XML configuration file that I use for the winsw service wrapper contains the following:

Code:
<service>
  <id>MinecraftServer</id>
  <name>MinecraftServer</name>
  <description>Wrap and run MinecraftServer as a service</description>
  <env name="MCServerHome" value="%BASE%"/>
  <executable>MCServer_debug.exe</executable>
  <arguments></arguments>
  <workingdirectory>%BASE%\MCServer</workingdirectory>
  <logpath>%BASE%/logs</logpath>
  <log mode="roll-by-size">
  <sizeThreshold>20480</sizeThreshold>
  <keepFiles>10</keepFiles>
</log>
</service>

You will have to modify it based on your configuration, but it should be very similar. I have attached a .zip file that contains the service wrappper, winsw.exe and the source file, main.cs, that contains the modified C# source code. The executable is a Windows x86 33-bit requiring .Net.

Best of luck.


Attached Files
.zip   winsw.zip (Size: 28.19 KB / Downloads: 238)
Reply
Thanks given by: xoft
#10
Excellent, thank you for this guide!
Reply
Thanks given by:




Users browsing this thread: 4 Guest(s)