Configurable KeepAlive added

This commit is contained in:
Abdullah Saleem 2015-04-05 15:41:26 +05:00
parent 5d20aad9d6
commit 860b59ac7d
6 changed files with 172 additions and 2 deletions

View File

@ -63,6 +63,7 @@
<Compile Include="Core\Helper\UnsafeStreamCodec.cs" />
<Compile Include="Core\Information\OSInfo.cs" />
<Compile Include="Core\Misc\JpgCompression.cs" />
<Compile Include="Core\Misc\KeepAlive.cs" />
<Compile Include="Core\Packets\ClientPackets\DesktopResponse.cs" />
<Compile Include="Core\Packets\ClientPackets\DirectoryResponse.cs" />
<Compile Include="Core\Packets\ClientPackets\DownloadFileResponse.cs" />

View File

@ -70,6 +70,9 @@ namespace xClient.Core
Payload
}
public const uint KEEP_ALIVE_TIME = 5000;
public const uint KEEP_ALIVE_INTERVAL = 5000;
public const int HEADER_SIZE = 4;
public const int MAX_PACKET_SIZE = (1024 * 1024) * 1; //1MB
private Socket _handle;
@ -102,7 +105,8 @@ namespace xClient.Core
Initialize();
_handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
//_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
Misc.KeepAliveEx.SetKeepAliveEx(_handle, KEEP_ALIVE_INTERVAL, KEEP_ALIVE_TIME);
_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
_handle.NoDelay = true;

View File

@ -0,0 +1,79 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace xClient.Core.Misc
{
/// <summary>
/// Socket Extension for KeepAlive
/// </summary>
/// <Author>Abdullah Saleem</Author>
/// <Email>a.saleem2993@gmail.com</Email>
public static class KeepAliveEx
{
/// <summary>
/// Sets the Keep-Alive values for the current tcp connection
/// </summary>
/// <param name="socket">Current socket instance</param>
/// <param name="keepAliveInterval">Specifies how often TCP repeats keep-alive transmissions when no response is received. TCP sends keep-alive transmissions to verify that idle connections are still active. This prevents TCP from inadvertently disconnecting active lines.</param>
/// <param name="keepAliveTime">Specifies how often TCP sends keep-alive transmissions. TCP sends keep-alive transmissions to verify that an idle connection is still active. This entry is used when the remote system is responding to TCP. Otherwise, the interval between transmissions is determined by the value of the keepAliveInterval entry.</param>
public static void SetKeepAliveEx( /*this */ Socket socket, uint keepAliveInterval, uint keepAliveTime)
//extension removed, Missing System.Core.dll
{
var keepAlive = new TcpKeepAlive
{
onoff = 1,
keepaliveinterval = keepAliveInterval,
keepalivetime = keepAliveTime
};
int size = Marshal.SizeOf(keepAlive);
IntPtr keepAlivePtr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(keepAlive, keepAlivePtr, true);
var buffer = new byte[size];
Marshal.Copy(keepAlivePtr, buffer, 0, size);
Marshal.FreeHGlobal(keepAlivePtr);
socket.IOControl(IOControlCode.KeepAliveValues, buffer, null);
}
/// <summary>
/// Sets the Keep-Alive values for the current tcp connection
/// </summary>
/// <param name="socket">Current socket instance</param>
/// <param name="keepAliveInterval">Specifies how often TCP repeats keep-alive transmissions when no response is received. TCP sends keep-alive transmissions to verify that idle connections are still active. This prevents TCP from inadvertently disconnecting active lines.</param>
/// <param name="keepAliveTime">Specifies how often TCP sends keep-alive transmissions. TCP sends keep-alive transmissions to verify that an idle connection is still active. This entry is used when the remote system is responding to TCP. Otherwise, the interval between transmissions is determined by the value of the keepAliveInterval entry.</param>
public static void SetKeepAlive(Socket socket, uint keepAliveInterval, uint keepAliveTime)
{
// //Removed, LINQ
// //byte[] dataWord =
// // (new byte[] { 0x01, 0x00, 0x00, 0x00 }).Concat(BitConverter.GetBytes(keepAliveTime))
// // .Concat(BitConverter.GetBytes(keepAliveInterval))
// // .ToArray();
byte[] dataWord;
using (var memoryStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write(new byte[] {0x01, 0x00, 0x00, 0x00});
binaryWriter.Write(keepAliveTime);
binaryWriter.Write(keepAliveInterval);
}
dataWord = memoryStream.ToArray();
}
socket.IOControl(IOControlCode.KeepAliveValues, dataWord, null);
}
/// <summary>
/// A structure used by SetKeepAliveEx(obsolete) Method
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct TcpKeepAlive
{
internal uint onoff;
internal uint keepalivetime;
internal uint keepaliveinterval;
};
}
}

View File

@ -58,6 +58,9 @@ namespace xServer.Core
Payload
}
public const uint KEEP_ALIVE_TIME = 5000;
public const uint KEEP_ALIVE_INTERVAL = 5000;
public const int HEADER_SIZE = 4;
public const int MAX_PACKET_SIZE = (1024 * 1024) * 1; //1MB
private Socket _handle;
@ -94,7 +97,10 @@ namespace xServer.Core
_handle = sock;
_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
//_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
Misc.KeepAliveEx.SetKeepAliveEx(_handle,KEEP_ALIVE_INTERVAL,KEEP_ALIVE_TIME);
_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
_handle.NoDelay = true;

View File

@ -0,0 +1,79 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace xServer.Core.Misc
{
/// <summary>
/// Socket Extension for KeepAlive
/// </summary>
/// <Author>Abdullah Saleem</Author>
/// <Email>a.saleem2993@gmail.com</Email>
public static class KeepAliveEx
{
/// <summary>
/// Sets the Keep-Alive values for the current tcp connection
/// </summary>
/// <param name="socket">Current socket instance</param>
/// <param name="keepAliveInterval">Specifies how often TCP repeats keep-alive transmissions when no response is received. TCP sends keep-alive transmissions to verify that idle connections are still active. This prevents TCP from inadvertently disconnecting active lines.</param>
/// <param name="keepAliveTime">Specifies how often TCP sends keep-alive transmissions. TCP sends keep-alive transmissions to verify that an idle connection is still active. This entry is used when the remote system is responding to TCP. Otherwise, the interval between transmissions is determined by the value of the keepAliveInterval entry.</param>
public static void SetKeepAliveEx( /*this */ Socket socket, uint keepAliveInterval, uint keepAliveTime)
//extension removed, Missing System.Core.dll
{
var keepAlive = new TcpKeepAlive
{
onoff = 1,
keepaliveinterval = keepAliveInterval,
keepalivetime = keepAliveTime
};
int size = Marshal.SizeOf(keepAlive);
IntPtr keepAlivePtr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(keepAlive, keepAlivePtr, true);
var buffer = new byte[size];
Marshal.Copy(keepAlivePtr, buffer, 0, size);
Marshal.FreeHGlobal(keepAlivePtr);
socket.IOControl(IOControlCode.KeepAliveValues, buffer, null);
}
/// <summary>
/// Sets the Keep-Alive values for the current tcp connection
/// </summary>
/// <param name="socket">Current socket instance</param>
/// <param name="keepAliveInterval">Specifies how often TCP repeats keep-alive transmissions when no response is received. TCP sends keep-alive transmissions to verify that idle connections are still active. This prevents TCP from inadvertently disconnecting active lines.</param>
/// <param name="keepAliveTime">Specifies how often TCP sends keep-alive transmissions. TCP sends keep-alive transmissions to verify that an idle connection is still active. This entry is used when the remote system is responding to TCP. Otherwise, the interval between transmissions is determined by the value of the keepAliveInterval entry.</param>
public static void SetKeepAlive(Socket socket, uint keepAliveInterval, uint keepAliveTime)
{
// //Removed, LINQ
// //byte[] dataWord =
// // (new byte[] { 0x01, 0x00, 0x00, 0x00 }).Concat(BitConverter.GetBytes(keepAliveTime))
// // .Concat(BitConverter.GetBytes(keepAliveInterval))
// // .ToArray();
byte[] dataWord;
using (var memoryStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write(new byte[] { 0x01, 0x00, 0x00, 0x00 });
binaryWriter.Write(keepAliveTime);
binaryWriter.Write(keepAliveInterval);
}
dataWord = memoryStream.ToArray();
}
socket.IOControl(IOControlCode.KeepAliveValues, dataWord, null);
}
/// <summary>
/// A structure used by SetKeepAliveEx(obsolete) Method
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct TcpKeepAlive
{
internal uint onoff;
internal uint keepalivetime;
internal uint keepaliveinterval;
};
}
}

View File

@ -384,6 +384,7 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="Core\Misc\KeepAlive.cs" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>