1 | #Region "Microsoft.VisualBasic::bcfdaad7954d579a41f1d3687a4428d0, Microsoft.VisualBasic.Core\Net\Protocol\Reflection\Protocol.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class Protocol |
35 | ' |
36 | ' Properties: DeclaringType, EntryPoint |
37 | ' |
38 | ' Constructor: (+2 Overloads) Sub New |
39 | ' Function: GetEntryPoint, GetProtocolCategory, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Namespace Net.Protocols.Reflection |
47 | |
48 | ''' <summary> |
49 | ''' This attribute indicates the entry point of the protocol processor definition location |
50 | ''' and the details of the protocol processor. |
51 | ''' </summary> |
52 | <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method, AllowMultiple:=True, Inherited:=True)> |
53 | Public Class Protocol : Inherits Attribute |
54 | |
55 | ''' <summary> |
56 | ''' Entry point for the data protocols, this property usually correspondent to the request stream's |
57 | ''' property: <see cref="RequestStream.Protocol"/> and <see cref="RequestStream.ProtocolCategory"/> |
58 | ''' </summary> |
59 | ''' <returns></returns> |
60 | Public ReadOnly Property EntryPoint As Long |
61 | ''' <summary> |
62 | ''' 这个属性对于方法而言为空,但是对于类型入口点而言则不为空 |
63 | ''' </summary> |
64 | ''' <returns></returns> |
65 | Public ReadOnly Property DeclaringType As Type |
66 | |
67 | ''' <summary> |
68 | ''' Generates the protocol method entrypoint.(应用于服务器上面的协议处理方法) |
69 | ''' </summary> |
70 | ''' <param name="entryPoint"></param> |
71 | Sub New(entryPoint As Long) |
72 | Me.EntryPoint = entryPoint |
73 | End Sub |
74 | |
75 | ''' <summary> |
76 | ''' Generates the <see cref="ProtocolHandler"/> on the server side, |
77 | ''' this is using for initialize a protocol API entry point. |
78 | ''' (客户端上面的类型) |
79 | ''' </summary> |
80 | ''' <param name="type">客户端上面的类型</param> |
81 | Sub New(type As Type) |
82 | EntryPoint = SecurityString.MD5Hash.ToLong(type.GUID.ToByteArray) |
83 | DeclaringType = type |
84 | End Sub |
85 | |
86 | Public Overrides Function ToString() As String |
87 | If DeclaringType Is Nothing Then |
88 | Return EntryPoint |
89 | Else |
90 | Return $"*{EntryPoint} ==> {DeclaringType.FullName}" |
91 | End If |
92 | End Function |
93 | |
94 | ''' <summary> |
95 | ''' This method is usually using for generates a <see cref="protocolhandler"/> object. |
96 | ''' Correspondent to the protocol class property <see cref="RequestStream.ProtocolCategory"/> |
97 | ''' </summary> |
98 | ''' <param name="Type"></param> |
99 | ''' <returns></returns> |
100 | Public Shared Function GetProtocolCategory(Type As Type) As Protocol |
101 | Dim attrs As Object() = Type.GetCustomAttributes(attributeType:=GetType(Protocol), inherit:=True) |
102 | If attrs.IsNullOrEmpty Then |
103 | Return Nothing |
104 | End If |
105 | |
106 | Dim attr As Protocol = DirectCast(attrs(Scan0), Protocol) |
107 | Return attr |
108 | End Function |
109 | |
110 | ''' <summary> |
111 | ''' This method is usually using for generates a details protocol processor, example |
112 | ''' is calling the method interface: <see cref="Net.Abstract.DataRequestHandler"/> |
113 | ''' Correspondent to the protocol entry property <see cref="RequestStream.Protocol"/> |
114 | ''' </summary> |
115 | ''' <param name="Method"></param> |
116 | ''' <returns></returns> |
117 | Public Shared Function GetEntryPoint(Method As System.Reflection.MethodInfo) As Protocol |
118 | Dim attrs As Object() = Method.GetCustomAttributes(attributeType:=GetType(Protocol), inherit:=True) |
119 | If attrs.IsNullOrEmpty Then |
120 | Return Nothing |
121 | End If |
122 | |
123 | Dim attr As Protocol = DirectCast(attrs(Scan0), Protocol) |
124 | Return attr |
125 | End Function |
126 | End Class |
127 | End Namespace |