Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 177 additions & 81 deletions DarkRift.Client/BichannelClientConnection.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions DarkRift.Client/DarkRift.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\DarkRift.Client.xml</DocumentationFile>
<Configurations>Debug;Free</Configurations>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net3.5' ">
Expand Down
55 changes: 25 additions & 30 deletions DarkRift.Client/DarkRiftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ public void Connect(NetworkClientConnection connection)
{
setupMutex.Reset();

if (this.Connection != null)
this.Connection.Dispose();
this.Connection?.Dispose();

this.Connection = connection;
connection.MessageReceived = MessageReceivedHandler;
Expand Down Expand Up @@ -371,26 +370,25 @@ public bool Disconnect()
/// <param name="sendMode">The SendMode used to send the data.</param>
private void MessageReceivedHandler(MessageBuffer buffer, SendMode sendMode)
{
using (Message message = Message.Create(buffer, true))
using Message message = Message.Create(buffer, true);

//Record any ping acknowledgements
if (message.IsPingAcknowledgementMessage)
{
//Record any ping acknowledgements
if (message.IsPingAcknowledgementMessage)
try
{
try
{
RoundTripTime.RecordInboundPing(message.PingCode);
}
catch (KeyNotFoundException)
{
//Nothing we can really do about this
}
RoundTripTime.RecordInboundPing(message.PingCode);
}
catch (KeyNotFoundException)
{
//Nothing we can really do about this
}

if (message.IsCommandMessage)
HandleCommand(message);
else
HandleMessage(message, sendMode);
}

if (message.IsCommandMessage)
HandleCommand(message);
else
HandleMessage(message, sendMode);
}

/// <summary>
Expand All @@ -399,16 +397,14 @@ private void MessageReceivedHandler(MessageBuffer buffer, SendMode sendMode)
/// <param name="message">The message that was received.</param>
private void HandleCommand(Message message)
{
using (DarkRiftReader reader = message.GetReader())
using DarkRiftReader reader = message.GetReader();
switch ((CommandCode)message.Tag)
{
switch ((CommandCode)message.Tag)
{
case CommandCode.Configure:
ID = reader.ReadUInt16();
case CommandCode.Configure:
ID = reader.ReadUInt16();

setupMutex.Set();
break;
}
setupMutex.Set();
break;
}
}

Expand All @@ -420,8 +416,8 @@ private void HandleCommand(Message message)
private void HandleMessage(Message message, SendMode sendMode)
{
//Invoke for message received event
using (MessageReceivedEventArgs args = MessageReceivedEventArgs.Create(message, sendMode))
MessageReceived?.Invoke(this, args);
using MessageReceivedEventArgs args = MessageReceivedEventArgs.Create(message, sendMode);
MessageReceived?.Invoke(this, args);
}

/// <summary>
Expand Down Expand Up @@ -455,8 +451,7 @@ protected virtual void Dispose(bool disposing)
{
disposed = true;

if (Connection != null)
Connection.Dispose();
Connection?.Dispose();

setupMutex.Close();
}
Expand Down
1 change: 1 addition & 0 deletions DarkRift.Server/DarkRift.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\DarkRift.Server.xml</DocumentationFile>
<Configurations>Debug;Release</Configurations>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net3.5' ">
Expand Down
1 change: 1 addition & 0 deletions DarkRift/DarkRift.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Configurations>Debug;Release</Configurations>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net3.5' ">
Expand Down
15 changes: 13 additions & 2 deletions DarkRift/DarkRiftWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,18 @@ internal DarkRiftWriter()
{
serializeEventSingleton = new SerializeEvent(this);
}


/// <summary>
/// Clears out the buffer, making it empty and reusable.
/// </summary>
/// <remarks>Added by [email protected] 2022-05-20</remarks>
public void Clear()
{
Position = 0;
buffer.Count = 0;
buffer.Offset = 0;
}

/// <summary>
/// Writes a single byte to the writer.
/// </summary>
Expand Down Expand Up @@ -565,7 +576,7 @@ public void Write<T>(T[] value) where T : IDarkRiftSerializable
public void WriteRaw(byte[] bytes, int offset, int length)
{
buffer.EnsureLength(Position + length);
System.Buffer.BlockCopy(bytes, offset, buffer.Buffer, Position, length);
Buffer.BlockCopy(bytes, offset, buffer.Buffer, Position, length);

Position += length;
buffer.Count = Math.Max(Length, Position);
Expand Down
19 changes: 14 additions & 5 deletions DarkRift/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace DarkRift
/// </remarks>
public sealed class Message : IDisposable
{
/// <summary>
/// How many bytes is reserved for message header with value of rest of the message
/// </summary>
public const int HEADER_RESERVED_BYTES_COUNT = 4;

/// <summary>
/// Bitmask for the command message flag.
/// </summary>
Expand Down Expand Up @@ -420,19 +425,23 @@ public void MakePingAcknowledgementMessage(Message acknowledging)
//TODO DR3 Make this return an IMessageBuffer
internal MessageBuffer ToBuffer()
{
int headerLength = IsPingMessage || IsPingAcknowledgementMessage ? 5 : 3;
int headerLength = HEADER_RESERVED_BYTES_COUNT + (IsPingMessage || IsPingAcknowledgementMessage ? 5 : 3);
int totalLength = headerLength + DataLength;

MessageBuffer buffer = MessageBuffer.Create(totalLength);
buffer.Count = totalLength;

buffer.Buffer[buffer.Offset] = flags;
BigEndianHelper.WriteBytes(buffer.Buffer, buffer.Offset + 1, tag);
// write length of the rest of the message
BigEndianHelper.WriteBytes(buffer.Buffer, 0, totalLength - HEADER_RESERVED_BYTES_COUNT);

buffer.Buffer[HEADER_RESERVED_BYTES_COUNT] = flags;
BigEndianHelper.WriteBytes(buffer.Buffer, buffer.Offset + 1 + HEADER_RESERVED_BYTES_COUNT, tag);

if (IsPingMessage || IsPingAcknowledgementMessage)
BigEndianHelper.WriteBytes(buffer.Buffer, buffer.Offset + 3, PingCode);
BigEndianHelper.WriteBytes(buffer.Buffer, buffer.Offset + 3 + HEADER_RESERVED_BYTES_COUNT, PingCode);

//Due to poor design, here's un unavoidable memory copy! Hooray!
// Due to poor design, here's un unavoidable memory copy! Hooray!
// Copy the content of the message into rest of the new buffer
Buffer.BlockCopy(this.buffer.Buffer, this.buffer.Offset, buffer.Buffer, buffer.Offset + headerLength, this.buffer.Count);

return buffer;
Expand Down
3 changes: 3 additions & 0 deletions DarkRift/MessageBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ internal void EnsureLength(int newLength)
#region IDisposable Support
public void Dispose()
{
Count = 0;
Offset = 0;

//AutoRecyclingArray is reference counted, mark that we're no longer using it
backingBuffer.DecrementReference();

Expand Down
10 changes: 10 additions & 0 deletions DarkRift/ObjectCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ public static void ReturnSocketAsyncEventArgs(SocketAsyncEventArgs socketAsyncEv
ObjectCacheTestHelper.SocketAsyncEventArgsWasReturned();
#endif

// added safety restore to default state before going ito the pool
socketAsyncEventArgs.SocketFlags = SocketFlags.None;
socketAsyncEventArgs.SocketError = SocketError.Success;
socketAsyncEventArgs.SendPacketsSendSize = 0;
socketAsyncEventArgs.SendPacketsFlags = TransmitFileOptions.UseDefaultWorkerThread;
socketAsyncEventArgs.SendPacketsElements = null;
socketAsyncEventArgs.BufferList = null;
socketAsyncEventArgs.UserToken = null;
socketAsyncEventArgs.SetBuffer(null, 0, 0);

if (!socketAsyncEventArgsPool.ReturnInstance(socketAsyncEventArgs))
socketAsyncEventArgs.Dispose();
}
Expand Down