Giter Club home page Giter Club logo

hazel-networking's Introduction

Hazel Networking is a low-level networking library for C# providing connection-oriented, message based communication via UDP and RUDP.

This was originally a fork of a project started by the Dark Rift Networking developer. I stripped that down with the aim of making a simple interface for ultra-fast connection-based UDP communication for games. After Among Us launched, the agreed to make this the primary fork. If you see mention of the original repo, know that it's just a tad out of date.


Features

  • UDP and Reliable UDP.
  • Encrypted packets using DTLS
  • UDP Broadcast for local-multiplayer.
  • Completely thread safe.
  • All protocols are connection oriented (similar to TCP) and message based (similar to UDP)
  • IPv4 and IPv6 support
  • Automatic statistics about data passing in and out of connections
  • Designed to be as fast and lightweight as possible

This fork has been heavily modified from the original to reduce allocations, copies, and locking. It's pretty stable and Among Us uses it for all platforms, but still has the occasional issue.


There is currently no online documentation. I might get around to it someday. I have changed some interfaces in "unintuitive ways", it is my hope that this example repo will be able to help users get started.

If you want to make improvements, I am open to pull requests. If you find bugs, feel free raise issues.


Installing Hazel

For non unity projects, Hazel can be installed via the NuGet innersloth.Hazel-Networking package.

For Unity projects, you'll have to build Hazel first. I recommend just dropping the output DLLs somewhere in the Assets folder.


Building Hazel

To build Hazel open solution file using your favourite C# IDE (I use Visual Studio 2019) and then build as you would any other project.


Tips with this fork

  • Pay attention to which callbacks give you ownership of the MessageReader, making you responsible for recycling. In particular:
    • You should not recycle messages after NewConnection events.
    • You should not recycle messages after Disconnect events.
    • You should recycle messages after DataReceived events.
  • Hazel doesn't support fragmented packets. It used to, but I wasn't sure of it so I removed it and have never needed it since. Just stay under 1kb packets.

Tips for using Hazel with Unity

  • Unity doesn't like other threads messing with GameObjects. This isn't a problem for tasks like relaying information. But for tasks like spawning GameObjects on clients or correcting physics, you will want to have a thread safe list of events that are run and cleared during Update or FixedUpdate.
    • A List+lock(object) is fine because you have many writers, one reader and Hazel doesn't guarantee event order.
    • A ConcurrentBag is not a bad choice, but you will have to do something special to keep the Update method from hanging if you get an overwhelming number of new events (which suggests problems with your code elsewhere).
  • I also recommend using the ConnectAsync method in a Coroutine that waits for State to change so you don't hang the game while connecting.

hazel-networking's People

Contributors

cloutier116 avatar elsensee avatar izackp avatar jamjar00 avatar js6pak avatar mendsley avatar mukikaizoku avatar shaunthesheep644 avatar willardf 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  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  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  avatar

Watchers

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

hazel-networking's Issues

Does Hazel support any form of encryption?

Hey, Forest.

I'm using the Hazel library to develop my MMO video game, and am wondering if it supports any type of high-level encryption. I'm looking for levels of automatic SSL encryption comparable to Secure WebSocket (WSS) or HTTPS. I don't want eavesdroppers to steal my users' credentials, and I do not know if Hazel supports what I'm looking for out of the box.

Is this possible? Thanks

How to detect disconnections?

I was wondering how I can detect disconnections.
The wiki from DarkriftNetworking doesn't explain anything. I understand it like it's just straight forward
Connection.Disconnected += Disconnected, but that doesn't work. Also I know this wiki doesn't give any info yet.
So I'm a bit stuck...

Also a sample project would be appreciated!

[Bug] The server blindly trusts the packet size that is specified in the packet itself

This issue is shares similarities with issue #5 and its impact is the same. Setting a very high packet size will cause MessageReader.GetString() to not throw an exception. Instead, it will happily read the specified amount of bytes, regardless of whether the actual packet contained the said bytes or not. This can be used to leak older packets, and it can be used to cause an exception to be thrown on the client related to out-of-bounds reads.

Client code to exploit this bug
Server code that is vulnerable to this bug

Does Hazel support TCP?

I see in the included .docx (Hazel Networking Protocol.docx in the root) that there is a TCP implementation:
https://i.imgur.com/b8rYxsN.png

However, this isn't listed in the features list on the main repo page.

Wondering if TCP is supported? And if not, what should I be aware of when working with RUDP, as someone with a TCP background?

How to host using Hamachi?

I know this is not good place to ask but...

How to host using Hamachi?
Apparently you can use server-ip in Minecraft Configuration. But how to do that in Hazel UDP?

(To be exact you can type server-ip on MC Configuration)

Sent 6 pings that remote has not responded to

Hello, I have the library in a game with over 3k daily active users and I get a lot of reports of disconnects.
They get those errors:

  • Sent 6 pings that remote has not responded to.
  • Reliable packet 42 (size=10) was not ack'd after 5090ms (6 resends)

More info:

  • it works if they use VPN
  • I host the server(dedicate server not peer to peer) on Linode in Germany

Most of them are from Turkey, most of the players are able to play fine.
The disconnects happen almost instantly to them(or after few seconds)

Any idea how can I fix it?
Thank you!

Stack overflow at random times(might be related to DDOS attacks)

Can happen instantly when I start my server, it worked great for a year with no problem but now in the last 24 hours it crashed 10+ times with this weird error:
`Stack overflow.
at Hazel.MessageReader.GetSized(Int32)
at Hazel.Udp.UdpConnectionListener.StartListeningForData()
at Hazel.Udp.UdpConnectionListener.ReadCallback(System.IAsyncResult)
at System.Threading.Tasks.TaskToApm+TaskAsyncResult..ctor(System.Threading.Tasks.Task, System.Object, System.AsyncCallback)
at System.Net.Sockets.Socket.BeginReceiveFrom(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.Net.EndPoint ByRef, System.AsyncCallback, System.Object)
at Hazel.Udp.UdpConnectionListener.StartListeningForData()
at Hazel.Udp.UdpConnectionListener.ReadCallback(System.IAsyncResult)
at System.Threading.Tasks.TaskToApm+TaskAsyncResult..ctor(System.Threading.Tasks.Task, System.Object, System.AsyncCallback)
at System.Net.Sockets.Socket.BeginReceiveFrom(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.Net.EndPoint ByRef, System.AsyncCallback, System.Object)

It goes like this for around 10K lines!
then at the end it says

at Hazel.Udp.UdpConnectionListener.StartListeningForData()
at Hazel.Udp.UdpConnectionListener.Start()
at Prison_Life_Server.ServerProgram.Init(Boolean, Boolean)`

Any idea how can I fix it or can start to investigate? I suspect it might be related to DDOS attack but it happens instantly so I can't even track what message I'm getting.
on Linode I saw at one point I get around 1GBPS for few moments.

And I also have a question what is the benefit of using "ThreadLimitedUdpConnectionListener" instead of the regular one?

[Question] What is correct implementation for communicating consoles in other networks?

Hi,

First of all, is this the correct place to ask any questions? If not, tell me the other proper place and forgive please.

I'm working on online multiplay game (up to 4 people) and use Hazel to make P2P communication. I also use Unity.

I'm a newbie about network coding and I don't have confidence that this implementation is right... anyway, I did like below:

I made one console have both of UdpConnectionListener and UdpClientConnection. When host console makes a room via outer matchmake service (and get IP and port number etc.), host does ConnectAsync() to guest consoles. When host's ConnectAsync succeed, guests do ConnectAsync(). Then they can communicate each other.

The problem is:
This implementation works right when all consoles are in the same network. However, when they are in different network, host's or guest's ConnectAsync() doesn't succeed. The status doesn't change "Connecting" to "Connected."
(Curiously, the other side's ConnectAsync() succeeds.)

What should I do to fix this?

I'm sorry about my poor English.
Thank you for any help.

[Bug] A malformed packet can be used to leak old packets

  • MessageReader.GetSized() will return a reader without cleaning the buffer if there is a reader in the reader pool with a buffer that is big enough.
  • MessageReader.ReadString() doesn't do any bound checks. You can supply it any packed integer and it will happily try to read up to 0x7FFFFFFF bytes from the reader buffer. Any value higher than 0xFFFF will cause an exception to be thrown on servers that do not manually specify a buffer size. This issue can also be used to leak old packets. If the server echoes what it reads from the packet back to the client, and if the reader was recycled using MessageReader.GetSized(), the server will send the data of an old packet (probably from a different client) to the client.

Example client code that exploits this and example vulnerable server

Exception: Duplicate add MessageReader

Exception: Duplicate add MessageReader
at Hazel.ObjectPool`1[T].PutObject (T item) [0x00000] in <00000000000000000000000000000000>:0
at Hazel.Udp.UdpConnection.HandleReceive (Hazel.MessageReader message, System.Int32 bytesReceived) [0x00000] in <00000000000000000000000000000000>:0
at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00000] in <00000000000000000000000000000000>:0
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)

(I am using the unity udp client)
Happens only to people in Turkey that use the ISP "turkcell superonline", any idea where I can investigate to get to the root of the issue? after few seconds of connecting to the server they are getting the 6 pings error.
EDIT: I read online that this ISP is using some kind of IPs cluster for their customers, it might be part of the problem, maybe enabling IPv6 would solve it? but the problem is that you can only choose v4 or v6 but not both, in the docs it says you can choose both and I'm not sure how. Thanks!

Creating several clients in one application causes "Duplicate add MessageReader" exception

After last update i begin to receive this when create two clients in one application

Unhandled Exception: System.Exception: Duplicate add MessageReader
at Hazel.ObjectPool1.PutObject(T item) in /Users/mtiganov/dev/MS.Shaman.Server/Shaman.Server/Network/Hazel/ObjectPool.cs:line 74 at Hazel.MessageReader.Recycle() in /Users/mtiganov/dev/MS.Shaman.Server/Shaman.Server/Network/Hazel/MessageReader.cs:line 128 at Hazel.Udp.UdpConnectionListener.StartListeningForData() in /Users/mtiganov/dev/MS.Shaman.Server/Shaman.Server/Network/Hazel/Udp/UdpConnectionListener.cs:line 131 at Hazel.Udp.UdpConnectionListener.ReadCallback(IAsyncResult result) in /Users/mtiganov/dev/MS.Shaman.Server/Shaman.Server/Network/Hazel/Udp/UdpConnectionListener.cs:line 190 at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Net.ContextAwareResult.CompleteCallback() at System.Net.ContextAwareResult.<>c.<Complete>b__15_0(Object s) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Net.ContextAwareResult.Complete(IntPtr userToken) at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken) at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionCallback(Int32 numBytes, SocketError errorCode) at System.Net.Sockets.OverlappedAsyncResult.CompletionCallback(Int32 numBytes, Byte[] socketAddress, Int32 socketAddressSize, SocketFlags receivedFlags, SocketError errorCode) at System.Net.Sockets.SocketAsyncContext.BufferMemoryReceiveOperation.InvokeCallback(Boolean allowPooling) at System.Net.Sockets.SocketAsyncContext.OperationQueue1.ProcessAsyncOperation(TOperation op)
at System.Net.Sockets.SocketAsyncContext.OperationQueue`1.<>c.<.cctor>b__18_0(Object op)
at System.Threading.ThreadPoolWorkQueue.Dispatch()

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.