(Show all Space Tyrant articles . . .
Headlines | Linux | Apps | Coding | BSD | Admin | News
Information for Linux System Administration 

Space Tyrant: A threaded game server project in C


[Update, June 25, 2005: A Space Tyrant home page has been created as a central index to the various ST articles, links, and files.]

[Update, March 21, 2007: A Space Tyrant has its own website! It's small but growing and will provide quick access to the latest code and developments in the ST universe.]

Space Tyrant: Today we kick off a new multithreaded, network socket programming project which we will call Space Tyrant. Our mission is to write an open source, multiplayer, networked, strategy game in the C programming language. The goal of this project is to make a solid code base which implements a simple space trading game upon which other games can then be built. The game will be a subset of The Last Resort (TLR) that currently runs at [offline]. This project will be a learning exercise for me as well as for any interested readers. The current state of the source code will be released with each article update.

The game design: While my TLR game consists of over 25,000 lines of C source code and supports a web interface as well as telnet and a graphical client, this code will be far smaller and simpler. It will initially only support telnet and will implement a far simpler game design.

Players will be able to telnet into the game, create an account, and play in a universe that contains ports, planets, as well as other players. Each player will be issued a starship, some cargo holds, and an amount of starship fuel. Additional fuel will be issued hourly and will accumulate in the starship. Fuel will be used to move the ship between sectors -- locations within the game universe -- and to dock with ports. Once a ship runs out of fuel it can't move at all until new fuel is issued.

Players will be able to buy and sell commodities (Iron, alcohol, and hardware) between the three different kinds of ports. Each port type will sell one of the three commodities and buy the other two. Prices will be based on supply and demand with rarely-used ports offering the better prices.

With the money players earn trading they will be able to buy more cargo holds to make their ships more efficient for trading. They will also be able to buy fighters -- small military drones -- that can be used to attack other ships or deployed to guard a sector and its contents. The fighters carried with a ship will guard it against attacks from other players.

Games will run for a predetermined length of time, then reset and start anew.

The programming model: Now, on to the software design. I've compared and considered various models for the server design. TLR is based on the forking model using inetd or xinetd to handle the listening and forking. While the forking model is inherently distributable to multiple processors, it introduces inefficiencies (forking multiple processes) and makes interprocess communications more difficult and slower.

Next, I considered a non-blocking, single process model. In this approach, one process handles everything in a single thread. It would use non-blocking IO (read and write functions that never wait for completion but, rather, return immediately if they aren't ready to read or write actual data). The thttpd web server is an example of a non-blocking, single process server. It's extremely fast and efficient. However, this model is quite complicated to code, and, I believe would make it more likely to introduce subtle timing bugs.

Next, I considered a pure multithreaded, single process model with a thread for each player. While appealing in many ways, this model would require the same kind of coordination between threads that the forking model requires between processes. Such interprocess communication would be simplified in that the various threads share memory, but the coordination issues otherwise remain the same.

Last, I considered another multithreaded model, this time with only IO threads for each user and a single thread that implements all game logic. While that one central thread might someday be a bottleneck that limits scalability on large SMP systems, it does distribute the IO on any additional processors that might be present, and requires minimal coordination. In short, this model combines the logic simplicity of the non-blocking single process model with the coding simplicity of the threaded model, while separating the IO from the main logic. There will also be two other simple threads in this model. There will be a thread that listens for new connections and spawns the IO threads for each new connection. There will also be a thread that writes the data to disk periodically.

This is the approach that I intend to take for this project. The code will be written for both Linux and Mac OS X.

More info: I have set up an email address for programmers following this series to provide recommendations, bug reports, and other feedback. Send email about this project to spacetyrant [at]
mail this link | score:9608 | -Ray, March 18, 2005 (Updated: July 26, 2008)
More Programming articles...

Abstract Art in Tallahassee

Selected articles

Apple DIY Repair

Librenix T-Shirts and Coffee Mugs!

Linux dominates Windows

How to install Ubuntu Linux on the decTOP SFF computer

Microsoft to push unlicensed users to Linux

Space Tyrant: Multithreading lessons learned on SMP hardware

Apple to Intel move no threat to Linux

Linux vs. Windows: Why Linux will win

Space Tyrant: A threaded C game project: First Code

Tutorial: Introduction to Linux files

Mono-culture and the .NETwork effect

Closed Source Linux Distribution Launched

No, RMS, Linux is not GNU/Linux

Why software sucks

Why Programmers are not Software Engineers A simple directory shadowing script for Linux

Programming Language Tradeoffs: 3GL vs 4GL

Graffiti Server Download Page

Beneficial Computer Viruses

The life cycle of a programmer


Firefox sidebar

Site map

Site info

News feed


(to post)


Articles are owned by their authors.   © 2000-2012 Ray Yeargin