Ejemplo n.º 1
0
        /// <summary>
        /// 异步回调
        /// </summary>
        /// <param name="returnValue">返回值</param>
        /// <returns>是否成功加入回调队列</returns>
        public override bool Callback(TcpServer.ReturnValue returnValue)
        {
            ServerSocket socket = this.socket;

            this.socket = null;
            return(socket != null && socket.SendAsync(returnValue.Type));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 获取远程表达式数据
 /// </summary>
 /// <param name="node">远程表达式节点</param>
 /// <returns>返回值类型</returns>
 public TcpServer.ReturnValue <returnType> GetRemoteExpression <returnType>(RemoteExpression.Node <returnType> node)
 {
     if (remoteExpressionServerNodeIdChecker != null)
     {
         RemoteExpression.ClientNode clientNode;
         TcpServer.ReturnType        type = GetRemoteExpressionClientNode(node, out clientNode);
         if (type == TcpServer.ReturnType.Success)
         {
             TcpServer.ReturnValue <RemoteExpression.ReturnValue> value = getRemoteExpression(ref clientNode);
             if (value.Type == TcpServer.ReturnType.Success)
             {
                 return(((RemoteExpression.ReturnValue <returnType>)value.Value).Value);
             }
             return(new TcpServer.ReturnValue <returnType> {
                 Type = value.Type
             });
         }
         return(new TcpServer.ReturnValue <returnType> {
             Type = type
         });
     }
     return(new TcpServer.ReturnValue <returnType> {
         Type = TcpServer.ReturnType.RemoteExpressionNotSupport
     });
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 客户端轮询
        /// </summary>
        /// <param name="result">轮询结果</param>
        private void onLog(TcpServer.ReturnValue <Log> result)
        {
            if (result.Type == TcpServer.ReturnType.Success)
            {
                switch (result.Value.Type)
                {
                case LogType.RegisterServer: registerServer(result.Value); return;

                case LogType.RemoveServer: removeServer(result.Value); return;

                case LogType.RegisterLoaded:
                    isRegisterLoaded = true;
                    Monitor.Enter(serverSetLock);
                    try
                    {
                        foreach (ClientServerSet serverSet in serverSets.Values)
                        {
                            serverSet.OnLoaded();
                        }
                    }
                    finally { Monitor.Exit(serverSetLock); }
                    return;

                case LogType.ClientError: isClientErrorLog = true; return;
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 时间验证客户端验证
        /// </summary>
        /// <param name="verify">时间验证服务客户端委托</param>
        /// <param name="client"></param>
        /// <param name="userID">用户ID</param>
        /// <param name="verifyString">验证字符串</param>
        /// <returns></returns>
        public unsafe static bool Verify(Verifier verify, AutoCSer.Net.TcpOpenSimpleServer.Client client, string userID, string verifyString)
        {
            long            ticks;
            ulong           markData  = 0;
            ServerAttribute attribute = client.Attribute;

            if (attribute.IsMarkData)
            {
                markData = attribute.VerifyHashCode;
            }
            ticks = Date.NowTime.SetUtc().Ticks;
            do
            {
                ulong randomPrefix = Random.Default.SecureNextULongNotZero();
                while (randomPrefix == markData)
                {
                    randomPrefix = Random.Default.SecureNextULongNotZero();
                }
                client.ReceiveMarkData = attribute.IsMarkData ? markData ^ randomPrefix : 0UL;
                client.SendMarkData    = 0;
                long lastTicks = ticks;
                TcpServer.ReturnValue <bool> isVerify = verify(userID, randomPrefix, TcpServer.TimeVerifyServer.Md5(verifyString, randomPrefix, ticks), ref ticks);
                if (isVerify.Value)
                {
                    client.SendMarkData = client.ReceiveMarkData;
                    return(true);
                }
                if (isVerify.Type != TcpServer.ReturnType.Success || ticks <= lastTicks)
                {
                    client.Log.Add(AutoCSer.Log.LogType.Error, "TCP客户端验证失败 [" + isVerify.Type.ToString() + "] " + ticks.toString() + " <= " + lastTicks.toString());
                    return(false);
                }
                client.Log.Add(AutoCSer.Log.LogType.Error, "TCP客户端验证时间失败重试 " + ticks.toString() + " - " + lastTicks.toString());
            }while (true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 时间验证客户端验证
        /// </summary>
        /// <param name="verify">时间验证服务客户端委托</param>
        /// <param name="client"></param>
        /// <param name="userID">用户ID</param>
        /// <param name="verifyString">验证字符串</param>
        /// <returns></returns>
        public unsafe static bool Verify(Verifier verify, Client client, string userID, string verifyString)
        {
            ulong markData = 0;

            TcpServer.ServerBaseAttribute attribute = client.Attribute;
            if (attribute.IsMarkData)
            {
                markData = attribute.VerifyHashCode;
            }
            long ticks = AutoCSer.Net.TcpInternalServer.TimeVerifyClient.GetTicks();

            do
            {
                ulong randomPrefix = Random.Default.SecureNextULongNotZero();
                while (randomPrefix == markData)
                {
                    randomPrefix = Random.Default.SecureNextULongNotZero();
                }
                client.ReceiveMarkData = attribute.IsMarkData ? markData ^ randomPrefix : 0UL;
                client.SendMarkData    = 0;
                long lastTicks = ticks;
                TcpServer.ReturnValue <bool> isVerify = verify(userID, randomPrefix, TcpServer.TimeVerifyServer.Md5(verifyString, randomPrefix, ticks), ref ticks);
                if (isVerify.Value)
                {
                    client.SendMarkData = client.ReceiveMarkData;
                    return(true);
                }
                if (isVerify.Type != TcpServer.ReturnType.Success || ticks <= lastTicks)
                {
                    client.Log.Error("TCP客户端验证失败 [" + isVerify.Type.ToString() + "] " + ticks.toString() + " <= " + lastTicks.toString(), LogLevel.Error | LogLevel.AutoCSer);
                    return(false);
                }
                client.Log.Error("TCP客户端验证时间失败重试 " + ticks.toString() + " - " + lastTicks.toString(), LogLevel.Error | LogLevel.AutoCSer);
            }while (true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 异步回调
        /// </summary>
        /// <param name="returnValue">返回值</param>
        /// <returns>是否成功加入回调队列</returns>
        public override bool Callback(TcpServer.ReturnValue returnValue)
        {
            ServerSocketSender socket = this.socket;

            this.socket = null;
            return(socket != null && socket.TryPush(TcpServer.Server.GetCommandIndex(commandIndex, returnValue.Type), isBuildOutputThread));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 异步回调
        /// </summary>
        /// <param name="returnValue">返回值</param>
        /// <returns>是否成功加入回调队列</returns>
        private bool onReturn(TcpServer.ReturnValue returnValue)
        {
            ServerSocket socket = this.socket;

            this.socket = null;
            AutoCSer.Threading.RingPool <ServerCallback> .Default.PushNotNull(this);

            return(socket.SendAsync(returnValue.Type));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 异步回调
        /// </summary>
        /// <param name="returnValue">返回值</param>
        /// <returns>是否成功加入回调队列</returns>
        public override bool Callback(TcpServer.ReturnValue returnValue)
        {
            ServerSocketSender sender = socket;

            if (sender != null && sender.IsSocket)
            {
                TcpServer.ServerOutput.ReturnTypeOutput output = sender.TryGetOutput(TcpServer.Server.GetCommandIndex(commandIndex, returnValue.Type));
                if (output != null)
                {
                    if (returnValue.Type == TcpServer.ReturnType.Success)
                    {
                        while (System.Threading.Interlocked.CompareExchange(ref keepLock, 1, 0) != 0)
                        {
                            AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.TcpServerKeepCallback);
                        }
                        if (socket == null)
                        {
                            System.Threading.Interlocked.Exchange(ref keepLock, 0);
                        }
                        else
                        {
                            bool isHead = sender.Outputs.IsPushHead(output);
                            System.Threading.Interlocked.Exchange(ref keepLock, 0);
                            if (isHead)
                            {
                                sender.TryBuildOutput(isBuildOutputThread);
                            }
                            return(true);
                        }
                    }
                    else
                    {
                        while (System.Threading.Interlocked.CompareExchange(ref keepLock, 1, 0) != 0)
                        {
                            AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.TcpServerKeepCallback);
                        }
                        if (socket == null)
                        {
                            System.Threading.Interlocked.Exchange(ref keepLock, 0);
                        }
                        else
                        {
                            bool isHead = sender.Outputs.IsPushHead(output);
                            socket = null;
                            System.Threading.Interlocked.Exchange(ref keepLock, 0);
                            if (isHead)
                            {
                                sender.TryBuildOutput(isBuildOutputThread);
                            }
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 异步回调
        /// </summary>
        /// <param name="returnValue">返回值</param>
        /// <returns>是否成功加入回调队列</returns>
        private bool onReturn(TcpServer.ReturnValue returnValue)
        {
            ServerSocketSender socket = this.socket;
            uint commandIndex         = this.commandIndex;

            this.socket = null;
            AutoCSer.Threading.RingPool <ServerCallback> .Default.PushNotNull(this);

            return(socket.TryPush(TcpServer.Server.GetCommandIndex(commandIndex, returnValue.Type), isBuildOutputThread));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 异步回调
        /// </summary>
        /// <param name="returnValue">返回值</param>
        /// <returns>是否成功加入回调队列</returns>
        public override bool Callback(TcpServer.ReturnValue returnValue)
        {
            ServerSocketSender sender = socket;

            if (sender != null && sender.IsSocket)
            {
                TcpServer.ServerOutput.ReturnTypeOutput output = sender.TryGetOutput(TcpServer.Server.GetCommandIndex(commandIndex, returnValue.Type));
                if (output != null)
                {
                    if (returnValue.Type == TcpServer.ReturnType.Success)
                    {
                        keepLock.EnterYield();
                        if (socket == null)
                        {
                            keepLock.Exit();
                        }
                        else
                        {
                            bool isHead = sender.Outputs.IsPushHead(output);
                            keepLock.Exit();
                            if (isHead)
                            {
                                sender.TryBuildOutput(isBuildOutputThread);
                            }
                            return(true);
                        }
                    }
                    else
                    {
                        keepLock.EnterYield();
                        if (socket == null)
                        {
                            keepLock.Exit();
                        }
                        else
                        {
                            bool isHead = sender.Outputs.IsPushHead(output);
                            socket = null;
                            keepLock.Exit();
                            if (isHead)
                            {
                                sender.TryBuildOutput(isBuildOutputThread);
                            }
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 客户端轮询
        /// </summary>
        /// <param name="result">轮询结果</param>
        private void onLog(TcpServer.ReturnValue <ServerLog> result)
        {
            if (result.Type == TcpServer.ReturnType.Success)
            {
                switch (result.Value.LogType)
                {
                case LogType.RegisterServer: registerServer(result.Value); return;

                case LogType.RemoveServer: removeServer(result.Value); return;

                default:
                    registerClient._TcpClient_.Log.Error("未知的 TCP 内部注册服务更新日志类型 " + result.Value.toJson(), LogLevel.Error | LogLevel.AutoCSer);
                    return;
                }
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 时间验证客户端验证
        /// </summary>
        /// <param name="verify">时间验证服务客户端委托</param>
        /// <param name="sender"></param>
        /// <param name="client"></param>
        /// <param name="userID">用户ID</param>
        /// <param name="verifyString">验证字符串</param>
        /// <returns></returns>
        public unsafe static bool Verify(Verifier verify, ClientSocketSender sender, AutoCSer.Net.TcpInternalServer.Client client, string userID, string verifyString)
        {
            TcpServer.ServerBaseAttribute attribute = client.Attribute;
            ulong markData = 0;

            if (attribute.IsMarkData)
            {
                markData = attribute.VerifyHashCode;
            }
            long ticks = GetTicks();

            TcpServer.ClientSocketBase socket = sender.ClientSocket;
            bool isError = false;

            do
            {
                ulong randomPrefix = Random.Default.SecureNextULongNotZero();
                while (randomPrefix == markData)
                {
                    randomPrefix = Random.Default.SecureNextULongNotZero();
                }
                socket.ReceiveMarkData = attribute.IsMarkData ? markData ^ randomPrefix : 0UL;
                sender.SendMarkData    = 0;
                long lastTicks = ticks;
                TcpServer.ReturnValue <bool> isVerify = verify(sender, userID, randomPrefix, TcpServer.TimeVerifyServer.Md5(verifyString, randomPrefix, ticks), ref ticks);
                if (isVerify.Value)
                {
                    sender.SendMarkData = socket.ReceiveMarkData;
                    if (isError)
                    {
                        socket.Log.Debug(sender.ClientSocket.IpAddress.ToString() + ":" + sender.ClientSocket.Port.toString() + " TCP客户端验证时间重试成功");
                    }
                    return(true);
                }
                if (isVerify.Type != TcpServer.ReturnType.Success || ticks <= lastTicks)
                {
                    socket.Log.Error(sender.ClientSocket.IpAddress.ToString() + ":" + sender.ClientSocket.Port.toString() + " TCP客户端验证失败 [" + isVerify.Type.ToString() + "] " + ticks.toString() + " <= " + lastTicks.toString());
                    return(false);
                }
                socket.Log.Error(sender.ClientSocket.IpAddress.ToString() + ":" + sender.ClientSocket.Port.toString() + " TCP客户端验证时间失败重试 " + ticks.toString() + " - " + lastTicks.toString());
                isError = true;
            }while (true);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 时间验证客户端验证
        /// </summary>
        /// <param name="verify">时间验证服务客户端委托</param>
        /// <param name="sender"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public unsafe static bool Verify(Verifier verify, ClientSocketSender sender, AutoCSer.Net.TcpOpenStreamServer.Client client)
        {
            long            ticks;
            ServerAttribute attribute    = client.Attribute;
            string          verifyString = attribute.VerifyString;

            if (verifyString == null)
            {
                ticks = 0;
                return(verify(sender, 0, null, ref ticks).Value);
            }
            ulong markData = 0;

            if (attribute.IsMarkData)
            {
                markData = attribute.VerifyHashCode;
            }
            ticks = Date.NowTime.SetUtc().Ticks;
            TcpServer.ClientSocketBase socket = sender.ClientSocket;
            do
            {
                ulong randomPrefix = Random.Default.SecureNextULongNotZero();
                while (randomPrefix == markData)
                {
                    randomPrefix = Random.Default.SecureNextULongNotZero();
                }
                socket.ReceiveMarkData = attribute.IsMarkData ? markData ^ randomPrefix : 0UL;
                sender.SendMarkData    = 0;
                long lastTicks = ticks;
                TcpServer.ReturnValue <bool> isVerify = verify(sender, randomPrefix, TcpServer.TimeVerifyServer.Md5(verifyString, randomPrefix, ticks), ref ticks);
                if (isVerify.Value)
                {
                    sender.SendMarkData = socket.ReceiveMarkData;
                    return(true);
                }
                if (isVerify.Type != TcpServer.ReturnType.Success || ticks <= lastTicks)
                {
                    socket.Log.add(AutoCSer.Log.LogType.Error, "TCP客户端验证失败 [" + isVerify.Type.ToString() + "] " + ticks.toString() + " <= " + lastTicks.toString());
                    return(false);
                }
                socket.Log.add(AutoCSer.Log.LogType.Error, "TCP客户端验证时间失败重试 " + ticks.toString() + " - " + lastTicks.toString());
            }while (true);
        }
Ejemplo n.º 14
0
        private void onReceive()
        {
            Callback <TcpServer.ReturnValue <outputParameterType> > callback = Callback;
            ClientSocket socket = Socket;

            TcpServer.ReturnValue <outputParameterType> outputParameter = OutputParameter;
            Callback = null;
            Socket   = null;
            OutputParameter.Value = default(outputParameterType);
            if ((Interlocked.Increment(ref FreeLock) & 1) == 0)
            {
                free();
            }
            try
            {
                callback.Call(ref outputParameter);
            }
            catch (Exception error)
            {
                socket.Log.add(AutoCSer.Log.LogType.Error, error);
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <typeparam name="outputParameterType">输出数据类型</typeparam>
 /// <param name="outputInfo">服务端输出信息</param>
 /// <param name="outputParameter">输出数据</param>
 /// <returns>是否发送成功</returns>
 //[AutoCSer.IOS.Preserve(Conditional = true)]
 public bool SendAsync<outputParameterType>(TcpSimpleServer.OutputInfo outputInfo, ref TcpServer.ReturnValue<outputParameterType> outputParameter)
     where outputParameterType : struct
 {
     bool isSend = false;
     try
     {
         if (outputParameter.Type == TcpServer.ReturnType.Success)
         {
             if (send(outputInfo, ref outputParameter.Value, ref isSend)) return isSend;
         }
         else
         {
             Socket socket = Socket;
             if (socket != null)
             {
                 fixed (byte* bufferFixed = Buffer.Buffer)
                 {
                     byte* start = bufferFixed + Buffer.StartIndex;
                     *(int*)start = 0;
                     *(start + sizeof(int)) = (byte)outputParameter.Type;
                 }
                 if (socket.Send(Buffer.Buffer, Buffer.StartIndex, sizeof(int) * 2, SocketFlags.None, out socketError) == sizeof(int) * 2)
                 {
                     if (IsVerifyMethod ? isReceiveCommand() : (--verifyMethodCount != 0 && isReceiveVerifyCommand())) return true;
                     isSend = true;
                 }
             }
         }
     }
     catch (Exception error)
     {
         Server.Log.Add(AutoCSer.Log.LogType.Debug, error);
     }
     close();
     return isSend;
 }