1 #Region "Microsoft.VisualBasic::f904cc12c9573031dc19c48f2dbceff8, Microsoft.VisualBasic.Core\Net\Tcp\StateObject.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 StateObject
35     
36     '         FunctionGetRequest, ToString
37     
38     '         Sub: (+2 Overloads) Dispose
39     
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.Net.Sockets
46 Imports Microsoft.VisualBasic.Net.Protocols
47
48 Namespace Net.Tcp
49
50     ''' <summary>
51     ''' State object for reading client data asynchronously
52     ''' </summary>
53     ''' <remarks></remarks>
54     Public Class StateObject : Implements IDisposable
55
56         ''' <summary>
57         ''' Client  socket.
58         ''' </summary>
59         ''' <remarks></remarks>
60         Public workSocket As Socket
61         ''' <summary>
62         ''' Size of receive buffer.
63         ''' </summary>
64         ''' <remarks></remarks>
65         Public Const BufferSize As Integer = 1024 * 1024
66         ''' <summary>
67         ''' Receive buffer.
68         ''' </summary>
69         ''' <remarks></remarks>
70         Public readBuffer(BufferSize) As Byte
71         ''' <summary>
72         ''' Received data.
73         ''' </summary>
74         ''' <remarks></remarks>
75         Public ChunkBuffer As New List(Of Byte)
76
77         Public Overrides Function ToString() As String
78             Return workSocket.RemoteEndPoint.ToString & " <=====> " & workSocket.LocalEndPoint.ToString
79         End Function
80
81         Public Function GetRequest() As RequestStream
82             Return New RequestStream(ChunkBuffer.ToArray)
83         End Function
84
85 #Region "IDisposable Support"
86         Protected disposedValue As Boolean To detect redundant calls
87
88         ' IDisposable
89         Protected Overridable Sub Dispose(disposing As Boolean)
90             If Not disposedValue Then
91                 If disposing Then
92
93                     On Error Resume Next
94
95                     Dim ep As String = workSocket.RemoteEndPoint.ToString
96
97                     ' TODO: dispose managed state (managed objects).
98                     Call ChunkBuffer.Clear()
99                     Call ChunkBuffer.Free
100                     Call readBuffer.Free
101                     Call workSocket.Shutdown(SocketShutdown.Both)
102                     Call workSocket.Free
103
104                     Call Console.WriteLine($"[DEBUG {Now.ToString}] Socket {ep} clean up!")
105                 End If
106
107                 ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
108                 ' TODO: set large fields to null.
109             End If
110             disposedValue = True
111         End Sub
112
113         ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
114         'Protected Overrides Sub Finalize()
115         '    ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
116         '    Dispose(False)
117         '    MyBase.Finalize()
118         'End Sub
119
120         ' This code added by Visual Basic to correctly implement the disposable pattern.
121         Public Sub Dispose() Implements IDisposable.Dispose
122             Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
123             Dispose(True)
124             ' TODO: uncomment the following line if Finalize() is overridden above.
125             ' GC.SuppressFinalize(Me)
126         End Sub
127 #End Region
128     End Class
129 End Namespace