206 lines
6.8 KiB
C#
206 lines
6.8 KiB
C#
namespace BF
|
||
{
|
||
/// <summary>
|
||
/// 用于创建连接的configuration
|
||
/// </summary>
|
||
public sealed class NetConnectConfiguration
|
||
{
|
||
private NetIncomingMessageType disabledMessageTypes = NetIncomingMessageType.DebugMessage;
|
||
|
||
private NetServiceType serviceType = NetServiceType.TCPService;
|
||
/// <summary>
|
||
/// 需要的服务类型,目前只支持TCPService,默认TCPService
|
||
/// </summary>
|
||
public NetServiceType ServiceType
|
||
{
|
||
get => serviceType;
|
||
}
|
||
|
||
private string uniqueIdentifier = "Default";
|
||
/// <summary>
|
||
/// 目标连接的ID,每一个连接应该唯一;相同ID不会再次创建
|
||
/// </summary>
|
||
public string UniqueIdentifier
|
||
{
|
||
get => uniqueIdentifier;
|
||
}
|
||
|
||
private string initializeExchangeRc4Key = "saoisc09341010SSAWrevnvn";
|
||
/// <summary>
|
||
/// socket连接成功后,交换密钥阶段的初始密钥,只能设置一次;
|
||
/// 需要和服务器统一;
|
||
/// </summary>
|
||
public string InitializeExchangeRc4Key
|
||
{
|
||
get => initializeExchangeRc4Key;
|
||
set => initializeExchangeRc4Key = value;
|
||
}
|
||
|
||
private int sendBufferCapacity = 1024 * 8;
|
||
/// <summary>
|
||
/// 等待socket发送buffer的大小,默认 1024*8
|
||
/// </summary>
|
||
public int SendBufferCapacity
|
||
{
|
||
get => sendBufferCapacity;
|
||
set => sendBufferCapacity = value;
|
||
}
|
||
|
||
private int receiveBufferCapacity = 1024 * 8;
|
||
/// <summary>
|
||
/// 接受socket数据的buffer,默认 1024*8
|
||
/// </summary>
|
||
public int ReceiveBufferCapacity
|
||
{
|
||
get => receiveBufferCapacity;
|
||
set => receiveBufferCapacity = value;
|
||
}
|
||
|
||
private int messagePoolCapacity = 20;
|
||
/// <summary>
|
||
/// incoming message pool 和 outgoing message pool 的最大值;默认 20
|
||
/// 对象池缓存的数量超过此值,新的回收message 将被释放,会有GC产生
|
||
/// </summary>
|
||
public int MessagePoolCapacity
|
||
{
|
||
get => messagePoolCapacity;
|
||
set => messagePoolCapacity = value;
|
||
}
|
||
|
||
private int alreadySendMessageCacheCount = 10;
|
||
/// <summary>
|
||
/// 缓存已经发送的消息数量,默认 15;超过缓存数量,连接成功后,发送消息会失败!!!可以适当大一点
|
||
/// </summary>
|
||
public int AlreadySendMessageCacheCount
|
||
{
|
||
get => alreadySendMessageCacheCount;
|
||
set => alreadySendMessageCacheCount = value;
|
||
}
|
||
|
||
private bool enableSilenceReconnect = true;
|
||
/// <summary>
|
||
/// 是否使用静默重连,默认启用;
|
||
/// 如果特殊原因禁用,应该应用层自己实现重连逻辑
|
||
/// </summary>
|
||
public bool EnableSilenceReconnect
|
||
{
|
||
get => enableSilenceReconnect;
|
||
set => enableSilenceReconnect = value;
|
||
}
|
||
|
||
private int autoReconnectCount = 3;
|
||
/// <summary>
|
||
/// 自动重连次数,到达次数后,连接断开 PeerDisconnect,默认 3 次
|
||
/// </summary>
|
||
public int AutoReconnectCount
|
||
{
|
||
get => autoReconnectCount;
|
||
set => autoReconnectCount = value;
|
||
}
|
||
|
||
private float reconnectTimeoutTime = 10;
|
||
/// <summary>
|
||
/// 每次重连超时时间,默认 10s;共尝试AutoReconnectCount次
|
||
/// </summary>
|
||
public float ReconnectTimeoutTime
|
||
{
|
||
get => reconnectTimeoutTime;
|
||
set => reconnectTimeoutTime = value;
|
||
}
|
||
|
||
private float reconnectBaseInterval = 5;
|
||
/// <summary>
|
||
/// 每次重连尝试的间隔时间,默认5s
|
||
/// </summary>
|
||
public float ReconnectBaseInterval
|
||
{
|
||
get => reconnectBaseInterval;
|
||
set => reconnectBaseInterval = value;
|
||
}
|
||
|
||
private bool enableHeartBeat = true;
|
||
/// <summary>
|
||
/// 是否启用心跳包,默认启用;
|
||
/// 静默心跳包可以在后台发送,如果没有特别明确的需求,应该启用
|
||
/// </summary>
|
||
public bool EnableHeartBeat
|
||
{
|
||
get => enableHeartBeat;
|
||
set => enableHeartBeat = value;
|
||
}
|
||
|
||
private float heartBeatInterval = 30f;
|
||
/// <summary>
|
||
/// 心跳间隔,默认30s,需要和服务器统一
|
||
/// </summary>
|
||
public float HeartBeatInterval
|
||
{
|
||
get => heartBeatInterval;
|
||
set => heartBeatInterval = value;
|
||
}
|
||
|
||
private int maxHeartBeatMissCount = 3;
|
||
/// <summary>
|
||
/// 1. 丢失心跳包的最大数量,默认3次
|
||
/// 2. 超过最大数量后,进行重连;
|
||
/// 3. 重连失败后,抛出PeerDisconnect消息,应用层处理(应用层应该调用close关闭连接)
|
||
/// </summary>
|
||
public int MaxHeartBeatMissCount
|
||
{
|
||
get => maxHeartBeatMissCount;
|
||
set => maxHeartBeatMissCount = value;
|
||
}
|
||
|
||
/// <summary>
|
||
///网络状态的统计(BI有需要上传一些网络状态数据), 暂时没有使用,为后续ULog启用的标识
|
||
/// </summary>
|
||
private bool enableStatistics = false;
|
||
public bool EnableStatistics
|
||
{
|
||
get => enableStatistics;
|
||
set => enableStatistics = value;
|
||
}
|
||
|
||
private float connectTimeout = 30;
|
||
/// <summary>
|
||
/// Connect 超时时间, 默认30s,
|
||
/// 连接超时后会先抛出 NetErrorCode.PeerConnectTimeout,然后抛出NetErrorCode.PeerDisconnect
|
||
/// </summary>
|
||
public float ConnectTimeout
|
||
{
|
||
get => connectTimeout;
|
||
set => connectTimeout = value;
|
||
}
|
||
|
||
public NetConnectConfiguration(NetServiceType serviceType, string uniqueIdentifier)
|
||
{
|
||
this.serviceType = serviceType;
|
||
this.uniqueIdentifier = uniqueIdentifier;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Enables receiving of the specified type of message
|
||
/// </summary>
|
||
public void EnableMessageType(NetIncomingMessageType type)
|
||
{
|
||
disabledMessageTypes &= (~type);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Disables receiving of the specified type of message
|
||
/// </summary>
|
||
public void DisableMessageType(NetIncomingMessageType type)
|
||
{
|
||
disabledMessageTypes |= type;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Gets if receiving of the specified type of message is enabled
|
||
/// </summary>
|
||
public bool IsMessageTypeEnabled(NetIncomingMessageType type)
|
||
{
|
||
return (disabledMessageTypes & type) != type;
|
||
}
|
||
|
||
}
|
||
} |