コード例 #1
0
ファイル: HttpComponentSystem.cs プロジェクト: wikieden/ET
        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
ファイル: HttpComponentSystem.cs プロジェクト: wikieden/ET
 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
ファイル: HttpComponentSystem.cs プロジェクト: wikieden/ET
        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);
                }
            }
        }