Esempio n. 1
0
        /// <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
                    LazyLog.Append(TcpServer.ClientLazyLog.LogType.SocketConnect);
                    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);
                    LazyLog.Append(TcpServer.ClientLazyLog.LogType.CreateSocketReceiveAsync);
                    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());
                            }
                            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());
                    }
                    ClientCreator.VerifyCount = 0;
                }
                if (Sender != null)
                {
                    LazyLog.Append(TcpServer.ClientLazyLog.LogType.CreateSocketError);
                    VerifyMethodSleep();
                    return;
                }
                ClientCreator.VerifyCount = 0;
                LazyLog.Append(TcpServer.ClientLazyLog.LogType.CreateSocketErrorSleep);
                CreateSleep();
            }
            while (true);
        }
Esempio n. 2
0
        /// <summary>
        /// 数据接收完成后的回调委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="async">异步回调参数</param>
        private void onReceive(object sender, SocketAsyncEventArgs async)
#endif
        {
            try
            {
#if DOTNET2
                Socket socket = (Socket)async.AsyncState;
                if (socket == Socket)
                {
                    int count = socket.EndReceive(async, out socketError);
                    if (socketError == SocketError.Success)
                    {
#else
                if (receiveAsyncEventArgs.SocketError == SocketError.Success)
                {
                    int count = receiveAsyncEventArgs.BytesTransferred;
#endif
                    if (count > 0)
                    {
                        ++ReceiveCount;
                        switch (ReceiveType)
                        {
                            case TcpServer.ClientSocketReceiveType.CommandIdentity:
                                LazyLog.Append(TcpServer.ClientLazyLog.LogType.OnReceiveTypeCommandIdentity);
                                if (commandIdentityAsync(count)) return;
                                break;
                            case TcpServer.ClientSocketReceiveType.CommandIdentityAgain:
                                LazyLog.Append(TcpServer.ClientLazyLog.LogType.OnReceiveTypeCommandIdentityAgain);
                                if (commandIdentityAsync(count)) return;
                                break;
                            case TcpServer.ClientSocketReceiveType.Data:
                                LazyLog.Append(TcpServer.ClientLazyLog.LogType.OnReceiveTypeData);
                                if (dataAsync(count)) return;
                                break;
                            case TcpServer.ClientSocketReceiveType.BigData:
                                LazyLog.Append(TcpServer.ClientLazyLog.LogType.OnReceiveTypeBigData);
                                if (bigDataAsync(count)) return;
                                break;
                        }
                    }
#if DOTNET2
                    }
                    else Log.Error(IpAddress.ToString() + ":" + Port.toString() + " " + socketError.ToString());
#endif
                }
#if !DOTNET2
                else
                {
                    socketError = receiveAsyncEventArgs.SocketError;
                    Log.Error(IpAddress.ToString() + ":" + Port.toString() + " " + socketError.ToString());
                }
#endif
            }
            catch (Exception error)
            {
                Log.Exception(error, IpAddress.ToString() + ":" + Port.toString());
            }
            LazyLog.Append(TcpServer.ClientLazyLog.LogType.OnReceiveError);
            if (CheckCreateVersion())
            {
                try
                {
                    LazyLog.Append(TcpServer.ClientLazyLog.LogType.OnClose);
                    close();
                }
                catch (Exception error)
                {
                    this.Log.Exception(error, "套接字关闭异常");
                }
                finally
                {
                    LazyLog.Append(TcpServer.ClientLazyLog.LogType.OnCreateNew);
                    CreateNew();
                }
            }
            else
            {
                try
                {
                    close();
                }
                catch (Exception error)
                {
                    this.Log.Exception(error);
                }
                finally
                {
                    if (CreateVersion == ClientCreator.CreateVersion) ClientCreator.CommandClient.SocketWait.Set();
                }
            }
        }