Skip to content

Commit

Permalink
Add IsConnected & KeepAlive extension method.
Browse files Browse the repository at this point in the history
  • Loading branch information
seayxu committed Jan 3, 2018
1 parent 92b2e82 commit a8974ce
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ static void Main()
{
//get client data
string message = server.Encoding.GetString(data, 0, data.Length);
Console.WriteLine($"server received data from {sender.RemoteEndPoint}£º{message}");
Console.WriteLine($"server received data from {sender.RemoteEndPoint}: {message}");
//message = "server repley " + message;
message = random.Next(100000000, 999999999).ToString();
sender.Send(message);
Console.WriteLine($"server send data to {sender.RemoteEndPoint}£º{message}");
Console.WriteLine($"server send data to {sender.RemoteEndPoint}: {message}");
};
server.Listen();
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup Condition="'$(AssemblyName)'=='GodSharp.Socket'">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.2-preview1</Version>
<Version>1.0.2-preview2</Version>
</PropertyGroup>
</Project>
5 changes: 4 additions & 1 deletion src/GodSharp.Socket/Base/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace GodSharp.Sockets
{
/// <summary>
/// Socket sender.
/// </summary>
public class Sender
{
private Listener listener;
Expand Down Expand Up @@ -38,7 +41,7 @@ public class Sender
/// <summary>
/// Initializes a new instance of the <see cref="Sender"/> class.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="listener">The <paramref name="listener"/>.</param>
/// <param name="encoding">The encoding.</param>
internal Sender(Listener listener,Encoding encoding)
{
Expand Down
6 changes: 6 additions & 0 deletions src/GodSharp.Socket/Base/SocketBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@

namespace GodSharp.Sockets
{
/// <summary>
/// Socket base class.
/// </summary>
public abstract class SocketBase
{
/// <summary>
/// The socket
/// </summary>
protected internal Socket socket = null;

/// <summary>
Expand Down
56 changes: 56 additions & 0 deletions src/GodSharp.Socket/Extension/SocketClient.Extension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using GodSharp.Sockets.Internal.Extension;
using System;
using System.Net.Sockets;

namespace GodSharp.Sockets
{
/// <summary>
/// Extension class.
/// </summary>
public static class Extensions
{
/// <summary>
/// Determines whether this instance is connected.
/// </summary>
/// <param name="client">The client.</param>
/// <returns>
/// <c>true</c> if the specified client is connected; otherwise, <c>false</c>.
/// </returns>
public static bool IsConnected(this SocketBase client)
{
return client.socket.IsConnected();
}

/// <summary>
/// Set <see cref="SocketClient"/> keep-alive option.
/// </summary>
/// <param name="client">The socket client.</param>
/// <param name="on">if set to <c>true</c> [on].</param>
/// <param name="time">The time.</param>
/// <param name="interval">The interval.</param>
/// <returns>return <see cref="bool"/> value of KeepAlive value.</returns>
public static bool KeepAlive(this SocketBase client, bool on = true, uint time = 5000, uint interval = 5000)
{
try
{
byte[] inOptionValues = new byte[12];

uint off = (uint)(on ? 1 : 0);

BitConverter.GetBytes(off).CopyTo(inOptionValues, 0);
BitConverter.GetBytes(interval).CopyTo(inOptionValues, 4);
BitConverter.GetBytes(interval).CopyTo(inOptionValues, 8);

client.socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);

object obj = client.socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive);

return Convert.ToInt32(obj) == 1;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
9 changes: 9 additions & 0 deletions src/GodSharp.Socket/GodSharp.Socket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
<PropertyGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<DefineConstants>NETSTANDARD2_0;$(AdditionalConstants)</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Object\**" />
<EmbeddedResource Remove="Object\**" />
<None Remove="Object\**" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Base\SenderCollection.cs" />
<Compile Remove="Internal\Listener\ListenerCollection.cs" />
</ItemGroup>

<Import Project="..\..\build\version.props" />
</Project>
5 changes: 4 additions & 1 deletion src/GodSharp.Socket/Internal/Extension/SocketExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal static class SocketExtension
/// <summary>
/// Sends data to a connected <see cref="Socket"/>.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
/// <param name="encoding">The <see cref="Encoding"/> for data.</param>
/// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
Expand All @@ -21,6 +22,7 @@ public static int Send(this Socket socket, string data, Encoding encoding)
/// <summary>
/// Sends data to a connected <see cref="Socket" /> using the specified <see cref="SocketFlags" />.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
/// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
/// <param name="encoding">The <see cref="Encoding"/> for data.</param>
Expand All @@ -30,10 +32,11 @@ public static int Send(this Socket socket, string data, SocketFlags socketFlags,
byte[] buffers = encoding.GetBytes(data);
return socket.Send(buffers, socketFlags);
}

/// <summary>
/// Sends the specified number of bytes of data to a connected <see cref="Socket" />, starting at the specified offset, and using the specified <see cref="SocketFlags" />.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
/// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
/// <param name="socketError">A <see cref="SocketError" /> object that stores the socket error.</param>
Expand Down
5 changes: 2 additions & 3 deletions src/GodSharp.Socket/Internal/Listener/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ internal class Listener
/// <summary>
/// Initializes a new instance of the <see cref="Listener"/> class.
/// </summary>
/// <param name="_base">The base.</param>
/// <param name="socket">The socket.</param>
/// <param name="onData">The on data.</param>
/// <param name="onException">The on exception.</param>
/// <param name="onClosed">The on closed.</param>
/// <param name="type">The type.</param>
internal Listener(SocketBase _base,Socket socket, ListenerType type)
{
parent = _base;
Expand Down
8 changes: 8 additions & 0 deletions src/GodSharp.Socket/Internal/Util/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace GodSharp.Sockets.Internal.Util
{
/// <summary>
/// Socket utils.
/// </summary>
public class Utils
{
/// <summary>
Expand Down Expand Up @@ -47,6 +50,11 @@ internal static Exception ValidateHost(string host)
}
}

/// <summary>
/// MD5s the specified string.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
internal static byte[] Md5(string str)
{
if (string.IsNullOrEmpty(str) || str.Trim() == "")
Expand Down
3 changes: 3 additions & 0 deletions src/GodSharp.Socket/Object/KeepAliveOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace GodSharp.Sockets
{
/// <summary>
///
/// </summary>
public class KeepAliveOptions
{
/// <summary>
Expand Down

0 comments on commit a8974ce

Please sign in to comment.