The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,502,482
since: 19 Jan 2005

Ulysses Agenda - Network Engine Test 1

posted Sun 08 Oct 06

You may already know that, when I manage to find a few seconds of spare time every once in a while, I am working on a game called Ulysses Agenda. This game is basically a combination of the Windows Communication Foundation, Windows Presentation Foundation, and even some Windows Workflow Foundation (hopefully). In order to build the network engine, I decided to get the smallest unit of functionality running that I could so I decided to build just the chat channel.

WCF is all about the interfaces - everything is driven by the interfaces, especially peer-to-peer WCF applications. Here's a look at my IChat interface:

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace UlyssesAgenda.NetworkLibrary
{
  [ServiceContract(CallbackContract=typeof(IChat))]
  public interface IChat
  {
    [OperationContract(IsOneWay = true)]
    void SendChatMessage(ChatMessage chatMessage);

    [OperationContract(IsOneWay = true)]
    void Join(NetworkPlayer p);

    [OperationContract(IsOneWay = true)]
    void Leave(NetworkPlayer p);
  }
 
  public interface IChatChannel : IChat, IClientChannel
  {
  }

  [DataContract]
  public class ChatMessage
  {
    [DataMember]
    public string messageBody;

    [DataMember]
    public NetworkPlayer originator;

    [DataMember]
    public TargetScope destination;
  }
}

This interface is in a library all on its own called UlyssesAgenda.NetworkLibrary. This library is going to contain all of the shared interfaces and data contract structs required for network communications. I've got a WPF application called UlyssesAgenda.GameServer and a WPF application called UlyssesAgenda.GameClient - pretty straightforward. One technique that I'm using to test these applications is that I have switched them from WPF applications to console applications. The WPF main window still shows up - but I also get an attached console window - giving me a lot of troubleshooting flexibility - specifically - I get to have a visible trace of network messages on non-debuggable machines (like my wife's PC... *ahem* not that I would ever *cough* use her machine to test *cough* networking code... )

GameClient's App.config:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <endpoint
        name="ChatEndpoint"
        address="net.p2p://ulyssesagenda/chat"
        binding="netPeerTcpBinding"
        bindingConfiguration="PeerBinding"
        contract="UlyssesAgenda.NetworkLibrary.IChat"/>
    </client>
    <bindings>
      <netPeerTcpBinding>
        <binding
          name="PeerBinding"
          port="8092"
          maxReceivedMessageSize="2147483647" >
          <security mode="None">           
          </security>         
        </binding>
      </netPeerTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

What we're looking at here is that the Game Client is going to listen for IChat messages on net.p2p://ulyssesagenda/chat on port 8092. If you're running on Vista, you will be prompted to unlock this port. And here's the App.config file for the UniverseServer:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>         
    <client>
      <endpoint
          name="ChatEndpoint"
          address="net.p2p://ulyssesagenda/chat"
          binding="netPeerTcpBinding"
          bindingConfiguration="PeerBinding"
          contract="UlyssesAgenda.NetworkLibrary.IChat"/>     
    </client>
    <bindings>
      <netPeerTcpBinding>
        <binding
          name="PeerBinding"
          port="8091"
          maxReceivedMessageSize="2147483647" >
          <security mode="None">
          </security>       
        </binding>
      </netPeerTcpBinding>
    </bindings>
    </system.serviceModel>
</configuration>

The only noticeable difference ( so far ), is that the UniverseServer is running on a different port. This allows you to run a game client and a Universe Server on the same box, either for testing purposes or because you want to be able to play the game you're hosting. Finally, let's take a look at the code in the App.xaml.cs file that connects to the peer mesh:

using System;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using System.ServiceModel;
using UlyssesAgenda.NetworkLibrary;
namespace UlyssesAgenda.UniverseServer
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {
        private DuplexChannelFactory<IChatChannel> _chatChannelFactory;
        private InstanceContext _site;
        private NetLibImplementations.Chat _chat;
        private IChatChannel _chatProxy;
        private NetworkPlayer _me;       
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Console.WriteLine("Universe Server Starting Up...");
            _me = new NetworkPlayer();
            _me.PlayerName = "UniverseServer";
            _chat = new UlyssesAgenda.UniverseServer.NetLibImplementations.Chat();
            _site = new InstanceContext(_chat);

            _chatChannelFactory = new DuplexChannelFactory<IChatChannel>(_site, "ChatEndpoint");
            _chatProxy = (IChatChannel)_chatChannelFactory.CreateChannel();
            Console.WriteLine("Created Chat Proxy Channel.");
            _chatProxy.Open();
            Console.WriteLine("Chat Proxy Started...");
        }
    }
}

The code is nearly identical for the game client, except the game client harness actually sends some chat messages. In theory, the server will only send broadcast type messages that might indicate that the game is shutting down, whereas clients will be sending messages to players, guilds, galaxies, starports, and more:

_chatProxy.Join(_me);
ChatMessage message = new ChatMessage();
message.originator = _me;
message.destination = new TargetScope();
message.messageBody = "Hello from the game client.";
_chatProxy.SendChatMessage(message);

Now take a look at a screenshot of the game client and game server running simultaneously on my Vista laptop:

tags:              

links: digg this    del.icio.us    technorati    reddit




Tag Related Posts

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

CLINQ v1 Demo - Network Message Filtering

Wed 09 Jan 08 7:47 P GMT-05
tags:        

Building a Ledger Style for WPF Grids

Tue 21 Aug 07 3:30 P GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

My 2008 Wishlist : A Transformers MMORPG

Tue 26 Jun 07 12:04 P GMT-05
tags:        

My first "Acropolis" Application

Mon 04 Jun 07 1:40 P GMT-05
tags:      

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05

Orcas' Hidden Gem - The managed PNRP stack

Fri 11 May 07 6:45 P GMT-05
tags:        

Will Silverlight be DOA?

Mon 16 Apr 07 8:02 P GMT-05

Exploring the MVC Pattern in WPF

Tue 10 Apr 07 12:51 P GMT-05
tags:                      

WPF Bindings == WTF Bindings?

Mon 12 Mar 07 6:31 P GMT-05

On MUDs

Thu 08 Mar 07 5:00 A GMT-05
tags:                    

Cocoa Programming vs. WPF : NIB vs XAML

Tue 20 Feb 07 2:09 P GMT-05

Cocoa Bindings vs. WPF Binding

Thu 15 Feb 07 5:41 P GMT-05
tags:                

What I think is a bug in WCF POX messaging

Thu 04 Jan 07 4:58 P GMT-05
tags:      

Ulysses Agenda Makes Redmond Developer News

Wed 29 Nov 06 7:10 P GMT-05
tags:                

A visit to Nintendo World on "Wii Day"

Mon 20 Nov 06 12:37 P GMT-05
tags:          

Ulysses Agenda - Network Engine Test 1

Mon 09 Oct 06 2:26 A GMT-05
tags:              

Ulysses Agenda : First Cut Networking Design

Thu 14 Sep 06 12:46 A GMT-05
tags:                  

First Impressions of Windows Vista RC1

Thu 07 Sep 06 1:30 P GMT-05
tags:                      

Localizing a WPF Application

Tue 22 Aug 06 11:39 A GMT-05
tags:            

WPF Slide Show and Photo Album

Fri 18 Aug 06 6:48 P GMT-05

.NET Framework 3.0 June CTP is out!

Fri 23 Jun 06 6:23 P GMT-05
tags:                

Tech-Ed 2006 - Session Reviews

Tue 13 Jun 06 6:22 P GMT-05
tags:                    

Tech-Ed 2006 Day 1 - Registration Day

Sun 11 Jun 06 7:17 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags: