Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuovi committed Sep 7, 2023
1 parent 10ab6fb commit 2605e8f
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 297 deletions.
164 changes: 164 additions & 0 deletions Net/BaseSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;
using System.Threading;

/****************************************************************
* Copyright © (2023) www.eelf.cn All Rights Reserved. *
* Author : jacky *
* QQ : 7092734 *
* Email : jacky@eelf.cn *
* Site : www.eelf.cn *
* Create Time : 2023-09-07 11:32:50 *
* Version : v 1.0.0 *
* CLR Version : 4.0.30319.42000 *
*****************************************************************/
namespace XiaoFeng.Net
{
/// <summary>
/// 基础网络库
/// </summary>
public abstract class BaseSocket: Disposable,INetSocket
{
#region 构造器
/// <summary>
/// 无参构造器
/// </summary>
public BaseSocket()
{

}
#endregion

#region 事件
/// <summary>
/// 启动事件
/// </summary>
public abstract event OnStartEventHandler OnStart;
/// <summary>
/// 停止事件
/// </summary>
public abstract event OnStopEventHandler OnStop;
/// <summary>
/// 客户端错误信息事件
/// </summary>
public abstract event OnClientErrorEventHandler OnClientError;
/// <summary>
/// 接收消息(string)事件
/// </summary>
public abstract event OnMessageEventHandler OnMessage;
/// <summary>
/// 接收消息(byte[])事件
/// </summary>
public abstract event OnMessageByteEventHandler OnMessageByte;
/// <summary>
/// 认证事件
/// </summary>
public abstract event OnAuthenticationEventHandler OnAuthentication;
#endregion

#region 属性
///<inheritdoc/>
public Encoding Encoding { get; set; } = Encoding.UTF8;
/// <summary>
/// 激活状态
/// </summary>
internal Boolean _Active = false;
///<inheritdoc/>
public Boolean Active { get => this._Active; }
///<inheritdoc/>
public SocketState SocketState { get; set; } = SocketState.Idle;
///<inheritdoc/>
public SslProtocols SslProtocols { get; set; } = SslProtocols.None;
///<inheritdoc/>
public SocketType SocketType { get; set; } = SocketType.Stream;
///<inheritdoc/>
public ProtocolType ProtocolType { get; set; } = ProtocolType.Tcp;
///<inheritdoc/>
public Boolean NoDelay { get; set; } = false;
///<inheritdoc/>
public int ReceiveTimeout { get; set; } = -1;
///<inheritdoc/>
public int SendTimeout { get; set; } = -1;
/// <summary>
/// 指定之后连接服务端将超时的时间长度
/// </summary>
private int _ConnectTimeout = 0;
/// <inheritdoc/>
public int ConnectTimeout
{
get
{
if (this._ConnectTimeout >= 1 && this._ConnectTimeout < 500)
this._ConnectTimeout = 500;
if (this._ConnectTimeout <= 0) this._ConnectTimeout = 0;
return this._ConnectTimeout;
}
set
{
if (value >= 1 && value < 500) this._ConnectTimeout = 500;
if (value <= 0) this._ConnectTimeout = 0;
this._ConnectTimeout = value;
}
}
///<inheritdoc/>
public int ReceiveBufferSize { get; set; } = 8192;
///<inheritdoc/>
public int SendBufferSize { get; set; } = 8192;
///<inheritdoc/>
public IPEndPoint EndPoint { get; set; }
///<inheritdoc/>
public CancellationTokenSource CancelToken { get; set; } = new CancellationTokenSource();
///<inheritdoc/>
public virtual Boolean ExclusiveAddressUse { get; set; }
///<inheritdoc/>
public SocketDataType DataType { get; set; } = SocketDataType.String;
/// <summary>
/// 网络延时时长 默认为10毫秒
/// </summary>
private int _NetworkDelay = 10;
///<inheritdoc/>
public int NetworkDelay
{
get
{
if (this._NetworkDelay < 0) this._NetworkDelay = 1;
else if (this._NetworkDelay > 10_000) this._NetworkDelay = 5_000;
return this._NetworkDelay;
}
set
{
if (value < 0) this._NetworkDelay = 0;
else if (value > 10_000) this._NetworkDelay = 5_000;
this._NetworkDelay = value;
}
}
#endregion

#region 回调事件
///<inheritdoc/>
public abstract void StartEventHandler();
///<inheritdoc/>
public abstract void StopEventHandler();
#endregion

#region 方法
/// <summary>
/// 启动
/// </summary>
public abstract void Start();
/// <summary>
/// 停止
/// </summary>
public abstract void Stop();
/// <summary>
/// 获取Socket
/// </summary>
/// <returns>返回Soccket</returns>
public abstract Socket GetSocket();
#endregion
}
}
99 changes: 11 additions & 88 deletions Net/SocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace XiaoFeng.Net
/// <summary>
/// Socket客户端
/// </summary>
public class SocketClient : Disposable, ISocketClient
public class SocketClient : BaseSocket, ISocketClient
{
#region 构造器
/// <summary>
Expand Down Expand Up @@ -87,65 +87,16 @@ public SocketClient(IPEndPoint endPoint)
/// </summary>
private Socket Client { get; set; }
///<inheritdoc/>
public Encoding Encoding { get; set; } = Encoding.UTF8;
///<inheritdoc/>
public SocketType SocketType { get; set; } = SocketType.Stream;
///<inheritdoc/>
public ProtocolType ProtocolType { get; set; } = ProtocolType.Tcp;
///<inheritdoc/>
public ConnectionType ConnectionType { get; set; } = ConnectionType.Socket;
///<inheritdoc/>
public Boolean NoDelay { get; set; } = false;
///<inheritdoc/>
public int ReceiveTimeout { get; set; } = -1;
///<inheritdoc/>
public int SendTimeout { get; set; } = -1;
/// <summary>
/// 指定之后连接服务端将超时的时间长度
/// </summary>
private int _ConnectTimeout = 0;
/// <inheritdoc/>
public int ConnectTimeout
{
get
{
if (this._ConnectTimeout >= 1 && this._ConnectTimeout < 500)
this._ConnectTimeout = 500;
if (this._ConnectTimeout <= 0) this._ConnectTimeout = 0;
return this._ConnectTimeout;
}
set
{
if (value >= 1 && value < 500) this._ConnectTimeout = 500;
if (value <= 0) this._ConnectTimeout = 0;
this._ConnectTimeout = value;
}
}
///<inheritdoc/>
public int ReceiveBufferSize { get; set; } = 8192;
///<inheritdoc/>
public int SendBufferSize { get; set; } = 8192;
/// <summary>
/// 标签数据
/// </summary>
private Dictionary<string, object> TagsData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// 激活状态
/// </summary>
private Boolean _Active = false;
///<inheritdoc/>
public Boolean Active { get => this._Active; }
///<inheritdoc/>
public SocketState SocketState { get; set; } = SocketState.Idle;
///<inheritdoc/>
public SslProtocols SslProtocols { get; set; } = SslProtocols.None;
///<inheritdoc/>
public X509CertificateCollection ClientCertificates { get; set; }
///<inheritdoc/>
private X509Certificate Certificate { get; set; }
///<inheritdoc/>
public IPEndPoint EndPoint { get; set; }
///<inheritdoc/>
public int Available => this.Client?.Available ?? 0;
///<inheritdoc/>
public bool Connected => Client?.Connected ?? false;
Expand All @@ -158,7 +109,7 @@ public int ConnectTimeout
///<inheritdoc/>
public string HostName { get; set; }
///<inheritdoc/>
public bool ExclusiveAddressUse
public override Boolean ExclusiveAddressUse
{
get { return this.Client?.ExclusiveAddressUse ?? false; }
set
Expand All @@ -177,8 +128,6 @@ public bool ExclusiveAddressUse
/// SSL 网络流
/// </summary>
private SslStream SslStream;
///<inheritdoc/>
public CancellationTokenSource CancelToken { get; set; } = new CancellationTokenSource();
/// <summary>
/// 权限认证
/// </summary>
Expand All @@ -196,8 +145,6 @@ public bool ExclusiveAddressUse
///<inheritdoc/>
public string RequestHeader => this._RequestHeader;
///<inheritdoc/>
public SocketDataType DataType { get; set; } = SocketDataType.String;
///<inheritdoc/>
public Boolean IsPing { get; set; }
///<inheritdoc/>
public int PingTime { get; set; } = 120;
Expand All @@ -208,51 +155,31 @@ public bool ExclusiveAddressUse
///<inheritdoc/>
public DateTime ConnectedTime { get; set; }
/// <summary>
/// 网络延时时长 默认为10毫秒
/// </summary>
private int _NetworkDelay = 10;
///<inheritdoc/>
public int NetworkDelay
{
get
{
if (this._NetworkDelay < 0) this._NetworkDelay = 1;
else if (this._NetworkDelay > 10_000) this._NetworkDelay = 5_000;
return this._NetworkDelay;
}
set
{
if (value < 0) this._NetworkDelay = 0;
else if (value > 10_000) this._NetworkDelay = 5_000;
this._NetworkDelay = value;
}
}
/// <summary>
/// 频道KEY
/// </summary>
private const string CHANNEL_KEY = "CHANNELS";
#endregion

#region 事件
///<inheritdoc/>
public event OnStartEventHandler OnStart;
public override event OnStartEventHandler OnStart;
///<inheritdoc/>
public event OnStopEventHandler OnStop;
public override event OnStopEventHandler OnStop;
///<inheritdoc/>
public event OnClientErrorEventHandler OnClientError;
public override event OnClientErrorEventHandler OnClientError;
///<inheritdoc/>
public event OnMessageEventHandler OnMessage;
public override event OnMessageEventHandler OnMessage;
///<inheritdoc/>
public event OnMessageByteEventHandler OnMessageByte;
public override event OnMessageByteEventHandler OnMessageByte;
///<inheritdoc/>
public event OnAuthenticationEventHandler OnAuthentication;
public override event OnAuthenticationEventHandler OnAuthentication;
#endregion

#region 方法

#region 获取Socket
/// <inheritdoc/>
public Socket GetSocket() => this.Client;
public override Socket GetSocket() => this.Client;
#endregion

#region 连接
Expand Down Expand Up @@ -552,7 +479,7 @@ private async Task<SocketError> CompleteConnectAsync(Func<Task<SocketError>> tas

#region 启动
///<inheritdoc/>
public virtual void Start()
public override void Start()
{
if (this.IsServer)
this.CheckClientConnectionTypeAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -627,7 +554,7 @@ Task AutoReceviceData() =>

#region 停止
///<inheritdoc/>
public virtual void Stop()
public override void Stop()
{
this.OnStop?.Invoke(this, EventArgs.Empty);
if (this.Active)
Expand Down Expand Up @@ -1239,10 +1166,6 @@ private async Task<int> NetStreamSendAsync(byte[] buffers, OpCode opCode = OpCod

#region 事件回调
///<inheritdoc/>
public void StartEventHandler() => OnStart?.Invoke(this, EventArgs.Empty);
///<inheritdoc/>
public void StopEventHandler() => OnStop?.Invoke(this, EventArgs.Empty);
///<inheritdoc/>
public void ClientErrorEventHandler(IPEndPoint endPoint, Exception e) => OnClientError?.Invoke(this, endPoint, e);
///<inheritdoc/>
public void MessageEventHandler(string message) => OnMessage?.Invoke(this, message, EventArgs.Empty);
Expand Down
Loading

0 comments on commit 2605e8f

Please sign in to comment.