Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have reviewed the relevant WinForms/XNA samples here and here.

However, my requirements are slightly different.

Given the following:

  • I am developing a multiplayer (Client - Server) game
  • There will be three projects in the solution, one for Client and Server respectively, plus an additional one containing the core game/engine
  • The instances will share common code from this engine project (core Game Engine functionality)
  • I require the Server instance to also allow management of the server (e.g. connection list etc.) via a Windows Form
  • Finally, and importantly, I am proposing to use the Dependency Injection pattern on initialisation of the client/server instances to inject the relevant network management class as required - similar to the approach here

What is the best way to achieve this? Specifically, are there any flaws with organising the projects in the solution the way I have proposed above? For the Server project, do I start it as an XNA project and then call the Winform, or vice versa?

Thank you for any advice you can spare.

Edit - a brief example of the "dependency injection" style pattern I plan to use. Forgive me if this is not an example of "pure" / full / traditional DI!

// Create the server
using (var game = new ExampleGame(new ServerNetworkManager()))
{
    game.Run();
}

// Create the client
using (var game = new ExampleGame(new ClientNetworkManager()))
{
    game.Run();
}
share|improve this question
4  
Try to make a prototype and you will see if it works. I dont think it will, but it depends on what you mean by Game engine functionality, only things that i was able to share between Client and Server was some constant stuff, message structs helper methods. And i doubt that you will ever make any use of dependency injection. –  Kikaimaru Mar 7 '13 at 15:15
    
The dependency injection will certainly be used. I will append an example to the original question for you. And in my case, most of the functionality will be shared, as the Server will be authoritative for all object positions etc. –  George Mar 7 '13 at 15:27
3  
There's nothing "non-tradiitonal" about your approach of DI. This is legit; frameworks are useful when you get to the point that you need them. Try prototyping this and let us know what specifically you get stuck on. Your approach seems fine (coming from someone who never tried sharing client/server code like this). –  ashes999 Mar 7 '13 at 15:40
    
Thank you, if there is nothing wrong with my plan so far, I will try implementing the project and let you know how I go. –  George Mar 7 '13 at 16:03

1 Answer 1

up vote 1 down vote accepted

OK, so after the positive reception of my design idea, I have gone ahead and implemented a prototype. For completeness, I'll describe the structure of my project below. Most importantly, it builds and runs, and the DI works great.

Solution Structure (in VS2010)

  • Solution Root:
    • Client. Console Application. Startup Project.
    • Server. Console Application. Startup Project.
    • XNAGame. Windows XNA Project. No entry point (Program.cs removed). Output Type: Class Library. No Startup Object.
    • XNA Game Content. Standard XNA content project.

Client/Server Projects
Reference the XNAGame project and Microsoft.Xna namespace. Both contain a Program.cs with a static void main() method as expected. This method invokes the XNA game, considering Dependency Injection, as follows:

// Network Manager type injected as appropriate.
using (XNAGame game = new XNAGame(new ClientNetworkManager()))
{
    game.Run();
}

XNAGame Project
Contains all the usual XNA structure (class with Update/Draw methods, etc.) but with one fundamental difference: there is a Windows Form in this project.

The RTTI features in C# allow this class instance to ascertain whether it has been instantiated as a Client or a Server. Obviously, the relevant Network Manager classes should have been created that implement a common interface for the polymorphism to work. For example:

// Constructor accepts the "injected" type as below.
public XnaGame(INetworkManager networkManager)
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";

    this.networkManager = networkManager;
}

// RTTI / Polymorphism. Determine if this instance is a Server or a Client.
public bool IsServer()
{
    return this.networkManager is ServerNetworkManager ? true : false;
}

// The Windows form within this project can then be run as follows.
protected override void Initialize()
{
    // TODO: Add your initialization logic here

    // If this is the server, we need to create and show the management form.
    if (this.IsServer() == true)
    {
        Form = new MainServerForm();
        Form.Show();
    }

    base.Initialize();
}
share|improve this answer
    
So your question is...solved? It works? –  ashes999 Mar 7 '13 at 23:24
    
So far, so good, yes. Thank you for your input originally. I'm genuinely puzzled as to why my question was voted down... my question was regarding the design and whether there would be any glaring problems with my idea. I chose to implement it, and it's turned out OK, yet I am left feeling that my question was not a good fit for this type of community? Strange. And frustrating. –  George Mar 7 '13 at 23:43
1  
this is a very unique and fickle community. They expect very specific questions with "correct" answers, generally. Anyway, I'm glad your problem was solved, and I hope you'll stick around -- it's a good site, once you get the hang of the culture. –  ashes999 Mar 8 '13 at 2:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.