<?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>#AltDevBlogADay &#187; Cutch</title>
	<atom:link href="http://www.altdevblogaday.com/author/john-mccutchan/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.altdevblogaday.com</link>
	<description>Each day a little more #gamedev love</description>
	<lastBuildDate>Thu, 17 May 2012 03:06:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Controlling Your Game Engine Over WebSocket</title>
		<link>http://www.altdevblogaday.com/2012/02/01/controlling-your-game-engine-over-websocket/</link>
		<comments>http://www.altdevblogaday.com/2012/02/01/controlling-your-game-engine-over-websocket/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 02:34:27 +0000</pubDate>
		<dc:creator>Cutch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=23594</guid>
		<description><![CDATA[<p>In my previous article I introduced WebSocket and detailed building your own WebSocket server. I also explained that I have embedded a WebSocket server into my engine and use a web application to control, configure and monitor my engine. Check out my previous article for some concrete examples of what I have been doing. In this article I will show you how I do this by explaining the remote procedure call system I designed and implemented.</p>
<p><a href="http://www.altdevblogaday.com/2012/02/01/controlling-your-game-engine-over-websocket/" class="more-link">Read more on Controlling Your Game Engine Over WebSocket&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In my previous article I introduced WebSocket and detailed building your own WebSocket server. I also explained that I have embedded a WebSocket server into my engine and use a web application to control, configure and monitor my engine. Check out my previous article for some concrete examples of what I have been doing. In this article I will show you how I do this by explaining the remote procedure call system I designed and implemented.</p>
<p><strong>RPC</strong></p>
<p>What exactly is a <a href="http://en.wikipedia.org/wiki/Remote_procedure_call">remote procedure call</a> system? Simply put, it’s a function call from one process to another. The processes do not have to be running on the same machine or share the same architecture. To help understand, first consider a local procedure call in C/C++. The function arguments are pushed on to the stack, then the program branches to the address of the function being called. When the called function returns the result is stored on the stack for the calling function to access. This works because the caller, callee and data live in the same address space and agree on calling conventions. In a remote procedure call system, the caller, callee and data do not share an address space and thus can not share data by memory address. In order to make a remote procedure call the parameters must be <a href="http://en.wikipedia.org/wiki/Marshalling_(computer_science)">marshalled</a> for transmission over the network and packed into a message for the remote system. The return value(s) from an RPC are returned in a similar way. Still with me? Don’t worry, I will not spend too much time in the plumbing for this article.</p>
<p>The first step in building a RPC system is to pick the data format that messages will be exchanged in. Since we are talking about the web, naturally, I chose the Java Script Object Notation, better known as <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>, as the data format for my RPC system. JSON is a simple, human readable data format. It supports numbers, strings, booleans, arrays and key value maps. Here is an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;type&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;command&quot;</span>
  <span style="color: #3366CC;">&quot;command&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Memory.Stats&quot;</span>
  <span style="color: #3366CC;">&quot;id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;50&quot;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Another benefit of JSON is that web browsers natively support it. Your browser can convert almost any Javascript data structure into JSON with a single function call. You can also go the other way, from JSON to a native Javascript data structure just as easily.</p>
<p>Now that the data interchange format has been settled, we need to come up with the RPC message framing. That is, the mandatory portion of the RPC message. Each RPC is a JSON map with two required keys, the message type and the message serial number.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  “type” <span style="color: #339933;">:</span> “”
  “id” <span style="color: #339933;">:</span> “”
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The type field is used to indicate the type of message. My system supports three types, “command”, “result” and “report”. I will explain these shortly. But first, the id field is used to connect related messages together. For example, this command has an id of “4”:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> “type” <span style="color: #339933;">:</span> “command”<span style="color: #339933;">,</span> “id” <span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span>”<span style="color: #339933;">,</span> … <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The result of the command sent back from the engine also has an id of “4” allowing to easily connect calls with their return values.</p>
<p>Getting back to the type field:</p>
<ul>
<li>“<strong>command</strong>” : Commands initiate some sort of action or state change.</li>
<li>“<strong>result</strong>” : Result of a command. Linked by the id field. Commands are not required to reply with a result.</li>
<li>“<strong>report</strong>” : A regular, repeating message from a subscription. Clients can subscribe to a data stream and set an interval between reports.</li>
</ul>
<p>The other, non-mandatory, fields in a message are type specific.</p>
<p><strong>Examples</strong></p>
<p>Some example commands that my system supports:</p>
<ul>
<li>“Echo” &#8211; Replies with a copy of the “message” field.</li>
<li>“Subscribe” &#8211; Subscribes the caller the “reporter” field.</li>
<li>“Unsubscribe” &#8211; Unsubscribes the caller from the “reporter” field.</li>
<li>“Memory.Stats” &#8211; Replies with the state of all registered allocators.</li>
<li>“Config.Set” &#8211; Sets “variable_name” to “variable_value”.</li>
<li>“Config.Delete” &#8211; Removes “variable_name” from the configuration variable set.</li>
<li>“Config.Get” &#8211; Returns the value of “variable_name”.</li>
<li>“Config.Dump” &#8211; Returns all registered configuration variables.</li>
<li>“Object.Create” &#8211; Creates a game object</li>
<li>“Object.Set” &#8211; Sets “property_name” to “value”. “render_model” is an example property.</li>
<li>“Message” &#8211; Sends a message to another connected client.</li>
</ul>
<p>I have two reporters:</p>
<ul>
<li>“Memory” &#8211; Regularly reports the state of all registered allocators.</li>
<li>“Config” &#8211; Sends configuration variable change notifications.</li>
</ul>
<p>Here is a screenshot of a live graph of memory allocators:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2012/02/AllocatorTrace.png"><img class="aligncenter size-full wp-image-23597" src="http://altdevblogaday.com/wp-content/uploads/2012/02/AllocatorTrace.png" alt="" width="1377" height="486" /></a></p>
<p><strong>Quake Style Console</strong></p>
<p>What developer interface would be complete without a quake style console triggered by the ~ key? For this, I used <a href="http://terminal.jcubic.pl/">JQuery Terminal</a>. It is a great library which implements a command terminal. Adding custom commands to the terminal window is really simple. I have also made it possible for the user to construct messages by hand from within the terminal, making the terminal window very powerful. The following screenshot shows a user sending a custom Echo message by using the “dcc” terminal command. It also shows the response from the server.</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2012/02/CommandConsole.png"><img class="aligncenter size-full wp-image-23598" src="http://altdevblogaday.com/wp-content/uploads/2012/02/CommandConsole.png" alt="" width="637" height="431" /></a></p>
<p><strong>C++ implementation notes</strong></p>
<p><strong>Connection Management</strong></p>
<p>I started with the explicit goal of supporting multiple connected clients. My previous article discussed the importance of decoupling the code that waits for a connection over TCP from the code that manages a WebSocket connection. Because of that decoupling, supporting multiple connections was practically free.</p>
<p><strong>Commanders</strong></p>
<p>Support for commands is added by implementing the commander interface:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> doCommandCenterCommanderInterface <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  doCommandCenterCommanderInterface<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">virtual</span> ~doCommandCenterCommanderInterface<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> CommanderName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">bool</span> CanProcessCommand<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> doCommandCenterCommandPacket<span style="color: #000040;">*</span> packet<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> ProcessCommand<span style="color: #008000;">&#40;</span>doCommandCenterConnection<span style="color: #000040;">*</span> connection, <span style="color: #0000ff;">const</span> doCommandCenterCommandPacket<span style="color: #000040;">*</span> packet<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>A commander can process one or many commands (CanProcessCommand). Each commander is registered with the central command center, which does the routing of messages from connected clients to the appropriate commander. doCommandCenterCommandPacket just contains a parsed JSON object and doCommandCenterConnection has the WebSocket connection and various buffers in it.</p>
<p><strong>Reporters</strong></p>
<p>Support for reports is added by implementing the reporter interface:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> doCommandCenterReporterInterface <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  doCommandCenterReporterInterface<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">virtual</span> ~doCommandCenterReporterInterface<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">void</span> Subscribe<span style="color: #008000;">&#40;</span>doCommandCenterConnection<span style="color: #000040;">*</span> connection<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> Unsubscribe<span style="color: #008000;">&#40;</span>doCommandCenterConnection<span style="color: #000040;">*</span> connection<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">bool</span> ShouldRefresh<span style="color: #008000;">&#40;</span>palTimerTick tick<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> ChangeRefreshDelay<span style="color: #008000;">&#40;</span>doCommandCenterConnection<span style="color: #000040;">*</span> connection, palTimerTick delay<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> ReporterName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// Update internal state</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Refresh<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// Report state to all subscribed connections</span>
  <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Report<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Each reporter is responsible for generating a single type of report. Similar to commanders, reporters are registered in the central command center.</p>
<p>Client commands to subscribe and unsubscribe are processed by a commander, like all other commands.</p>
<p><strong>Javascript implementation notes</strong></p>
<p><strong>Connection</strong></p>
<p>My web application has a communication class ‘Connection’. The interesting methods are:</p>
<p><em>FireAndForget</em>(command) &#8211; Send a command. No callback function is registered.<br />
<em>Fire</em>(command, callback) &#8211; Send a command. Register a callback to be called when the result arrives.<br />
<em>Subscribe</em>(reporter_name, report_callback) &#8211; Subscribes the client to the reporter. Registers a callback to be called whenever a report comes in.<br />
<em>Unsubscribe</em>(reporter_name) &#8211; Unsubscribes the client to the reporter. Unregister report callback.</p>
<p><strong>Message Processing</strong></p>
<p>When a message arrives the behaviour depends on the type of message. If it is a result and the caller registered a callback, the message is passed to the callback and the callback is removed from the callback table. If it is a report the report callback is called.</p>
<p><strong>Modules</strong></p>
<p>My web application consists of many modules. Each module covers a set of related tasks. For example, the Memory module can query for all open allocations and updates a live graph of memory used by each allocator. Each of the modules registers subscriptions and command callbacks with the connection to update itself when new messages arrive. Each module is given an HTML</p>
<div>
<p>element to render itself into.</p>
<p><strong>UI</strong></p>
<p>As you have already guessed, the UI for my web application is written with the help of <a href="http://jquery.com/">JQuery</a>. A couples months ago I only had a vague idea of what JQuery is, but after using it I say- it is awesome and makes working with the DOM a cinch.</p>
<p><strong>Why a web application?</strong></p>
<p>There are many advantages to building development tools as a web application in concert with an actual instance of a game. First of all, deployment is trivial. Rolling out updated tools to the team happens transparently. Tools are platform independent running on Mac, Linux, Windows and even smart phones. Separating the tools UI from an actual game engine will force a less coupled design and require a clean design on how the tools communicate with the engine. Tools UI can be used with multiple games, so long as they agree on the RPC mechanism and a common set of commands. Forcing the tools to communicate with the engine over the network allows for a developer to connect to an instance of the engine from anywhere, view the output, and make changes at run-time. When working with console development, the game is not running on your computer and interacting with it may be cumbersome. Moving the UI into the web browser solves the problem. Imagine showing a colleague the latest feature you have implemented, but she is all the way across the building. With a remote viewer that runs in a web browser, she can just connect to the engine running on your development kit and see what is going on. Finally, as I have discovered, modern web development is a great platform for developing applications. Did you know that Chrome has a debugger, profiler and other tools built in? HTML5, WebGL, WebSocket, and Canvas make rich web applications possible. These are just some of the advantages to using a web application.</p>
<p><strong>Conclusion</strong></p>
<p>In order to control my game engine with a web application I chose to use WebSocket as the medium. I built an RPC system that supports commands and subscriptions to updates using JSON as the data interchange . I chose JSON because it is the native web data structure. The UI was built with JQuery and some off the shelf JQuery libraries. My engine offers a rich set of commands and allows for multiple simultaneous connections. I am very happy with this system because it is really easy to add new commands and reporters on the C++ side and Javascript is so easy it feels criminal. Next week I will be back with my final article in this series explaining how I was able to add simple streaming video to my game engine. Below is a screenshot of a screenshot of my game engine:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2012/02/Screenshot.png"><img class="aligncenter size-full wp-image-23599" src="http://altdevblogaday.com/wp-content/uploads/2012/02/Screenshot.png" alt="" width="995" height="707" /></a></p>
<p>Don’t forget that I am giving a related talk at AltDevConf on Saturday, February 11th at 14:00 PST. I hope you check it out.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2012/02/01/controlling-your-game-engine-over-websocket/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Your Own WebSocket Server</title>
		<link>http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/</link>
		<comments>http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 06:05:39 +0000</pubDate>
		<dc:creator>Cutch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=23340</guid>
		<description><![CDATA[<p>The WebSocket protocol has applications beyond plain vanilla web development.  I will explain how the protocol works, how to implement your own server and share some insights I had along the way. Before we get down and dirty, I will explain what I’ve been doing with it.</p>
<p><a href="http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/" class="more-link">Read more on Writing Your Own WebSocket Server&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The WebSocket protocol has applications beyond plain vanilla web development.  I will explain how the protocol works, how to implement your own server and share some insights I had along the way. Before we get down and dirty, I will explain what I’ve been doing with it.</p>
<p>At this point I expect many of you are saying “I’m not working on a web game this doesn’t seem relevant to me.” Well, neither am I. I embed a WebSocket server into my game engine and with a local web application use the WebSocket protocol as a medium to control, configure and monitor my game engine. Some concrete examples of what I’ve done so far:<strong><strong><br />
</strong></strong></p>
<ul>
<li>Monitor memory allocation statistics</li>
<li>Monitor performance of subsystems</li>
<li>Set and query configuration variables</li>
<li>Live edit the world</li>
<li>Loaded asset preview</li>
</ul>
<p>Soon, I will attempt to stream the display of the game to the web browser and stream mouse,  as well as the keyboard data back to the game. In other words, remote desktop for the game engine. Another use case I would like to investigate is writing unit tests for the engine in javascript and drive it from the web browser. The possibilities are endless.</p>
<p>Another benefit of this approach is that your development UI is platform independent. This is nice when you are developing a title against many architectures. For example, consoles where the target does not usually have keyboard or mouse input are harder to interact with- by moving your UI to the web browser this problem is avoided.</p>
<p>Hang in there, this article is about the plumbing. My next article will be about the above applications.</p>
<p>WebSocket is a communication protocol that allows for bi-directional text and binary message passing. The client is a Javascript application running inside a web browser and, typically, the server is a web server. I say ‘typically’ because that is not how I use it.</p>
<p>WebSocket was developed because sending data between the web server and web application over HTTP was inefficient. The WebSocket protocol is very bandwidth efficient (message framing is at most 14 bytes) and the payloads are custom to the application. A WebSocket connection begins life as a regular HTTP connection. The connection is upgraded from HTTP to WebSocket. This upgrade is one way- you can’t revert back to an HTTP connection. You can read more about WebSocket at <a href="http://en.wikipedia.org/wiki/WebSocket">WikiPedia</a> and the complete specification is <a href="http://tools.ietf.org/html/rfc6455">available</a>. The WebSocket protocol was only recently finalized in December 2011.</p>
<p>Okay, let’s dive into how WebSocket works. I will cover creating a connection, sending and receiving messages, and responding to pings.</p>
<div><strong>Connecting</strong></div>
<div></div>
<div>
<p>Creating a WebSocket connection is initiated by the client sending the following upgrade request:<span style="text-align: left"> </span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="http" style="font-family:monospace;">       GET /servicename HTTP/1.1
       Host: server.example.com
       Upgrade: websocket
       Connection: Upgrade
       Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
       Origin: http://example.com</pre></td></tr></table></div>

<p>The server responds with:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="http" style="font-family:monospace;">       HTTP/1.1 101 Switching Protocols
       Upgrade: websocket
       Connection: Upgrade
       Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=</pre></td></tr></table></div>

<p>Most of these HTTP fields are self explanatory but not <span style="color: #ff0000">Sec-WebSocket-Key</span> and <span style="color: #339966">Sec-WebSocket-Accept</span>. <span style="color: #ff0000">Sec-WebSocket-Key</span> is a string sent by the client as a challenge to the server. This leads to the question- how does the server calculate the value of Sec-WebSocket-Accept and complete the challenge? It is quite simple. The server first takes <span style="color: #ff0000">Sec-WebSocket-Key</span> and concatenates it with a GUID string from the WebSocket specification. Then the SHA-1 hash of the resulting string is computed and, finally, <span style="color: #339966">Sec-WebSocket-Accept</span> is the base64 encoding of the hash value. Let’s work through an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">  SpecifcationGUID <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&quot;</span><span style="color: #339933;">;</span>
  FullWebSocketKey <span style="color: #339933;">=</span> concatenate<span style="color: #009900;">&#40;</span>Sec<span style="color: #339933;">-</span>WebSocket<span style="color: #339933;">-</span>Key<span style="color: #339933;">,</span> SpecifcationGUID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11</span>
  KeyHash <span style="color: #339933;">=</span> SHA<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#40;</span>FullWebSocketKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 0x46 0x06 0xcf 0x38 0x59 0x45 0xb2 0xbe 0xc4 0xea</span>
  Sec<span style="color: #339933;">-</span>Websocket<span style="color: #339933;">-</span>Accept <span style="color: #339933;">=</span> base64<span style="color: #009900;">&#40;</span>KeyHash<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// s3pPLMBiTxaQ9kYGzzhZRbK+xOo=</span></pre></td></tr></table></div>

</div>
<div>
<div><strong>Transmission</strong></div>
<div></div>
<div>
<p>WebSocket is a message based protocol. Each message begins with a header defining the length of the message, the type (text, binary or control) and other meta-data. The payload immediately follows the header. All incoming messages will include a 32-bit mask which must be applied to the entire payload with a XOR operation. Each message will have a different mask. The masking is used to guard against simple snooping.</p>
<p>The header begins with a 16-bit mask (blue) and up to 12-bytes of optional header (orange).</p>
</div>
<div></div>
<div><a href="http://altdevblogaday.com/wp-content/uploads/2012/01/image00.png"><img class="aligncenter size-full wp-image-23342" src="http://altdevblogaday.com/wp-content/uploads/2012/01/image00.png" alt="" width="183" height="531" /></a></div>
</div>
<div></div>
<div>
<div></div>
<div>The header mask indicates whether this is the final fragment of a message (messages can be split into fragments), the op-code, and whether a mask is present. The payload length field plays double duty. For small messages (less than 125 bytes) it is the length of the message, but for messages that are longer, the payload length is used as a flag to indicate how large the extended payload length field is. The extended payload length follows immediately after the first 16-bits of the header (it comes before the mask). When payload length is equal to 126, the extended payload length is 16-bits and when it is equal to 127 the extended payload length is 64-bits.WebSocket op-codes are split into three categories: continuation, non-control and control. Continuation and non-control op-codes indicate user messages and control frames are used to configure the protocol itself. Presently the following op-codes are defined:</div>
</div>
<div></div>
<div>
<div dir="ltr">
<table>
<col width="84" />
<col width="540" />
<tbody>
<tr>
<td><strong>Op-code</strong></td>
<td><strong>Meaning</strong></td>
</tr>
<tr>
<td>0&#215;0</td>
<td>Message continuation [continuation]</td>
</tr>
<tr>
<td>0&#215;1</td>
<td>Text message [non-control]</td>
</tr>
<tr>
<td>0&#215;2</td>
<td>Binary message [non-control]</td>
</tr>
<tr>
<td>0&#215;8</td>
<td>Connection Close [control]</td>
</tr>
<tr>
<td>0&#215;9</td>
<td>Ping [control]</td>
</tr>
<tr>
<td>0xA</td>
<td>Pong [control]</td>
</tr>
</tbody>
</table>
</div>
</div>
<div dir="ltr">
<div></div>
<div>Once you have parsed the header, extracting the payload is trivial. Do not forget to XOR in the mask. Parsing the header is made interesting by the fact that its size and layout is variable and thus cannot be mapped directly to a C structure. Or can it?</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> WebSocketMessageHeader <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">union</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">struct</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> OP_CODE <span style="color: #008080;">:</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> RSV1 <span style="color: #008080;">:</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> RSV2 <span style="color: #008080;">:</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> RSV3 <span style="color: #008080;">:</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> FIN <span style="color: #008080;">:</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> PAYLOAD <span style="color: #008080;">:</span> <span style="color: #0000dd;">7</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> MASK <span style="color: #008080;">:</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> bits<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">uint16_t</span> short_header<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">size_t</span> GetMessageLength<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">size_t</span> GetPayloadOffset<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">size_t</span> GetPayloadLength<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">uint32_t</span> GetMask<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">uint8_t</span> GetOpCode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">bool</span> IsFinal<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">bool</span> IsMasked<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #666666;">// …</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>A <strong>WebSocketMessageHeader</strong> will always be at least 16-bits long, so the only data element defined inside the struct is <span style="color: #0000ff">short_header</span>. Accessing the mask, extended payload lengths, or the payload is done with an offset from &amp;<span style="color: #0000ff">short_header</span>. When I want to parse a header, I simply do this:</p>
<div><strong>WebSocketMessageHeader</strong>* header = &amp;incoming_buffer[read_index];</div>
<div></div>
<div></div>
</p>
<div>
<p>I found this to be a very clean approach and is generally useful when dealing with structures that do not have a fixed length or layout.</p></div>
<div>
<p>Messages can be split into multiple fragments. When this happens the FINAL-FRAGMENT bit will be zero until the final fragment of the message. The first fragment will have the op-code indicating either a text (<strong>0&#215;1</strong>) or binary (<strong>0&#215;2</strong>) message and the rest of the fragments will have the op-code of continuation (<strong>0&#215;0</strong>).</p>
<p><strong>Ping Pong</strong></p>
<p>The protocol supports ping (<strong>0&#215;9</strong>) and pong (<strong>0xA</strong>) messages. When a ping message has a payload, the resulting pong message must have an identical payload. You are only required to pong the most recent ping if more than one arrive.</p>
<p><strong>Server Design</strong></p>
<p>Finally, I want to describe the high level design of my WebSocket server. My server uses three buffers. One buffer for incoming WebSocket data, one for outgoing WebSocket data and one to store fully parsed incoming messages. An outline of the API:</p>
</div>
<div>
<div>
<div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> WebSocketServer <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  WebSocketServer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> AcceptConnection<span style="color: #008000;">&#40;</span>TcpListener<span style="color: #000040;">*</span> listener<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> CloseConnection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> Update<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">int</span> SendTextMessage<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> msg<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> SendTextMessage<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> msg, <span style="color: #0000ff;">size_t</span> msg_length<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">uint64_t</span> PendingMessageCount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> ProcessMessages<span style="color: #008000;">&#40;</span>OnMessageDelegate del, <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> userdata<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> ClearMessages<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>NOTE: I’ve trimmed a bunch of trivial methods from the outline and only left the ones worth discussing.</p>
</div>
<div>
<div><strong>Connection Handling</strong></div>
<div>
</div>
<div>It is important to decouple listening for a connection over TCP from the WebSocket server itself. Each instance of WebSocketServer is responsible for only one client. This keeps the code and resource allocation simple. A higher level system should manage multiple connection requests and multiple live WebSocket connections.</div>
<div>
<p><strong>Updating</strong></p>
<p>My WebSocket server has a single Update method. This method pumps the connection, it is responsible for sending any pending messages, receiving any new messages (ultimately moving them to the message buffer), and updating status flags (connection opened, connection closed, connection error).</p>
<p><strong>Message Processing</strong></p>
<p>Complete incoming messages are stored in their own buffer. When the engine system is ready to process incoming messages, a call to ProcessMessages is made and a delegate function is passed in. The WebSocketServer will iterate over all messages in the buffer and call this delegate for each one. When the engine is done with the messages they must be cleared by calling ClearMessages.</p>
<p><strong>Conclusion</strong></p>
<p>Hopefully, you are still with me and have a clear grasp on how WebSocket protocol works and how I designed my WebSocket server. In my next article, I will take this a step further- using WebSocket inside my engine as a remote procedure call medium and controlling my engine using a web browser. Next month I will be speaking at AltDevConf on this subject. Hope to see you there.</p>
</div>
</div>
</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intrusive Lists</title>
		<link>http://www.altdevblogaday.com/2011/12/06/intrusive-lists/</link>
		<comments>http://www.altdevblogaday.com/2011/12/06/intrusive-lists/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 15:35:35 +0000</pubDate>
		<dc:creator>Cutch</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[data oriented programming]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[intrusive linked lists]]></category>
		<category><![CDATA[intrusive list]]></category>
		<category><![CDATA[IntrusiveListNode]]></category>
		<category><![CDATA[linked lists]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=21139</guid>
		<description><![CDATA[<p>I will finish off my series of articles on data oriented programming by discussing the linked list. I will detail the linked list implementation I use, the intrusive linked list, which I first encountered many years ago hacking on the Linux kernel. The intrusive linked list is the data oriented version of the linked list container. But, before I detail intrusive linked lists, I will detail the typical linked list, the external linked list, so I can easily point out it&#8217;s limitations compared to the intrusive linked list.</p>
<p><a href="http://www.altdevblogaday.com/2011/12/06/intrusive-lists/" class="more-link">Read more on Intrusive Lists&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I will finish off my series of articles on data oriented programming by discussing the linked list. I will detail the linked list implementation I use, the intrusive linked list, which I first encountered many years ago hacking on the Linux kernel. The intrusive linked list is the data oriented version of the linked list container. But, before I detail intrusive linked lists, I will detail the typical linked list, the external linked list, so I can easily point out it&#8217;s limitations compared to the intrusive linked list.</p>
<p>The external linked list of type T looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
  <span style="color: #0000ff;">struct</span> ListNode <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">struct</span> ListNode<span style="color: #000040;">*</span> next<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">struct</span> ListNode<span style="color: #000040;">*</span> prev<span style="color: #008080;">;</span>
  T node_data<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> List <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">struct</span> ListNode<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span> root<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This is straight forward enough. The node contains a pointer to the next node, the previous node and memory for a single instance of T. The list itself contains the root node. I trust we have all implemented this linked list, at school or elsewhere.</p>
<p>I hope regular readers will notice this linked list couples memory allocation with algorithms, a pet peeve of mine. Whenever an instance of T is added to the list the following happens:</p>
<ol>
<li>A new node is allocated</li>
<li>A copy of the instance being added to the list is made and stored in node_data</li>
<li>The list surgery is performed</li>
</ol>
<p>Depending on the type of T the copy could be very expensive.</p>
<p>Another potential inefficiency is that root.next and root.prev can be NULL when the list is empty. Making the code to implement adding and removing nodes from the list unnecessarily branchy.</p>
<p>Also, depending on the implementation the node_data memory in root is wasted or we make the code even more complex. The complexity comes from handling special cases, for example, are we adding the first node to the list, in which case put the result in root.node_data otherwise, allocate a new node and store the copy in it. Ugh.</p>
<p>Okay, enough build up. Let me introduce the intrusive linked list:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> IntrusiveListNode <span style="color: #008000;">&#123;</span>
  IntrusiveListNode<span style="color: #000040;">*</span> next<span style="color: #008080;">;</span>
  IntrusiveListNode<span style="color: #000040;">*</span> prev<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> IntrusiveList <span style="color: #008000;">&#123;</span>
  IntrusiveListNode root<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Note that there is no mention of the type that the list stores. This probably seems strange at first. In particular, there is no memory to store the actual data item being stored in the list.</p>
<p>Intrusive linked lists flip the memory layout inside out. Instead of the list node providing memory for a T, T provides memory for a list node. The &#8216;intrusive&#8217; part of the name comes from the fact that we store the list node inside the type T.</p>
<p>Another thing to note is that the list is circular. Upon creation this happens:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">IntrusiveList <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// circular</span>
  root.<span style="color: #007788;">next</span> <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>root<span style="color: #008080;">;</span>
  root.<span style="color: #007788;">prev</span> <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>root<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The circular nature of the list makes inserting and removing nodes simple and branch free.  Add and Remove look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Add node between next and previous</span>
<span style="color: #0000ff;">void</span> Add<span style="color: #008000;">&#40;</span>palIListNode<span style="color: #000040;">*</span> node, palIListNode<span style="color: #000040;">*</span> prev, palIListNode<span style="color: #000040;">*</span> next<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>next <span style="color: #000080;">=</span> next<span style="color: #008080;">;</span>
  node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>prev <span style="color: #000080;">=</span> prev<span style="color: #008080;">;</span>
  next<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>prev <span style="color: #000080;">=</span> node<span style="color: #008080;">;</span>
  prev<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>next <span style="color: #000080;">=</span> node<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Remove node</span>
<span style="color: #0000ff;">void</span> Remove<span style="color: #008000;">&#40;</span>palIListNode<span style="color: #000040;">*</span> node<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>next<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>prev <span style="color: #000080;">=</span> node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>prev<span style="color: #008080;">;</span>
  node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>prev<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>next <span style="color: #000080;">=</span> node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>next<span style="color: #008080;">;</span>
  node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>next <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
  node<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>prev <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
The API looks like <span style="color: #0000dd;">this</span><span style="color: #008080;">:</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Add to head of list</span>
<span style="color: #0000ff;">void</span> AddHead<span style="color: #008000;">&#40;</span>IntrusiveListNode <span style="color: #000040;">*</span> node<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  Add<span style="color: #008000;">&#40;</span>node, <span style="color: #000040;">&amp;</span>root_, root_.<span style="color: #007788;">next</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Again, note that this list does not know or care what type T the IntrusiveListNode is stored in. Or that the IntrusiveListNode is even stored in a type that C/C++ is aware of- it can work inside a blob easily.</p>
<p>At this point, at least one of you should be pointing out that Boost already has an intrusive linked list container type available.  Great.  Look everyone, it&#8217;s that guy. First of all, stop using Boost. That is only half serious. But, I don’t use it. It is just that the intrusive linked list that I am detailing is better than the Boost implementation because in my implementation, IntrusiveListNode, is just plain old data, allowing many of them inside a T. Unfortunately, the Boost implementation relies on T inheriting from the Boost intrusive linked list node type.</p>
<p>Time for an example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> GameObject <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">char</span> name<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">32</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> health<span style="color: #008080;">;</span>
  IntrusiveListNode render_list_node<span style="color: #008080;">;</span>
  IntrusiveListNode update_list_node<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
GameObject monkey<span style="color: #008080;">;</span>
GameObject crate<span style="color: #008080;">;</span>
GameObject banana<span style="color: #008080;">;</span>
IntrusiveList render_list<span style="color: #008080;">;</span></pre></div></div>

<p>To add our game objects to the render list you might write this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Render_list.<span style="color: #007788;">AddHead</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>crate.<span style="color: #007788;">render_list_node</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
Render_list.<span style="color: #007788;">AddHead</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>monkey.<span style="color: #007788;">render_list_node</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
Render_list.<span style="color: #007788;">AddHead</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>banana.<span style="color: #007788;">render_list_node</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Let us take a moment to look at how an intrusive list looks in memory:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/12/IntrusiveList.jpg"><img class="aligncenter size-full wp-image-21140" src="http://altdevblogaday.com/wp-content/uploads/2011/12/IntrusiveList.jpg" alt="" width="765" height="494" /></a></p>
<p>After looking at this diagram, you should note that as you walk render_list you don’t actually point at a GameObject, but an IntrusiveList item. So, how do you recover the address of the GameObject from the update_list_node pointer that is actually used in the list? It is quite simple, you just subtract the offset from the address. Yes, you can do that. You get the offset with offsetof:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">size_t</span> <span style="color: #0000dd;">offsetof</span><span style="color: #008000;">&#40;</span>type, member<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>What is that you say? offsetof is only valid for plain old data? You cannot use it when you have diamond virtual inheritance? Uh oh, that guy is back. Yes, you are correct. But, luckily no sane programmer would ever use such a thing.</p>
<p>This might sound tedious to work with. Having to walk the list using IntrusiveListNode pointers and apply the offset when you want the data is definitely not as nice as the standard linked list implementation. To solve this, you create a list node iteration helper:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T, <span style="color: #0000ff;">size_t</span> offset<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> IntrusiveListForeach <span style="color: #008000;">&#123;</span>
  …
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Aside from the expected, Next() and Prev() functions for moving along the list, the GetListEntry() function applies the negative offset and returns a pointer to data associated with the list. IntrusiveListForeach also has a Remove method that removes the node safely:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Remove<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  palIListNode<span style="color: #000040;">*</span> safe_next <span style="color: #000080;">=</span> current<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>next<span style="color: #008080;">;</span>
  list_<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Remove<span style="color: #008000;">&#40;</span>current<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  current <span style="color: #000080;">=</span> safe_next<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Compared to the external linked list, the intrusive linked list decouples memory allocation from the container itself. Meaning that to add something to the list, you do not necessarily need to perform an allocation. A side effect of this decoupling is that the linked list container never copies the data by calling the copy constructor. This will avoid many unnecessary copies. You could even add some static data to a list without creating a copy at run time. Another benefit of the intrusive list is that the list node and the surrounding data are guaranteed to be share a cache line, you can even place it strategically! The circular implementation allows the core list manipulation routines to be implemented without branches and very little code (Note: circular list implementations are not restricted to intrusive linked lists). Unlike the Boost intrusive linked list, this implementation does not use inheritance.</p>
<p>A downside of this approach, as discussed in my previous article is that the debugger is not very helpful at displaying this structure out of the box. If you’re going to spend the time implementing your own container structures, it is very little extra work to teach your debugger about them.</p>
<p>I have both an intrusive and external in my container library. I have not done this, but,  it should be possible to implement an external implementation on top of the intrusive.</p>
<p>I should note that I rarely use linked lists anymore. Dynamically sized arrays are almost always more efficient because they are packed tightly.  In cases where you need to add/remove items from arbitrary locations and keep the order (stability), a linked list is still a good bet.</p>
<p>What do your linked list look like? Does it decouple memory allocations from the container work itself? Can it work inside a blob? I am always interested in hearing about your ideas.  Yes, even if you are that guy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/12/06/intrusive-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A blob array</title>
		<link>http://www.altdevblogaday.com/2011/10/25/a-blob-array/</link>
		<comments>http://www.altdevblogaday.com/2011/10/25/a-blob-array/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 13:26:49 +0000</pubDate>
		<dc:creator>Cutch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=19190</guid>
		<description><![CDATA[<p>After reading my last <a href="http://www.johnmccutchan.com/2011/10/programmer-and-his-blobs.html">article</a>, some readers asked for an example of a structure that lives inside a blob. For this article, I thought I would share my blob array (std::vector equivalent). It is, internally, a departure from every other array class I’ve ever read. Also, my blob array demonstrates the utility of decoupling memory allocation from algorithms, which I touched on near the end of my previous <a href="http://altdevblogaday.com/2011/10/11/a-programmer-and-his-blobs/">article</a>.</p>
<p><a href="http://www.altdevblogaday.com/2011/10/25/a-blob-array/" class="more-link">Read more on A blob array&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>After reading my last <a href="http://www.johnmccutchan.com/2011/10/programmer-and-his-blobs.html">article</a>, some readers asked for an example of a structure that lives inside a blob. For this article, I thought I would share my blob array (std::vector equivalent). It is, internally, a departure from every other array class I’ve ever read. Also, my blob array demonstrates the utility of decoupling memory allocation from algorithms, which I touched on near the end of my previous <a href="http://altdevblogaday.com/2011/10/11/a-programmer-and-his-blobs/">article</a>.</p>
<p>Before presenting my blob array, I will present my foil, the typical dynamic array:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> dynamicArray <span style="color: #008000;">&#123;</span>
  T<span style="color: #000040;">*</span> buffer<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">size_t</span> capacity<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">size_t</span> size<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Pretty straight forward, right? You have a contiguous chunk of <em>T</em>s in <em>buffer</em>, of which [0, <em>size</em>) are used. The <em>buffer</em> has room for <em>capacity</em> elements. When elements are added and <em>size</em> becomes equal to <em>capacity</em>, the <em>capacity</em> is increased and the old <em>buffer</em> is copied into the new larger <em>buffer</em>’ before being deallocated. Text book.</p>
<p>The keen reader won’t be satisfied with this array. First of all, it has memory allocation and algorithms coupled together. What if you want to use a static buffer to store the contents of the array? Sure, the capacity is fixed when using a static buffer but the algorithms for working with the array haven’t changed and thus shouldn’t be tied to the memory management scheme. Also, what if you know that you only ever need 8-bits to store the capacity and size information instead of 64-bits (the size of size_t on a 64-bit OS)? What if you want to serialize this in and out of a file? This dynamic array is starting to look tired.</p>
<p>There’s gotta be a better array.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T, <span style="color: #0000ff;">typename</span> INDEX_TYPE <span style="color: #000080;">=</span> <span style="color: #0000ff;">int32_t</span><span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> blobArray <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">uintptr_t</span> memory<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>That’s it. Where is the capacity, size and contents? As usual with blob structures, you will have to look inside the blob to gain insight:</p>
<p><a href="http://altdevblogaday.com/wp-content/uploads/2011/10/BlobArray.png"><img class="aligncenter" src="http://altdevblogaday.com/wp-content/uploads/2011/10/BlobArray.png" alt="" /></a></p>
<p>&nbsp;</p>
<p>Assuming INDEX_TYPE is int32_t, capacity is offset 0 bytes into the blob, size is offset 4 bytes into the blob and contents is offset 8 bytes into the blob.</p>
<p>blobArray has an Initialization method which takes a memory address and a size. The type of contents is specified as a template parameter, so Initialize just has to zero out size and set capacity using the following formula:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">capacity <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>blob_size <span style="color: #000040;">-</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>INDEX_TYPE<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Note: I’ve simplified the above formula by removing alignment considerations.</p>
<p>Compared to my foil, my blobArray is looking good. Most importantly, it has decoupled memory allocation from the algorithms used to manage a dynamic sized array. Put another way, it works beautifully with static buffers. The type of the index is parametrized, allowing for the most compact storage. Because the entire state is encoded into one contiguous piece of memory, serializing is free, you can write the entire array to a file and read it back atomically.</p>
<p>Keen readers should take this opportunity to point out that a blobArray couldn’t possibly expand it’s capacity.</p>
<p>Don’t worry, I’m with you. Enter dynamicBlobArray:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T, <span style="color: #0000ff;">typename</span> INDEX_TYPE <span style="color: #000080;">=</span> <span style="color: #0000ff;">int32_t</span><span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> dynamicBlobArray <span style="color: #008000;">&#123;</span>
  blobArray<span style="color: #000080;">&lt;</span>T, INDEX_TYPE<span style="color: #000080;">&gt;</span> blob_array<span style="color: #008080;">;</span>
  allocatorInterface<span style="color: #000040;">*</span> allocator<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This class couples the algorithms offered by blobArray with a memory allocator. Allowing the capacity to increase when needed. dynamicBlobArray exposes the same interface that blobArray exposes by forwarding function calls, example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">T<span style="color: #000040;">*</span> dynamicBlobArray<span style="color: #008080;">::</span><span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> blob_array.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Both of these template classes will be implemented in header files, allowing the compiler to inline the function forwarding. Of course, dynamicBlobArray will special case methods that add elements to the array, example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> dynamicBlobArray<span style="color: #008080;">::</span><span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>IsFull<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    ExpandCapacity<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  _blob_array.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Hopefully, no one thought I was going to make push_back a virtual method, use inheritance and override it in dynamicBlobArray. Ick.</p>
<p>Both blobArray and dynamicblobArray expose a super-set of std::vector methods. Making them transparent replacements for an existing code base.</p>
<p>There is one down side to using this blob structure: the debugger cannot automatically display state information for these arrays. To get around this, you need to extend the debugger so that it can visualize the internal state hidden in the blob. You can extend Visual Studio by adding custom visualizers to autoexp.dat.</p>
<p>This container has worked well for me. I get both static and dynamic capacity arrays with just one implementation. Also, easy serialization and compact storage. How have you implemented yours?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/10/25/a-blob-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Programmer and His Blobs</title>
		<link>http://www.altdevblogaday.com/2011/10/11/a-programmer-and-his-blobs/</link>
		<comments>http://www.altdevblogaday.com/2011/10/11/a-programmer-and-his-blobs/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 13:25:06 +0000</pubDate>
		<dc:creator>Cutch</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://altdevblogaday.com/?p=18453</guid>
		<description><![CDATA[<p>As I moved further towards programming in a data oriented fashion, I found that I kept needing a ‘blob’ structure. I got sick of writing pointer offset code and casting void pointers to the correct type. Currently, I have four blob abstractions in my engine and I thought I would share them and the motivation behind each one.  I’m hoping that, in turn, you will share your blob abstractions and I will get to steal your great ideas!</p>
<p><a href="http://www.altdevblogaday.com/2011/10/11/a-programmer-and-his-blobs/" class="more-link">Read more on A Programmer and His Blobs&#8230;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>As I moved further towards programming in a data oriented fashion, I found that I kept needing a ‘blob’ structure. I got sick of writing pointer offset code and casting void pointers to the correct type. Currently, I have four blob abstractions in my engine and I thought I would share them and the motivation behind each one.  I’m hoping that, in turn, you will share your blob abstractions and I will get to steal your great ideas!</p>
<p>Some of you may be asking ‘So what is a blob?’. A blob is just a chunk of memory. It does not care the type of data being stored in it, whether or not it’s homogeneous, etc. I differentiate between blobs by the type of access patterns it offers. This is my first blob, the basic blob:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> BasicBlob <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">uintptr_t</span> buffer<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">size_t</span> buffer_size<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>That is all the data it stores. It’s either 8 or 16 bytes depending on the pointer size. Aside from initialization, my basic blob only has two methods:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">size_t</span> GetBufferSize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
T<span style="color: #000040;">*</span> GetPtr<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span> byte_offset<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The purpose of the first method is obvious. The second, <em>GetPtr&lt;T&gt;</em>, does the pointer offset computation and returns a pointer of the requested type.</p>
<p>The most important feature of the basic blob is that it does not own the memory. It only stores an address and a size. This moves the memory allocation policy outside of the blob, where it belongs. As a side effect, the basic blob is a POD-type. Meaning that, instances can be passed by value, re-initialized with different addresses and sizes without side effect. I use this blob everywhere I need to pass a pointer and a size.</p>
<p>The second blob I use is the growing memory blob. Unlike the basic blob and the remaining two, this blob does own the memory. It is initialized with an allocator, has a (dynamic) capacity and a (dynamic) size. This blob could and should be used in your dynamic array and dynamic string classes, which are just dynamically sized contiguous blobs of memory anyway. Also, my file system abstraction relies on this blob when returning the contents of a file on disk.</p>
<p>Like the basic blob, the growing blob offers the GetPtr access method. It adds an Append operation:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">int</span> Append<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span> items, <span style="color: #0000ff;">int</span> item_count<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>There are a couple of variations on this definition, but you get the picture. It appends <strong><em>item_count</em> * <em>T</em></strong>s to the buffer. If the buffer is full, the capacity is increased and then the append takes place. I primarily use this blob when I am building larger blobs to be written to disk and later read back in as a read-only resource (a basic blob).</p>
<p>My third, and most complex blob, is a ring buffer blob. This blob does no memory allocation and does not own the memory, but keeps track of a read and write pointer inside the buffer. It has the same <em>Append</em> method as the growing blob. It adds an analogous <em>Consume</em> routine.</p>
<p>A ring buffer, while simple, is not as simple as a linear chunk of memory. Both appending and consuming from a ring buffer must handle the case when the pointer must wrap from the end of the buffer to the beginning. There are also many choices on how to implement a <a title="circular buffer" href="http://en.wikipedia.org/wiki/Circular_buffer#Difficulties">circular buffer</a></p>
<p>This blob is starting to feel kind of heavy, isn’t it? What if you want to read some data without removing it from the blob or if you just want to append in small chunks and then atomically adjust the write pointer? A good abstraction will always get out of the way and let you, the one with the brain, the programmer, dig inside it&#8217;s guts. My ring blob offers that in spades. You can access the read and write pointer through a template function like the above <em>GetPtr&lt;T&gt;</em>. But, what about the discontinuous point at the end of the buffer? You can query for the consecutive amount of bytes each pointer has before it will wrap around back to the beginning, allowing you to split your direct reads and writes around the end of the buffer. You can <em>Peek</em> into the buffer and later <em>Skip</em> those bytes. You can write into the buffer and then <em>MoveWriterPointer</em> over what you wrote.</p>
<p>I use the ring blob primarily as a message passing pipe. One part of my system writes messages with the following header:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> MsgHeader <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">uint8_t</span> op_code<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">uint16_t</span> payload_length<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>While another part of the system consumes those messages and reacts to each op code and payload. This avoids the coupling together of two different modules in my code, allowing a module to be completely replaced without impacting the rest of the code base so long as it still responds to the same messages.</p>
<p>The ring blob is great, but it is heavy and I found I wanted something simpler. Also, the ring blob did not work well when dealing network streams and native socket APIs. So, the final blob I use, I call the ‘Append &amp; Chop’ buffer. This blob keeps track of an append pointer and offers the above <em>Append</em> method. The <em>Chop</em> method could also be thought of as a shift operation. When you’re done with the beginning of the buffer you <em>Chop</em> it off and shift all the memory down. Because this blob&#8217;s memory is continuous within the region it works better with network stream APIs and is generally simpler.</p>
<p>I want to say something about implementing blobs that also applies to programming in general. Separate your memory allocation policy from your algorithm code. A higher level module should be used to glue the two together in the most optimal way. It always troubles me to see modules that allocate their own memory, restricting programmer freedom to pass in a pointer from inside a blob or on the stack. This is another form of coupling, and coupling sucks.</p>
<p>Another implementation note is that although these blobs share subsets of each others APIs, I don&#8217;t use inheritance or virtual methods. This avoids all of that unnecessary <del>shit </del>overhead. But, because the syntax of calling the methods is shared across blob types, I can still swap out the type of blob being used and have minimal impact on existing code.</p>
<p>I also have functions that read from one blob and write into another.</p>
<p>I love my blobs. They’ve become a core part of my technology and the freedom of not being tied to C/C++’s type system and structure layout algorithms is very liberating. What do your blob abstractions look like? By including small descriptive headers (see MsgHeader) you can describe the data inside the blob for the module using it. This gives us a very simple type system. Have you experimented with a more sophisticated blob type system than an op (or type) code followed by a length field?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.altdevblogaday.com/2011/10/11/a-programmer-and-his-blobs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.463 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-17 03:52:50 -->
<!-- Compression = gzip -->
