1 |
#Region "Microsoft.VisualBasic::e9fc2b90e6ed847e0a43175fe081b3d1, Microsoft.VisualBasic.Core\Net\Protocol\Reflection\ProtocolHandler.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
#End Region
|
46 |
|
47 |
Imports System.Reflection
|
48 |
Imports System.Runtime.CompilerServices
|
49 |
Imports Microsoft.VisualBasic.Net.Abstract
|
50 |
Imports Microsoft.VisualBasic.Net.Http
|
51 |
Imports Microsoft.VisualBasic.Net.Tcp
|
52 |
Imports TcpEndPoint = System.Net.IPEndPoint
|
53 |
|
54 |
Namespace Net.Protocols.Reflection
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
Public Class ProtocolHandler : Inherits IProtocolHandler
|
64 |
|
65 |
Protected Protocols As Dictionary(Of Long, DataRequestHandler)
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
Public ReadOnly Property DeclaringType As Type
|
71 |
Public Overrides ReadOnly Property ProtocolEntry As Long
|
72 |
|
73 |
Public Overrides Function ToString() As String
|
74 |
Return $"*{ProtocolEntry} ---> {DeclaringType.FullName} //{Protocols.Count} Protocols."
|
75 |
End Function
|
76 |
|
77 |
Const AllInstanceMethod As BindingFlags =
|
78 |
BindingFlags.Public Or
|
79 |
BindingFlags.NonPublic Or
|
80 |
BindingFlags.Instance
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
Sub New(obj As Object)
|
88 |
Dim type As Type = obj.GetType
|
89 |
Dim entry As Protocol = Protocol.GetProtocolCategory(type)
|
90 |
|
91 |
Me.DeclaringType = entry?.DeclaringType
|
92 |
Me.ProtocolEntry = entry?.EntryPoint
|
93 |
|
94 |
|
95 |
Dim Methods = type.GetMethods(bindingAttr:=AllInstanceMethod)
|
96 |
Dim LQuery = (From entryPoint As MethodInfo
|
97 |
In Methods
|
98 |
Let Protocol As Protocol = Protocol.GetEntryPoint(entryPoint)
|
99 |
Let method As DataRequestHandler = GetMethod(obj, entryPoint)
|
100 |
Where Not (Protocol Is Nothing) AndAlso
|
101 |
Not method Is Nothing
|
102 |
Select Protocol, entryPoint, method)
|
103 |
|
104 |
Me.Protocols = LQuery.ToDictionary(Function(element)
|
105 |
Return element.Protocol.EntryPoint
|
106 |
End Function,
|
107 |
Function(element) element.method)
|
108 |
End Sub
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
Public Shared Function SafelyCreateObject(Of T As Class)(App As T) As ProtocolHandler
|
117 |
Try
|
118 |
Return New ProtocolHandler(App)
|
119 |
Catch ex As Exception
|
120 |
Return Nothing
|
121 |
End Try
|
122 |
End Function
|
123 |
|
124 |
Public Shared Function SafelyCreateObject(App As Object) As ProtocolHandler
|
125 |
Try
|
126 |
Return New ProtocolHandler(App)
|
127 |
Catch ex As Exception
|
128 |
Return Nothing
|
129 |
End Try
|
130 |
End Function
|
131 |
|
132 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
133 |
Public Function HandlePush(uid As Long, request As RequestStream) As RequestStream
|
134 |
Return HandleRequest(request, Nothing)
|
135 |
End Function
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
Public Overrides Function HandleRequest(request As RequestStream, remoteDevcie As TcpEndPoint) As RequestStream
|
144 |
If request.ProtocolCategory <> Me.ProtocolEntry Then
|
145 |
#If DEBUG Then
|
146 |
Call $"Protocol_entry:={request.ProtocolCategory} was not found!".__DEBUG_ECHO
|
147 |
#End If
|
148 |
Return NetResponse.RFC_VERSION_NOT_SUPPORTED
|
149 |
End If
|
150 |
|
151 |
If Not Me.Protocols.ContainsKey(request.Protocol) Then
|
152 |
#If DEBUG Then
|
153 |
Call $"Protocol:={request.Protocol} was not found!".__DEBUG_ECHO
|
154 |
#End If
|
155 |
|
156 |
Return NetResponse.RFC_NOT_IMPLEMENTED
|
157 |
End If
|
158 |
|
159 |
Dim EntryPoint As DataRequestHandler = Me.Protocols(request.Protocol)
|
160 |
Dim value As RequestStream = EntryPoint(request, remoteDevcie)
|
161 |
Return value
|
162 |
End Function
|
163 |
|
164 |
Private Shared Function GetMethod(obj As Object, entryPoint As MethodInfo) As DataRequestHandler
|
165 |
Dim parameters As ParameterInfo() = entryPoint.GetParameters
|
166 |
|
167 |
If Not entryPoint.ReturnType.Equals(GetType(RequestStream)) Then
|
168 |
Return Nothing
|
169 |
ElseIf parameters.Length > 2 Then
|
170 |
Return Nothing
|
171 |
End If
|
172 |
|
173 |
If parameters.Length = 0 Then
|
174 |
Return AddressOf New ProtocolInvoker(obj, entryPoint).InvokeProtocol0
|
175 |
ElseIf parameters.Length = 1 Then
|
176 |
Return method1(obj, entryPoint, parameters)
|
177 |
ElseIf parameters.Length = 2 Then
|
178 |
Return method2(obj, entryPoint, parameters)
|
179 |
End If
|
180 |
|
181 |
Return Nothing
|
182 |
End Function
|
183 |
|
184 |
Private Shared Function method2(obj As Object, entryPoint As MethodInfo, parameters As ParameterInfo()) As DataRequestHandler
|
185 |
If (Not parameters.First.ParameterType.Equals(GetType(RequestStream)) OrElse
|
186 |
Not parameters.Last.ParameterType.Equals(GetType(TcpEndPoint))) Then
|
187 |
Return Nothing
|
188 |
Else
|
189 |
Return AddressOf New ProtocolInvoker(obj, entryPoint).InvokeProtocol2
|
190 |
End If
|
191 |
End Function
|
192 |
|
193 |
Private Shared Function method1(obj As Object, entryPoint As MethodInfo, parameters As ParameterInfo()) As DataRequestHandler
|
194 |
If Not parameters.First.ParameterType.Equals(GetType(RequestStream)) Then
|
195 |
Return Nothing
|
196 |
Else
|
197 |
Return AddressOf New ProtocolInvoker(obj, entryPoint).InvokeProtocol1
|
198 |
End If
|
199 |
End Function
|
200 |
|
201 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
202 |
Public Shared Narrowing Operator CType(handler As ProtocolHandler) As DataRequestHandler
|
203 |
Return AddressOf handler.HandleRequest
|
204 |
End Operator
|
205 |
End Class
|
206 |
End Namespace
|