Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Added headers for all outgoing packets.
Browse files Browse the repository at this point in the history
Work on reading incoming packets with the headers.
  • Loading branch information
DJGosnell committed May 29, 2018
1 parent e45f64e commit 6fc4a08
Showing 1 changed file with 49 additions and 24 deletions.
73 changes: 49 additions & 24 deletions src/DtronixMessageQueue/TcpSocket/TcpSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public enum State : byte
private byte[] _receivePartialBuffer = new byte[16];
private int _receivePartialBufferLength = 0;

private int receiveBodyLengthRemaining;

private RSACng _rsa;

/// <summary>
Expand Down Expand Up @@ -422,7 +424,7 @@ protected virtual void IoCompleted(object sender, SocketAsyncEventArgs e)
}


private int TransformDataBuffer(byte[] bufferSource, int offsetSource, int lengthSource, byte[] bufferDest, int offsetDest, byte[] transformBuffer, ref int transformBufferLength, ICryptoTransform transformer, bool preDebug, bool postDebug)
private short TransformDataBuffer(byte[] bufferSource, int offsetSource, int lengthSource, byte[] bufferDest, int offsetDest, byte[] transformBuffer, ref int transformBufferLength, ICryptoTransform transformer, bool preDebug, bool postDebug)
{
int transformLength = 0;
if (transformBufferLength > 0)
Expand Down Expand Up @@ -488,7 +490,7 @@ private int TransformDataBuffer(byte[] bufferSource, int offsetSource, int lengt
transformBufferLength = transformRemain;
}

return transformLength;
return (short)transformLength;
}

private enum EncryptedMessageType : byte
Expand Down Expand Up @@ -521,24 +523,30 @@ protected virtual void Send(byte[] buffer, int offset, int length)
return;

_writeSemaphore.Wait(-1);
int sendLength;
short sendLength;

if (_encryptor != null)
{

sendLength = TransformDataBuffer(buffer, offset, length, _sendArgs.Buffer, _sendArgs.Offset, _sendBuffer,
sendLength = TransformDataBuffer(buffer, offset, length, _sendArgs.Buffer, _sendArgs.Offset + 3, _sendBuffer,
ref _sendBufferLength, _encryptor, false, false);

if (sendLength == 0)
{
// If the packet was small, the message could have gone completely into the send buffer.
_writeSemaphore.Release();
return;
}
var lengthBytes = BitConverter.GetBytes(sendLength);
// Set the message type.
_sendArgs.Buffer[_sendArgs.Offset] = (byte)EncryptedMessageType.FullMessage;
_sendArgs.Buffer[_sendArgs.Offset + 1] = lengthBytes[0];
_sendArgs.Buffer[_sendArgs.Offset + 2] = lengthBytes[1];
}
else
{
Buffer.BlockCopy(buffer, offset, _sendArgs.Buffer, _sendArgs.Offset, length);
sendLength = length;
sendLength = (short)length;
}

// Update the buffer length.
Expand Down Expand Up @@ -589,7 +597,6 @@ protected void RecieveComplete(SocketAsyncEventArgs e)
HandleIncomingBytes(null);
return;
}
int receiveLength = 0;

if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
Expand All @@ -598,33 +605,51 @@ protected void RecieveComplete(SocketAsyncEventArgs e)

// Create a copy of these bytes.
byte[] buffer;

if (_decryptor != null)
{
receiveLength = TransformDataBuffer(_receiveArgs.Buffer, _receiveArgs.Offset, e.BytesTransferred,
_receiveTransformedBuffer, 0, _receivePartialBuffer,
ref _receivePartialBufferLength, _decryptor, false, false);
if (receiveLength == 0)
var currentPosition = 0;

while (currentPosition < e.BytesTransferred)
{
// If the packet was small, the message could have gone completely into the send buffer.
_writeSemaphore.Release();
return;
if (receiveBodyLengthRemaining == 0
&& (EncryptedMessageType)e.Buffer[currentPosition + e.Offset] == EncryptedMessageType.FullMessage)
{
receiveBodyLengthRemaining = BitConverter.ToInt16(e.Buffer, currentPosition + e.Offset + 1);
currentPosition += 3;
}

int readLength = Math.Min(e.BytesTransferred - currentPosition, receiveBodyLengthRemaining);
int receiveLength = TransformDataBuffer(e.Buffer, currentPosition + e.Offset, readLength,
_receiveTransformedBuffer, 0, _receivePartialBuffer,
ref _receivePartialBufferLength, _decryptor, false, false);


if (receiveLength == 0)
{
// If the packet was small, the message could have gone completely into the send buffer.
_writeSemaphore.Release();
break;
}

buffer = new byte[receiveLength];
Buffer.BlockCopy(_receiveTransformedBuffer, 0, buffer, 0, receiveLength);

HandleIncomingBytes(buffer);

if (readLength <= receiveBodyLengthRemaining)
{
currentPosition += readLength;
receiveBodyLengthRemaining -= readLength;
}
}
buffer = new byte[receiveLength];
Buffer.BlockCopy(_receiveTransformedBuffer, 0, buffer, 0, receiveLength);

//WriteBuffer(buffer);
}
else
{
buffer = new byte[_receiveArgs.BytesTransferred];
Buffer.BlockCopy(_receiveArgs.Buffer, _receiveArgs.Offset, buffer, 0, _receiveArgs.BytesTransferred);
}

if (CurrentState == State.Securing)
SecureConnectionReceive(buffer);
else
HandleIncomingBytes(buffer);
}

try
{
Expand Down Expand Up @@ -708,7 +733,7 @@ public virtual void Close(CloseReason reason)
/// <returns>String representation.</returns>
public override string ToString()
{
return $"{SocketHandler.Mode} RcpSocketSession;";
return $"{SocketHandler.Mode} TcpSocketSession;";
}

/// <summary>
Expand Down

0 comments on commit 6fc4a08

Please sign in to comment.