Browse Source

Socket 扩展方法移至 NetworkUtility

master
王厅 6 days ago
parent
commit
650d29bb12
  1. 40
      Apewer/Network/Extensions.cs
  2. 2
      Apewer/Network/TcpClient.cs
  3. 44
      Apewer/NetworkUtility.cs

40
Apewer/Network/Extensions.cs

@ -155,46 +155,6 @@ namespace Apewer.Network
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>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SocketException"></exception>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="System.Security.SecurityException"></exception>
public static byte[] Receive(this Socket socket, int maxLength = 1024)
{
if (socket == null) throw new ArgumentNullException(nameof(socket));
if (maxLength < 0 || maxLength > 65535) throw new ArgumentOutOfRangeException(nameof(maxLength));
var buffer = new byte[maxLength];
var received = socket.Receive(buffer, 0, maxLength, SocketFlags.None);
if (received < maxLength)
{
var newBuffer = new byte[received];
Array.Copy(buffer, newBuffer, received);
return newBuffer;
}
else
{
return buffer;
}
}
}
}

2
Apewer/Network/TcpClient.cs

@ -15,7 +15,7 @@ namespace Apewer.Network
public Socket Socket { get => _socket; }
/// <summary>在线。</summary>
public bool Online { get => Extensions.Online(_socket); }
public bool Online { get => NetworkUtility.Online(_socket); }
/// <summary>本地终结点。</summary>
public IPEndPoint LocalEndPoint { get; private set; }

44
Apewer/NetworkUtility.cs

@ -591,6 +591,50 @@ namespace Apewer
#endregion
#region Socket
/// <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>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="SocketException"></exception>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="System.Security.SecurityException"></exception>
public static byte[] Receive(this Socket socket, int maxLength = 1024)
{
if (socket == null) throw new ArgumentNullException(nameof(socket));
if (maxLength < 0 || maxLength > 65535) throw new ArgumentOutOfRangeException(nameof(maxLength));
var buffer = new byte[maxLength];
var received = socket.Receive(buffer, 0, maxLength, SocketFlags.None);
if (received < maxLength)
{
var newBuffer = new byte[received];
Array.Copy(buffer, newBuffer, received);
return newBuffer;
}
else
{
return buffer;
}
}
#endregion
}
}

Loading…
Cancel
Save