Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#

Design and Implementation of a High-performance TCP/IP Communications Library

Rate me:
Please Sign up or sign in to vote.
4.83/5 (34 votes)
22 Jul 2009CPOL13 min read 114.9K   2.7K   209  
A TCP/IP Communications Library, designed to handle client-server data transmission for a massive multiplayer online game.
#define TrackConflicts

using System;
using System.ComponentModel;
using Hyper.ComponentModel;
using System.Runtime;
using System.Configuration;
using System.Threading;

namespace BrainTechLLC.ThreadSafeObjects
{
    /// <summary>
    /// Basic support for simple spin-lock - for locking tiny sections of code only, avoiding lock()
    /// Uses atomic CompareExchange operation.  NOTE: no deadlock detection, same thread cannot lock an already-locked object    
    /// </summary>
    [TypeDescriptionProvider(typeof(HyperTypeDescriptionProvider)), Serializable]
    public class Lockable : ILockable
    {
        [NonSerialized]
        private readonly static int SpinCycles = 20;//Properties.Settings.Default.SpinCycles;

        [NonSerialized]
        protected static long _conflicts;

        [NonSerialized]
        protected int _lock;

        /// <summary>
        /// Returns the number of lock conflicts that have occurred
        /// </summary>
        public static long Conflicts { get { return _conflicts; } }

        /// <summary>
        /// Aquire the lock
        /// </summary>
        public void AquireLock()
        {
            // Assume that we will grab the lock - call CompareExchange
            if (Interlocked.CompareExchange(ref _lock, 1, 0) == 1)
            {
                int n = 0;

                // Could not grab the lock - spin/wait until the lock looks obtainable
                while (_lock == 1)
                {
                    if (n++ > SpinCycles)
                    {
#if TrackConflicts
                        Interlocked.Increment(ref _conflicts);
#endif
                        n = 0;
                        Thread.Sleep(0);
                    }
                }

                // Try to grab the lock - call CompareExchange
                while (Interlocked.CompareExchange(ref _lock, 1, 0) == 1)
                {
                    n = 0;

                    // Someone else grabbed the lock.  Continue to spin/wait until the lock looks obtainable
                    while (_lock == 1)
                    {
                        if (n++ > SpinCycles)
                        {
#if TrackConflicts
                            Interlocked.Increment(ref _conflicts);
#endif
                            n = 0;
                            Thread.Sleep(0);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Release the lock
        /// </summary>
        public void ReleaseLock()
        {
            _lock = 0;
        }

        public bool Locked { get { return (_lock != 0); } }

        public bool TryAquireLock()
        {
			if (_lock == 1) return false;
            return (Interlocked.CompareExchange(ref _lock, 1, 0) != 1);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Troppus Software
United States United States
Currently working as a Senior Silverlight Developer with Troppus Software in Superior, CO. I enjoy statistics, programming, new technology, playing the cello, and reading codeproject articles. Smile | :)

Comments and Discussions