Skip to content

Commit 48ecc0a

Browse files
committed
added extension methods for Connection.
1 parent b1920d9 commit 48ecc0a

6 files changed

Lines changed: 187 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A easy to use socket server and client for .NET.
1616

1717
# Getting Started
1818

19-
See [samples](./src/samples).
19+
See [samples](./samples).
2020

2121
[0]: https://github.com/godsharp/GodSharp.Socket
2222
[si]: https://img.shields.io/github/languages/code-size/godsharp/GodSharp.Socket.svg?style=flat-square

build/version.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup Condition="'$(AssemblyName)'=='GodSharp.Socket'">
33
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
4-
<Version>2019.1.0.0-preview1</Version>
5-
<FileVersion>2019.1.0.0-preview1</FileVersion>
4+
<Version>2019.1.0.0-preview2</Version>
5+
<FileVersion>2019.1.0.0-preview</FileVersion>
66
</PropertyGroup>
77
</Project>

samples/GodSharp.Socket.TcpServerSample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void Main(string[] args)
4040
Console.WriteLine($"{c.RemoteEndPoint} exception:{c.Exception.StackTrace.ToString()}.");
4141
}
4242
};
43-
43+
4444
server.Start();
4545

4646
Console.WriteLine("GodSharp.Socket.TcpServer Started!");
Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,130 @@
1-
using System.Net;
1+
using System;
2+
using System.Net;
23
using System.Net.Sockets;
4+
using System.Text;
35

46
namespace GodSharp.Sockets
57
{
68
public static class SocketExtensions
79
{
810
/// <summary>
9-
/// Gets the TCP connection information.
11+
/// Sends data to a connected <see cref="Socket"/>.
1012
/// </summary>
1113
/// <param name="socket">The socket.</param>
14+
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
15+
/// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
16+
/// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
17+
public static int Send(this Socket socket, string data, Encoding encoding = null)
18+
{
19+
if (encoding == null) encoding = Encoding.UTF8;
20+
21+
return socket.Send(encoding.GetBytes(data));
22+
}
23+
24+
/// <summary>
25+
/// Begins the send.
26+
/// </summary>
27+
/// <param name="socket">The socket.</param>
28+
/// <param name="data">The data.</param>
29+
/// <param name="callback">The callback.</param>
30+
/// <param name="state">The state.</param>
31+
/// <param name="encoding">The encoding.</param>
32+
/// <returns></returns>
33+
public static IAsyncResult BeginSend(this Socket socket, string data, AsyncCallback callback, object state, Encoding encoding = null)
34+
{
35+
if (encoding == null) encoding = Encoding.UTF8;
36+
byte[] buffers = encoding.GetBytes(data);
37+
return socket.BeginSend(buffers, 0, buffers.Length, SocketFlags.None, callback, state);
38+
}
39+
40+
/// <summary>
41+
/// Sends data to a connected <see cref="Socket" /> using the specified <see cref="SocketFlags" />.
42+
/// </summary>
43+
/// <param name="socket">The socket.</param>
44+
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
45+
/// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
46+
/// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
47+
/// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
48+
public static int Send(this Socket socket, string data, SocketFlags socketFlags, Encoding encoding = null)
49+
{
50+
if (encoding == null) encoding = Encoding.UTF8;
51+
52+
return socket.Send(encoding.GetBytes(data), socketFlags);
53+
}
54+
55+
/// <summary>
56+
/// 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" />.
57+
/// </summary>
58+
/// <param name="socket">The socket.</param>
59+
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
60+
/// <param name="socketFlags">A bitwise combination of the <see cref="SocketFlags" /> values.</param>
61+
/// <param name="socketError">A <see cref="SocketError" /> object that stores the socket error.</param>
62+
/// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
63+
/// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
64+
public static int Send(this Socket socket, string data, SocketFlags socketFlags, out SocketError socketError, Encoding encoding = null)
65+
{
66+
if (encoding == null) encoding = Encoding.UTF8;
67+
byte[] buffers = encoding.GetBytes(data);
68+
return socket.Send(buffers, 0, buffers.Length, socketFlags, out socketError);
69+
}
70+
71+
/// <summary>
72+
/// Sends data to a <see cref="Socket"/>.
73+
/// </summary>
74+
/// <param name="socket">The socket.</param>
75+
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
76+
/// <param name="endPoint">The end point.</param>
77+
/// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
78+
/// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
79+
public static int SendTo(this Socket socket, string data, EndPoint endPoint, Encoding encoding = null)
80+
{
81+
if (encoding == null) encoding = Encoding.UTF8;
82+
83+
return socket.SendTo(encoding.GetBytes(data), endPoint);
84+
}
85+
86+
/// <summary>
87+
/// Sends the specified number of bytes of data to a <see cref="Socket" />, using the specified <see cref="SocketFlags" />.
88+
/// </summary>
89+
/// <param name="socket">The socket.</param>
90+
/// <param name="data">An string of type <see cref="string" /> that contains the data to be sent.</param>
91+
/// <param name="socketFlags">The socket flags.</param>
92+
/// <param name="endPoint">The end point.</param>
93+
/// <param name="encoding">The <see cref="Encoding"/> for data, default is <see cref="Encoding.UTF8"/>.</param>
94+
/// <returns>The number of bytes sent to the <see cref="Socket"/>.</returns>
95+
public static int SendTo(this Socket socket, string data, SocketFlags socketFlags, EndPoint endPoint, Encoding encoding = null)
96+
{
97+
if (encoding == null) encoding = Encoding.UTF8;
98+
99+
return socket.SendTo(encoding.GetBytes(data), socketFlags, endPoint);
100+
}
101+
102+
/// <summary>
103+
/// Begins the send to.
104+
/// </summary>
105+
/// <param name="socket">The socket.</param>
106+
/// <param name="buffers">The buffers.</param>
107+
/// <param name="remoteEP">The remote ep.</param>
108+
/// <param name="callback">The callback.</param>
109+
/// <param name="state">The state.</param>
12110
/// <returns></returns>
13-
//public static ConnectionInformation GetConnectionInformation(this Socket socket)
14-
//{
15-
// IPEndPoint remote = socket.RemoteEndPoint as IPEndPoint;
16-
// IPEndPoint local = socket.LocalEndPoint as IPEndPoint;
111+
public static IAsyncResult BeginSendTo(this Socket socket, byte[] buffers, EndPoint remoteEP, AsyncCallback callback, object state) => socket.BeginSendTo(buffers, 0, buffers.Length, SocketFlags.None, remoteEP, callback, state);
17112

18-
// return NetworkHelper.GetTcpConnectionInformation(local?.Port ?? 0, remote?.Port ?? 0) as ConnectionInformation;
19-
//}
113+
/// <summary>
114+
/// Begins the send to.
115+
/// </summary>
116+
/// <param name="socket">The socket.</param>
117+
/// <param name="data">The data.</param>
118+
/// <param name="remoteEP">The remote ep.</param>
119+
/// <param name="callback">The callback.</param>
120+
/// <param name="state">The state.</param>
121+
/// <param name="encoding">The encoding.</param>
122+
/// <returns></returns>
123+
public static IAsyncResult BeginSendTo(this Socket socket, string data, EndPoint remoteEP, AsyncCallback callback, object state, Encoding encoding = null)
124+
{
125+
if (encoding == null) encoding = Encoding.UTF8;
126+
byte[] buffers = encoding.GetBytes(data);
127+
return socket.BeginSendTo(buffers, 0, buffers.Length, SocketFlags.None, remoteEP, callback, state);
128+
}
20129
}
21130
}

src/GodSharp.Socket/Tcp/ITcpConnection.Extensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net.Sockets;
3+
using System.Text;
34

45
namespace GodSharp.Sockets
56
{
@@ -33,6 +34,15 @@ private static bool OnSend(ITcpConnection connection)
3334
/// <returns></returns>
3435
public static int Send(this ITcpConnection connection, byte[] buffer) => OnSend(connection, () => connection.Instance.Send(buffer), -1);
3536

37+
/// <summary>
38+
/// Sends the specified string.
39+
/// </summary>
40+
/// <param name="connection">The connection.</param>
41+
/// <param name="str">The string.</param>
42+
/// <param name="encoding">The encoding.</param>
43+
/// <returns></returns>
44+
public static int Send(this ITcpConnection connection, string str, Encoding encoding = null) => OnSend(connection, () => connection.Instance.Send(str, encoding), -1);
45+
3646
/// <summary>
3747
/// Sends the specified buffer.
3848
/// </summary>
@@ -52,6 +62,18 @@ private static bool OnSend(ITcpConnection connection)
5262
/// <returns></returns>
5363
public static bool SendAsync(this ITcpConnection connection, SocketAsyncEventArgs e) => OnSend(connection, () => connection.Instance.SendAsync(e), false);
5464

65+
/// <summary>
66+
/// Begins the send.
67+
/// </summary>
68+
/// <param name="connection">The connection.</param>
69+
/// <param name="buffer">The buffer.</param>
70+
/// <param name="offset">The offset.</param>
71+
/// <param name="size">The size.</param>
72+
/// <param name="socketFlags">The socket flags.</param>
73+
/// <param name="errorCode">The error code.</param>
74+
/// <param name="callback">The callback.</param>
75+
/// <param name="state">The state.</param>
76+
/// <returns></returns>
5577
public static IAsyncResult BeginSend(this ITcpConnection connection, byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
5678
{
5779
errorCode = SocketError.Success;
@@ -61,6 +83,17 @@ public static IAsyncResult BeginSend(this ITcpConnection connection, byte[] buff
6183
return connection.Instance.BeginSend(buffer, offset, size, socketFlags, out errorCode, callback, state);
6284
}
6385

86+
/// <summary>
87+
/// Begins the send.
88+
/// </summary>
89+
/// <param name="connection">The connection.</param>
90+
/// <param name="str">The string.</param>
91+
/// <param name="callback">The callback.</param>
92+
/// <param name="state">The state.</param>
93+
/// <param name="encoding">The encoding.</param>
94+
/// <returns></returns>
95+
public static IAsyncResult BeginSend(this ITcpConnection connection, string str, AsyncCallback callback, object state, Encoding encoding = null) => OnSend(connection, () => connection.Instance.BeginSend(str, callback, state, encoding), null);
96+
6497
/// <summary>
6598
/// Ends the send.
6699
/// </summary>

src/GodSharp.Socket/Udp/IUdpConnection.Extensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Net;
33
using System.Net.Sockets;
4+
using System.Text;
45

56
namespace GodSharp.Sockets
67
{
@@ -35,6 +36,15 @@ private static bool OnSend(IUdpConnection connection)
3536
/// <returns></returns>
3637
public static int SendTo(this IUdpConnection connection, byte[] buffer, EndPoint remoteEP) => OnSend(connection, () => connection.Instance.SendTo(buffer,remoteEP), -1);
3738

39+
/// <summary>
40+
/// Sends to.
41+
/// </summary>
42+
/// <param name="connection">The connection.</param>
43+
/// <param name="str">The string.</param>
44+
/// <param name="remoteEP">The remote ep.</param>
45+
/// <returns></returns>
46+
public static int SendTo(this IUdpConnection connection, string str, EndPoint remoteEP) => OnSend(connection, () => connection.Instance.SendTo(str, remoteEP), -1);
47+
3848
/// <summary>
3949
/// Sends to.
4050
/// </summary>
@@ -69,6 +79,29 @@ private static bool OnSend(IUdpConnection connection)
6979
/// <returns></returns>
7080
public static IAsyncResult BeginSendTo(this IUdpConnection connection, byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback callback, object state) => OnSend(connection, () => connection.Instance.BeginSendTo(buffer, offset, size, socketFlags, remoteEP, callback, state), null);
7181

82+
/// <summary>
83+
/// Begins the send to.
84+
/// </summary>
85+
/// <param name="connection">The connection.</param>
86+
/// <param name="buffer">The buffer.</param>
87+
/// <param name="remoteEP">The remote ep.</param>
88+
/// <param name="callback">The callback.</param>
89+
/// <param name="state">The state.</param>
90+
/// <returns></returns>
91+
public static IAsyncResult BeginSendTo(this IUdpConnection connection, byte[] buffer, EndPoint remoteEP, AsyncCallback callback, object state) => OnSend(connection, () => connection.Instance.BeginSendTo(buffer, remoteEP, callback, state), null);
92+
93+
/// <summary>
94+
/// Begins the send to.
95+
/// </summary>
96+
/// <param name="connection">The connection.</param>
97+
/// <param name="str">The string.</param>
98+
/// <param name="remoteEP">The remote ep.</param>
99+
/// <param name="callback">The callback.</param>
100+
/// <param name="state">The state.</param>
101+
/// <param name="encoding">The encoding.</param>
102+
/// <returns></returns>
103+
public static IAsyncResult BeginSendTo(this IUdpConnection connection, string str, EndPoint remoteEP, AsyncCallback callback, object state, Encoding encoding = null) => OnSend(connection, () => connection.Instance.BeginSendTo(str, remoteEP, callback, state, encoding), null);
104+
72105
/// <summary>
73106
/// Ends the send.
74107
/// </summary>

0 commit comments

Comments
 (0)