Пример #1
0
        public static void Load(this HttpComponent self)
        {
            self.dispatcher = new Dictionary <string, IHttpHandler>();

            HashSet <Type> types = EventSystem.Instance.GetTypes(typeof(HttpHandlerAttribute));

            SceneType sceneType = self.GetParent <Scene>().SceneType;

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(HttpHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }

                HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute)attrs[0];

                if (httpHandlerAttribute.SceneType != sceneType)
                {
                    continue;
                }

                object obj = Activator.CreateInstance(type);

                IHttpHandler ihttpHandler = obj as IHttpHandler;
                if (ihttpHandler == null)
                {
                    throw new Exception($"HttpHandler handler not inherit IHttpHandler class: {obj.GetType().FullName}");
                }
                self.dispatcher.Add(httpHandlerAttribute.Path, ihttpHandler);
            }
        }
Пример #2
0
 public static async ETTask Handle(this HttpComponent self, HttpListenerContext context)
 {
     try
     {
         IHttpHandler handler;
         if (self.dispatcher.TryGetValue(context.Request.Url.AbsolutePath, out handler))
         {
             await handler.Handle(self.Domain, context);
         }
     }
     catch (Exception e)
     {
         Log.Error(e);
     }
     context.Request.InputStream.Dispose();
     context.Response.OutputStream.Dispose();
 }
Пример #3
0
        public static async ETTask Accept(this HttpComponent self)
        {
            long instanceId = self.InstanceId;

            while (self.InstanceId == instanceId)
            {
                try
                {
                    HttpListenerContext context = await self.Listener.GetContextAsync();

                    self.Handle(context).Coroutine();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            }
        }