コード例 #1
0
ファイル: ServerSocket.cs プロジェクト: AutoCSer/AutoCSer
        private bool isCommand()
        {
#if DOTNET2
            Socket socket = (Socket)receiveAsyncEventArgs.AsyncState;
            if (socket == Socket)
            {
                receiveCount += socket.EndReceive(receiveAsyncEventArgs, out socketError);
                if (socketError == SocketError.Success)
                {
#else
            if (receiveAsyncEventArgs.SocketError == SocketError.Success)
            {
                receiveCount += receiveAsyncEventArgs.BytesTransferred;
#endif
                    fixed (byte* receiveDataFixed = ReceiveBuffer.GetFixedBuffer())
                    {
                        receiveDataStart = receiveDataFixed + ReceiveBuffer.StartIndex;
                        return loop(true);
                    }
#if DOTNET2
                }
#endif
            }
#if !DOTNET2
            else socketError = receiveAsyncEventArgs.SocketError;
#endif
            return false;
        }
コード例 #2
0
ファイル: ServerSocket.cs プロジェクト: AutoCSer/AutoCSer
        /// <summary>
        /// 接收验证命令
        /// </summary>
        /// <returns></returns>
        private bool isVerifyCommand()
        {
            if (IsVerifyMethod)
            {
                receiveIndex = receiveCount = 0;
                return isCommand();
            }
#if DOTNET2
            Socket socket = (Socket)receiveAsyncEventArgs.AsyncState;
            if (socket == Socket)
            {
                receiveCount = socket.EndReceive(receiveAsyncEventArgs, out socketError);
                if (socketError == SocketError.Success)
                {
#else
            if (receiveAsyncEventArgs.SocketError == SocketError.Success)
            {
                receiveCount = receiveAsyncEventArgs.BytesTransferred;
#endif
                    if (receiveCount >= (sizeof(int) * 2 + sizeof(uint)))
                    {
                        fixed (byte* receiveDataFixed = ReceiveBuffer.GetFixedBuffer())
                        {
                            if (*(int*)(receiveDataStart = receiveDataFixed + ReceiveBuffer.StartIndex) == Server.VerifyCommandIdentity)
                            {
                                if ((compressionDataSize = *(int*)(receiveDataStart + (sizeof(uint) + sizeof(int)))) > 0)
                                {
                                    CommandIndex = *(uint*)(receiveDataStart + sizeof(int));
                                    dataSize = compressionDataSize;
                                    receiveIndex = sizeof(int) * 2 + sizeof(uint);
                                    return checkVerifyCommandFixed();
                                }
                                else if (compressionDataSize < 0 && receiveCount >= (sizeof(int) * 3 + sizeof(uint)))
                                {
                                    if ((dataSize = *(int*)(receiveDataStart + (sizeof(uint) + sizeof(int) * 2))) > (compressionDataSize = -compressionDataSize))
                                    {
                                        CommandIndex = *(uint*)(receiveDataStart + sizeof(int));
                                        receiveIndex = sizeof(int) * 3 + sizeof(uint);
                                        return checkVerifyCommandFixed();
                                    }
                                }
                            }
                            else if (Server.Log.IsAnyLevel(LogLevel.Info))
                            {
#if !DOTNET2
                            Socket socket = Socket;
#endif
                            Server.Log.Info(socket == null ? "TCP 验证函数命令错误" : ("TCP 验证函数命令错误 " + socket.RemoteEndPoint.ToString()), LogLevel.Info | LogLevel.AutoCSer);
                            }
                        }
                    }
#if DOTNET2
                }
#endif
            }
#if !DOTNET2
            else socketError = receiveAsyncEventArgs.SocketError;
#endif
            return false;
        }
コード例 #3
0
ファイル: ClientSocket.cs プロジェクト: AutoCSer/AutoCSer
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        protected bool dataAsync(int count)
        {
#if !DOTNET2
START:
#endif
            if (compressionDataSize <= (receiveCount += count) - receiveIndex)
            {
                fixed(byte *receiveDataFixed = ReceiveBuffer.GetFixedBuffer())
                {
                    receiveDataStart = receiveDataFixed + ReceiveBuffer.StartIndex;
                    return(isOnDataLoopFixed() && loop());
                }
            }
            Socket socket = this.Socket;
            if (socket != null)
            {
#if DOTNET2
                socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex + receiveCount, receiveBufferSize - receiveCount, SocketFlags.None, out socketError, onReceiveAsyncCallback, socket);
                if (socketError == SocketError.Success)
                {
                    return(true);
                }
#else
#if !DotNetStandard
                receiveAsyncLock.EnterYield();
#endif
                receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.StartIndex + receiveCount, receiveBufferSize - receiveCount);
                if (socket.ReceiveAsync(receiveAsyncEventArgs))
                {
#if !DotNetStandard
                    receiveAsyncLock.Exit();
#endif
                    return(true);
                }
                if (receiveAsyncEventArgs.SocketError == SocketError.Success)
                {
                    if ((count = receiveAsyncEventArgs.BytesTransferred) > 0)
                    {
#if !DotNetStandard
                        receiveAsyncLock.Exit();
#endif
                        ++ReceiveCount;
                        goto START;
                    }
                }
                else
                {
                    socketError = receiveAsyncEventArgs.SocketError;
                }
#endif
            }
            return(false);
        }
コード例 #4
0
ファイル: ServerSocket.cs プロジェクト: AutoCSer/AutoCSer
        /// <summary>
        /// 获取数据
        /// </summary>
        private void dataAsync()
        {
            try
            {
#if DOTNET2
                Socket socket = (Socket)receiveAsyncEventArgs.AsyncState;
                if (socket == Socket)
                {
                    receiveCount += socket.EndReceive(receiveAsyncEventArgs, out socketError);
                    if (socketError == SocketError.Success)
                    {
#else
            CHECK:
                if (receiveAsyncEventArgs.SocketError == SocketError.Success)
                {
                    receiveCount += receiveAsyncEventArgs.BytesTransferred;
#endif
                        if (compressionDataSize <= receiveCount - receiveIndex)
                        {
                            fixed (byte* receiveDataFixed = ReceiveBuffer.GetFixedBuffer())
                            {
                                receiveDataStart = receiveDataFixed + ReceiveBuffer.StartIndex;
                                if (isDoCommandLoop() && loop(false)) return;
                            }
                        }
                        else
                        {
#if DOTNET2
                            if (socket == Socket)
                            {
                                socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex + receiveCount, receiveBufferSize - receiveCount, SocketFlags.None, out socketError, onReceiveAsyncCallback, socket);
                                if (socketError == SocketError.Success) return;
                            }
#else
                        Socket socket = Socket;
                        if (socket != null)
                        {
#if !DotNetStandard
                            receiveAsyncLock.EnterNotCheckSleepFlag();
#endif
                            receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.StartIndex + receiveCount, receiveBufferSize - receiveCount);
                            if (socket.ReceiveAsync(receiveAsyncEventArgs))
                            {
#if !DotNetStandard
                                receiveAsyncLock.Exit();
#endif
                                return;
                            }
#if !DotNetStandard
                            receiveAsyncLock.Exit();
#endif
                            goto CHECK;

                        }
#endif
                        }
#if DOTNET2
                    }
#endif
                }
#if !DOTNET2
                else socketError = receiveAsyncEventArgs.SocketError;
#endif
            }
            catch (Exception error)
            {
                Server.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer);
            }
            Close();
        }
コード例 #5
0
ファイル: ClientSocket.cs プロジェクト: AutoCSer/AutoCSer
        /// <summary>
        /// 获取命令回调序号
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        protected bool commandIdentityAsync(int count)
        {
START:
            int receiveSize = (receiveCount += count) - receiveIndex;

            if (receiveSize >= sizeof(uint))
            {
                fixed(byte *receiveDataFixed = ReceiveBuffer.GetFixedBuffer())
                {
                    receiveDataStart = receiveDataFixed + ReceiveBuffer.StartIndex;
                    byte *start = receiveDataStart + receiveIndex;

                    CommandIndex = *(uint *)start;
                    ReturnType type = Server.GetReturnType(ref CommandIndex);

                    if (type == ReturnType.Unknown)
                    {
                        if (receiveSize >= (sizeof(uint) + sizeof(int)))
                        {
                            if ((compressionDataSize = *(int *)(start + sizeof(uint))) < 0)
                            {
                                if (receiveSize < (sizeof(uint) + sizeof(int) * 2))
                                {
                                    goto AGAIN;
                                }
                                if ((compressionDataSize = -compressionDataSize) >= (dataSize = *(int *)(start + (sizeof(uint) + sizeof(int)))))
                                {
                                    return(false);
                                }
                                receiveIndex += (sizeof(uint) + sizeof(int) * 2);
                            }
                            else if (compressionDataSize == 0)
                            {
                                return(false);
                            }
                            else
                            {
                                dataSize      = compressionDataSize;
                                receiveIndex += (sizeof(uint) + sizeof(int));
                            }
                            if (compressionDataSize <= receiveCount - receiveIndex)
                            {
                                return(isOnDataLoopFixed() && loop());
                            }
                            switch (checkDataLoopFixed())
                            {
                            case 0: return(true);

                            case 1: return(loop());

                            default: return(false);
                            }
                        }
                    }
                    else
                    {
                        onReceive(type);
                        receiveIndex += sizeof(uint);
                        return(loop());
                    }
                }
            }
AGAIN:
            if ((count = receiveCommandIdentityAgain()) > 0)
            {
                goto START;
            }
            return(count == 0);
        }