|
| 1 | +namespace Quick.Protocol { |
| 2 | + /// <summary> |
| 3 | + /// 命令信息 |
| 4 | + /// </summary> |
| 5 | + public class QpCommandInfo : Object { |
| 6 | + /// <summary> |
| 7 | + /// 名称 |
| 8 | + /// </summary> |
| 9 | + public string Name { get; set; } |
| 10 | + /// <summary> |
| 11 | + /// 描述 |
| 12 | + /// </summary> |
| 13 | + public string Description { get; set; } |
| 14 | + /// <summary> |
| 15 | + /// 命令请求类型名称 |
| 16 | + /// </summary> |
| 17 | + public string RequestTypeName { get; set; } |
| 18 | + /// <summary> |
| 19 | + /// 请求示例 |
| 20 | + /// </summary> |
| 21 | + public string RequestTypeSchemaSample { get; set; } |
| 22 | + /// <summary> |
| 23 | + /// 命令响应类型名称 |
| 24 | + /// </summary> |
| 25 | + public string ResponseTypeName { get; set; } |
| 26 | + /// <summary> |
| 27 | + /// 响应示例 |
| 28 | + /// </summary> |
| 29 | + public string ResponseTypeSchemaSample { get; set; } |
| 30 | + private IQpSerializer requestSerializer; |
| 31 | + private IQpSerializer responseSerializer; |
| 32 | + |
| 33 | + public IQpSerializer GetRequestSeriliazer() { |
| 34 | + return requestSerializer; |
| 35 | + } |
| 36 | + |
| 37 | + public IQpSerializer GetResponseSeriliazer() { |
| 38 | + return responseSerializer; |
| 39 | + } |
| 40 | + |
| 41 | + private Type requestType; |
| 42 | + private Type responseType; |
| 43 | + |
| 44 | + public QpCommandInfo() {} |
| 45 | + |
| 46 | + public QpCommandInfo.withParameters(string name, string description, |
| 47 | + Type requestType, Type responseType, |
| 48 | + Object defaultRequestTypeInstance, Object defaultResponseTypeInstance, |
| 49 | + IQpSerializer requestSerializer, IQpSerializer responseSerializer) |
| 50 | + { |
| 51 | + Name = name; |
| 52 | + Description = description; |
| 53 | + this.requestSerializer = requestSerializer; |
| 54 | + this.responseSerializer = responseSerializer; |
| 55 | + |
| 56 | + this.requestType = requestType; |
| 57 | + requestType.name(); |
| 58 | + RequestTypeName = requestType.name(); |
| 59 | + size_t serialize_gobject_length; |
| 60 | + RequestTypeSchemaSample = Json.gobject_to_data(defaultRequestTypeInstance, out serialize_gobject_length); |
| 61 | + this.responseType = responseType; |
| 62 | + ResponseTypeName = responseType.name(); |
| 63 | + ResponseTypeSchemaSample = Json.gobject_to_data(defaultResponseTypeInstance, out serialize_gobject_length); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// 获取命令请求类型 |
| 68 | + /// </summary> |
| 69 | + /// <returns></returns> |
| 70 | + public Type GetRequestType() { |
| 71 | + return requestType; |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// 获取命令响应类型 |
| 76 | + /// </summary> |
| 77 | + /// <returns></returns> |
| 78 | + public Type GetResponseType() { |
| 79 | + return responseType; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// 创建命令信息实例 |
| 84 | + /// </summary> |
| 85 | + public static QpCommandInfo Create<TRequest, TResponse> () { |
| 86 | + var requestType = typeof (TRequest); |
| 87 | + var responseType = typeof (TResponse); |
| 88 | + return CreateWithRequestAndResponse(Object.new(requestType), Object.new(responseType)); |
| 89 | + } |
| 90 | + |
| 91 | + public static QpCommandInfo CreateWithRequestAndResponse<TRequest, TResponse> (TRequest request, TResponse response) { |
| 92 | + var requestType = typeof (TRequest); |
| 93 | + var responseType = typeof (TResponse); |
| 94 | + string name = null; |
| 95 | + if (name == null) |
| 96 | + name = requestType.name(); |
| 97 | + |
| 98 | + return new QpCommandInfo.withParameters(name, "", |
| 99 | + requestType, responseType, |
| 100 | + (Object) request, (Object) response, |
| 101 | + (IQpSerializer) request, (IQpSerializer) response); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments