Exemplo n.º 1
0
        private object DoInput(object[] arguments, Type returnType, HproseResultMode resultMode, Stream istream)
        {
            int tag;

            if (filter != null)
            {
                istream = filter.InputFilter(istream);
            }
            object       result       = null;
            HproseReader hproseReader = new HproseReader(istream, mode);
            MemoryStream memstream    = null;

            if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
            {
                memstream = new MemoryStream();
            }
            while ((tag = istream.ReadByte()) != HproseTags.TagEnd)
            {
                switch (tag)
                {
                case HproseTags.TagResult:
                    if (resultMode == HproseResultMode.Normal)
                    {
                        hproseReader.Reset();
                        result = hproseReader.Unserialize(returnType);
                    }
                    else if (resultMode == HproseResultMode.Serialized)
                    {
                        result = hproseReader.ReadRaw();
                    }
                    else
                    {
                        memstream.WriteByte(HproseTags.TagResult);
                        hproseReader.ReadRaw(memstream);
                    }
                    break;

                case HproseTags.TagArgument:
                    if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
                    {
                        memstream.WriteByte(HproseTags.TagArgument);
                        hproseReader.ReadRaw(memstream);
                    }
                    else
                    {
                        hproseReader.Reset();
                        Object[] args   = hproseReader.ReadObjectArray();
                        int      length = arguments.Length;
                        if (length > args.Length)
                        {
                            length = args.Length;
                        }
                        Array.Copy(args, 0, arguments, 0, length);
                    }
                    break;

                case HproseTags.TagError:
                    if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
                    {
                        memstream.WriteByte(HproseTags.TagError);
                        hproseReader.ReadRaw(memstream);
                    }
                    else
                    {
                        hproseReader.Reset();
                        result = new HproseException(hproseReader.ReadString());
                    }
                    break;

                default:
                    throw hproseReader.UnexpectedTag(tag, (char)HproseTags.TagResult + "" +
                                                     (char)HproseTags.TagArgument + "" +
                                                     (char)HproseTags.TagError + "" +
                                                     (char)HproseTags.TagEnd);
                }
            }
            if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
            {
                if (resultMode == HproseResultMode.RawWithEndTag)
                {
                    memstream.WriteByte(HproseTags.TagEnd);
                }
                memstream.Position = 0;
                result             = memstream;
            }
            return(result);
        }
Exemplo n.º 2
0
        protected MemoryStream DoInvoke(MemoryStream istream, HproseMethods methods, object context)
        {
            HproseReader reader = new HproseReader(istream, mode);
            MemoryStream data   = new MemoryStream(4096);
            int          tag;

            do
            {
                reader.Reset();
                string       name         = reader.ReadString();
                HproseMethod remoteMethod = null;
                int          count        = 0;
                object[]     args         = null;
                object[]     arguments    = null;
                bool         byRef        = false;
                tag = reader.CheckTags((char)HproseTags.TagList + "" +
                                       (char)HproseTags.TagEnd + "" +
                                       (char)HproseTags.TagCall);
                if (tag == HproseTags.TagList)
                {
                    reader.Reset();
                    count = reader.ReadInt(HproseTags.TagOpenbrace);
                    if (methods != null)
                    {
                        remoteMethod = methods.GetMethod(name, count);
                    }
                    if (remoteMethod == null)
                    {
                        remoteMethod = GlobalMethods.GetMethod(name, count);
                    }
                    if (remoteMethod == null)
                    {
                        arguments = reader.ReadArray(count);
                    }
                    else
                    {
                        arguments = new object[count];
                        reader.ReadArray(remoteMethod.paramTypes, arguments, count);
                    }
                    tag = reader.CheckTags((char)HproseTags.TagTrue + "" +
                                           (char)HproseTags.TagEnd + "" +
                                           (char)HproseTags.TagCall);
                    if (tag == HproseTags.TagTrue)
                    {
                        byRef = true;
                        tag   = reader.CheckTags((char)HproseTags.TagEnd + "" +
                                                 (char)HproseTags.TagCall);
                    }
                }
                else
                {
                    if (methods != null)
                    {
                        remoteMethod = methods.GetMethod(name, 0);
                    }
                    if (remoteMethod == null)
                    {
                        remoteMethod = GlobalMethods.GetMethod(name, 0);
                    }
                    arguments = new object[0];
                }
                if (OnBeforeInvoke != null)
                {
                    OnBeforeInvoke(name, arguments, byRef, context);
                }
                if (remoteMethod == null)
                {
                    args = arguments;
                }
                else
                {
                    args = FixArguments(remoteMethod.paramTypes, arguments, count, context);
                }
                object result;
                if (remoteMethod == null)
                {
                    if (methods != null)
                    {
                        remoteMethod = methods.GetMethod("*", 2);
                    }
                    if (remoteMethod == null)
                    {
                        remoteMethod = GlobalMethods.GetMethod("*", 2);
                    }
                    if (remoteMethod == null)
                    {
                        throw new MissingMethodException("Can't find this method " + name);
                    }
                    result = remoteMethod.method.Invoke(remoteMethod.obj, new object[] { name, args });
                }
                else
                {
                    result = remoteMethod.method.Invoke(remoteMethod.obj, args);
                }
                if (byRef)
                {
                    Array.Copy(args, 0, arguments, 0, count);
                }
                if (OnAfterInvoke != null)
                {
                    OnAfterInvoke(name, arguments, byRef, result, context);
                }
                if (remoteMethod.mode == HproseResultMode.RawWithEndTag)
                {
                    data.Write((byte[])result, 0, ((byte[])result).Length);
                    return(ResponseEnd(data, context));
                }
                else if (remoteMethod.mode == HproseResultMode.Raw)
                {
                    data.Write((byte[])result, 0, ((byte[])result).Length);
                }
                else
                {
                    data.WriteByte(HproseTags.TagResult);
                    bool         simple = remoteMethod.simple;
                    HproseWriter writer = new HproseWriter(data, mode, simple);
                    if (remoteMethod.mode == HproseResultMode.Serialized)
                    {
                        data.Write((byte[])result, 0, ((byte[])result).Length);
                    }
                    else
                    {
                        writer.Serialize(result);
                    }
                    if (byRef)
                    {
                        data.WriteByte(HproseTags.TagArgument);
                        writer.Reset();
                        writer.WriteArray(arguments);
                    }
                }
            } while (tag == HproseTags.TagCall);
            data.WriteByte(HproseTags.TagEnd);
            return(ResponseEnd(data, context));
        }
Exemplo n.º 3
0
        private object DoInput(MemoryStream inData, object[] arguments, Type returnType, HproseResultMode resultMode, HproseClientContext context)
        {
            for (int i = filters.Count - 1; i >= 0; --i)
            {
                inData.Position = 0;
#if (dotNET10 || dotNET11 || dotNETCF10 || dotNETMF)
                IHproseFilter filter = (IHproseFilter)filters[i];
                inData = filter.InputFilter(inData, context);
#else
                inData = filters[i].InputFilter(inData, context);
#endif
            }
            inData.Position = inData.Length - 1;
            int tag = inData.ReadByte();
            if (tag != HproseTags.TagEnd)
            {
                throw new HproseException("Wrong Response: \r\n" + HproseHelper.ReadWrongInfo(inData));
            }
            inData.Position = 0;
            if (resultMode == HproseResultMode.Raw)
            {
                inData.SetLength(inData.Length - 1);
            }
            if (resultMode == HproseResultMode.RawWithEndTag ||
                resultMode == HproseResultMode.Raw)
            {
                return(MemoryStreamToType(inData, returnType));
            }
            object result = null;
#if !dotNETMF
            HproseReader reader = new HproseReader(inData, mode);
#else
            HproseReader reader = new HproseReader(inData);
#endif
            while ((tag = inData.ReadByte()) != HproseTags.TagEnd)
            {
                switch (tag)
                {
                case HproseTags.TagResult:
                    if (resultMode == HproseResultMode.Normal)
                    {
                        reader.Reset();
                        result = reader.Unserialize(returnType);
                    }
                    else
                    {
                        result = MemoryStreamToType(reader.ReadRaw(), returnType);
                    }
                    break;

                case HproseTags.TagArgument:
                    reader.Reset();
                    Object[] args   = reader.ReadObjectArray();
                    int      length = arguments.Length;
                    if (length > args.Length)
                    {
                        length = args.Length;
                    }
                    Array.Copy(args, 0, arguments, 0, length);
                    break;

                case HproseTags.TagError:
                    reader.Reset();
                    result = new HproseException(reader.ReadString());
                    break;

                default:
                    throw new HproseException("Wrong Response: \r\n" + HproseHelper.ReadWrongInfo(inData));
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        protected void DoInvoke(HproseMethods methods)
        {
            Stream       istream = InputStream;
            HproseReader reader  = new HproseReader(istream, mode);
            Stream       ostream = OutputStream;
            HproseWriter writer  = new HproseWriter(ostream, mode);
            int          tag;

            do
            {
                reader.Reset();
                string       name         = ((string)reader.ReadString());
                HproseMethod remoteMethod = null;
                int          count        = 0;
                object[]     args         = null;
                object[]     arguments    = null;
                bool         byRef        = false;
                tag = reader.CheckTags((char)HproseTags.TagList + "" +
                                       (char)HproseTags.TagEnd + "" +
                                       (char)HproseTags.TagCall);
                if (tag == HproseTags.TagList)
                {
                    reader.Reset();
                    count = reader.ReadInt(HproseTags.TagOpenbrace);
                    if (methods != null)
                    {
                        remoteMethod = methods.GetMethod(name, count);
                    }
                    if (remoteMethod == null)
                    {
                        remoteMethod = GlobalMethods.GetMethod(name, count);
                    }
                    if (remoteMethod == null)
                    {
                        arguments = reader.ReadArray(count);
                    }
                    else
                    {
                        arguments = new object[count];
                        reader.ReadArray(remoteMethod.paramTypes, arguments, count);
                    }
                    reader.CheckTag(HproseTags.TagClosebrace);
                    tag = reader.CheckTags((char)HproseTags.TagTrue + "" +
                                           (char)HproseTags.TagEnd + "" +
                                           (char)HproseTags.TagCall);
                    if (tag == HproseTags.TagTrue)
                    {
                        byRef = true;
                        tag   = reader.CheckTags((char)HproseTags.TagEnd + "" +
                                                 (char)HproseTags.TagCall);
                    }
                }
                else
                {
                    if (methods != null)
                    {
                        remoteMethod = methods.GetMethod(name, 0);
                    }
                    if (remoteMethod == null)
                    {
                        remoteMethod = GlobalMethods.GetMethod(name, 0);
                    }
                    arguments = new object[0];
                }
                if (OnBeforeInvoke != null)
                {
                    OnBeforeInvoke(name, arguments, byRef);
                }
                if (remoteMethod == null)
                {
                    args = arguments;
                }
                else
                {
                    args = FixArguments(remoteMethod.paramTypes, arguments, count);
                }
                object result;
                if (remoteMethod == null)
                {
                    if (methods != null)
                    {
                        remoteMethod = methods.GetMethod("*", 2);
                    }
                    if (remoteMethod == null)
                    {
                        remoteMethod = GlobalMethods.GetMethod("*", 2);
                    }
                    if (remoteMethod == null)
                    {
                        throw new MissingMethodException("Can't find this method " + name);
                    }
                    result = remoteMethod.method.Invoke(remoteMethod.obj, new object[] { name, args });
                }
                else
                {
                    result = remoteMethod.method.Invoke(remoteMethod.obj, args);
                }
                if (byRef)
                {
                    Array.Copy(args, 0, arguments, 0, count);
                }
                if (OnAfterInvoke != null)
                {
                    OnAfterInvoke(name, arguments, byRef, result);
                }
                if (remoteMethod.mode == HproseResultMode.RawWithEndTag)
                {
                    ostream.Write((byte[])result, 0, ((byte[])result).Length);
                    ostream.Flush();
                    return;
                }
                else if (remoteMethod.mode == HproseResultMode.Raw)
                {
                    ostream.Write((byte[])result, 0, ((byte[])result).Length);
                }
                else
                {
                    ostream.WriteByte(HproseTags.TagResult);
                    if (remoteMethod.mode == HproseResultMode.Serialized)
                    {
                        ostream.Write((byte[])result, 0, ((byte[])result).Length);
                    }
                    else
                    {
                        writer.Reset();
                        writer.Serialize(result);
                    }
                    if (byRef)
                    {
                        ostream.WriteByte(HproseTags.TagArgument);
                        writer.Reset();
                        writer.WriteArray(arguments, false);
                    }
                }
            } while (tag == HproseTags.TagCall);
            ostream.WriteByte(HproseTags.TagEnd);
            ostream.Flush();
        }
Exemplo n.º 5
0
        private object DoInput(object[] arguments, Type returnType, HproseResultMode resultMode, Stream istream)
        {
            int          tag;
            object       result       = null;
            HproseReader hproseReader = new HproseReader(istream, mode);
            MemoryStream memstream    = null;

            if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
            {
                memstream = new MemoryStream();
            }
            while ((tag = hproseReader.CheckTags(
                        (char)HproseTags.TagResult + "" +
                        (char)HproseTags.TagArgument + "" +
                        (char)HproseTags.TagError + "" +
                        (char)HproseTags.TagEnd)) != HproseTags.TagEnd)
            {
                switch (tag)
                {
                case HproseTags.TagResult:
                    if (resultMode == HproseResultMode.Normal)
                    {
                        hproseReader.Reset();
                        result = hproseReader.Unserialize(returnType);
                    }
                    else if (resultMode == HproseResultMode.Serialized)
                    {
                        result = hproseReader.ReadRaw();
                    }
                    else
                    {
                        memstream.WriteByte(HproseTags.TagResult);
                        hproseReader.ReadRaw(memstream);
                    }
                    break;

                case HproseTags.TagArgument:
                    if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
                    {
                        memstream.WriteByte(HproseTags.TagArgument);
                        hproseReader.ReadRaw(memstream);
                    }
                    else
                    {
                        hproseReader.Reset();
                        Object[] args = (Object[])hproseReader.ReadList(HproseHelper.typeofObjectArray);
                        Array.Copy(args, 0, arguments, 0, arguments.Length);
                    }
                    break;

                case HproseTags.TagError:
                    if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
                    {
                        memstream.WriteByte(HproseTags.TagError);
                        hproseReader.ReadRaw(memstream);
                    }
                    else
                    {
                        hproseReader.Reset();
                        result = new HproseException((string)hproseReader.ReadString());
                    }
                    break;
                }
            }
            if (resultMode == HproseResultMode.RawWithEndTag || resultMode == HproseResultMode.Raw)
            {
                if (resultMode == HproseResultMode.RawWithEndTag)
                {
                    memstream.WriteByte(HproseTags.TagEnd);
                }
                memstream.Position = 0;
                result             = memstream;
            }
            return(result);
        }