Skip to content

Commit a8974ce

Browse files
committed
Add IsConnected & KeepAlive extension method.
1 parent 92b2e82 commit a8974ce

10 files changed

Lines changed: 95 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ static void Main()
3434
{
3535
//get client data
3636
string message = server.Encoding.GetString(data, 0, data.Length);
37-
Console.WriteLine($"server received data from {sender.RemoteEndPoint}£º{message}");
37+
Console.WriteLine($"server received data from {sender.RemoteEndPoint}: {message}");
3838
3939
//message = "server repley " + message;
4040
message = random.Next(100000000, 999999999).ToString();
4141
sender.Send(message);
4242
43-
Console.WriteLine($"server send data to {sender.RemoteEndPoint}£º{message}");
43+
Console.WriteLine($"server send data to {sender.RemoteEndPoint}: {message}");
4444
};
4545
4646
server.Listen();

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup Condition="'$(AssemblyName)'=='GodSharp.Socket'">
33
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
4-
<Version>1.0.2-preview1</Version>
4+
<Version>1.0.2-preview2</Version>
55
</PropertyGroup>
66
</Project>

src/GodSharp.Socket/Base/Sender.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
namespace GodSharp.Sockets
88
{
9+
/// <summary>
10+
/// Socket sender.
11+
/// </summary>
912
public class Sender
1013
{
1114
private Listener listener;
@@ -38,7 +41,7 @@ public class Sender
3841
/// <summary>
3942
/// Initializes a new instance of the <see cref="Sender"/> class.
4043
/// </summary>
41-
/// <param name="socket">The socket.</param>
44+
/// <param name="listener">The <paramref name="listener"/>.</param>
4245
/// <param name="encoding">The encoding.</param>
4346
internal Sender(Listener listener,Encoding encoding)
4447
{

src/GodSharp.Socket/Base/SocketBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55

66
namespace GodSharp.Sockets
77
{
8+
/// <summary>
9+
/// Socket base class.
10+
/// </summary>
811
public abstract class SocketBase
912
{
13+
/// <summary>
14+
/// The socket
15+
/// </summary>
1016
protected internal Socket socket = null;
1117

1218
/// <summary>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using GodSharp.Sockets.Internal.Extension;
2+
using System;
3+
using System.Net.Sockets;
4+
5+
namespace GodSharp.Sockets
6+
{
7+
/// <summary>
8+
/// Extension class.
9+
/// </summary>
10+
public static class Extensions
11+
{
12+
/// <summary>
13+
/// Determines whether this instance is connected.
14+
/// </summary>
15+
/// <param name="client">The client.</param>
16+
/// <returns>
17+
/// <c>true</c> if the specified client is connected; otherwise, <c>false</c>.
18+
/// </returns>
19+
public static bool IsConnected(this SocketBase client)
20+
{
21+
return client.socket.IsConnected();
22+
}
23+
24+
/// <summary>
25+
/// Set <see cref="SocketClient"/> keep-alive option.
26+
/// </summary>
27+
/// <param name="client">The socket client.</param>
28+
/// <param name="on">if set to <c>true</c> [on].</param>
29+
/// <param name="time">The time.</param>
30+
/// <param name="interval">The interval.</param>
31+
/// <returns>return <see cref="bool"/> value of KeepAlive value.</returns>
32+
public static bool KeepAlive(this SocketBase client, bool on = true, uint time = 5000, uint interval = 5000)
33+
{
34+
try
35+
{
36+
byte[] inOptionValues = new byte[12];
37+
38+
uint off = (uint)(on ? 1 : 0);
39+
40+
BitConverter.GetBytes(off).CopyTo(inOptionValues, 0);
41+
BitConverter.GetBytes(interval).CopyTo(inOptionValues, 4);
42+
BitConverter.GetBytes(interval).CopyTo(inOptionValues, 8);
43+
44+
client.socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
45+
46+
object obj = client.socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive);
47+
48+
return Convert.ToInt32(obj) == 1;
49+
}
50+
catch (Exception ex)
51+
{
52+
throw ex;
53+
}
54+
}
55+
}
56+
}

src/GodSharp.Socket/GodSharp.Socket.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
<PropertyGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
4040
<DefineConstants>NETSTANDARD2_0;$(AdditionalConstants)</DefineConstants>
4141
</PropertyGroup>
42+
<ItemGroup>
43+
<Compile Remove="Object\**" />
44+
<EmbeddedResource Remove="Object\**" />
45+
<None Remove="Object\**" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Remove="Base\SenderCollection.cs" />
49+
<Compile Remove="Internal\Listener\ListenerCollection.cs" />
50+
</ItemGroup>
4251

4352
<Import Project="..\..\build\version.props" />
4453
</Project>

src/GodSharp.Socket/Internal/Extension/SocketExtension.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal static class SocketExtension
99
/// <summary>
1010
/// Sends data to a connected <see cref="Socket"/>.
1111
/// </summary>
12+
/// <param name="socket">The socket.</param>
1213
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
1314
/// <param name="encoding">The <see cref="Encoding"/> for data.</param>
1415
/// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
@@ -21,6 +22,7 @@ public static int Send(this Socket socket, string data, Encoding encoding)
2122
/// <summary>
2223
/// Sends data to a connected <see cref="Socket" /> using the specified <see cref="SocketFlags" />.
2324
/// </summary>
25+
/// <param name="socket">The socket.</param>
2426
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
2527
/// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
2628
/// <param name="encoding">The <see cref="Encoding"/> for data.</param>
@@ -30,10 +32,11 @@ public static int Send(this Socket socket, string data, SocketFlags socketFlags,
3032
byte[] buffers = encoding.GetBytes(data);
3133
return socket.Send(buffers, socketFlags);
3234
}
33-
35+
3436
/// <summary>
3537
/// 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" />.
3638
/// </summary>
39+
/// <param name="socket">The socket.</param>
3740
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
3841
/// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
3942
/// <param name="socketError">A <see cref="SocketError" /> object that stores the socket error.</param>

src/GodSharp.Socket/Internal/Listener/Listener.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ internal class Listener
7171
/// <summary>
7272
/// Initializes a new instance of the <see cref="Listener"/> class.
7373
/// </summary>
74+
/// <param name="_base">The base.</param>
7475
/// <param name="socket">The socket.</param>
75-
/// <param name="onData">The on data.</param>
76-
/// <param name="onException">The on exception.</param>
77-
/// <param name="onClosed">The on closed.</param>
76+
/// <param name="type">The type.</param>
7877
internal Listener(SocketBase _base,Socket socket, ListenerType type)
7978
{
8079
parent = _base;

src/GodSharp.Socket/Internal/Util/Utils.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace GodSharp.Sockets.Internal.Util
77
{
8+
/// <summary>
9+
/// Socket utils.
10+
/// </summary>
811
public class Utils
912
{
1013
/// <summary>
@@ -47,6 +50,11 @@ internal static Exception ValidateHost(string host)
4750
}
4851
}
4952

53+
/// <summary>
54+
/// MD5s the specified string.
55+
/// </summary>
56+
/// <param name="str">The string.</param>
57+
/// <returns></returns>
5058
internal static byte[] Md5(string str)
5159
{
5260
if (string.IsNullOrEmpty(str) || str.Trim() == "")

src/GodSharp.Socket/Object/KeepAliveOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace GodSharp.Sockets
22
{
3+
/// <summary>
4+
///
5+
/// </summary>
36
public class KeepAliveOptions
47
{
58
/// <summary>

0 commit comments

Comments
 (0)