• Stars
    star
    211
  • Rank 180,771 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created over 4 years ago
  • Updated 16 days ago

Reviews

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

Repository Details

A library for building server application using loop-action programming model on .NET.

Build-Master Releases

LogicLooper

ζ—₯本θͺž

A library is for building server application using loop-action programming model on .NET Core. This library focuses on building game servers with server-side logic.

For example, if you have the following game loops, the library will provide a way to aggregate and process in a more efficient way than driving with a simple Task.

while (true)
{
    // some stuff to do ...
    network.Receive();
    world.Update();
    players.Update();
    network.Send();
    // some stuff to do ...

    // wait for next frame
    await Task.Delay(16);
}
using var looper = new LogicLooper(60);
await looper.RegisterActionAsync((in LogicLooperActionContext ctx) =>
{
    // The action will be called by looper every frame.
    // some stuff to do ...
    network.Receive();
    world.Update();
    players.Update();
    network.Send();
    // some stuff to do ...

    return true; // wait for next update
});

Table of Contents

Installation

PS> Install-Package LogicLooper
$ dotnet add package LogicLooper

Usage

Single-loop application

A Looper bound one thread and begin a main-loop. You can register multiple loop actions for the Looper. It's similar to be multiple Update methods called in one frame of the game engine.

using Cysharp.Threading;

// Create a looper.
const int targetFps = 60;
using var looper = new LogicLooper(targetFps);

// Register a action to the looper and wait for completion.
await looper.RegisterActionAsync((in LogicLooperActionContext ctx) =>
{
    // If you want to stop/complete the loop, return false to stop.
    if (...) { return false; }

    // some stuff to do ...

    return true; // wait for a next update.
});

Multiple-loop application using LooperPool

For example, if your server has many cores, it is more efficient running multiple loops. LooperPool provides multiple loopers and facade for using them.

using Cysharp.Threading;

// Create a looper pool.
// If your machine has 4-cores, the LooperPool creates 4-Looper instances.
const int targetFps = 60;
var looperCount = Environment.ProcessorCount;
using var looperPool = new LogicLooperPool(targetFps, looperCount, RoundRobinLogicLooperPoolBalancer.Instance);

// Register a action to the looper and wait for completion.
await looperPool.RegisterActionAsync((in LogicLooperActionContext ctx) =>
{
    // If you want to stop/complete the loop, return false to stop.
    if (...) { return false; }

    // some stuff to do ...

    return true; // wait for a next update.
});

Integrate with Microsoft.Extensions.Hosting

See samples/LoopHostingApp.

Advanced

Unit tests / Frame-by-Frame execution

If you want to write unit tests with LogicLooper or update frames manually, you can use ManualLogicLooper / ManualLogicLooperPool.

var looper = new ManualLogicLooper(60.0); // `ElapsedTimeFromPreviousFrame` will be fixed to `1000 / FrameTargetFrameRate`.

var count = 0;
var t1 = looper.RegisterActionAsync((in LogicLooperActionContext ctx) =>
{
    count++;
    return count != 3;
});

looper.Tick(); // Update frame
Console.WriteLine(count); // => 1

looper.Tick(); // Update frame
Console.WriteLine(count); // => 2

looper.Tick(); // Update frame (t1 will be completed)
Console.WriteLine(count); // => 3

looper.Tick(); // Update frame (no action)
Console.WriteLine(count); // => 3

Coroutine

LogicLooper has support for the coroutine-like operation. If you are using Unity, you are familiar with the coroutine pattern.

using var looper = new LogicLooper(60);

var coroutine = default(LogicLooperCoroutine);
await looper.RegisterActionAsync((in LogicLooperActionContext ctx) =>
{
    if (/* ... */)
    {
        // Launch a coroutine in the looper that same as the loop action.
        coroutine = ctx.RunCoroutine(async coCtx =>
        {
            // NOTE: `DelayFrame`, `DelayNextFrame`, `Delay` methods are allowed and awaitable in the coroutine.
            // If you await a Task or Task-like, the coroutine throws an exception.
            await coCtx.DelayFrame(60);

            // some stuff to do ...

            await coCtx.DelayNextFrame();

            // some stuff to do ...

            await coCtx.Delay(TimeSpan.FromMilliseconds(16.66666));
        });
    }

    if (coroutine.IsCompleted)
    {
        // When the coroutine has completed, you can do some stuff ...
    }

    return true;
});

More Repositories

1

UniTask

Provides an efficient allocation free async/await integration for Unity.
C#
7,314
star
2

MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
C#
3,611
star
3

MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
C#
2,807
star
4

ZString

Zero Allocation StringBuilder for .NET and Unity.
C#
1,860
star
5

MasterMemory

Embedded Typed Readonly In-Memory Document Database for .NET and Unity.
C#
1,410
star
6

R3

The new future of dotnet/reactive and UniRx.
C#
1,332
star
7

MessagePipe

High performance in-memory/distributed messaging pipeline for .NET and Unity.
C#
1,258
star
8

ConsoleAppFramework

Micro-framework for console applications to building CLI tools/Daemon/Batch for .NET, C#.
C#
1,201
star
9

ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
C#
1,081
star
10

Ulid

Fast .NET C# Implementation of ULID for .NET and Unity.
C#
831
star
11

SimdLinq

Drop-in replacement of LINQ aggregation operations extremely faster with SIMD.
C#
726
star
12

csbindgen

Generate C# FFI from Rust for automatically brings native code and C native library to .NET and Unity.
Rust
558
star
13

ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
C#
419
star
14

ProcessX

Simplify call an external process with the async streams in C# 8.0.
C#
410
star
15

UnitGenerator

C# Source Generator to create value-object, inspired by units of measure.
C#
309
star
16

YetAnotherHttpHandler

YetAnotherHttpHandler brings the power of HTTP/2 (and gRPC) to Unity and .NET Standard.
C#
290
star
17

AlterNats

An alternative high performance NATS client for .NET.
C#
279
star
18

RuntimeUnitTestToolkit

CLI/GUI Frontend of Unity Test Runner to test on any platform.
C#
268
star
19

NativeMemoryArray

Utilized native-memory backed array for .NET and Unity - over the 2GB limitation and support the modern API(IBufferWriter, ReadOnlySequence, scatter/gather I/O, etc...).
C#
246
star
20

PrivateProxy

Source Generator and .NET 8 UnsafeAccessor based high-performance strongly-typed private accessor for unit testing and runtime.
C#
234
star
21

StructureOfArraysGenerator

Structure of arrays source generator to make CPU Cache and SIMD friendly data structure for high-performance code in .NET and Unity.
C#
229
star
22

MagicPhysX

.NET PhysX 5 binding to all platforms(win, osx, linux) for 3D engine, deep learning, dedicated server of gaming.
Rust
221
star
23

DFrame

Distributed load testing framework for .NET and Unity.
C#
206
star
24

Utf8StreamReader

Utf8 based StreamReader for high performance text processing.
C#
194
star
25

LitJWT

Lightweight, Fast JWT(JSON Web Token) implementation for .NET.
C#
190
star
26

KcpTransport

KcpTransport is a Pure C# implementation of RUDP for high-performance real-time network communication
C#
183
star
27

Utf8StringInterpolation

Successor of ZString; UTF8 based zero allocation high-peformance String Interpolation and StringBuilder.
C#
140
star
28

CsprojModifier

CsprojModifier performs additional processing when Unity Editor generates the .csproj.
C#
135
star
29

ValueTaskSupplement

Append supplemental methods(WhenAny, WhenAll, Lazy) to ValueTask.
C#
126
star
30

Kokuban

Simplifies styling strings in the terminal for .NET application.
C#
121
star
31

Claudia

Unofficial Anthropic Claude API client for .NET.
C#
118
star
32

SlnMerge

SlnMerge merges the solution files when generating solution file by Unity Editor.
C#
111
star
33

GrpcWebSocketBridge

Yet Another gRPC over HTTP/1 using WebSocket implementation, primarily targets .NET platform.
C#
71
star
34

WebSerializer

Convert Object into QueryString/FormUrlEncodedContent for C# HttpClient REST Request.
C#
60
star
35

RandomFixtureKit

Fill random/edge-case value to target type for unit testing, supports both .NET Standard and Unity.
C#
43
star
36

Actions

30
star
37

DocfxTemplate

Patchworked DocFX v2 template for Cysharp
JavaScript
6
star
38

com.unity.ide.visualstudio-backport

Backport of com.unity.ide.visualstudio to before Unity 2019.4.21
C#
1
star