• Stars
    star
    256
  • Rank 158,552 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A pure managed C# implementation of the Netcode.IO spec

Netcode.IO.NET

A pure managed C# implementation of the Netcode.IO spec

Project goals

The goal of this project is to provide a pure managed implementation of the Netcode.IO spec coded against .NET 3.5 and using zero native DLLs or wrappers for maximum portability. Instead of using libsodium like the original C reference implementation, this implementation uses a customized version of the Bouncy Castle cryptography library. You can find the original source code here: https://github.com/bcgit/bc-csharp

Additionally, it is designed for use in games. To this end, it has been designed from the ground up to have as minimal an impact on GC allocations as possible. For the most part, you should not see any GC impact from using Netcode.IO.NET at all.

API usage

Most of the API resides in the namespace NetcodeIO.NET

Server API

To create and start a new server:

Server server = new Server(
	maxClients,		// int maximum number of clients which can connect to this server at one time
	publicAddress, port,	// string public address and int port clients will connect to
	protocolID,		// ulong protocol ID shared between clients and server
	privateKeyBytes		// byte[32] private crypto key shared between backend servers
);
server.Start();			// start the server running

To listen for various events:

// Called when a client has connected
server.OnClientConnected += clientConnectedHandler;		// void( RemoteClient client )

// Called when a client disconnects
server.OnClientDisconnected += clientDisconnectedHandler;	// void( RemoteClient client )

// Called when a payload has been received from a client
// Note that you should not keep a reference to the payload, as it will be returned to a pool after this call completes.
server.OnClientMessageRecieved += messageReceivedHandler;	// void( RemoteClient client, byte[] payload, int payloadSize )

// Called when the server logs a message
// If you are not using a custom logger, a handler using Console.Write() is sufficient.
server.OnLogMessage += logMessageHandler;			// void( string message, NetcodeLogLevel logLevel )

To send a payload to a remote client connected to the server:

remoteClient.Send(byte[] payload, int payloadSize);

// or:
server.SendPayload( RemoteClient client, byte[] payload, int payloadSize );

To disconnect a client:

server.Disconnect( RemoteClient client );

To get at the arbitrary 256-byte user data which can be passed with a connect token:

remoteClient.UserData; // byte[256]

To stop a server and disconnect any clients:

server.Stop();

Client API

To create a new client:

Client client = new Client();

To listen for various events:

// Called when the client's state has changed
// Use this to detect when a client has connected to a server, or has been disconnected from a server, or connection times out, etc.
client.OnStateChanged += clientStateChanged;			// void( ClientState state )

// Called when a payload has been received from the server
// Note that you should not keep a reference to the payload, as it will be returned to a pool after this call completes.
client.OnMessageReceived += messageReceivedHandler;		// void( byte[] payload, int payloadSize )

To connect to a server using a connect token:

client.Connect( connectToken );		// byte[2048] public connect token as returned by a TokenFactory

To send a message to a server when connected:

client.Send( byte[] payload, int payloadSize );

To disconnect a client:

client.Disconnect();

TokenFactory API

TokenFactory can be used to generate the public connect tokens used by clients to connect to game servers. To create a new TokenFactory:

TokenFactory tokenFactory = new TokenFactory(
	protocolID,		// must be the same protocol ID as passed to both client and server constructors
	privateKey		// byte[32], must be the same as the private key passed to the Server constructor
);

To generate a new 2048-byte public connect token:

tokenFactory.GenerateConnectToken(
	addressList,		// IPEndPoint[] list of addresses the client can connect to. Must have at least one and no more than 32.
	expirySeconds,		// in how many seconds will the token expire
	serverTimeout,		// how long it takes until a connection attempt times out and the client tries the next server.
	sequenceNumber,		// ulong token sequence number used to uniquely identify a connect token.
	clientID,		// ulong ID used to uniquely identify this client
	userData		// byte[], up to 256 bytes of arbitrary user data (available to the server as RemoteClient.UserData)
);

A note about UDP and unreliability

Netcode.IO.NET is a pure port of the Netcode.IO protocol - nothing more, and nothing less. At its heart, Netcode.IO is an encryption and connection based abstraction on top of UDP. And, just like UDP, it has zero guarantees about reliability. Your messages may not make it, and they may not make it in order. That's just a fact of the internet. That said, any game will almost certainly need some kind of reliability layer. To that end, my ReliableNetcode.NET project provides an agnostic and easy to use reliability layer you can use to add this functionality to your game.

More Repositories

1

RetroTVFX

A small collection of shaders for a range of authentic old TV effects (Composite, S-Video, RF, etc)
C#
172
star
2

ReliableNetcode.NET

A pure managed C# socket-agnostic reliability layer inspired by reliable.io and yojimbo
C#
151
star
3

Unity-Netcode.IO

A lightweight plugin to allow Unity games to use Netcode.IO for secure UDP socket communication.
C#
122
star
4

Pretty-Fast-Video

Designed as successor to Pretty-Good-Video for improved codec structure, API design & performance
Rust
52
star
5

BNA

A minimal game framework inspired by XNA for the Beef programming language
HyPhy
22
star
6

Open-World-Builder

Open source level editor toolkit built on FNA+ImGui.NET
C#
17
star
7

ContentPipe

A simple framework for automating content pipelines for indie games
C#
13
star
8

Effect-Build

A simple commandline frontend for D3DCompile to build FX 2.0 HLSL effect files, designed for FNA-based games
C++
13
star
9

Godot-RetroTV

Authentic TV shaders for Godot with accurate composite video artifacts
GDScript
8
star
10

ResourceCache

A simple resource loading framework for .NET games
C#
8
star
11

Beef-Toml

A simple TOML serializer/deserializer for Beef
HyPhy
7
star
12

Praxis.NET

A C# ECS game engine built on FNA
HTML
7
star
13

com.glairedaggers.sonke

Implementation of Sonic Adventure inspired physics for Unity 2020.3 and above
C#
6
star
14

dreambox-doom

A port of DOOM to the Dreambox fantasy console
C
5
star
15

Pretty-Good-Video

A toy MPEG-like video codec
Rust
4
star
16

CritChanceStudio.Tools

Source code of various CritChanceStudios internal tools (built on FNA & ImGUI)
C#
3
star
17

godot-datatable

Implementation of DataTable-style resources for Godot
GDScript
3
star
18

libpfvdec

A fast & portable C library for decoding PFV video streams
C
3
star
19

ImGuizmo.FNA

A demo project showcasing ImGuizmo integration with FNA
C#
1
star
20

GD-Localization-For-RMMV

Localization plugin for RPG Maker MV
JavaScript
1
star
21

cozi-lang

A work-in-progress hobby language project
C#
1
star
22

PSX-VAG-.NET

Simple minimal lib for encoding & decoding PSX vag/vagi audio
C#
1
star
23

DBSDK-Rust

SDK for writing DreamBox games in Rust
Rust
1
star