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,551,710
since: 19 Jan 2005

Ulysses Agenda: Refactoring some networking code

posted Sat 16 Dec 06

If you've been reading my series of blog posts on Ulysses Agenda, then you probably remember the following bit of code:

_auth = new NetLibImplementations.Authenticator();
_authSite = new InstanceContext(_auth);
_authChannelFactory = new DuplexChannelFactory<IAuthenticatorChannel>(_authSite, "AuthEndPoint");
_authProxy = (IAuthenticatorChannel)_authChannelFactory.CreateChannel();

What you didn't see what that this code relied on a big pile of XML in the App.config file, shown below:

<?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"/>
      <endpoint
          name="AuthEndPoint"
          address="net.p2p://ulyssesagenda/auth"
          binding="netPeerTcpBinding"
          bindingConfiguration="PeerBinding"
          contract="UlyssesAgenda.NetworkLibrary.IAuthenticator" />
    </client>
    <bindings>
      <netPeerTcpBinding>
        <binding
          name="PeerBinding"
          port="8091"
          maxReceivedMessageSize="2147483647" >
          <security mode="None">
          </security>       
        </binding>
      </netPeerTcpBinding>
    </bindings>
    </system.serviceModel>
</configuration>

There's a couple of problems with this scenario. The first problem is that if I need to dynamically open new channels at runtime with varying endpoints (such as having a player move in an out of galaxies, NPCs moving in an out of galaxies, etc) then there's really no way to do it using App.config. App.config only allows me to define static, persistent channels, not transient channels.

What I've done is refactored this so that there is no longer a dependency on the App.config file and all the channels are created and opened dynamically at runtime, and now I can set up a new channel and channel listener with the following two lines of code:

_auth = new UlyssesAgenda.UniverseServer.NetLibImplementations.Authenticator(); 
_authProxy = CommunicationCenter.OpenChannel<IAuthenticator, IAuthenticatorChannel>(
    _auth, "net.p2p://ulyssesagenda/auth", 8091);

Here's the code for my OpenChannel<T,U> method, which does all the work of creating, configuring, and opening a channel at runtime:

public static U OpenChannel<T, U>(T sourceObject,
    string p2pUrl,
    int portNumber) where U: IClientChannel
{
    InstanceContext sourceContext = new InstanceContext(sourceObject);
    NetPeerTcpBinding binding = new NetPeerTcpBinding();
    binding.Port = portNumber;
    binding.Name = p2pUrl + "@" + portNumber.ToString();
    binding.Security.Mode = SecurityMode.None;
    binding.MaxReceivedMessageSize = 2147483647;
    EndpointAddress address = new EndpointAddress(p2pUrl);
    DuplexChannelFactory<U> sourceFactory = new DuplexChannelFactory<U>(sourceContext,
        binding,
        address);
    U sourceProxy = (U)sourceFactory.CreateChannel();
    sourceProxy.Open();
    return sourceProxy;
}

What this refactor does is get me a little bit closer to where I need to be - a spot where I can write code that will dynamically bring up and shut down P2P channels at will at runtime to account for players moving in and out of galaxies, moving in and out of chat channels, and much more. With the dependency on statically defined App.config channels gone, the code for the game is much more dynamic and much more flexible.

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: