Giter VIP home page Giter VIP logo

telepathy's Introduction

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 400 lines of code. There is no magic.
  • High performance: Telepathy can handle thousands of connections and packages.
  • Concurrent: Telepathy uses one thread 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: didn't perform better in our benchmarks.
  • ConcurrentQueue: .NET 3.5 compatibility is important for Unity. Wasn't faster than our SafeQueue anyway.
  • 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.Connect:
            Console.WriteLine(msg.connectionId + " Connected");
            break;
        case Telepathy.EventType.Data:
            Console.WriteLine(msg.connectionId + " Data: " + BitConverter.ToString(msg.data));
            break;
        case Telepathy.EventType.Disconnect:
            Console.WriteLine(msg.connectionId + " Disconnected");
            break;
    }
}

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

// 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.Connect:
            Console.WriteLine("Connected");
            break;
        case Telepathy.EventType.Data:
            Console.WriteLine("Data: " + BitConverter.ToString(msg.data));
            break;
        case Telepathy.EventType.Disconnect:
            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.LogMethod = Debug.Log;
        Telepathy.Logger.LogWarningMethod = Debug.LogWarning;
        Telepathy.Logger.LogErrorMethod = 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:
                        Console.WriteLine("Connected");
                        break;
                    case Telepathy.EventType.Data:
                        Console.WriteLine("Data: " + BitConverter.ToString(msg.data));
                        break;
                    case Telepathy.EventType.Disconnected:
                        Console.WriteLine("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:
                        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;
                }
            }
        }
    }

    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 100+ 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.
The next huge test will come soon...

Connections Test
We also test only the raw Telepathy library by spawing 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

telepathy's People

Contributors

miwarnec avatar nxrighthere avatar paulpach avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.