c1_unity/Assets/Scripts/Common/Network/NetConnectConfiguration.cs
2023-04-03 11:04:31 +08:00

206 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}