|
@ -9,7 +9,7 @@ namespace Apewer.Network |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
/// <summary>扩展方法。</summary>
|
|
|
/// <summary>扩展方法。</summary>
|
|
|
public static class Extension |
|
|
public static class Extensions |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
/// <summary>添加邮件账户。</summary>
|
|
|
/// <summary>添加邮件账户。</summary>
|
|
@ -155,15 +155,31 @@ namespace Apewer.Network |
|
|
return json; |
|
|
return json; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>检查 Socket 在线状态。</summary>
|
|
|
|
|
|
/// <exception cref="NotSupportedException"></exception>
|
|
|
|
|
|
/// <exception cref="ObjectDisposedException"></exception>
|
|
|
|
|
|
/// <exception cref="SocketException"></exception>
|
|
|
|
|
|
public static bool Online(this Socket socket) |
|
|
|
|
|
{ |
|
|
|
|
|
if (socket == null) return false; |
|
|
|
|
|
|
|
|
|
|
|
var pending = socket.Poll(1000, System.Net.Sockets.SelectMode.SelectRead); |
|
|
|
|
|
var available = socket.Available; |
|
|
|
|
|
var offline = pending && available == 0; |
|
|
|
|
|
return !offline; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>从套接字接收数据。</summary>
|
|
|
/// <summary>从套接字接收数据。</summary>
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
|
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
|
|
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
|
|
|
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
|
/// <exception cref="SocketException"></exception>
|
|
|
/// <exception cref="SocketException"></exception>
|
|
|
/// <exception cref="ObjectDisposedException"></exception>
|
|
|
/// <exception cref="ObjectDisposedException"></exception>
|
|
|
/// <exception cref="System.Security.SecurityException"></exception>
|
|
|
/// <exception cref="System.Security.SecurityException"></exception>
|
|
|
public static byte[] Receive(this Socket socket, int maxLength = 1024) |
|
|
public static byte[] Receive(this Socket socket, int maxLength = 1024) |
|
|
{ |
|
|
{ |
|
|
if (socket == null) throw new ArgumentNullException(nameof(socket)); |
|
|
if (socket == null) throw new ArgumentNullException(nameof(socket)); |
|
|
|
|
|
if (maxLength < 0 || maxLength > 65535) throw new ArgumentOutOfRangeException(nameof(maxLength)); |
|
|
|
|
|
|
|
|
var buffer = new byte[maxLength]; |
|
|
var buffer = new byte[maxLength]; |
|
|
var received = socket.Receive(buffer, 0, maxLength, SocketFlags.None); |
|
|
var received = socket.Receive(buffer, 0, maxLength, SocketFlags.None); |