Exemplo n.º 1
0
 public void Set(ClientSocket socket, CommandInfo command, Action <ReturnValue> onCall)
 {
     KeepCallback = new KeepCallback(this);
     Socket       = socket;
     Callback     = onCall;
     CommandInfo  = command;
 }
Exemplo n.º 2
0
 internal void Set(ClientSocket socket, CommandInfo command, Action <ReturnValue> onCall, ref inputParameterType inputParameter)
 {
     KeepCallback   = new KeepCallback(this);
     Socket         = socket;
     Callback       = onCall;
     CommandInfo    = command;
     InputParameter = inputParameter;
 }
Exemplo n.º 3
0
 internal void Set(ClientSocket socket, CommandInfo command, Callback <ReturnValue <outputParameterType> > callback)
 {
     KeepCallback    = new KeepCallback(this);
     Socket          = socket;
     Callback        = callback;
     CommandInfo     = command;
     OutputParameter = default(outputParameterType);
 }
Exemplo n.º 4
0
 internal void Set(ClientSocket socket, CommandInfo command, Callback <ReturnValue <outputParameterType> > callback, ref inputParameterType inputParameter, ref outputParameterType outputParameter)
 {
     KeepCallback    = new KeepCallback(this);
     Socket          = socket;
     Callback        = callback;
     CommandInfo     = command;
     InputParameter  = inputParameter;
     OutputParameter = outputParameter;
 }
Exemplo n.º 5
0
        /// <summary>
        /// 接收数据回调处理
        /// </summary>
        /// <param name="data">输出数据</param>
        internal override void OnReceive(ref SubArray <byte> data)
        {
            Action <ReturnValue> callback = Callback;

            if (callback != null)
            {
                ReturnType value = GetReturnType(ref data);
                if ((byte)value < (byte)ReturnType.Success)
                {
                    KeepCallback.Cancel();
                }
                if (CommandInfo.TaskType == ClientTaskType.Synchronous)
                {
                    try
                    {
                        callback(new ReturnValue {
                            Type = value
                        });
                    }
                    catch (Exception error) { Socket.Log.Add(AutoCSer.Log.LogType.Error, error); }
                }
                else
                {
                    int isOutput = 1;
                    Monitor.Enter(OutputLock);
                    try
                    {
                        outputParameters.Add(value);
                        isOutput      = this.isOutput;
                        this.isOutput = 1;
                    }
                    catch (Exception error)
                    {
                        Socket.Log.Add(AutoCSer.Log.LogType.Error, error);
                    }
                    finally { Monitor.Exit(OutputLock); }
                    if (isOutput == 0)
                    {
                        switch (CommandInfo.TaskType)
                        {
                        case ClientTaskType.ThreadPool: if (!System.Threading.ThreadPool.QueueUserWorkItem(threadPoolOnReceive))
                            {
                                AutoCSer.Threading.LinkTask.Task.Add(this);
                            }
                            return;

                        case ClientTaskType.Timeout: AutoCSer.Threading.LinkTask.Task.Add(this); return;

                        case ClientTaskType.TcpTask: ClientCallTask.Task.Add(this); return;

                        case ClientTaskType.TcpQueue: ClientCallQueue.Default.Add(this); return;
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 创建命令输入数据
        /// </summary>
        /// <param name="buildInfo">TCP 客户端创建命令参数</param>
        /// <returns>是否成功</returns>
        internal unsafe override CommandBase Build(ref SenderBuildInfo buildInfo)
        {
            UnmanagedStream stream = Socket.OutputSerializer.Stream;

            if (buildInfo.Count == 0 || (buildInfo.SendBufferSize - stream.ByteSize) >= CommandInfo.MaxDataSize)
            {
                int streamLength = stream.ByteSize;
                stream.PrepLength(sizeof(uint) + sizeof(int) * 3);
                CommandBase nextBuild = LinkNext;
                stream.ByteSize += sizeof(uint) + sizeof(int) * 2;
                if ((CommandInfo.CommandFlags & CommandFlags.JsonSerialize) == 0)
                {
                    Socket.Serialize(CommandInfo, ref InputParameter);
                }
                else
                {
                    Socket.JsonSerialize(ref InputParameter);
                }
                int dataLength = stream.ByteSize - streamLength - (sizeof(int) * 2 + sizeof(uint));
                InputParameter = default(inputParameterType);
                if (dataLength <= Socket.MaxInputSize)
                {
                    int commandIndex = Socket.CommandPool.Push(this);
                    if (commandIndex != 0)
                    {
                        if (KeepCallback.SetCommandIndex(commandIndex))
                        {
                            byte *write = stream.Data.Byte + streamLength;
                            buildInfo.IsVerifyMethod |= CommandInfo.IsVerifyMethod;
                            ++buildInfo.Count;
                            *(int *)write = CommandInfo.Command;
                            *(uint *)(write + sizeof(int)) = (uint)commandIndex | (uint)CommandInfo.CommandFlags;
                            *(int *)(write + (sizeof(uint) + sizeof(int))) = dataLength;
                            CommandInfo.CheckMaxDataSize(Math.Max(dataLength + (sizeof(int) * 2 + sizeof(uint)), stream.LastPrepSize - streamLength));
                            LinkNext = null;
                            return(nextBuild);
                        }
                        Socket.CommandPool.Cancel(commandIndex);
                    }
                    else
                    {
                        KeepCallback.BuildCancel();
                    }
                }
                else
                {
                    KeepCallback.BuildCancel();
                }
                stream.ByteSize = streamLength;
                LinkNext        = null;
                return(nextBuild);
            }
            buildInfo.isFullSend = 1;
            return(this);
        }
Exemplo n.º 7
0
 public void Set(ClientSocket socket, CommandInfo command, Action <ReturnValue> onCall)
 {
     if (command.TaskType != ClientTaskType.Synchronous)
     {
         OutputLock = new object();
     }
     KeepCallback = new KeepCallback(this);
     Socket       = socket;
     Callback     = onCall;
     CommandInfo  = command;
 }
Exemplo n.º 8
0
 internal void Set(ClientSocket socket, CommandInfo command, Callback <ReturnValue <outputParameterType> > callback)
 {
     if (command.TaskType != ClientTaskType.Synchronous)
     {
         OutputLock = new object();
     }
     KeepCallback    = new KeepCallback(this);
     Socket          = socket;
     Callback        = callback;
     CommandInfo     = command;
     OutputParameter = default(outputParameterType);
 }
Exemplo n.º 9
0
 internal void Set(ClientSocket socket, CommandInfo command, Action <ReturnValue> onCall, ref inputParameterType inputParameter)
 {
     if (command.TaskType != ClientTaskType.Synchronous)
     {
         OutputLock = new object();
     }
     KeepCallback   = new KeepCallback(this);
     Socket         = socket;
     Callback       = onCall;
     CommandInfo    = command;
     InputParameter = inputParameter;
 }
Exemplo n.º 10
0
        private unsafe void onReceive(ref SubArray <byte> data, ref ReturnValue <outputParameterType> outputParameter)
        {
            outputParameter.Type = GetReturnType(ref data);
            if (outputParameter.Type == ReturnType.ClientDeSerializeError)
            {
                outputParameter.Value = OutputParameter;
                if ((CommandInfo.CommandFlags & CommandFlags.JsonSerialize) == 0)
                {
                    if (CommandInfo.SimpleSerializeOutputParamter == 0)
                    {
                        if (Socket.DeSerialize(ref data, ref outputParameter.Value))
                        {
                            outputParameter.Type = ReturnType.Success;
                        }
                    }
                    else
                    {
                        fixed(byte *dataFixed = data.Array)
                        {
                            byte *start = dataFixed + data.Start, end = start + data.Length;

                            if (SimpleSerialize.TypeDeSerializer <outputParameterType> .DeSerialize(start, ref outputParameter.Value, end) == end)
                            {
                                outputParameter.Type = ReturnType.Success;
                            }
                        }
                    }
                }
                else
                {
                    if (Socket.ParseJson(ref data, ref outputParameter.Value))
                    {
                        outputParameter.Type = ReturnType.Success;
                    }
                }
            }
            else if ((byte)outputParameter.Type < (byte)ReturnType.Success)
            {
                KeepCallback.Cancel();
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// 接收数据回调处理
        /// </summary>
        /// <param name="data">输出数据</param>
        internal override void OnReceive(ref SubArray <byte> data)
        {
            Action <ReturnValue> callback = Callback;

            if (callback != null)
            {
                ReturnType value = GetReturnType(ref data);
                if ((byte)value < (byte)ReturnType.Success)
                {
                    KeepCallback.Cancel();
                }
                if (CommandInfo.TaskType == ClientTaskType.Synchronous)
                {
                    try
                    {
                        callback(new ReturnValue {
                            Type = value
                        });
                    }
                    catch (Exception error) { Socket.Log.Exception(error, null, LogLevel.Exception | LogLevel.AutoCSer); }
                }
                else
                {
                    int       isOutput  = 1;
                    Exception exception = null;
                    OutputLock.Enter();
                    try
                    {
                        if (outputParameters.FreeCount == 0)
                        {
                            OutputLock.SleepFlag = 1;
                        }
                        outputParameters.Add(value);
                        isOutput      = this.isOutput;
                        this.isOutput = 1;
                    }
                    catch (Exception error) { exception = error; }
                    finally
                    {
                        OutputLock.ExitSleepFlag();
                        if (exception != null)
                        {
                            Socket.Log.Exception(exception, null, LogLevel.Exception | LogLevel.AutoCSer);
                        }
                    }
                    if (isOutput == 0)
                    {
                        switch (CommandInfo.TaskType)
                        {
                        case ClientTaskType.ThreadPool: if (!System.Threading.ThreadPool.QueueUserWorkItem(threadPoolOnReceive))
                            {
                                AutoCSer.Threading.TaskSwitchThreadArray.Default.CurrentThread.Add(this);
                            }
                            return;

                        case ClientTaskType.Timeout: AutoCSer.Threading.TaskSwitchThreadArray.Default.CurrentThread.Add(this); return;

                        case ClientTaskType.TcpTask: ClientCallThreadArray.Default.CurrentThread.Add(this); return;

                        case ClientTaskType.TcpQueue: ClientCallQueue.Default.Add(this); return;
                        }
                    }
                }
            }
        }