1 | #Region "Microsoft.VisualBasic::6f35238d4e079cd2d617b33b2fcecd44, Microsoft.VisualBasic.Core\Net\Protocol\Abstract.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 | ' Interface ISerializable |
35 | ' |
36 | ' Function: Serialize |
37 | ' |
38 | ' Delegate Sub |
39 | ' |
40 | ' |
41 | ' Class RawStream |
42 | ' |
43 | ' Constructor: (+2 Overloads) Sub New |
44 | ' Function: GetRawStream |
45 | ' Operators: <=, >= |
46 | ' |
47 | ' |
48 | ' |
49 | ' /********************************************************************************/ |
50 | |
51 | #End Region |
52 | |
53 | Imports System.Runtime.CompilerServices |
54 | Imports Microsoft.VisualBasic.Net.Tcp |
55 | |
56 | Namespace Net.Protocols |
57 | |
58 | ''' <summary> |
59 | ''' 支持序列化的对象,则这个对象可以被应用于<see cref="RequestStream"/>数据载体的网络传输操作过程 |
60 | ''' </summary> |
61 | Public Interface ISerializable |
62 | ''' <summary> |
63 | ''' Transform this .NET object into a raw stream object for the network data transfer. |
64 | ''' </summary> |
65 | ''' <returns></returns> |
66 | Function Serialize() As Byte() |
67 | End Interface |
68 | |
69 | Public Delegate Sub ProcessMessagePush(message As RequestStream) |
70 | |
71 | ''' <summary> |
72 | ''' 原始串流的基本模型,这个流对象应该具备有两个基本的方法: |
73 | ''' 1. 从原始的字节流之中反序列化构造出自身的构造函数 |
74 | ''' 2. 将自身序列化为字节流的<see cref="ISerializable.Serialize()"/>序列化方法 |
75 | ''' </summary> |
76 | <Serializable> Public MustInherit Class RawStream : Implements ISerializable |
77 | |
78 | ''' <summary> |
79 | ''' You should overrides this constructor to generate a stream object.(必须要有一个这个构造函数来执行反序列化) |
80 | ''' </summary> |
81 | ''' <param name="rawStream"></param> |
82 | Sub New(rawStream As Byte()) |
83 | |
84 | End Sub |
85 | |
86 | Public Sub New() |
87 | End Sub |
88 | |
89 | ''' <summary> |
90 | ''' <see cref="ISerializable.Serialize"/>序列化方法 |
91 | ''' </summary> |
92 | ''' <returns></returns> |
93 | Public MustOverride Function Serialize() As Byte() Implements ISerializable.Serialize |
94 | |
95 | ''' <summary> |
96 | ''' 按照类型的定义进行反序列化操作 |
97 | ''' </summary> |
98 | ''' <typeparam name="TRawStream"></typeparam> |
99 | ''' <param name="rawStream"></param> |
100 | ''' <returns></returns> |
101 | ''' |
102 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
103 | Public Shared Function GetRawStream(Of TRawStream As RawStream)(rawStream As Byte()) As TRawStream |
104 | Return Activator.CreateInstance(GetType(TRawStream), {rawStream}) |
105 | End Function |
106 | |
107 | Protected Shared ReadOnly _rawStreamType As Type = GetType(Byte()) |
108 | |
109 | Public Const INT64 As Integer = 8 |
110 | ''' <summary> |
111 | ''' Single/Integer |
112 | ''' </summary> |
113 | Public Const INT32 As Integer = 4 |
114 | ''' <summary> |
115 | ''' System.Double |
116 | ''' </summary> |
117 | Public Const DblFloat As Integer = 8 |
118 | Public Const ShortInt As Integer = 2 |
119 | Public Const SingleFloat As Integer = 4 |
120 | Public Const DecimalInt As Integer = 12 |
121 | |
122 | ''' <summary> |
123 | ''' |
124 | ''' </summary> |
125 | ''' <param name="addr">IPEndPoint string value likes 127.0.0.1:8080</param> |
126 | ''' <param name="raw"></param> |
127 | ''' <returns></returns> |
128 | ''' <![CDATA[ |
129 | ''' |
130 | ''' Dim rep As RequestStream = "127.0.0.1:80" <= New RequestStream With { |
131 | ''' ... |
132 | ''' } |
133 | ''' ]]> |
134 | Public Shared Operator <=(addr As String, raw As RawStream) As RequestStream |
135 | Dim ep As New IPEndPoint(addr) |
136 | Dim invoke As New TcpRequest(ep) |
137 | Dim rep As New RequestStream(invoke.SendMessage(raw.Serialize)) |
138 | Return rep |
139 | End Operator |
140 | |
141 | Public Shared Operator >=(addr As String, raw As RawStream) As RequestStream |
142 | Throw New NotSupportedException |
143 | End Operator |
144 | End Class |
145 | End Namespace |