| 1 | #Region "Microsoft.VisualBasic::b3da10841d6f9a62ea415710a98e602d, Microsoft.VisualBasic.Core\ApplicationServices\ServerModule.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 ServerModule |
| 35 | ' |
| 36 | ' Constructor: (+1 Overloads) Sub New |
| 37 | ' |
| 38 | ' Function: Run |
| 39 | ' |
| 40 | ' Sub: (+2 Overloads) Dispose |
| 41 | ' |
| 42 | ' Class ProtocolInvoker |
| 43 | ' |
| 44 | ' Properties: Protocol, TcpRequest, TextEncoding |
| 45 | ' |
| 46 | ' Constructor: (+1 Overloads) Sub New |
| 47 | ' Function: (+3 Overloads) SendMessage |
| 48 | ' |
| 49 | ' |
| 50 | ' /********************************************************************************/ |
| 51 | |
| 52 | #End Region |
| 53 | |
| 54 | Imports System.Runtime.CompilerServices |
| 55 | Imports System.Text |
| 56 | Imports Microsoft.VisualBasic.Language.Default |
| 57 | Imports Microsoft.VisualBasic.Net.Protocols |
| 58 | Imports Microsoft.VisualBasic.Net.Protocols.Reflection |
| 59 | Imports Microsoft.VisualBasic.Net.Tcp |
| 60 | Imports Microsoft.VisualBasic.Serialization.JSON |
| 61 | Imports Microsoft.VisualBasic.Text |
| 62 | |
| 63 | Namespace ApplicationServices |
| 64 | |
| 65 | ''' <summary> |
| 66 | ''' The Tcp socket server abstract |
| 67 | ''' </summary> |
| 68 | Public MustInherit Class ServerModule : Implements IDisposable |
| 69 | |
| 70 | ''' <summary> |
| 71 | ''' Tcp socket |
| 72 | ''' </summary> |
| 73 | Protected socket As TcpServicesSocket |
| 74 | |
| 75 | ''' <summary> |
| 76 | ''' Create a new server module based on a tcp server socket. |
| 77 | ''' </summary> |
| 78 | ''' <param name="port">The listen port of the tcp socket.</param> |
| 79 | Sub New(port As Integer) |
| 80 | socket = New TcpServicesSocket(port, AddressOf LogException) With { |
| 81 | .Responsehandler = ProtocolHandler() |
| 82 | } |
| 83 | End Sub |
| 84 | |
| 85 | Protected MustOverride Sub LogException(ex As Exception) |
| 86 | ''' <summary> |
| 87 | ''' Generally, using a <see cref="Protocol"/> attribute using reflection way is recommended. |
| 88 | ''' </summary> |
| 89 | ''' <returns></returns> |
| 90 | Protected MustOverride Function ProtocolHandler() As ProtocolHandler |
| 91 | |
| 92 | Public Overridable Function Run() As Integer |
| 93 | Return socket.Run |
| 94 | End Function |
| 95 | |
| 96 | #Region "IDisposable Support" |
| 97 | Private disposedValue As Boolean ' To detect redundant calls |
| 98 | |
| 99 | ' IDisposable |
| 100 | Protected Overridable Sub Dispose(disposing As Boolean) |
| 101 | If Not disposedValue Then |
| 102 | If disposing Then |
| 103 | ' TODO: dispose managed state (managed objects). |
| 104 | Call socket.Dispose() |
| 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 | |
| 130 | Public Class ProtocolInvoker(Of T As {IComparable, IFormattable, IConvertible}) |
| 131 | |
| 132 | Public ReadOnly Property Protocol As New Protocol(GetType(T)) |
| 133 | Public ReadOnly Property TcpRequest As TcpRequest |
| 134 | Public ReadOnly Property TextEncoding As DefaultValue(Of Encoding) |
| 135 | |
| 136 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 137 | Sub New(hostName$, remotePort%, Optional encoding As Encodings = Encodings.UTF8WithoutBOM) |
| 138 | TcpRequest = New TcpRequest(hostName, remotePort) |
| 139 | TextEncoding = encoding.CodePage |
| 140 | End Sub |
| 141 | |
| 142 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 143 | Public Function SendMessage(protocol As T, message$, Optional textEncoding As Encoding = Nothing) As RequestStream |
| 144 | Return SendMessage(protocol, (textEncoding Or Me.TextEncoding).GetBytes(message)) |
| 145 | End Function |
| 146 | |
| 147 | Public Function SendMessage(protocol As T, data As Byte()) As RequestStream |
| 148 | Dim category& = Me.Protocol.EntryPoint |
| 149 | Dim protocolL& = CLng(CObj(protocol)) |
| 150 | Dim message As New RequestStream(category, protocolL, data) |
| 151 | |
| 152 | Return TcpRequest.SendMessage(message) |
| 153 | End Function |
| 154 | |
| 155 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 156 | Public Function SendMessage(Of V As {New, Class})(protocol As T, data As V) As RequestStream |
| 157 | Return SendMessage(protocol, data.GetJson) |
| 158 | End Function |
| 159 | End Class |
| 160 | End Namespace |