<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BakedOn Entertainment - Online Multiplayer Games &#187; server</title>
	<atom:link href="http://www.bakedon.com/tag/server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bakedon.com</link>
	<description></description>
	<lastBuildDate>Wed, 29 Dec 2010 17:47:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to use Lidgren with Unity3D for MMOs &#8211; Part 1</title>
		<link>http://www.bakedon.com/2010/04/how-to-use-lidgren-with-unity3d-for-mmos-part-1/</link>
		<comments>http://www.bakedon.com/2010/04/how-to-use-lidgren-with-unity3d-for-mmos-part-1/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 23:58:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[lidgren]]></category>
		<category><![CDATA[MMORPG]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[udp]]></category>

		<guid isPermaLink="false">http://www.bakedon.com/?p=91</guid>
		<description><![CDATA[Why use Lidgren? Don&#8217;t kid yourself.  Writing a multiplayer game that runs over the Internet can be tricky. If you are using Unity you have the entire .NET network library at your disposal. Simply making a TCP connection or sending UDP packets is pretty trivial. Why on earth would you bother using someone else&#8217;s library? [...]]]></description>
			<content:encoded><![CDATA[<h2>Why use Lidgren?</h2>
<p>Don&#8217;t kid yourself.  Writing a multiplayer game that runs over the Internet can be tricky. If you are using Unity you have the entire .NET network library at your disposal. Simply making a TCP connection or sending UDP packets is pretty trivial. Why on earth would you bother using someone else&#8217;s library?</p>
<p><span id="more-91"></span></p>
<p>The debate between TCP and UDP for MMOs is as strong as ever and I&#8217;m certainly not going to talk about it here. For <a href="http://www.ghostees.com">Ghostees</a>, I am using UDP and the <a title="Lidgren Network Library" href="http://code.google.com/p/lidgren-network/">Lidgren</a> library. It&#8217;s open source, free but best of all it solves a lot of the issues revolving around using UDP as your transport.</p>
<p>The types of things you can encounter with UDP can be a pain but since it is low level, you get incredible flexibility and performance.<a href="http://www.bakedon.com/wp-content/uploads/2010/04/Shoot-yourself-in-the-foot.jpg"><img class="alignright size-full wp-image-94" style="float: right;" title="Shoot yourself in the foot" src="http://www.bakedon.com/wp-content/uploads/2010/04/Shoot-yourself-in-the-foot.jpg" alt="" width="200" height="143" /></a> Think of UDP as a message passing protocol as opposed to TCP which is a stream based protocol.  TCP will ensure that your data makes its way across the trenches of the internet in tact and in order.  UDP on the other hand makes no guarantees.  Some messages may go missing.  You might even get two of the same or get messages out of order. Tricky stuff.  Lidgren takes care of all that stuff for your but only where you want it.  It will definitely let you shoot yourself in the foot also.</p>
<h2>The (very) Basic Server</h2>
<p>By the way, Lidgren builds in Mono so you can run your server on Linux. I&#8217;m pretty sure it works but I haven&#8217;t done enough testing to say 100% at this point and I&#8217;m pretty much a Windows guy. Enough yapping, lets get coding.</p>
<p>Start a new project for your server and create your server class. I prefer to add the Lidgren project to my solution but either way, add a reference to <strong>Lidgren.Network</strong>.</p>
<p>Your class will need two private members to start: NetServer and NetBuffer, both in the Lidgren.Network namespace.</p>
<pre class="brush: csharp; title: ; notranslate">
class MyServer
{
    private NetServer server;
    private NetBuffer buffer;

    public MyServer()
    {
        var config = new NetConfiguration(&amp;quot;hello-world&amp;quot;);
        config.Port = 42948;

        server = new NetServer(config);
        buffer = new NetBuffer();
        server.Start();
    }
}
</pre>
<p>Lets go through this quickly.  On line 3 the NetServer is the class from Lidgren that will listen for incoming traffic.  The NetBuffer on the next line is what we read our messages into.  We can reuse it so it is declared as a class member.</p>
<p>We create an instance of NetConfiguration to pass configuration information into the NetServer constructor. NetConfiguration takes a single string argument that is our application identifier.  This can be anything you like as long as both the client and server use the same string.</p>
<p><a href="http://www.bakedon.com/wp-content/uploads/2010/04/homer-doh.jpg"><img class="alignleft size-medium wp-image-112" title="homer-doh" src="http://www.bakedon.com/wp-content/uploads/2010/04/homer-doh-211x300.jpg" alt="" width="89" height="126" /></a><strong>DOH!</strong> you wouldn&#8217;t believe how many times I&#8217;ve had projects where I change the application identifier on either the client or server but not the other and then subsequently spend w-a-a-a-y too long trying to figure out why I can&#8217;t connect.</p>
<p>On line 9 I set the port that the server will listen on for incoming messages. Choose pretty much any number you like that is less than 65535 as long as it doesn&#8217;t conflict with another application listening on the same port. Only one port per network card can be in use at any time. You are best off choosing a number somewhere over 32768 to avoid &#8220;well known&#8221; ports.</p>
<p>Lastly, we just call Start() on the server to have it bind to the port and buffering messages.</p>
<h2>The Pump</h2>
<p><a href="http://www.bakedon.com/wp-content/uploads/2010/04/joeblowsport.jpg"><img class="alignright size-full wp-image-123" style="float: right;" title="joeblowsport" src="http://www.bakedon.com/wp-content/uploads/2010/04/joeblowsport.jpg" alt="" width="100" height="161" /></a>When the server receives a message, after some processing it places it into a queue. This all happens behind the scenes in another thread. You need to write code to retrieve them.  I usually have a single thread reading and distributing incoming messages and you might as well make it your main thread.  Add this method to your server class:</p>
<pre class="brush: csharp; title: ; notranslate">
    public void Process()
    {
        NetMessageType type;
        NetConnection source;

        while (server.ReadMessage(buffer, out type, out source))
        {
            HandleMessage(buffer, type, source);
        }
    }
</pre>
<p>Your server host, whether its a console application or Windows service, will be responsible for pumping the messages by calling Process. Avoid the temptation of over complicating your server with a thread for the message pump. You won&#8217;t see any speed or efficiency increase putting more threads in here.</p>
<p>Each time through the while loop, the server will hand you one message. It will also give you the type of message and which client connection it came from.</p>
<h2>The Mail Room</h2>
<p><a href="http://www.bakedon.com/wp-content/uploads/2010/04/Wood-36-Compartment-Mail-Sorter-7766GR_big.jpg"><img class="alignleft size-full wp-image-126" style="float: right;" title="Wood-36-Compartment-Mail-Sorter-(7766GR)_big" src="http://www.bakedon.com/wp-content/uploads/2010/04/Wood-36-Compartment-Mail-Sorter-7766GR_big.jpg" alt="" width="150" height="153" /></a>Now we need to decide what&#8217;s in the message we just got, and what to do with it. You will receive messages not only from your connected clients but also from Lidgren.  Lets cover Lidgren&#8217;s messages first.</p>
<p>Among the several types of messages Lidgren can send you, debug messages are one of the two basic ones you definitely want to handle. They will simply be strings that you can output to the console or debug stream.</p>
<p>Another  message you want to handle is the StatusChanged message. This message tells you when someone has connected or disconnected. I&#8217;ve created another method called HandleMessage which is where I will switch on the message type.</p>
<p>The last of the most basic messages is the Data message. These are messages sent to you from your connected clients.</p>
<p>I would suggest you throw an exception or at least log a warning if a message comes through that you didn&#8217;t handle. I&#8217;ve caught many weird bugs this way simply because I wasn&#8217;t aware I was receiving a BadMessageReceived or other message.</p>
<pre class="brush: csharp; title: ; notranslate">
    public event Action&amp;lt;NetConnection&amp;gt; ConnectionStatus;
    public event Action&amp;lt;NetConnection, NetBuffer&amp;gt; DataReceived;

    private void HandleMessage(NetBuffer buffer, NetMessageType type, NetConnection source)
    {
        switch (type)
        {
            case NetMessageType.DebugMessage:
                {
                    Debug.WriteLine(buffer.ReadString());
                    break;
                }

            case NetMessageType.StatusChanged:
                {
                    if (ConnectionStatus != null)
                        ConnectionStatus(source);
                    break;
                }

            case NetMessageType.Data:
                {
                    if (DataReceived != null)
                        DataReceived(source, buffer);
                    break;
                }

            default:
                throw new NotImplementedException(&amp;quot;Message type not handled: &amp;quot; + type.ToString());
        }
    }
</pre>
<p>You can see in the snippet above on the first two lines, I&#8217;ve declared two public events. Other classes in your server will want to know when a client comes and goes so its wise to publish this information. The other event gets fired when we receive data from clients.</p>
<p>HandleMessage works its magic by simply switching on the message type. In the case of a debug message, we simply write it to the debug stream.  In the case of a ConnectionStatus or Data message, we fire the appropriate events.</p>
<p>OK so here is the code so far:</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Diagnostics;
using Lidgren.Network;

namespace BakedOn
{
    class MyServer
    {
        private NetServer server;
        private NetBuffer buffer;

        public event Action&amp;lt;NetConnection&amp;gt; ConnectionStatus;
        public event Action&amp;lt;NetConnection, NetBuffer&amp;gt; DataReceived;

        public MyServer()
        {
            var config = new NetConfiguration(&amp;quot;hello-world&amp;quot;);
            config.Port = 42948;

            server = new NetServer(config);
            buffer = server.CreateBuffer();
        }

        public void Process()
        {
            NetMessageType type;
            NetConnection source;

            while (server.ReadMessage(buffer, out type, out source))
            {
                HandleMessage(buffer, type, source);
            }
        }

        private void HandleMessage(NetBuffer buffer, NetMessageType type, NetConnection source)
        {
            switch (type)
            {
                case NetMessageType.DebugMessage:
                    {
                        Debug.WriteLine(buffer.ReadString());
                        break;
                    }

                case NetMessageType.StatusChanged:
                    {
                        if (ConnectionStatus != null)
                            ConnectionStatus(source);
                        break;
                    }

                case NetMessageType.Data:
                    {
                        if (DataReceived != null)
                            DataReceived(source, buffer);
                        break;
                    }

                default:
                    throw new NotImplementedException(&amp;quot;Message type not handled: &amp;quot; + type.ToString());
            }
        }
    }
}
</pre>
<p>In the next part, I will show you how to create a client that will work within Unity. It&#8217;s not tough at all. And if you deep-tech readers are concerned what we have done here is far too simplistic, not to worry. We will be going much deeper. After the client, we will start talking about message formats, serialization and message envelopes.</p>
<p>If this topic is useful or interesting to you, shoot me a quick comment and let me know so I&#8217;ll keep going with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bakedon.com/2010/04/how-to-use-lidgren-with-unity3d-for-mmos-part-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Ghostees: Server Technology</title>
		<link>http://www.bakedon.com/2008/07/ghostees-server-technology/</link>
		<comments>http://www.bakedon.com/2008/07/ghostees-server-technology/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 06:07:09 +0000</pubDate>
		<dc:creator>Tricky</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Ghostees]]></category>
		<category><![CDATA[Ghostees server technology]]></category>
		<category><![CDATA[MMORPG]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.bakedon.com/?p=31</guid>
		<description><![CDATA[Ghostees is not just an RPG, it is a proof of concept of the server platform. The server platform is designed so that we can develop additional MMORPG-style games and leverage all that back end technology as much as possible. The server technology is interesting. Our background is primarily in high performance serverdevelopment. We wanted [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Ghostees: Flash MMORPG Virtual World" href="http://www.ghostees.com" target="_blank">Ghostees</a> is not just an RPG, it is a proof of concept of the server platform. The server platform is designed so that we can develop additional MMORPG-style games and leverage all that back end technology as much as possible.<span id="more-31"></span></p>
<p>The server technology is interesting. Our background is primarily in <a title="Second largest DNS server on the planet" href="http://royal.pingdom.com/?p=125" target="_blank">high performance server</a>development. We wanted a design that could scale to support a very large number of players. Who doesn&#8217;t? Server-side tasks are logically divided into services and architected so that they can run and live independently on remote hardware. This way we can distribute the load based on capacities any particular service can handle.</p>
<p>Servers are auto discoverable and communicate with each other efficiently over <a title="UDP Reference" href="http://en.wikipedia.org/wiki/User_Datagram_Protocol" target="_blank">UDP</a>. This is a huge benefit. As servers come on and offline, we don&#8217;t need to do any reconfiguring of dependent servers. They are designed to locate the appropriate service by dropping a service discovery packet on the network. All servers that provide that service respond with server information and current server load. This way the seeking server can choose among servers with more available capacity.</p>
<blockquote><p>Servers are auto discoverable and communicate with each other efficiently over UDP</p></blockquote>
<p>For instance we have a combat service. All it does is create and maintain a combat encounter for a set of entities (players and NPCs). A <a title="Ghostees: Flash MMORPG Virtual World" href="http://www.ghostees.com" target="_blank">Ghostees</a>player initiating combat will cause the player&#8217;s gateway server to request a combat encounter instance on an available combat server. Other players and NPCs can join a combat encounter even if they are <strong>not </strong>on the same gateway.</p>
<p>A gateway is the server a player&#8217;s browser communicates directly with. It takes care of the core <a title="RTMP Reference" href="http://en.wikipedia.org/wiki/Real_Time_Messaging_Protocol" target="_blank">RTMP</a> / <a title="AMF Reference" href="http://en.wikipedia.org/wiki/Action_Message_Format" target="_blank">AMF</a>stuff that Flash communicates with natively. It is also responsible for persisting player information to the database and also for facilitating access to the other servers in the system. It does not do chat, movement, routing or even quests or NPC AI. Those are all different services potentially running elsewhere.</p>
<p style="text-align: center;"><a title="Ghostees: Flash MMORPG Virtual World" href="http://www.ghostees.com" target="_blank"><img class="size-medium wp-image-34 aligncenter" title="Ghostees Flash MMORPG Logo" src="http://www.bakedon.com/wp-content/uploads/2008/07/ghostees_logo_color.jpg" alt="Ghostees Flash MMORPG Logo" width="256" height="128" /></a></p>
<p>A zone server is responsible for simply tracking the entrance/exit and movement of entities. When an entity enters a zone (player or NPC), the zone broadcasts to all connected gateways that they need to subscribe to this new entity. It also sends messages back to the connecting player information about the zone, such as graphic asset information, music, collision layers and other existing entities.</p>
<blockquote><p>To a zone server, an NPC looks just like any other player.</p></blockquote>
<p>On each simulation step the zone server updates the positions of entities it knows about. When a player or NPC requests a route to a new destination, the zone server facilitates the route calculation, obstacle avoidance and potential zone transfers. Once the route is calculated it is sent to the requesting entity. This overrides any predicted route done by the client.  It&#8217;s pretty cool actually!</p>
<p>NPC AI runs on their own gateway server. To a zone server, an NPC looks just like any other player. NPCs can be scripted to request routes and respond to player interactions among other things just like a player.</p>
<p>I think your getting the idea by now. We&#8217;re really enjoying working with this design. It&#8217;s quite flexible and powerful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bakedon.com/2008/07/ghostees-server-technology/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

