Skip to content
Simple, message based, MMO Scale TCP networking in C#. And no magic.
C#
Branch: master
Clone or download
Latest commit 078f73c Aug 1, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LoadTest Fix loadtest not running in Rider Feb 1, 2019
Telepathy.Tests Server.counter not static anymore Jul 29, 2019
Telepathy use default(T) for C# <7.1 compatibility Jul 29, 2019
.gitignore Unit test project Aug 13, 2018
LICENSE License Aug 13, 2018
README.md Update README.md Aug 1, 2019
Telepathy.sln Add load testing project (#5) Aug 14, 2018
appveyor.yml Update appveyor.yml Jan 30, 2019

README.md

Telepathy Logo

Build status AppVeyor tests branch Discord Codecov

Simple, message based, MMO Scale TCP networking in C#. And no magic.

Telepathy was designed with the KISS Principle in mind.
Telepathy is fast and extremely reliable, designed for MMO scale Networking.
Telepathy uses framing, so anything sent will be received the same way.
Telepathy is raw C# and can be used in Unity3D too.

What makes Telepathy special?

Telepathy was originally designed for uMMORPG after 3 years in UDP hell.

We needed a library that is:

  • Stable & Bug free: Telepathy uses only 500 lines of code. There is no magic.
  • High performance: Telepathy can handle thousands of connections and packages.
  • Concurrent: Telepathy uses two threads per connection. It can make heavy use of multi core processors.
  • Simple: Telepathy takes care of everything. All you need to do is call Connect/GetNextMessage/Disconnect.
  • Message based: if we send 10 and then 2 bytes, then the other end receives 10 and then 2 bytes, never 12 at once.

MMORPGs are insanely difficult to make and we created Telepathy so that we would never have to worry about low level Networking again.

What about...

  • Async Sockets: perform great in regular C# projects, but poorly when used in Unity.
  • UDP vs. TCP: Minecraft and World of Warcraft are two of the biggest multiplayer games of all time and they both use TCP networking. There is a reason for that.

Using the Telepathy Server

// create and start the server
Telepathy.Server server = new Telepathy.Server();
server.Start(1337);

// grab all new messages. do this in your Update loop.
Telepathy.Message msg;
while (server.GetNextMessage(out msg))
{
    switch (msg.eventType)
    {
        case Telepathy.EventType.Connected:
            Console.WriteLine(msg.connectionId + " Connected");
            break;
        case Telepathy.EventType.Data:
            Console.WriteLine(msg.connectionId + " Data: " + BitConverter.ToString(msg.data));
            break;
        case Telepathy.EventType.Disconnected:
            Console.WriteLine(msg.connectionId + " Disconnected");
            break;
    }
}

// send a message to client with connectionId = 0 (first one)
server.Send(0, new byte[]{0x42, 0x13, 0x37});

// stop the server when you don't need it anymore
server.Stop();

Using the Telepathy Client

// create and connect the client
Telepathy.Client client = new Telepathy.Client();
client.Connect("localhost", 1337);

// grab all new messages. do this in your Update loop.
Telepathy.Message msg;
while (client.GetNextMessage(out msg))
{
    switch (msg.eventType)
    {
        case Telepathy.EventType.Connected:
            Console.WriteLine("Connected");
            break;
        case Telepathy.EventType.Data:
            Console.WriteLine("Data: " + BitConverter.ToString(msg.data));
            break;
        case Telepathy.EventType.Disconnected:
            Console.WriteLine("Disconnected");
            break;
    }
}

// send a message to server
client.Send(new byte[]{0xFF});

// disconnect from the server when we are done
client.Disconnect();

Unity Integration

Here is a very simple MonoBehaviour script for Unity. It's really just the above code with logging configured for Unity's Debug.Log:

using System;
using UnityEngine;

public class SimpleExample : MonoBehaviour
{
    Telepathy.Client client = new Telepathy.Client();
    Telepathy.Server server = new Telepathy.Server();

    void Awake()
    {
        // update even if window isn't focused, otherwise we don't receive.
        Application.runInBackground = true;

        // use Debug.Log functions for Telepathy so we can see it in the console
        Telepathy.Logger.Log = Debug.Log;
        Telepathy.Logger.LogWarning = Debug.LogWarning;
        Telepathy.Logger.LogError = Debug.LogError;
    }

    void Update()
    {
        // client
        if (client.Connected)
        {
            if (Input.GetKeyDown(KeyCode.Space))
                client.Send(new byte[]{0x1});

            // show all new messages
            Telepathy.Message msg;
            while (client.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                    case Telepathy.EventType.Connected:
                        Debug.Log("Connected");
                        break;
                    case Telepathy.EventType.Data:
                        Debug.Log("Data: " + BitConverter.ToString(msg.data));
                        break;
                    case Telepathy.EventType.Disconnected:
                        Debug.Log("Disconnected");
                        break;
                }
            }
        }

        // server
        if (server.Active)
        {
            if (Input.GetKeyDown(KeyCode.Space))
                server.Send(0, new byte[]{0x2});

            // show all new messages
            Telepathy.Message msg;
            while (server.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                    case Telepathy.EventType.Connected:
                        Debug.Log(msg.connectionId + " Connected");
                        break;
                    case Telepathy.EventType.Data:
                        Debug.Log(msg.connectionId + " Data: " + BitConverter.ToString(msg.data));
                        break;
                    case Telepathy.EventType.Disconnected:
                        Debug.Log(msg.connectionId + " Disconnected");
                        break;
                }
            }
        }
    }

    void OnGUI()
    {
        // client
        GUI.enabled = !client.Connected;
        if (GUI.Button(new Rect(0, 0, 120, 20), "Connect Client"))
            client.Connect("localhost", 1337);

        GUI.enabled = client.Connected;
        if (GUI.Button(new Rect(130, 0, 120, 20), "Disconnect Client"))
            client.Disconnect();

        // server
        GUI.enabled = !server.Active;
        if (GUI.Button(new Rect(0, 25, 120, 20), "Start Server"))
            server.Start(1337);

        GUI.enabled = server.Active;
        if (GUI.Button(new Rect(130, 25, 120, 20), "Stop Server"))
            server.Stop();

        GUI.enabled = true;
    }

    void OnApplicationQuit()
    {
        // the client/server threads won't receive the OnQuit info if we are
        // running them in the Editor. they would only quit when we press Play
        // again later. this is fine, but let's shut them down here for consistency
        client.Disconnect();
        server.Stop();
    }
}

Make sure to enable 'run in Background' for your project settings, which is a must for all multiplayer games. Then build it, start the server in the build and the client in the Editor and press Space to send a test message.

Benchmarks

Real World
Telepathy is constantly tested in production with uMMORPG. We recently tested 500 players all broadcasting to each other in the worst case scenario, without issues.

We had to stop the test because we didn't have more players to spawn clients.

Connections Test
We also test only the raw Telepathy library by spawning 1 server and 1000 clients, each client sending 100 bytes 14 times per second and the server echoing the same message back to each client. This test should be a decent example for an MMORPG scenario and allows us to test if the raw Telepathy library can handle it.

Test Computer: 2015 Macbook Pro with a 2,2 GHz Intel Core i7 processor.
Test Results:

Clients CPU Usage Ram Usage Bandwidth Client+Server Result
128 7% 26 MB 1-2 MB/s Passed
500 28% 51 MB 3-4 MB/s Passed
1000 42% 75 MB 3-5 MB/s Passed

Note: results will be significantly better on a really powerful server. Tests will follow.

You can run this test yourself by running the provided LoadTest

You can’t perform that action at this time.