Просмотр исходного кода

Merge pull request #50 from Yinmany/master

1. 让Http处理方法,支持async。
tanghai 7 лет назад
Родитель
Сommit
13556c208b

+ 1 - 1
Server/App/Program.cs

@@ -105,7 +105,7 @@ namespace App
 						Game.Scene.AddComponent<ConfigComponent>();
 						Game.Scene.AddComponent<ServerFrameComponent>();
 						Game.Scene.AddComponent<ActorManagerComponent>();
-						//Game.Scene.AddComponent<HttpComponent>();
+						// Game.Scene.AddComponent<HttpComponent>();
 						break;
 					case AppType.Benchmark:
 						Game.Scene.AddComponent<NetOuterComponent>();

+ 20 - 12
Server/Hotfix/Module/Http/HttpTest.cs

@@ -1,16 +1,11 @@
 using System.Net;
 using ETModel;
+using System.Threading.Tasks;
 
 namespace ETHotfix
 {
-	public class AccountInfo
-	{
-		public string name;
-		public string pwd;
-	}
-
 	[HttpHandler(AppType.Gate, "/")]
-	public class HttpTest: AHttpHandler
+	public class HttpTest : AHttpHandler
 	{
 		[Get] // url-> /Login?name=11&age=1111
 		public string Login(string name, int age, HttpListenerRequest req, HttpListenerResponse resp)
@@ -23,6 +18,7 @@ namespace ETHotfix
 		[Get("t")] // url-> /t
 		public int Test()
 		{
+			System.Console.WriteLine("");
 			return 1;
 		}
 
@@ -38,12 +34,24 @@ namespace ETHotfix
 			return 1;
 		}
 
-		[Post]
-		public object Test3(HttpListenerResponse resp, HttpListenerRequest req, string postBody, AccountInfo accountInfo)
+		[Get] // url-> /GetRechargeRecord
+		public async Task<HttpResult> GetRechargeRecord(long id)
 		{
-			Log.Info(postBody);
-			Log.Info(JsonHelper.ToJson(accountInfo));
-			return new { name = "1111" };
+			// var db = Game.Scene.GetComponent<DBProxyComponent>();
+
+			// var info = await db.Query<RechargeRecord>(id);
+
+			await Task.Delay(1000); // 用于测试
+
+			object info = null;
+			if (info != null)
+			{
+				return Ok(data: info);
+			}
+			else
+			{
+				return Error("ID不存在!");
+			}
 		}
 	}
 }

+ 6 - 4
Server/Model/Entity/Http.cs

@@ -8,11 +8,11 @@
 
 		// 充值数量
 		public int CardNumber { get; set; }
-		
+
 		// 充值时间
 		public long Time { get; set; }
 
-		public RechargeRecord(long id): base(id)
+		public RechargeRecord(long id) : base(id)
 		{
 		}
 	}
@@ -21,10 +21,10 @@
 	public sealed class Recharge : Entity
 	{
 		public int CardNumber { get; set; }
-		
+
 		public long UpdateTime { get; set; }
 
-		public Recharge(long id): base(id)
+		public Recharge(long id) : base(id)
 		{
 		}
 	}
@@ -34,6 +34,8 @@
 		public int code;
 		public bool status;
 		public string msg = "";
+		[MongoDB.Bson.Serialization.Attributes.BsonIgnoreIfNull]
+		public object data;
 	}
 
 	public static class HttpErrorCode

+ 23 - 18
Server/Model/Module/Http/HttpComponent.cs

@@ -3,11 +3,12 @@ using System.Collections.Generic;
 using System.IO;
 using System.Net;
 using System.Reflection;
+using System.Threading.Tasks;
 
 namespace ETModel
 {
 	[ObjectSystem]
-	public class HttpComponentComponentAwakeSystem: AwakeSystem<HttpComponent>
+	public class HttpComponentComponentAwakeSystem : AwakeSystem<HttpComponent>
 	{
 		public override void Awake(HttpComponent self)
 		{
@@ -16,7 +17,7 @@ namespace ETModel
 	}
 
 	[ObjectSystem]
-	public class HttpComponentComponentLoadSystem: LoadSystem<HttpComponent>
+	public class HttpComponentComponentLoadSystem : LoadSystem<HttpComponent>
 	{
 		public override void Load(HttpComponent self)
 		{
@@ -36,7 +37,7 @@ namespace ETModel
 	/// <summary>
 	/// http请求分发器
 	/// </summary>
-	public class HttpComponent: Component
+	public class HttpComponent : Component
 	{
 		public AppType appType;
 		public HttpListener listener;
@@ -76,7 +77,7 @@ namespace ETModel
 					continue;
 				}
 
-				HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute) attrs[0];
+				HttpHandlerAttribute httpHandlerAttribute = (HttpHandlerAttribute)attrs[0];
 				if (!httpHandlerAttribute.AppType.Is(this.appType))
 				{
 					continue;
@@ -136,7 +137,7 @@ namespace ETModel
 				object[] getAttrs = method.GetCustomAttributes(typeof(GetAttribute), false);
 				if (getAttrs.Length != 0)
 				{
-					GetAttribute get = (GetAttribute) getAttrs[0];
+					GetAttribute get = (GetAttribute)getAttrs[0];
 
 					string path = method.Name;
 					if (!string.IsNullOrEmpty(get.Path))
@@ -152,7 +153,7 @@ namespace ETModel
 				if (postAttrs.Length != 0)
 				{
 					// Post处理方法
-					PostAttribute post = (PostAttribute) postAttrs[0];
+					PostAttribute post = (PostAttribute)postAttrs[0];
 
 					string path = method.Name;
 					if (!string.IsNullOrEmpty(post.Path))
@@ -183,7 +184,7 @@ namespace ETModel
 				}
 
 				HttpListenerContext context = await this.listener.GetContextAsync();
-				InvokeHandler(context);
+				await InvokeHandler(context);
 				context.Response.Close();
 			}
 		}
@@ -192,7 +193,7 @@ namespace ETModel
 		/// 调用处理方法
 		/// </summary>
 		/// <param name="context"></param>
-		private void InvokeHandler(HttpListenerContext context)
+		private async Task InvokeHandler(HttpListenerContext context)
 		{
 			context.Response.StatusCode = 404;
 
@@ -231,21 +232,25 @@ namespace ETModel
 
 				// 自动把返回值,以json方式响应。
 				object resp = methodInfo.Invoke(httpHandler, args);
-
-				if (resp == null)
+				object result = resp;
+				if (resp is Task t)
 				{
-					return;
+					await t;
+					result = t.GetType().GetProperty("Result").GetValue(t, null);
 				}
 
-				using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
+				if (result != null)
 				{
-					if (resp is string)
-					{
-						sw.Write(resp.ToString());
-					}
-					else
+					using (StreamWriter sw = new StreamWriter(context.Response.OutputStream))
 					{
-						sw.Write(JsonHelper.ToJson(resp));
+						if (result.GetType() == typeof(string))
+						{
+							sw.Write(result.ToString());
+						}
+						else
+						{
+							sw.Write(JsonHelper.ToJson(result));
+						}
 					}
 				}
 			}

+ 3 - 3
Server/Model/Module/Http/HttpHandlerAttribute.cs

@@ -2,7 +2,7 @@
 
 namespace ETModel
 {
-	public class HttpHandlerAttribute: Attribute
+	public class HttpHandlerAttribute : Attribute
 	{
 		public AppType AppType { get; }
 
@@ -16,7 +16,7 @@ namespace ETModel
 	}
 
 	[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
-	public class GetAttribute: Attribute
+	public class GetAttribute : Attribute
 	{
 		public string Path { get; }
 
@@ -31,7 +31,7 @@ namespace ETModel
 	}
 
 	[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
-	public class PostAttribute: Attribute
+	public class PostAttribute : Attribute
 	{
 		public string Path { get; }
 

+ 21 - 1
Server/Model/Module/Http/IHttpHandler.cs

@@ -7,10 +7,30 @@ namespace ETModel
 		void Handle(HttpListenerContext context);
 	}
 
-	public abstract class AHttpHandler: IHttpHandler
+	public abstract class AHttpHandler : IHttpHandler
 	{
 		public virtual void Handle(HttpListenerContext context)
 		{
 		}
+		public virtual HttpResult Ok(string msg = "", object data = null)
+		{
+			return new HttpResult
+			{
+				code = HttpErrorCode.Success,
+				msg = msg,
+				status = true,
+				data = data
+			};
+		}
+
+		public virtual HttpResult Error(string msg = "")
+		{
+			return new HttpResult
+			{
+				code = HttpErrorCode.Exception,
+				msg = msg,
+				status = false
+			};
+		}
 	}
 }