/// <summary> /// 创建 TCP 服务客户端套接字 /// </summary> internal override void Create() { bool isErrorLog = false, isReceiveAsync = false; socketError = SocketError.Success; do { if (checkCreate() == 0) return; if (isSleep) { isSleep = false; Thread.Sleep(ClientCreator.CommandClient.TryCreateSleep); if (checkCreate() == 0) return; } try { Socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); #if !MONO Socket.ReceiveBufferSize = ClientCreator.CommandClient.ReceiveBufferPool.Size; Socket.SendBufferSize = ClientCreator.CommandClient.SendBufferPool.Size; #endif Socket.Connect(ipAddress, port); if (checkCreate() == 0) return; #if DOTNET2 if (onReceiveAsyncCallback == null) onReceiveAsyncCallback = onReceive; #else if (receiveAsyncEventArgs == null) { receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); receiveAsyncEventArgs.Completed += onReceive; } #endif if (ReceiveBuffer.Buffer == null) ClientCreator.CommandClient.ReceiveBufferPool.Get(ref ReceiveBuffer); if (Sender == null) SetSender(new ClientSocketSender(this)); receiveBufferSize = ReceiveBuffer.PoolBuffer.Pool.Size; receiveCount = receiveIndex = 0; ReceiveType = TcpServer.ClientSocketReceiveType.CommandIdentity; #if DOTNET2 Socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize, SocketFlags.None, out socketError, onReceiveAsyncCallback, Socket); if (socketError == SocketError.Success) #else #if !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock, 1); #endif receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize); if (Socket.ReceiveAsync(receiveAsyncEventArgs)) #endif { #if !DOTNET2 && !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock, 0); #endif isReceiveAsync = true; if (verifyMethod(ClientCreator.CommandClient)) { if (ClientCreator.OnSocketVerifyMethod(this)) { if (isErrorLog) { ClientCreator.CommandClient.Log.Add(AutoCSer.Log.LogType.Debug, ClientCreator.Attribute.ServerName + " 客户端 TCP 连接成功 " + ipAddress.ToString() + ":" + port.toString()); } return; } } if (Socket != null) { #if DotNetStandard AutoCSer.Net.TcpServer.CommandBase.CloseClientNotNull(Socket); #else Socket.Dispose(); #endif Socket = null; } return; } } catch (Exception error) { if (!isErrorLog) { isErrorLog = true; ClientCreator.CommandClient.Log.Add(AutoCSer.Log.LogType.Debug, error, ClientCreator.Attribute.ServerName + " 客户端 TCP 连接失败 " + ipAddress.ToString() + ":" + port.toString()); } } if (isReceiveAsync) return; CreateSleep(); } while (true); }
/// <summary> /// 创建 TCP 服务客户端套接字 /// </summary> internal override void Create() { bool isErrorLog = false; socketError = SocketError.Success; do { if (checkCreate() == 0) return; if (isSleep) { isSleep = false; Thread.Sleep(ClientCreator.CommandClient.FristTryCreateSleep); } try { Socket = new Socket(IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); #if !MONO Socket.ReceiveBufferSize = ClientCreator.CommandClient.ReceiveBufferPool.Size; Socket.SendBufferSize = ClientCreator.CommandClient.SendBufferPool.Size; #endif Socket.Connect(IpAddress, Port); if (checkCreate() == 0) return; if (onReceiveAsyncCallback == null) onReceiveAsyncCallback = onReceive; #if !DOTNET2 if (receiveAsyncEventArgs == null) { receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); receiveAsyncEventArgs.Completed += onReceiveAsyncCallback; } #endif if (ReceiveBuffer.Buffer == null) ClientCreator.CommandClient.ReceiveBufferPool.Get(ref ReceiveBuffer); if (Sender == null) SetSender(new ClientSocketSender(this)); receiveBufferSize = ReceiveBuffer.PoolBuffer.Pool.Size; receiveCount = receiveIndex = 0; ReceiveType = TcpServer.ClientSocketReceiveType.CommandIdentityAgain; #if DOTNET2 Socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize, SocketFlags.None, out socketError, onReceiveAsyncCallback, Socket); if (socketError == SocketError.Success) #else #if !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock.Lock, 1); #endif receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize); if (Socket.ReceiveAsync(receiveAsyncEventArgs)) #endif { #if !DOTNET2 && !DotNetStandard receiveAsyncLock.Exit(); #endif if (verifyMethod() && ClientCreator.OnSocketVerifyMethod(this)) { if (isErrorLog) { ClientCreator.CommandClient.Log.Debug(ClientCreator.Attribute.ServerName + " 客户端 TCP 连接成功 " + IpAddress.ToString() + ":" + Port.toString(), LogLevel.Debug | LogLevel.AutoCSer); } ClientCreator.VerifyCount = 0; return; } } ++ClientCreator.VerifyCount; } catch (Exception error) { if (!isErrorLog) { isErrorLog = true; ClientCreator.CommandClient.Log.Exception(error, ClientCreator.Attribute.ServerName + " 客户端 TCP 连接失败 " + IpAddress.ToString() + ":" + Port.toString(), LogLevel.Exception | LogLevel.AutoCSer); } } if (Sender != null) { VerifyMethodSleep(); return; } ClientCreator.VerifyCount = 0; CreateSleep(); } while (true); }