diff --git a/.gitignore b/.gitignore index 360e580..df74704 100644 --- a/.gitignore +++ b/.gitignore @@ -191,3 +191,6 @@ GeneratedArtifacts/ _Pvt_Extensions/ ModelManifest.xml *.ide + +# Visual Studio directories +.vs/ \ No newline at end of file diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index 5cf54e2..7c88762 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -65,6 +65,7 @@ + diff --git a/Hazel/Tcp/TcpConnection.cs b/Hazel/Tcp/TcpConnection.cs index 99d60c1..bed1208 100644 --- a/Hazel/Tcp/TcpConnection.cs +++ b/Hazel/Tcp/TcpConnection.cs @@ -190,6 +190,12 @@ void BodyReadCallback(byte[] bytes) //Begin receiving from the start try { + //Fire DataReceived event before reading next message. + if(TcpOptions.ForceReliableOrdering == true) + { + InvokeDataReceived(bytes, SendOption.FragmentedReliable); + } + StartWaitingForHeader(BodyReadCallback); } catch (Exception e) @@ -199,8 +205,11 @@ void BodyReadCallback(byte[] bytes) Statistics.LogFragmentedReceive(bytes.Length, bytes.Length + 4); - //Fire DataReceived event - InvokeDataReceived(bytes, SendOption.FragmentedReliable); + //Fire DataReceived event after reading next message for better throughput. + if(TcpOptions.ForceReliableOrdering == false) + { + InvokeDataReceived(bytes, SendOption.FragmentedReliable); + } } /// diff --git a/Hazel/Tcp/TcpOptions.cs b/Hazel/Tcp/TcpOptions.cs new file mode 100644 index 0000000..c400e12 --- /dev/null +++ b/Hazel/Tcp/TcpOptions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Hazel.Tcp +{ + public static class TcpOptions + { + /// + /// Force data received even to fire before reading the next message. + /// Setting to false may offer a slight performance advantage at the risk of compromising message ordering. + /// + public static bool ForceReliableOrdering { get; set; } = false; + } +}