コード例 #1
0
ファイル: ClientSocket.cs プロジェクト: liulaojian/AutoCSer
        /// <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(CommandClient.TryCreateSleep);
                    if (checkCreate() == 0) return;
                }
                try
                {
                    Socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
#if !MONO
                    Socket.ReceiveBufferSize = CommandClient.ReceiveBufferPool.Size;
                    Socket.SendBufferSize = 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) 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
                    receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize);
                    if (Socket.ReceiveAsync(receiveAsyncEventArgs))
#endif
                    {
                        isReceiveAsync = true;
                        if (verifyMethod(CommandClient))
                        {
                            if (CommandClient.SetSocket(this))
                            {
                                if (isErrorLog)
                                {
                                    CommandClient.Log.Add(AutoCSer.Log.LogType.Debug, CommandClient.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;
                        CommandClient.Log.Add(AutoCSer.Log.LogType.Debug, error, CommandClient.Attribute.ServerName + " 客户端 TCP 连接失败 " + ipAddress.ToString() + ":" + port.toString());
                    }
                }
                if (isReceiveAsync) return;
                CreateSleep();
            }
            while (true);
        }