1 #Region "Microsoft.VisualBasic::6b4ce6d00a21f82f2b63a7ceb8870138, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\MMFProtocol\MapStream\MSWriter.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 MSWriter
35     
36     '         Constructor: (+1 OverloadsSub New
37     
38     '         FunctionToString
39     
40     '         Sub: WriteStream
41     
42     '     Class MMFStream
43     
44     '         Properties: byteData, udtBadge
45     
46     '         Constructor: (+2 OverloadsSub New
47     '         Function: Serialize, ToString
48     
49     
50     ' /********************************************************************************/
51
52 #End Region
53
54 Imports System.IO
55 Imports Microsoft.VisualBasic.Net.Protocols
56
57 Namespace Parallel.MMFProtocol.MapStream
58
59 #Region "Nested Type Definitions"
60
61     ''' <summary>
62     ''' 
63     ''' </summary>
64     ''' <remarks>
65     ''' mmfServer的主要功能是创建并维护一个内存映射文件
66     ''' </remarks>
67     Public Class MSWriter : Inherits IMapBase
68
69         ''' <summary>
70         ''' 
71         ''' </summary>
72         ''' <param name="uri"></param>
73         ''' <remarks>对象实例会首先尝试以服务器的角色建立连接,当不成功的时候会以客户端的形式建立连接</remarks>
74         Sub New(uri As String, chunkSize As Long)
75             Call MyBase.New(uri, chunkSize)
76
77             Try
78                 _mmfileStream =
79                     MemoryMappedFiles.MemoryMappedFile.CreateNew(
80                     uri, chunkSize)
81             Catch ex As Exception
82                 _mmfileStream =
83                     MemoryMappedFiles.MemoryMappedFile.CreateOrOpen(
84                     uri, chunkSize)
85             Finally
86             End Try
87         End Sub
88
89         Public Sub WriteStream(byteData As Byte())
90             If Not byteData.IsNullOrEmpty Then
91                 Call _mmfileStream.CreateViewStream.Write(
92                     byteData, Scan0, byteData.Length)
93             End If
94         End Sub
95
96         Public Overrides Function ToString() As String
97             Return Uri
98         End Function
99     End Class
100
101     <Serializable> Public Class MMFStream : Inherits RawStream
102
103         ''' <summary>
104         ''' Stamp; Update Badge
105         ''' </summary>
106         Public Property udtBadge As Long
107
108         ''' <summary>
109         ''' 内存映射文件中所存储的将要进行进程间交换的数据
110         ''' </summary>
111         ''' <remarks></remarks>
112         Public Property byteData As Byte()
113
114         Sub New()
115         End Sub
116
117         Sub New(rawStream As Byte())
118             Dim bytTmp As Byte() = New Byte(INT64 - 1) {}
119             Call Array.ConstrainedCopy(rawStream, Scan0, bytTmp, Scan0, bytTmp.Length)
120             udtBadge = BitConverter.ToInt64(bytTmp, Scan0)
121             Dim bytLen As Integer = rawStream.Length - bytTmp.Length
122             bytTmp = New Byte(bytLen - 1) {}
123             Call Array.ConstrainedCopy(rawStream, INT64, bytTmp, Scan0, bytLen)
124             byteData = bytTmp
125         End Sub
126
127         Public Overrides Function ToString() As String
128             Return $"{NameOf(udtBadge)}:={udtBadge} // {NameOf(byteData)}:={byteData.Length} bytes...."
129         End Function
130
131         Public Overrides Function Serialize() As Byte()
132             Dim ChunkBuffer As Byte() = New Byte(INT64 + byteData.Length - 1) {}
133             Call Array.ConstrainedCopy(BitConverter.GetBytes(udtBadge), Scan0, ChunkBuffer, Scan0, INT64)
134             Call Array.ConstrainedCopy(byteData, Scan0, ChunkBuffer, INT64, byteData.Length)
135
136             Return ChunkBuffer
137         End Function
138     End Class
139 #End Region
140 End Namespace