public void ToChannelMethodInvoke(byte[] data, ServiceChannelManager channelManager, out ServiceChannel channel, out int methodIndex, out object[] args)
        {
            BinaryDataReader reader = new BinaryDataReader(data);

            channel = channelManager[reader.ReadString()];
            if (channel == null)
            {
                methodIndex = -1;
                args        = null;
                return;
            }
            methodIndex = reader.ReadInt32();
            if (methodIndex < 0 || methodIndex >= channel.Provider.ServerOperations.Count)
            {
                methodIndex = -1;
                args        = null;
                return;
            }
            var method    = channel.Provider.ServerOperations[methodIndex];
            var parameter = method.GetParameters();

            args = new object[reader.ReadByte()];
            for (int i = 0; i < args.Length; i++)
            {
                args[i] = Formatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes());
            }
        }
        public void ToChannelMethodResult(byte[] data, Type resultType, out object result, out string errorMsg)
        {
            BinaryDataReader reader = new BinaryDataReader(data);

            if (reader.ReadBoolean())
            {
                errorMsg = null;
                if (resultType == null || resultType == typeof(void))
                {
                    result = null;
                }
                else
                {
                    result = Formatter.Deserialize(resultType, reader.ReadBytes());
                }
            }
            else
            {
                result   = null;
                errorMsg = reader.ReadString();
            }
        }
Пример #3
0
        internal void Receive_3_InvokeMethod(CommunicationReceiveEventArgs e)
        {
            byte[]         data;
            ServiceChannel channel;
            int            methodIndex;

            object[]         args;
            BinaryDataReader reader = new BinaryDataReader(e.Data);
            var channelName         = reader.ReadString().ToLower().Trim();

            channel = ChannelBuffer.Keys.SingleOrDefault(t => t.Name.ToLower().Trim() == channelName);
            if (channel == null)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "ChannelNotExist");
                Client.Send(data, e.Head);
                return;
            }
            methodIndex = reader.ReadInt32();
            if (methodIndex >= channel.Provider.ClientOperations.Count)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "MethodNotExist");
                Client.Send(data, e.Head);
                return;
            }
            var method    = channel.Provider.ClientOperations[methodIndex];
            var parameter = method.GetParameters();

            args = new object[reader.ReadByte()];
            for (int i = 0; i < args.Length; i++)
            {
                if (channel.Formatter != null)
                {
                    args[i] = channel.Formatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes());
                }
                else
                {
                    args[i] = DataFormatter.Deserialize(parameter[i].ParameterType, reader.ReadBytes());
                }
            }

            var instance = ChannelBuffer[channel].GetType().GetMethod("GetChannel").Invoke(ChannelBuffer[channel], null);

            if (instance == null)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "InstanceCreateFailed");
                Client.Send(data, e.Head);
                return;
            }
            object result;

            try
            {
                result = method.Invoke(instance, args);
            }
            catch (ArgumentException)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "MethodParametersError");
                Client.Send(data, e.Head);
                return;
            }
            catch (Exception)
            {
                data = MessageFormatter.FromChannelMethodResult(null, "MethodRuntimeError");
                Client.Send(data, e.Head);
                return;
            }
            data = MessageFormatter.FromChannelMethodResult(result, null);
            Client.Send(data, e.Head);
        }