/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP服务调用配置</param> /// <param name="verify">获取客户端请求线程调用类型</param> /// <param name="serverCallQueue">自定义队列</param> /// <param name="onCustomData">自定义数据包处理</param> /// <param name="log">日志接口</param> /// <param name="getSocketThreadCallType">同步验证接口</param> /// <param name="callQueueCount">独占的 TCP 服务器端同步调用队列数量</param> /// <param name="isCallQueueLink">是否提供独占的 TCP 服务器端同步调用队列(低优先级)</param> /// <param name="isSynchronousVerifyMethod">验证函数是否同步调用</param> internal Server(ServerBaseAttribute attribute, Func <System.Net.Sockets.Socket, bool> verify, AutoCSer.Net.TcpServer.IServerCallQueueSet serverCallQueue, Action <SubArray <byte> > onCustomData, ILog log, AutoCSer.Threading.Thread.CallType getSocketThreadCallType, int callQueueCount, bool isCallQueueLink, bool isSynchronousVerifyMethod) : base(attribute, verify, log, callQueueCount, isCallQueueLink, isSynchronousVerifyMethod) { this.serverCallQueue = serverCallQueue; this.onCustomData = onCustomData; this.getSocketThreadCallType = getSocketThreadCallType; }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP服务调用配置</param> /// <param name="verify">获取客户端请求线程调用类型</param> /// <param name="extendCommandBits">扩展服务命令二进制位数</param> /// <param name="log">日志接口</param> /// <param name="callQueueCount">独占的 TCP 服务器端同步调用队列数量</param> /// <param name="isCallQueueLink">是否提供独占的 TCP 服务器端同步调用队列(低优先级)</param> /// <param name="isSynchronousVerifyMethod">验证函数是否同步调用</param> internal ServerBase(ServerBaseAttribute attribute, Func <System.Net.Sockets.Socket, bool> verify, byte extendCommandBits, ILog log, int callQueueCount, bool isCallQueueLink, bool isSynchronousVerifyMethod) : base(attribute, attribute.GetReceiveBufferSize, attribute.GetSendBufferSize, attribute.GetServerSendBufferMaxSize, log) { #if !NOJIT if (extendCommandBits == 0) { extendServerSet = ExtendServerSet.Null; } else if (extendCommandBits < 9 || extendCommandBits > 16) { throw new IndexOutOfRangeException("扩展服务命令二进制位数 " + extendCommandBits.toString() + " 超出范围 9-16"); } else { extendServerSet = new ExtendServerSet(this, extendCommandBits); } #endif this.verify = verify; if (callQueueCount > 0) { if (callQueueCount == 1) { CallQueue = new ServerCallCanDisposableQueue(true, Log); if (isCallQueueLink) { CallQueueLink = CallQueue.CreateLink(); } } else { CallQueueArray = new KeyValue <ServerCallCanDisposableQueue, Threading.TaskQueueThread <ServerCallBase> .LowPriorityLink> [Math.Min(callQueueCount, 256)]; if (isCallQueueLink) { for (int index = 0; index != CallQueueArray.Length; ++index) { ServerCallCanDisposableQueue callQueue = new ServerCallCanDisposableQueue(true, Log); CallQueueArray[index].Set(callQueue, callQueue.CreateLink()); } CallQueueLink = CallQueueArray[0].Value; } else { for (int index = 0; index != CallQueueArray.Length; ++index) { CallQueueArray[index].Key = new ServerCallCanDisposableQueue(true, Log); } } CallQueue = CallQueueArray[0].Key; } } ServerAttribute.Set(attribute); Port = attribute.Port; IpAddress = HostPort.HostToIPAddress(attribute.Host, Log); int binaryDeSerializeMaxArraySize = attribute.GetBinaryDeSerializeMaxArraySize; BinaryDeSerializeConfig = AutoCSer.Net.TcpOpenServer.ServerAttribute.GetBinaryDeSerializeConfig(binaryDeSerializeMaxArraySize <= 0 ? AutoCSer.BinaryDeSerializer.DefaultConfig.MaxArraySize : binaryDeSerializeMaxArraySize); VerifyMethodCount = isSynchronousVerifyMethod ? Server.DefaultVerifyMethodCount : (byte)(Server.DefaultVerifyMethodCount + 1); }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP服务调用配置</param> /// <param name="log">日志接口</param> /// <param name="onCustomData">自定义数据包处理</param> internal ClientBase(ServerBaseAttribute attribute, ILog log, Action <SubArray <byte> > onCustomData) : base(attribute, attribute.GetSendBufferSize, attribute.GetReceiveBufferSize, attribute.ClientSendBufferMaxSize, log) { OutputSleep = attribute.GetClientOutputSleep; FristTryCreateSleep = Math.Max(attribute.GetClientFirstTryCreateSleep, 10); TryCreateSleep = Math.Max(attribute.GetClientTryCreateSleep, 10); SocketWait.Set(0); this.onCustomData = onCustomData; clientCreator = new ClientSocketCreator(this); }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP 服务调用配置</param> /// <param name="sendBufferMaxSize">发送数据缓存区最大字节大小</param> /// <param name="log">日志接口</param> internal CommandBase(ServerBaseAttribute attribute, int sendBufferMaxSize, ILog log) { Attribute = attribute; MinCompressSize = attribute.GetMinCompressSize; Log = log ?? AutoCSer.LogHelper.Default; SendBufferMaxSize = sendBufferMaxSize; if (MinCompressSize <= 0) { MinCompressSize = int.MaxValue; } }
/// <summary> /// 设置配置数据 /// </summary> /// <param name="attribute"></param> internal void Set(ServerBaseAttribute attribute) { ServerName = attribute.ServerName; VerifyHashCode = attribute.VerifyHashCode; ReceiveVerifyCommandSeconds = attribute.GetReceiveVerifyCommandSeconds; if ((MaxInputSize = attribute.GetMaxInputSize) <= 0) { MaxInputSize = int.MaxValue; } OutputSleep = attribute.GetServerOutputSleep; IsBuildOutputThread = attribute.GetIsServerBuildOutputThread; RemoteExpressionTask = attribute.GetRemoteExpressionServerTask; RemoteExpressionCallQueueIndex = attribute.GetRemoteExpressionCallQueueIndex; }
/// <summary> /// 检测默认验证字符串 /// </summary> /// <param name="server"></param> /// <param name="attribute"></param> /// <returns></returns> internal static bool CheckVerifyString(CommandBase server, ServerBaseAttribute attribute) { string verify = attribute.VerifyString; if (verify == null) { if (AutoCSer.Config.Pub.Default.IsDebug) { server.Log.Add(AutoCSer.Log.LogType.Warn | AutoCSer.Log.LogType.Debug, "警告:调试模式未启用服务验证 " + attribute.ServerName, (StackFrame)null, true); return(true); } server.Log.Add(AutoCSer.Log.LogType.Error, "服务 " + attribute.ServerName + " 验证字符串不能为空", (StackFrame)null, true); } return(false); }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP服务调用配置</param> /// <param name="verify">获取客户端请求线程调用类型</param> /// <param name="log">日志接口</param> /// <param name="callQueueCount">独占的 TCP 服务器端同步调用队列数量</param> /// <param name="isCallQueueLink">是否提供独占的 TCP 服务器端同步调用队列(低优先级)</param> /// <param name="isSynchronousVerifyMethod">验证函数是否同步调用</param> internal ServerBase(ServerBaseAttribute attribute, Func <System.Net.Sockets.Socket, bool> verify, ILog log, int callQueueCount, bool isCallQueueLink, bool isSynchronousVerifyMethod) : base(attribute, attribute.GetReceiveBufferSize, attribute.GetSendBufferSize, attribute.GetServerSendBufferMaxSize, log) { this.verify = verify; if (callQueueCount > 0) { if (callQueueCount == 1) { CallQueue = new ServerCallCanDisposableQueue(true, Log); if (isCallQueueLink) { CallQueueLink = CallQueue.CreateLink(); } } else { CallQueueArray = new KeyValue <ServerCallCanDisposableQueue, Threading.QueueTaskLinkThread <ServerCallBase> .LowPriorityLink> [Math.Min(callQueueCount, 256)]; if (isCallQueueLink) { for (int index = 0; index != CallQueueArray.Length; ++index) { ServerCallCanDisposableQueue callQueue = new ServerCallCanDisposableQueue(true, Log); CallQueueArray[index].Set(callQueue, callQueue.CreateLink()); } CallQueueLink = CallQueueArray[0].Value; } else { for (int index = 0; index != CallQueueArray.Length; ++index) { CallQueueArray[index].Key = new ServerCallCanDisposableQueue(true, Log); } } CallQueue = CallQueueArray[0].Key; } } ServerAttribute.Set(attribute); Port = attribute.Port; IpAddress = HostPort.HostToIPAddress(attribute.Host, Log); int binaryDeSerializeMaxArraySize = attribute.GetBinaryDeSerializeMaxArraySize; BinaryDeSerializeConfig = AutoCSer.Net.TcpOpenServer.ServerAttribute.GetBinaryDeSerializeConfig(binaryDeSerializeMaxArraySize <= 0 ? AutoCSer.BinarySerialize.DeSerializer.DefaultConfig.MaxArraySize : binaryDeSerializeMaxArraySize); VerifyMethodCount = isSynchronousVerifyMethod ? Server.DefaultVerifyMethodCount : (byte)(Server.DefaultVerifyMethodCount + 1); }
/// <summary> /// 获取 TCP 内部注册服务客户端 /// </summary> /// <param name="attribute"></param> /// <param name="tcpRegisterName"></param> /// <param name="tcpRegisterClient"></param> /// <returns></returns> internal bool GetRegisterClient(ServerBaseAttribute attribute, string tcpRegisterName, ref TcpRegister.Client tcpRegisterClient) { if (tcpRegisterName != null) { tcpRegisterClient = TcpRegister.Client.Get(tcpRegisterName, Log); if (tcpRegisterClient == null) { Log.Add(AutoCSer.Log.LogType.Error, "TCP 内部注册服务 " + tcpRegisterName + " 客户端获取失败"); return(false); } if (attribute.ClientRegisterHost == null) { attribute.ClientRegisterHost = attribute.Host; } if (attribute.ClientRegisterPort == 0) { attribute.ClientRegisterPort = attribute.Port; } } return(true); }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP服务调用配置</param> /// <param name="maxTimeoutSeconds">最大超时秒数</param> /// <param name="onCustomData">自定义数据包处理</param> /// <param name="log">日志接口</param> internal Client(ServerBaseAttribute attribute, ushort maxTimeoutSeconds, Action <SubArray <byte> > onCustomData, ILog log) : base(attribute, log, onCustomData) { maxCustomDataSize = attribute.GetMaxCustomDataSize <= 0 ? int.MaxValue : attribute.GetMaxCustomDataSize; this.maxTimeoutSeconds = maxTimeoutSeconds; }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP 服务调用配置</param> /// <param name="receiveBufferSize">接受数据缓冲区字节大小</param> /// <param name="sendBufferSize">发送数据缓冲区字节大小</param> /// <param name="sendBufferMaxSize">发送数据缓存区最大字节大小</param> /// <param name="log">日志接口</param> internal CommandBuffer(ServerBaseAttribute attribute, AutoCSer.Memory.BufferSize receiveBufferSize, AutoCSer.Memory.BufferSize sendBufferSize, int sendBufferMaxSize, ILog log) : base(attribute, sendBufferMaxSize, log) { SendBufferPool = SubBuffer.Pool.GetPool(sendBufferSize); ReceiveBufferPool = SubBuffer.Pool.GetPool(receiveBufferSize); }