2018-10-06 03:14:37 +08:00
|
|
|
|
Imports System.Runtime.CompilerServices
|
|
|
|
|
|
Imports System.Text
|
2018-10-06 03:57:44 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Language.Default
|
2018-10-06 03:14:37 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Net.Protocols
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Net.Protocols.Reflection
|
2018-10-06 02:21:24 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Net.Tcp
|
2018-10-06 03:14:37 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Serialization.JSON
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Text
|
2018-10-06 02:21:24 +08:00
|
|
|
|
|
|
|
|
|
|
Namespace ApplicationServices
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' The Tcp socket server abstract
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
Public MustInherit Class ServerModule : Implements IDisposable
|
|
|
|
|
|
|
2018-10-06 02:55:09 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Tcp socket
|
|
|
|
|
|
''' </summary>
|
2018-10-06 02:21:24 +08:00
|
|
|
|
Protected socket As TcpServicesSocket
|
|
|
|
|
|
|
2018-10-06 02:55:09 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Create a new server module based on a tcp server socket.
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="port">The listen port of the tcp socket.</param>
|
|
|
|
|
|
Sub New(port As Integer)
|
2018-10-06 02:21:24 +08:00
|
|
|
|
socket = New TcpServicesSocket(port, AddressOf LogException) With {
|
|
|
|
|
|
.Responsehandler = ProtocolHandler()
|
|
|
|
|
|
}
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
|
|
Protected MustOverride Sub LogException(ex As Exception)
|
2018-10-06 02:55:09 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Generally, using a <see cref="Protocol"/> attribute using reflection way is recommended.
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <returns></returns>
|
2018-10-06 02:21:24 +08:00
|
|
|
|
Protected MustOverride Function ProtocolHandler() As ProtocolHandler
|
|
|
|
|
|
|
|
|
|
|
|
Public Overridable Function Run() As Integer
|
|
|
|
|
|
Return socket.Run
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
#Region "IDisposable Support"
|
|
|
|
|
|
Private disposedValue As Boolean ' To detect redundant calls
|
|
|
|
|
|
|
|
|
|
|
|
' IDisposable
|
|
|
|
|
|
Protected Overridable Sub Dispose(disposing As Boolean)
|
|
|
|
|
|
If Not disposedValue Then
|
|
|
|
|
|
If disposing Then
|
|
|
|
|
|
' TODO: dispose managed state (managed objects).
|
|
|
|
|
|
Call socket.Dispose()
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
|
|
|
|
|
|
' TODO: set large fields to null.
|
|
|
|
|
|
End If
|
|
|
|
|
|
disposedValue = True
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
|
|
' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
|
|
|
|
|
|
'Protected Overrides Sub Finalize()
|
|
|
|
|
|
' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
|
|
|
|
|
|
' Dispose(False)
|
|
|
|
|
|
' MyBase.Finalize()
|
|
|
|
|
|
'End Sub
|
|
|
|
|
|
|
|
|
|
|
|
' This code added by Visual Basic to correctly implement the disposable pattern.
|
|
|
|
|
|
Public Sub Dispose() Implements IDisposable.Dispose
|
|
|
|
|
|
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
|
|
|
|
|
|
Dispose(True)
|
|
|
|
|
|
' TODO: uncomment the following line if Finalize() is overridden above.
|
|
|
|
|
|
' GC.SuppressFinalize(Me)
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
End Class
|
2018-10-06 03:14:37 +08:00
|
|
|
|
|
|
|
|
|
|
Public Class ProtocolInvoker(Of T As {IComparable, IFormattable, IConvertible})
|
|
|
|
|
|
|
|
|
|
|
|
Public ReadOnly Property Protocol As New Protocol(GetType(T))
|
|
|
|
|
|
Public ReadOnly Property TcpRequest As TcpRequest
|
2018-10-06 03:57:44 +08:00
|
|
|
|
Public ReadOnly Property TextEncoding As DefaultValue(Of Encoding)
|
2018-10-06 03:14:37 +08:00
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2018-10-06 03:57:44 +08:00
|
|
|
|
Sub New(hostName$, remotePort%, Optional encoding As Encodings = Encodings.UTF8WithoutBOM)
|
2018-10-06 03:14:37 +08:00
|
|
|
|
TcpRequest = New TcpRequest(hostName, remotePort)
|
2018-10-06 03:57:44 +08:00
|
|
|
|
TextEncoding = encoding.CodePage
|
2018-10-06 03:14:37 +08:00
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2018-10-06 03:57:44 +08:00
|
|
|
|
Public Function SendMessage(protocol As T, message$, Optional textEncoding As Encoding = Nothing) As RequestStream
|
|
|
|
|
|
Return SendMessage(protocol, (textEncoding Or Me.TextEncoding).GetBytes(message))
|
2018-10-06 03:14:37 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
Public Function SendMessage(protocol As T, data As Byte()) As RequestStream
|
|
|
|
|
|
Dim category& = Me.Protocol.EntryPoint
|
|
|
|
|
|
Dim protocolL& = CLng(CObj(protocol))
|
|
|
|
|
|
Dim message As New RequestStream(category, protocolL, data)
|
|
|
|
|
|
|
|
|
|
|
|
Return TcpRequest.SendMessage(message)
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2018-10-06 03:57:44 +08:00
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2018-10-06 03:14:37 +08:00
|
|
|
|
Public Function SendMessage(Of V As {New, Class})(protocol As T, data As V) As RequestStream
|
|
|
|
|
|
Return SendMessage(protocol, data.GetJson)
|
|
|
|
|
|
End Function
|
|
|
|
|
|
End Class
|
2018-10-06 02:21:24 +08:00
|
|
|
|
End Namespace
|