This is the second installment in a series of articles. This series describes the server backend that I wrote for the mobile game app STACK41.
In the first post I roughly elaborated the different options of languages+frameworks that were evaluated before deciding to use D.
This post describes in more detail what kind of requirements the server infrastructure imposed and what kind of auxiliary libraries were open sourced in the process of developing the STACK4 server backend.
The STACK4 Client uses Unity3D2 as a cross-platform game engine. It makes it easy to develop for all the major mobile systems like Android, iOS and Windows-Phone with a (almost) unified code base.
Asynchronous I/O, Fibers and SockJs
I used nodejs a lot the last 2 years, even at work. Using Socket.IO I implemented a realtime multiplayer game in a toy project called beachwars-online (hiding the whole browser inconsistency hell from me). Socket.IO supports long polling as a fallback too, but unfortunately the Socket.IO implementation for D4 did not support that fallback to long polling yet. Since I already decided to use a similar but less feature rich lib called SockJs for the client side I implemented SockJs for the server-side in D too: https://github.com/Extrawurst/sockjs-d.
Through my work with nodejs I was already sold to the concept of asynchronous I/O and the performance of it. As much as I hated coding in JS all the servers I wrote in node where exceptionally fast. Good thing Sönke Ludwig wrote vibe.d5 which offers exactly that for D. It is based on libevent and offers a lot of functionality similar to nodejs making web development and network programming in general very convenient. That is why the sockjs server lib is based on vibe.d.
The major difference in using vibe.d compared to nodejs (other than the fact that it is D code) is the fact that vibe.d chose to use an approach different to cascading callbacks inside of callbacks to implement async events. vibe.d uses a co-routine-like approach (in d-land called Fibers6) which basically leaves the user code synchronous and linearly. As a bonus or more a natural consequence of that is that exceptions and debugging in general still have intact callstacks (sounds small but believe me debugging nodejs in comparison sucks big time).
Here is a simple list of dub7 libs that I used in the project. They all integrate nicely and use the asynchronous approach if needed. The ones that I developed during this project are all published on github8 and are going to be subject of the following posts:
SockJs server implementation based on vibe.d (see above)
The unity3d compatible implementation of the SockJs Protocol (see above)
The mysql connector used (vibe.d/async compatible)
Google Cloud Messaging connector (async)
Apple Push Notification Service connector (work in progress)
eXtended Tiny Encryption Algorithm implemented in native D
Simple lib implementing the elo rating system in native D
Tool based on node’s forever to keep a process running continuously
Since I was asked to show some actual code, I now did in Part 3…
- https://play.google.com/store/apps/details?id=com.Extrawurst.FIR [↩]
- http://unity3d.com/ [↩]
- http://docs.unity3d.com/Documentation/ScriptReference/WWW.html [↩]
- http://code.dlang.org/packages/socket.io [↩]
- http://vibed.org/features#performance [↩]
- http://vibed.org/features#fibers [↩]
- http://code.dlang.org/ [↩]
- https://github.com/Extrawurst [↩]