Giter Club home page Giter Club logo

enet.managed's Introduction

Discord ENet version Nuget Nuget (with prereleases) Build status

ENet.Managed

ENet is cross-platform reliable UDP networking library written in C and ENet.Managed is an unofficial, managed wrapper for ENet available for .NET and it supports specific set of platforms. You can checkout ENet's repo here.

Quick start (Usage)

Take a look at examples folder.

Features

  • Supports AnyCPU targets
  • Supports IPv6
  • Provides async-await interface.
  • It's cross-platform via .NET Standard
  • It's available via NuGet package manager. (Here)
  • You can set custom:
    • compression method (not enabled by default, recommended to specify a compressor using CompressWith*)
    • checksum algorithm (not enabled by default, recommended to specify a checksum using ChecksumWith*)
    • heap allocator (by default ManagedENet forces ENet to use a custom allocator which is faster than malloc, take a look at ENetManagedAllocator.cs)
  • Takes advantage of Span<byte> and friends to reduce GC allocations.
  • Provides nearly all features of ENet via managed API.

Benchmarks

You can see how ENet performs compared to other libraries by taking look at these benchmarks.
Benchmarks for the wrapper itself are not available yet but it should have near native performance when optimizations are enabled.

Supported platfroms

Platform\Arch X86 X86_64 ARM32 ARM64
Windows Yes Yes Yes -
Linux - Yes Yes Yes
Mac - Yes - -

Linux binaries are built and statically linked against MUSL.

Notes

  • This wrapper deploys ENet binaries to OS's temp folder and dynamically loads them, you can alter this behavior using ENetStartupOptions or by manually initializing ENet using LibENet class.
  • ENet binaries are built from https://github.com/moien007/enet
  • ENet's checksum feature is disabled by default, it is highly recommended to use checksum to avoid receiving damaged packets.
    • ChecksumWithCRC32 enables ENet's builtin checksum feature.
  • In case of consuming custom version of ENet you have to sync the data structures offsets with the wrapper.
  • ENetPeer.DisconnectNow method will not generate ENetEventType.Disconnect.
  • If you use SetUserData extension methods you have to call UnsetUserData when you done, otherwise memory leak will happen.
  • You can use ENetPooledAllocator in case if you want to make ENet use pooled buffers to reduce allocations.

Contribution

You can contribute by reporting bugs and making pull requests.

License

MIT open-source license.

enet.managed's People

Contributors

jstalnac avatar moien007 avatar nenkai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

enet.managed's Issues

[Question] Thread safety.

Is this wrapper of ENet thread-safe as I make use of its StartServiceThread function which basically explains what I am talking about.

Do I need any further code to deal with thread safety or did the wrapper have it included/implemented?

[BUG?] Unable to access ENetHost.PeerList

Unable to access ENetHost.PeerList

if you try to access ENetHost.PeerList it'll cause a crash.
I've tried to filter out non connected peers, yet still causes a crash.
it works for the first few peers i don't have any numbers though.

Sample code:

ManagedENet.Startup();

var ip = IPEndPoint.Parse("127.0.0.1:27015");
var _host = new ENetHost(ip, 1024, 10);
            
_host.ChecksumWithCRC32();
_host.CompressWithRangeCoder();

while (true)
{
    var ev = _host.Service(TimeSpan.FromSeconds(1));

    switch (ev.Type)
    {
         // Do stuff...
    }

    foreach (var peer in _host.PeerList.Where(s => s.State == ENetPeerState.Connected)) // Crash
    {
        // Handle pump data onto peers
    }
}

Exception:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at ENet.Managed.ENetPeer.get_State()
   at Sample.Program+<>c.<Main>b__0_0(ENet.Managed.ENetPeer)
   at System.Linq.Enumerable+WhereEnumerableIterator`1[[ENet.Managed.ENetPeer, ENet.Managed, Version=4.2.5.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at Sample.Program.Main(System.String[])

Details

Version: 4.2.5
.NET Version: .NET Core 3.1
OS: Windows 10 2004 x86_64

trying this on linux

Hello im trying to use this on linux and im getting this error while trying to connect

Segmentation fault (core dumped) while in windows it work any idea?

ENetAsyncHost stops immediately after starting

ENetAsyncHost stops immediately after it's started. I believe the problem is in ENetAsyncHost.RunServiceLoop:

private void RunServiceLoop()
{
var serviceTimeout = TimeSpan.FromMilliseconds(SERVICE_TIMEOUT_MS);
while (m_Stop != false)
{
Thread.Yield();
using (var lockguard = SharedStateLock.Acquire())
{
var host = lockguard.SharedState.Host;
var @event = host.Service(serviceTimeout);
if (@event.Type == ENetEventType.None)
continue;
HandleEvent(lockguard.SharedState, @event);
while (true)
{
@event = host.CheckEvents();
if (@event.Type != ENetEventType.None)
HandleEvent(lockguard.SharedState, @event);
else
break;
}
}
}
}

The condition in the outer while loop should be !m_Stop because m_Stop != false is only true when m_Stop is true, so when the host is shutting down, which means the service thread stops immediately.

I can prove my hypothesis by manually setting m_Stop to true with some hacky reflection at runtime before starting the host:

var field = typeof(ENetAsyncHost).GetField("m_Stop", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(myAsyncHost, true);

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.