Beispiel #1
0
 protected void FireErrorEvent(Exception e, HproseContext context)
 {
     if (OnSendError != null)
     {
         OnSendError(e, context);
     }
 }
 protected override object[] FixArguments(Type[] argumentTypes, object[] arguments, int count, HproseContext context) {
     HproseHttpListenerContext currentContext = (HproseHttpListenerContext)context;
     if (argumentTypes.Length != count) {
         object[] args = new object[argumentTypes.Length];
         System.Array.Copy(arguments, 0, args, 0, count);
         Type argType = argumentTypes[count];
         if (argType == typeof(HproseContext) ||
             argType == typeof(HproseHttpListenerContext)) {
             args[count] = currentContext;
         }
         else if (argType == typeof(HttpListenerContext)) {
             args[count] = currentContext.Context;
         }
         else if (argType == typeof(HttpListenerRequest)) {
             args[count] = currentContext.Request;
         }
         else if (argType == typeof(HttpListenerResponse)) {
             args[count] = currentContext.Response;
         }
         else if (argType == typeof(IPrincipal)) {
             args[count] = currentContext.User;
         }
         return args;
     }
     return arguments;
 }
        public MemoryStream OutputFilter(MemoryStream outStream, HproseContext context)
        {
            //client
            var buf = new byte[outStream.Length + 8];

            outStream.Read(buf, 8, (int)outStream.Length);
            var resultStream = new MemoryStream(buf);
            var tokenbuf     = Encoding.UTF8.GetBytes(this.secretKey);

            resultStream.Write(tokenbuf, 0, 8);
            resultStream.Position = resultStream.Length;
            return(resultStream);
        }
Beispiel #4
0
 protected virtual object[] FixArguments(Type[] argumentTypes, object[] arguments, int count, HproseContext context)
 {
     if (argumentTypes.Length != count)
     {
         object[] args = new object[argumentTypes.Length];
         System.Array.Copy(arguments, 0, args, 0, count);
         Type argType = argumentTypes[count];
         if (argType == typeof(HproseContext))
         {
             args[count] = context;
         }
         return(args);
     }
     return(arguments);
 }
Beispiel #5
0
        private MemoryStream ResponseEnd(MemoryStream data, HproseContext context)
        {
            data.Position = 0;
            for (int i = 0, n = filters.Count; i < n; ++i)
            {
#if (dotNET10 || dotNET11 || dotNETCF10 || dotNETMF)
                IHproseFilter filter = (IHproseFilter)filters[i];
                data = filter.OutputFilter(data, context);
#else
                data = filters[i].OutputFilter(data, context);
#endif
                data.Position = 0;
            }
            return(data);
        }
 protected override object[] FixArguments(Type[] argumentTypes, object[] arguments, int count, HproseContext context) {
     HproseTcpListenerContext currentContext = (HproseTcpListenerContext)context;
     if (argumentTypes.Length != count) {
         object[] args = new object[argumentTypes.Length];
         System.Array.Copy(arguments, 0, args, 0, count);
         Type argType = argumentTypes[count];
         if (argType == typeof(HproseContext) ||
             argType == typeof(HproseTcpListenerContext)) {
             args[count] = currentContext;
         }
         else if (argType == typeof(TcpClient)) {
             args[count] = currentContext.Client;
         }
         return args;
     }
     return arguments;
 }
Beispiel #7
0
            /// <inheritdoc />
            public MemoryStream InputFilter(MemoryStream inStream, HproseContext context)
            {
                var len = (int)inStream.Length - 7;

                if (len <= 0 || inStream.ReadByte() != 's' || inStream.ReadByte() != 'i' || inStream.ReadByte() != 'd')
                {
                    return(inStream);
                }
                var sid = inStream.ReadByte() << 24 |
                          inStream.ReadByte() << 16 |
                          inStream.ReadByte() << 8 |
                          inStream.ReadByte();

                _sessionIdMap[context] = sid;
                var buf = new byte[len];

                inStream.Read(buf, 0, len);
                return(new MemoryStream(buf));
            }
Beispiel #8
0
            /// <inheritdoc />
            public MemoryStream OutputFilter(MemoryStream outStream, HproseContext context)
            {
                if (!_sessionIdMap.ContainsKey(context))
                {
                    return(outStream);
                }
                var sid = _sessionIdMap[context];
                var buf = new byte[outStream.Length + 7];

                buf[0] = (byte)'s';
                buf[1] = (byte)'i';
                buf[2] = (byte)'d';
                buf[3] = (byte)(sid >> 24 & 0xff);
                buf[4] = (byte)(sid >> 16 & 0xff);
                buf[5] = (byte)(sid >> 8 & 0xff);
                buf[6] = (byte)(sid & 0xff);
                outStream.Read(buf, 7, (int)outStream.Length);
                return(new MemoryStream(buf));
            }
Beispiel #9
0
        public MemoryStream InputFilter(MemoryStream inStream, HproseContext context)
        {
            //server
            if (inStream.Length < 8)
            {
                throw new SystemException("token verification failed..");
            }
            var tokenbuf = new byte[8];

            inStream.Read(tokenbuf, 0, 8);
            string token = Encoding.UTF8.GetString(tokenbuf);

            if (!allowSecretKeys.Contains(token))
            {
                throw new SystemException("token verification failed..");
            }
            byte[] buf = new byte[inStream.Length - 8];
            inStream.Read(buf, 0, (int)inStream.Length - 8);
            return(new MemoryStream(buf));
        }
Beispiel #10
0
        protected MemoryStream SendError(Exception e, HproseContext context)
        {
            if (OnSendError != null)
            {
                OnSendError(e, context);
            }
            string error = debugEnabled ? e.ToString(): e.Message;

#if !dotNETMF
            MemoryStream data   = new MemoryStream(4096);
            HproseWriter writer = new HproseWriter(data, mode, true);
#else
            MemoryStream data   = new MemoryStream();
            HproseWriter writer = new HproseWriter(data, true);
#endif
            data.WriteByte(HproseTags.TagError);
            writer.WriteString(error);
            data.WriteByte(HproseTags.TagEnd);
            return(ResponseEnd(data, context));
        }
Beispiel #11
0
        protected MemoryStream DoFunctionList(HproseMethods methods, HproseContext context)
        {
#if dotNETMF
            ArrayList names = new ArrayList();
            foreach (object name in GlobalMethods.AllNames)
            {
                names.Add(name);
            }
#elif !(dotNET10 || dotNET11 || dotNETCF10)
            List <string> names = new List <string>(GlobalMethods.AllNames);
#else
            ArrayList names = new ArrayList(GlobalMethods.AllNames);
#endif
            if (methods != null)
            {
#if dotNETMF
                foreach (object name in methods.AllNames)
                {
                    names.Add(name);
                }
#else
                names.AddRange(methods.AllNames);
#endif
            }
#if !dotNETMF
            MemoryStream data   = new MemoryStream(4096);
            HproseWriter writer = new HproseWriter(data, mode, true);
#else
            MemoryStream data   = new MemoryStream();
            HproseWriter writer = new HproseWriter(data, true);
#endif
            data.WriteByte(HproseTags.TagFunctions);
#if !(dotNET10 || dotNET11 || dotNETCF10 || dotNETMF)
            writer.WriteList((IList <string>)names);
#else
            writer.WriteList((IList)names);
#endif
            data.WriteByte(HproseTags.TagEnd);
            return(ResponseEnd(data, context));
        }
Beispiel #12
0
        protected MemoryStream Handle(MemoryStream istream, HproseMethods methods, HproseContext context)
        {
#if !(Smartphone || dotNETMF)
            currentContext = context;
#endif
            try {
                istream.Position = 0;
                for (int i = filters.Count - 1; i >= 0; --i)
                {
#if (dotNET10 || dotNET11 || dotNETCF10 || dotNETMF)
                    IHproseFilter filter = (IHproseFilter)filters[i];
                    istream = filter.InputFilter(istream, context);
#else
                    istream = filters[i].InputFilter(istream, context);
#endif
                    istream.Position = 0;
                }
                switch (istream.ReadByte())
                {
                case HproseTags.TagCall:
                    return(DoInvoke(istream, methods, context));

                case HproseTags.TagEnd:
                    return(DoFunctionList(methods, context));

                default:
                    return(SendError(new HproseException("Wrong Request: \r\n" + HproseHelper.ReadWrongInfo(istream)), context));
                }
            }
            catch (Exception e) {
                return(SendError(e, context));
            }
#if !(Smartphone || dotNETMF)
            finally {
                currentContext = null;
            }
#endif
        }
Beispiel #13
0
 MemoryStream IHproseFilter.OutputFilter(MemoryStream outStream, HproseContext context)
 {
     return(outStream);
 }
Beispiel #14
0
 MemoryStream IHproseFilter.InputFilter(MemoryStream inStream, HproseContext context)
 {
     return(inStream);
 }
 private static void Service_OnSendError(Exception e, HproseContext context)
 {
     SystemLogger.getLogger().Debug(e.Message);
 }
 public MemoryStream InputFilter(MemoryStream inStream, HproseContext context)
 {
     //client
     return(inStream);
 }
        /// <summary>
        /// 调用方法前进认证
        /// </summary>
        private static void Service_OnBeforeInvoke(string name, object[] args, bool byRef, HproseContext context)
        {
            //throw new NotImplementedException();

            if (HttpContext.Current.Session == null)
            {
                throw new NotImplementedException();
            }
        }
Beispiel #18
0
 public MemoryStream OutputFilter(MemoryStream outStream, HproseContext context)
 {
     //server
     return(outStream);
 }
        protected override object[] FixArguments(Type[] argumentTypes, object[] arguments, int count, HproseContext context)
        {
            HproseHttpListenerContext currentContext = (HproseHttpListenerContext)context;

            if (argumentTypes.Length != count)
            {
                object[] args = new object[argumentTypes.Length];
                System.Array.Copy(arguments, 0, args, 0, count);
                Type argType = argumentTypes[count];
                if (argType == typeof(HproseContext) ||
                    argType == typeof(HproseHttpListenerContext))
                {
                    args[count] = currentContext;
                }
                else if (argType == typeof(HttpListenerContext))
                {
                    args[count] = currentContext.Context;
                }
                else if (argType == typeof(HttpListenerRequest))
                {
                    args[count] = currentContext.Request;
                }
                else if (argType == typeof(HttpListenerResponse))
                {
                    args[count] = currentContext.Response;
                }
#if !dotNETMF
                else if (argType == typeof(IPrincipal))
                {
                    args[count] = currentContext.User;
                }
#endif
                return(args);
            }
            return(arguments);
        }
        protected override object[] FixArguments(Type[] argumentTypes, object[] arguments, int count, HproseContext context)
        {
            HproseTcpListenerContext currentContext = (HproseTcpListenerContext)context;

            if (argumentTypes.Length != count)
            {
                object[] args = new object[argumentTypes.Length];
                System.Array.Copy(arguments, 0, args, 0, count);
                Type argType = argumentTypes[count];
                if (argType == typeof(HproseContext) ||
                    argType == typeof(HproseTcpListenerContext))
                {
                    args[count] = currentContext;
                }
                else if (argType == typeof(TcpClient))
                {
                    args[count] = currentContext.Client;
                }
                return(args);
            }
            return(arguments);
        }
Beispiel #21
0
        protected MemoryStream DoInvoke(MemoryStream istream, HproseMethods methods, HproseContext context)
        {
#if !dotNETMF
            HproseReader reader = new HproseReader(istream, mode);
            MemoryStream data   = new MemoryStream(4096);
#else
            HproseReader reader = new HproseReader(istream);
            MemoryStream data   = new MemoryStream();
#endif
            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;
#if !dotNETMF
                    HproseWriter writer = new HproseWriter(data, mode, simple);
#else
                    HproseWriter writer = new HproseWriter(data, simple);
#endif
                    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));
        }
Beispiel #22
0
 public MemoryStream OutputFilter(MemoryStream outStream, HproseContext context)
 {
     throw new NotImplementedException();
 }
Beispiel #23
0
        protected override object[] FixArguments(Type[] argumentTypes, object[] arguments, int count, HproseContext context)
        {
            HproseHttpContext currentContext = (HproseHttpContext)context;

            if (argumentTypes.Length != count)
            {
                object[] args = new object[argumentTypes.Length];
                System.Array.Copy(arguments, 0, args, 0, count);
                Type argType = argumentTypes[count];
                if (argType == typeof(HproseContext) ||
                    argType == typeof(HproseHttpContext))
                {
                    args[count] = currentContext;
                }
                else if (argType == typeof(HttpContext))
                {
                    args[count] = currentContext.Context;
                }
                else if (argType == typeof(HttpRequest))
                {
                    args[count] = currentContext.Request;
                }
                else if (argType == typeof(HttpResponse))
                {
                    args[count] = currentContext.Response;
                }
                else if (argType == typeof(HttpServerUtility))
                {
                    args[count] = currentContext.Server;
                }
                else if (argType == typeof(HttpApplicationState))
                {
                    args[count] = currentContext.Application;
                }
                else if (argType == typeof(HttpSessionState))
                {
                    args[count] = currentContext.Session;
                }
                return(args);
            }
            return(arguments);
        }