//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
using TouchSocket.Core;
namespace TouchSocket.Sockets
{
///
/// 客户端扩展类
///
public static class ClientExtension
{
///
/// 获取相关信息。格式:
///IPPort=IP:Port,ID=id,Protocol=Protocol
///
///
///
///
public static string GetInfo(this T client) where T : ISocketClient
{
return $"IP&Port={client.IP}:{client.Port},ID={client.ID},Protocol={client.Protocol}";
}
///
/// 获取服务器中,除自身以外的所有客户端id
///
///
///
///
public static IEnumerable GetOtherIDs(this T client) where T : ISocketClient
{
return client.Service.GetIDs().Where(id => id != client.ID);
}
///
/// 获取最后活动时间。即与的最近值。
///
///
///
///
public static DateTime GetLastActiveTime(this T client) where T : IClient
{
return client.LastSendTime > client.LastReceivedTime ? client.LastSendTime : client.LastReceivedTime;
}
///
/// 安全性发送关闭报文
///
///
///
///
public static bool TryShutdown(this T client, SocketShutdown how = SocketShutdown.Both) where T : ITcpClientBase
{
try
{
if (!client.MainSocket.Connected)
{
return false;
}
client?.MainSocket?.Shutdown(how);
return true;
}
catch
{
}
return false;
}
///
/// 安全性关闭。不会抛出异常。
///
///
///
///
public static void SafeClose(this T client, string msg) where T : ITcpClientBase
{
try
{
client.Close(msg);
}
catch
{
}
}
///
/// 获取IP和端口。
///
///
///
///
public static string GetIPPort(this T client) where T : ITcpClientBase
{
return $"{client.IP}:{client.Port}";
}
#region 连接
///
/// 尝试连接。不会抛出异常。
///
///
///
///
///
public static Result TryConnect(this TClient client, int timeout = 5000) where TClient : ITcpClient
{
try
{
client.Connect(timeout);
return new Result(ResultCode.Success);
}
catch (Exception ex)
{
return new Result(ResultCode.Exception, ex.Message);
}
}
///
/// 尝试连接。不会抛出异常。
///
///
///
///
///
public static async Task TryConnectAsync(this TClient client, int timeout = 5000) where TClient : ITcpClient
{
try
{
await client.ConnectAsync(timeout);
return new Result(ResultCode.Success);
}
catch (Exception ex)
{
return new Result(ResultCode.Exception, ex.Message);
}
}
#endregion 连接
}
}