Author:水如烟
写得还是比较粗陋,放在这是为查阅方便。项目简化了Remoting的服务配置,简化了事件传递编写。
若你参考了本项目,使用中发现有改进之处,烦请在留言上指明。谢谢。
非常的对不起,还没写注释(因为自己不记概念名称,写起注释要时间,以后加吧,或许)
项目名称:LzmTW.uRemoting 文件名称:LzmTW.uRemoting.Dll
文件:
ChannelType.vb,ServiceInformation.vb,ServiceInformationCollectionBase.vb
IMessage.vb,IService.vb,MessageBase.vb,ServiceBase.vb
ServicesFactory.vb,ClientFactory.vb,ServicesListener.vb
Serialize.vb
以下是上面各文件代码:
ChannelType.vb
ServiceInformation.vb
ServiceInformationCollectionBase.vb
IMessage.vb
IService.vb
MessageBase.vb
Public MustInherit Class MessageBase
Inherits MarshalByRefObject
Implements IMessage
Private _ID As String
Private _Name As String
Private _Body As Object
Public Property Body() As Object Implements IMessage.Body
Get
Return _Body
End Get
Set(ByVal Value As Object)
_Body = Value
End Set
End Property
Public ReadOnly Property Machine() As String Implements IMessage.Machine
Get
Return System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(0).ToString
End Get
End Property
Public Property Name() As String Implements IMessage.Name
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property ID() As String Implements IMessage.ID
Get
Return _ID
End Get
Set(ByVal Value As String)
_ID = Value
End Set
End Property
End Class
ServiceBase.vb
ServicesFactory.vb
ClientFactory.vb
ServicesListener.vb
Public Class ServicesListener
Inherits MarshalByRefObject
Public Event SendServiceMessageToClient(ByVal sender As Object, ByVal arg As IMessage)
Private _Machine As String
Public ReadOnly Property Machine() As String
Get
Return System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(0).ToString
End Get
End Property
Public Sub SendServiceMessageToClientArrived(ByVal sender As Object, ByVal arg As IMessage)
If Not SendServiceMessageToClientEvent Is Nothing Then
For Each del As [Delegate] In SendServiceMessageToClientEvent.GetInvocationList
Try
If TypeOf del.Target Is ClientFactory Then
If Not CType(del.Target, ClientFactory).ID.Equals(arg.ID) Then
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Else
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Catch ex As Exception
SendServiceMessageToClientEvent = [Delegate].Remove(SendServiceMessageToClientEvent, del)
End Try
Next
End If
End Sub
Public Sub AddListener(ByVal service As IService)
AddHandler service.SendMessage, AddressOf Me.SendServiceMessageToClientArrived
End Sub
Public Sub RemoveListenner(ByVal service As IService)
RemoveHandler service.SendMessage, AddressOf Me.SendServiceMessageToClientArrived
End Sub
End Class
Serialize.vb
Public Class Serialize
Private Shared Function GetXML(ByVal obj As Object) As String
Dim mSerializer As New System.Xml.Serialization.XmlSerializer(obj.GetType)
Dim mStringWriter As New System.IO.StringWriter
mSerializer.Serialize(mStringWriter, obj)
Return mStringWriter.ToString
End Function
Private Shared Function GetObj(ByVal objtype As Type, ByVal xml As String) As Object
Dim mSerializer As New System.Xml.Serialization.XmlSerializer(objtype)
Dim mStringReader As New System.IO.StringReader(xml)
Return mSerializer.Deserialize(mStringReader)
End Function
Public Shared Sub SaveXmlFile(ByVal filename As String, ByVal obj As Object)
Dim XmlWriter As New System.IO.StreamWriter(filename, False)
XmlWriter.Write(GetXML(obj))
XmlWriter.Close()
End Sub
Public Shared Function LoadXmlFile(ByVal filename As String, ByVal objtype As Type) As Object
Dim XmlReader As New System.IO.StreamReader(filename, System.Text.Encoding.Default)
Dim mObj As Object
mObj = GetObj(objtype, XmlReader.ReadToEnd)
XmlReader.Close()
Return mObj
End Function
Public Shared Sub SaveSerializerFile(ByVal filename As String, ByVal formatter As System.Runtime.Serialization.IFormatter, ByVal obj As Object)
Dim mFileStream As System.IO.Stream = System.IO.File.Open(filename, System.IO.FileMode.Create)
formatter.Serialize(mFileStream, obj)
mFileStream.Close()
End Sub
Public Shared Function LoadDeSerializeFile(ByVal FileName As String, ByVal formatter As System.Runtime.Serialization.IFormatter) As Object
Dim mFileStream As System.IO.Stream = System.IO.File.Open(FileName, System.IO.FileMode.Open)
Dim mObj As Object
mObj = formatter.Deserialize(mFileStream)
mFileStream.Close()
Return mObj
End Function
Public Shared Function Clone(ByVal obj As Object) As Object
Dim mFormatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mMemoryStream As New System.IO.MemoryStream
mFormatter.Serialize(mMemoryStream, obj)
mMemoryStream.Position = 0
Return mFormatter.Deserialize(mMemoryStream)
End Function
End Class
写得还是比较粗陋,放在这是为查阅方便。项目简化了Remoting的服务配置,简化了事件传递编写。
若你参考了本项目,使用中发现有改进之处,烦请在留言上指明。谢谢。
非常的对不起,还没写注释(因为自己不记概念名称,写起注释要时间,以后加吧,或许)
项目名称:LzmTW.uRemoting 文件名称:LzmTW.uRemoting.Dll
文件:
ChannelType.vb,ServiceInformation.vb,ServiceInformationCollectionBase.vb
IMessage.vb,IService.vb,MessageBase.vb,ServiceBase.vb
ServicesFactory.vb,ClientFactory.vb,ServicesListener.vb
Serialize.vb
以下是上面各文件代码:
ChannelType.vb
Public Enum ChannelType
HttpChannel
'HttpClientChannel
'HttpServerChannel
TcpChannel
'TcpClientChannel
'TcpServerChannel
End Enum
HttpChannel
'HttpClientChannel
'HttpServerChannel
TcpChannel
'TcpClientChannel
'TcpServerChannel
End Enum
ServiceInformation.vb
<Serializable()> _
Public Class ServiceInformation
Inherits MarshalByRefObject
Private _CludeMachines(-1) As String
Private _NotCludeMachines(-1) As String
Private _Channel As New ChannelInfo
Private _Service As New ServiceInfo
Public Property Channel() As ChannelInfo
Get
Return _Channel
End Get
Set(ByVal Value As ChannelInfo)
_Channel = Value
End Set
End Property
Public Property Service() As ServiceInfo
Get
Return _Service
End Get
Set(ByVal Value As ServiceInfo)
_Service = Value
End Set
End Property
Public Property CludeMachines() As String()
Get
Return _CludeMachines
End Get
Set(ByVal Value As String())
_CludeMachines = Value
End Set
End Property
Public Property NotCludeMachines() As String()
Get
Return _NotCludeMachines
End Get
Set(ByVal Value As String())
_NotCludeMachines = Value
End Set
End Property
Public ReadOnly Property ClientServerUri() As String
Get
Dim tmp As New ArrayList
tmp.Add(_Channel.ChnlType.ToString.Substring(0, _Channel.ChnlType.ToString.IndexOf("Channel")))
tmp.Add(_Channel.ServerMachine)
tmp.Add(_Channel.Port)
tmp.Add(_Service.ServerUri)
Return String.Format("{0}://{1}:{2}/{3}", tmp.ToArray)
End Get
End Property
Public Function Clone() As ServiceInformation
Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim MS As New System.IO.MemoryStream
BF.Serialize(MS, Me)
MS.Position = 0
Return (CType(BF.Deserialize(MS), ServiceInformation))
End Function
<Serializable()> _
Public Class ChannelInfo
Private _PortName As String
Private _Port As String
Private _FilterLevel As System.Runtime.Serialization.Formatters.TypeFilterLevel
Private _ChnlType As ChannelType
Private _ServerMachine As String
Public Property ChnlType() As ChannelType
Get
Return _ChnlType
End Get
Set(ByVal Value As ChannelType)
_ChnlType = Value
End Set
End Property
Public Property Port() As String
Get
Return _Port
End Get
Set(ByVal Value As String)
_Port = Value
End Set
End Property
Public Property PortName() As String
Get
Return _PortName
End Get
Set(ByVal Value As String)
_PortName = Value
End Set
End Property
Public Property ServerMachine() As String
Get
Return _ServerMachine
End Get
Set(ByVal Value As String)
_ServerMachine = Value
End Set
End Property
Public Property FilterLevel() As System.Runtime.Serialization.Formatters.TypeFilterLevel
Get
Return _FilterLevel
End Get
Set(ByVal Value As System.Runtime.Serialization.Formatters.TypeFilterLevel)
_FilterLevel = Value
End Set
End Property
End Class
<Serializable()> _
Public Class ServiceInfo
Private _ServiceName As String
Private _ServerUri As String
Private _Mode As System.Runtime.Remoting.WellKnownObjectMode
Public Property ServiceName() As String
Get
Return _ServiceName
End Get
Set(ByVal Value As String)
_ServiceName = Value
End Set
End Property
Public Property ServerUri() As String
Get
Return _ServerUri
End Get
Set(ByVal Value As String)
_ServerUri = Value
End Set
End Property
Public Property Mode() As System.Runtime.Remoting.WellKnownObjectMode
Get
Return _Mode
End Get
Set(ByVal Value As System.Runtime.Remoting.WellKnownObjectMode)
_Mode = Value
End Set
End Property
End Class
End Class
Public Class ServiceInformation
Inherits MarshalByRefObject
Private _CludeMachines(-1) As String
Private _NotCludeMachines(-1) As String
Private _Channel As New ChannelInfo
Private _Service As New ServiceInfo
Public Property Channel() As ChannelInfo
Get
Return _Channel
End Get
Set(ByVal Value As ChannelInfo)
_Channel = Value
End Set
End Property
Public Property Service() As ServiceInfo
Get
Return _Service
End Get
Set(ByVal Value As ServiceInfo)
_Service = Value
End Set
End Property
Public Property CludeMachines() As String()
Get
Return _CludeMachines
End Get
Set(ByVal Value As String())
_CludeMachines = Value
End Set
End Property
Public Property NotCludeMachines() As String()
Get
Return _NotCludeMachines
End Get
Set(ByVal Value As String())
_NotCludeMachines = Value
End Set
End Property
Public ReadOnly Property ClientServerUri() As String
Get
Dim tmp As New ArrayList
tmp.Add(_Channel.ChnlType.ToString.Substring(0, _Channel.ChnlType.ToString.IndexOf("Channel")))
tmp.Add(_Channel.ServerMachine)
tmp.Add(_Channel.Port)
tmp.Add(_Service.ServerUri)
Return String.Format("{0}://{1}:{2}/{3}", tmp.ToArray)
End Get
End Property
Public Function Clone() As ServiceInformation
Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim MS As New System.IO.MemoryStream
BF.Serialize(MS, Me)
MS.Position = 0
Return (CType(BF.Deserialize(MS), ServiceInformation))
End Function
<Serializable()> _
Public Class ChannelInfo
Private _PortName As String
Private _Port As String
Private _FilterLevel As System.Runtime.Serialization.Formatters.TypeFilterLevel
Private _ChnlType As ChannelType
Private _ServerMachine As String
Public Property ChnlType() As ChannelType
Get
Return _ChnlType
End Get
Set(ByVal Value As ChannelType)
_ChnlType = Value
End Set
End Property
Public Property Port() As String
Get
Return _Port
End Get
Set(ByVal Value As String)
_Port = Value
End Set
End Property
Public Property PortName() As String
Get
Return _PortName
End Get
Set(ByVal Value As String)
_PortName = Value
End Set
End Property
Public Property ServerMachine() As String
Get
Return _ServerMachine
End Get
Set(ByVal Value As String)
_ServerMachine = Value
End Set
End Property
Public Property FilterLevel() As System.Runtime.Serialization.Formatters.TypeFilterLevel
Get
Return _FilterLevel
End Get
Set(ByVal Value As System.Runtime.Serialization.Formatters.TypeFilterLevel)
_FilterLevel = Value
End Set
End Property
End Class
<Serializable()> _
Public Class ServiceInfo
Private _ServiceName As String
Private _ServerUri As String
Private _Mode As System.Runtime.Remoting.WellKnownObjectMode
Public Property ServiceName() As String
Get
Return _ServiceName
End Get
Set(ByVal Value As String)
_ServiceName = Value
End Set
End Property
Public Property ServerUri() As String
Get
Return _ServerUri
End Get
Set(ByVal Value As String)
_ServerUri = Value
End Set
End Property
Public Property Mode() As System.Runtime.Remoting.WellKnownObjectMode
Get
Return _Mode
End Get
Set(ByVal Value As System.Runtime.Remoting.WellKnownObjectMode)
_Mode = Value
End Set
End Property
End Class
End Class
ServiceInformationCollectionBase.vb
Public MustInherit Class ServiceInformationCollectionBase
Inherits System.Collections.DictionaryBase
Private _FileName As String = AppDomain.CurrentDomain.BaseDirectory & "ServiceInformation.bin"
Public Sub Add(ByVal info As ServiceInformation)
Me.Dictionary.Add(info.Service.ServiceName, info)
End Sub
Public Sub Remove(ByVal ServiceName As String)
Me.Dictionary.Remove(ServiceName)
End Sub
Default Public ReadOnly Property Item(ByVal ServiceName As String) As ServiceInformation
Get
Return CType(Me.Dictionary.Item(ServiceName), ServiceInformation)
End Get
End Property
Public Sub Save()
Dim Infos(Me.Count - 1) As ServiceInformation
Me.InnerHashtable.Values.CopyTo(Infos, 0)
Serialize.SaveSerializerFile(_FileName, New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, Infos)
End Sub
Public Sub Load()
If Not IO.File.Exists(_FileName) Then
Initialize()
Save()
Else
Dim Infos() As ServiceInformation
Infos = CType(Serialize.LoadDeSerializeFile(_FileName, New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter), ServiceInformation())
For Each info As ServiceInformation In Infos
Me.Add(info)
Next
End If
End Sub
Public MustOverride Sub Initialize()
Sub New()
Me.Load()
End Sub
End Class
Inherits System.Collections.DictionaryBase
Private _FileName As String = AppDomain.CurrentDomain.BaseDirectory & "ServiceInformation.bin"
Public Sub Add(ByVal info As ServiceInformation)
Me.Dictionary.Add(info.Service.ServiceName, info)
End Sub
Public Sub Remove(ByVal ServiceName As String)
Me.Dictionary.Remove(ServiceName)
End Sub
Default Public ReadOnly Property Item(ByVal ServiceName As String) As ServiceInformation
Get
Return CType(Me.Dictionary.Item(ServiceName), ServiceInformation)
End Get
End Property
Public Sub Save()
Dim Infos(Me.Count - 1) As ServiceInformation
Me.InnerHashtable.Values.CopyTo(Infos, 0)
Serialize.SaveSerializerFile(_FileName, New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, Infos)
End Sub
Public Sub Load()
If Not IO.File.Exists(_FileName) Then
Initialize()
Save()
Else
Dim Infos() As ServiceInformation
Infos = CType(Serialize.LoadDeSerializeFile(_FileName, New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter), ServiceInformation())
For Each info As ServiceInformation In Infos
Me.Add(info)
Next
End If
End Sub
Public MustOverride Sub Initialize()
Sub New()
Me.Load()
End Sub
End Class
IMessage.vb
Public Interface IMessage
ReadOnly Property Machine() As String
Property ID() As String
Property Name() As String
Property Body() As Object
End Interface
ReadOnly Property Machine() As String
Property ID() As String
Property Name() As String
Property Body() As Object
End Interface
IService.vb
Public Interface IService
Property Information() As ServiceInformation
Delegate Sub ServiceMessageHandler(ByVal sender As Object, ByVal arg As IMessage)
Event SendMessage As ServiceMessageHandler
Sub OnSendMessage(ByVal sender As Object, ByVal arg As IMessage)
End Interface
Property Information() As ServiceInformation
Delegate Sub ServiceMessageHandler(ByVal sender As Object, ByVal arg As IMessage)
Event SendMessage As ServiceMessageHandler
Sub OnSendMessage(ByVal sender As Object, ByVal arg As IMessage)
End Interface
MessageBase.vb
Public MustInherit Class MessageBase
Inherits MarshalByRefObject
Implements IMessage
Private _ID As String
Private _Name As String
Private _Body As Object
Public Property Body() As Object Implements IMessage.Body
Get
Return _Body
End Get
Set(ByVal Value As Object)
_Body = Value
End Set
End Property
Public ReadOnly Property Machine() As String Implements IMessage.Machine
Get
Return System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(0).ToString
End Get
End Property
Public Property Name() As String Implements IMessage.Name
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property ID() As String Implements IMessage.ID
Get
Return _ID
End Get
Set(ByVal Value As String)
_ID = Value
End Set
End Property
End Class
ServiceBase.vb
Public MustInherit Class ServiceBase
Inherits MarshalByRefObject
Implements IService, IDisposable
Private _Information As ServiceInformation
Public Event SendMessage(ByVal sender As Object, ByVal arg As IMessage) Implements IService.SendMessage
Public Overridable Sub OnSendMessage(ByVal sender As Object, ByVal arg As IMessage) Implements IService.OnSendMessage
If Not SendMessageEvent Is Nothing Then
For Each del As [Delegate] In SendMessageEvent.GetInvocationList
Try
If TypeOf del.Target Is ServicesListener Then
Dim ClientMachine As String = CType(del.Target, ServicesListener).Machine
If Array.IndexOf(_Information.NotCludeMachines, ClientMachine) = -1 Then
If _Information.CludeMachines.Length > 0 Then
If Array.IndexOf(_Information.CludeMachines, ClientMachine) <> -1 Then
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Else
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
End If
Else
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Catch ex As Exception
SendMessageEvent = CType([Delegate].Remove(SendMessageEvent, del), IService.ServiceMessageHandler)
End Try
Next
End If
End Sub
Public Property Information() As ServiceInformation Implements IService.Information
Get
Return _Information
End Get
Set(ByVal Value As ServiceInformation)
_Information = Value
End Set
End Property
Protected MustOverride Sub Dispose(ByVal disposing As Boolean)
Public Sub Dispose() Implements System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
Inherits MarshalByRefObject
Implements IService, IDisposable
Private _Information As ServiceInformation
Public Event SendMessage(ByVal sender As Object, ByVal arg As IMessage) Implements IService.SendMessage
Public Overridable Sub OnSendMessage(ByVal sender As Object, ByVal arg As IMessage) Implements IService.OnSendMessage
If Not SendMessageEvent Is Nothing Then
For Each del As [Delegate] In SendMessageEvent.GetInvocationList
Try
If TypeOf del.Target Is ServicesListener Then
Dim ClientMachine As String = CType(del.Target, ServicesListener).Machine
If Array.IndexOf(_Information.NotCludeMachines, ClientMachine) = -1 Then
If _Information.CludeMachines.Length > 0 Then
If Array.IndexOf(_Information.CludeMachines, ClientMachine) <> -1 Then
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Else
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
End If
Else
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Catch ex As Exception
SendMessageEvent = CType([Delegate].Remove(SendMessageEvent, del), IService.ServiceMessageHandler)
End Try
Next
End If
End Sub
Public Property Information() As ServiceInformation Implements IService.Information
Get
Return _Information
End Get
Set(ByVal Value As ServiceInformation)
_Information = Value
End Set
End Property
Protected MustOverride Sub Dispose(ByVal disposing As Boolean)
Public Sub Dispose() Implements System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
ServicesFactory.vb
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Public Class ServicesFactory
Public Shared ChnlServices As ChannelServices
Public Shared Services As RemotingServices
Public Shared Configuration As RemotingConfiguration
Public Shared Property ApplicationName() As String
Get
Return RemotingConfiguration.ApplicationName
End Get
Set(ByVal Value As String)
Configuration.ApplicationName = Value
End Set
End Property
Public Shared Sub ChannelRegister(ByVal ChannelName As String, ByVal port As String, ByVal TypeFilterLevel As Runtime.Serialization.Formatters.TypeFilterLevel, ByVal ChnlType As ChannelType)
Dim clientProvider As New BinaryClientFormatterSinkProvider
Dim serverProvider As New BinaryServerFormatterSinkProvider
serverProvider.TypeFilterLevel = TypeFilterLevel
Dim properties As IDictionary = New Hashtable
properties("name") = ChannelName
properties("port") = port
properties("typeFilterLevel") = TypeFilterLevel
Dim mServerChannel As IChannel
Select Case ChnlType
Case ChannelType.HttpChannel
mServerChannel = New Channels.http.HttpChannel(properties, clientProvider, serverProvider)
Case ChannelType.TcpChannel
mServerChannel = New Channels.Tcp.TcpChannel(properties, clientProvider, serverProvider)
End Select
ChnlServices.RegisterChannel(mServerChannel)
End Sub
Public Shared Function ChannelTcp(ByVal ChannelName As String) As TcpChannel
Return CType(ChnlServices.GetChannel(ChannelName), TcpChannel)
End Function
Public Shared Function ChannelHttp(ByVal ChannelName As String) As HttpChannel
Return CType(ChnlServices.GetChannel(ChannelName), HttpChannel)
End Function
Public Shared Sub ChannelUnregister(ByVal ChannelName As String)
Dim tmpChannel As IChannel = ChnlServices.GetChannel(ChannelName)
ChnlServices.UnregisterChannel(tmpChannel)
tmpChannel = Nothing
End Sub
Public Shared Function ServiceMarshal(ByVal service As IService) As ObjRef
ServiceChannelRegister(service.Information)
Return Services.Marshal(service, service.Information.Service.ServerUri)
End Function
Public Shared Sub ServiceRegister(ByVal IServiceObj As Object)
Dim tmp As IService = CType(IServiceObj, IService)
ServiceChannelRegister(tmp.Information)
With tmp.Information.Service
Configuration.RegisterWellKnownServiceType(IServiceObj.GetType, .ServerUri, .Mode)
End With
End Sub
Public Shared Sub ServiceChannelRegister(ByVal serviceinfo As ServiceInformation)
If ChnlServices.GetChannel(serviceinfo.Channel.PortName) Is Nothing Then
With serviceinfo.Channel
ChannelRegister(.PortName, .Port, .FilterLevel, .ChnlType)
End With
End If
End Sub
Public Shared Sub Configure(ByVal ConfigFileName As String)
Configuration.Configure(ConfigFileName)
End Sub
Public Shared Sub Configure()
Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
End Sub
Public Shared Function ActivatorGetObject(ByVal type As System.Type, ByVal ChnlType As ChannelType, ByVal ServerAddress As String, ByVal Port As String, ByVal ServiceUri As String) As Object
Dim tmp As New ArrayList
tmp.Add(ChnlType.ToString.Substring(0, ChnlType.ToString.IndexOf("Channel")))
tmp.Add(ServerAddress)
tmp.Add(Port)
tmp.Add(ServiceUri)
Return Activator.GetObject(type, String.Format("{0}://{1}:{2}/{3}", tmp.ToArray))
End Function
Public Shared Function ActivatorGetObject(ByVal serviceinformation As ServiceInformation) As IService
Return Activator.GetObject(GetType(IService), serviceinformation.ClientServerUri)
End Function
End Class
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Public Class ServicesFactory
Public Shared ChnlServices As ChannelServices
Public Shared Services As RemotingServices
Public Shared Configuration As RemotingConfiguration
Public Shared Property ApplicationName() As String
Get
Return RemotingConfiguration.ApplicationName
End Get
Set(ByVal Value As String)
Configuration.ApplicationName = Value
End Set
End Property
Public Shared Sub ChannelRegister(ByVal ChannelName As String, ByVal port As String, ByVal TypeFilterLevel As Runtime.Serialization.Formatters.TypeFilterLevel, ByVal ChnlType As ChannelType)
Dim clientProvider As New BinaryClientFormatterSinkProvider
Dim serverProvider As New BinaryServerFormatterSinkProvider
serverProvider.TypeFilterLevel = TypeFilterLevel
Dim properties As IDictionary = New Hashtable
properties("name") = ChannelName
properties("port") = port
properties("typeFilterLevel") = TypeFilterLevel
Dim mServerChannel As IChannel
Select Case ChnlType
Case ChannelType.HttpChannel
mServerChannel = New Channels.http.HttpChannel(properties, clientProvider, serverProvider)
Case ChannelType.TcpChannel
mServerChannel = New Channels.Tcp.TcpChannel(properties, clientProvider, serverProvider)
End Select
ChnlServices.RegisterChannel(mServerChannel)
End Sub
Public Shared Function ChannelTcp(ByVal ChannelName As String) As TcpChannel
Return CType(ChnlServices.GetChannel(ChannelName), TcpChannel)
End Function
Public Shared Function ChannelHttp(ByVal ChannelName As String) As HttpChannel
Return CType(ChnlServices.GetChannel(ChannelName), HttpChannel)
End Function
Public Shared Sub ChannelUnregister(ByVal ChannelName As String)
Dim tmpChannel As IChannel = ChnlServices.GetChannel(ChannelName)
ChnlServices.UnregisterChannel(tmpChannel)
tmpChannel = Nothing
End Sub
Public Shared Function ServiceMarshal(ByVal service As IService) As ObjRef
ServiceChannelRegister(service.Information)
Return Services.Marshal(service, service.Information.Service.ServerUri)
End Function
Public Shared Sub ServiceRegister(ByVal IServiceObj As Object)
Dim tmp As IService = CType(IServiceObj, IService)
ServiceChannelRegister(tmp.Information)
With tmp.Information.Service
Configuration.RegisterWellKnownServiceType(IServiceObj.GetType, .ServerUri, .Mode)
End With
End Sub
Public Shared Sub ServiceChannelRegister(ByVal serviceinfo As ServiceInformation)
If ChnlServices.GetChannel(serviceinfo.Channel.PortName) Is Nothing Then
With serviceinfo.Channel
ChannelRegister(.PortName, .Port, .FilterLevel, .ChnlType)
End With
End If
End Sub
Public Shared Sub Configure(ByVal ConfigFileName As String)
Configuration.Configure(ConfigFileName)
End Sub
Public Shared Sub Configure()
Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
End Sub
Public Shared Function ActivatorGetObject(ByVal type As System.Type, ByVal ChnlType As ChannelType, ByVal ServerAddress As String, ByVal Port As String, ByVal ServiceUri As String) As Object
Dim tmp As New ArrayList
tmp.Add(ChnlType.ToString.Substring(0, ChnlType.ToString.IndexOf("Channel")))
tmp.Add(ServerAddress)
tmp.Add(Port)
tmp.Add(ServiceUri)
Return Activator.GetObject(type, String.Format("{0}://{1}:{2}/{3}", tmp.ToArray))
End Function
Public Shared Function ActivatorGetObject(ByVal serviceinformation As ServiceInformation) As IService
Return Activator.GetObject(GetType(IService), serviceinformation.ClientServerUri)
End Function
End Class
ClientFactory.vb
Public Class ClientFactory
Private _ID As String
Public Event ServiceMessageArrived(ByVal sender As Object, ByVal arg As IMessage)
Private ListenerCollection As New Hashtable
Private ServiceCollection As New Hashtable
Public ReadOnly Property Machine() As String
Get
Return System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(0).ToString
End Get
End Property
Public ReadOnly Property ID() As String
Get
Return _ID
End Get
End Property
Private Sub OnServiceMessageArrived(ByVal sender As Object, ByVal arg As IMessage)
RaiseEvent ServiceMessageArrived(sender, arg)
End Sub
Public Sub ServiceAdd(ByVal serviceinfo As ServiceInformation)
If ServiceCollection.Contains(serviceinfo.Service.ServiceName) Then Exit Sub
Dim info As ServiceInformation = serviceinfo.Clone
With info.Channel
.PortName = String.Concat(.PortName, "Client")
.Port = "0"
End With
ServicesFactory.ServiceChannelRegister(info)
ServiceCollection.Add(serviceinfo.Service.ServiceName, ServicesFactory.ActivatorGetObject(serviceinfo))
End Sub
Public Sub ServiceRemove(ByVal serviceName As String)
ServiceEventDisconnect(serviceName)
If ServiceCollection.Contains(serviceName) Then ServiceCollection.Remove(serviceName)
End Sub
Public Sub ServiceEventConnect(ByVal serviceName As String)
If ListenerCollection.Contains(serviceName) Then Exit Sub
Dim mListener As New ServicesListener
AddHandler mListener.SendServiceMessageToClient, AddressOf OnServiceMessageArrived
mListener.AddListener(CType(ServiceCollection.Item(serviceName), IService))
ListenerCollection.Add(serviceName, mListener)
End Sub
Public Sub ServiceEventDisconnect(ByVal serviceName As String)
If Not ListenerCollection.Contains(serviceName) Then Exit Sub
CType(ListenerCollection.Item(serviceName), ServicesListener).RemoveListenner(CType(ServiceCollection.Item(serviceName), IService))
RemoveHandler CType(ListenerCollection.Item(serviceName), ServicesListener).SendServiceMessageToClient, AddressOf OnServiceMessageArrived
ListenerCollection.Remove(serviceName)
End Sub
Public Sub MessageSendToServer(ByVal serviceName As String, ByVal sender As Object, ByVal arg As IMessage)
If Service(serviceName) Is Nothing Then Exit Sub
arg.ID = _ID
Service(serviceName).OnSendMessage(sender, arg)
End Sub
Public Function Service(ByVal serviceName As String) As IService
Return CType(ServiceCollection.Item(serviceName), IService)
End Function
Sub New()
_ID = System.Guid.NewGuid.ToString
End Sub
End Class
Private _ID As String
Public Event ServiceMessageArrived(ByVal sender As Object, ByVal arg As IMessage)
Private ListenerCollection As New Hashtable
Private ServiceCollection As New Hashtable
Public ReadOnly Property Machine() As String
Get
Return System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(0).ToString
End Get
End Property
Public ReadOnly Property ID() As String
Get
Return _ID
End Get
End Property
Private Sub OnServiceMessageArrived(ByVal sender As Object, ByVal arg As IMessage)
RaiseEvent ServiceMessageArrived(sender, arg)
End Sub
Public Sub ServiceAdd(ByVal serviceinfo As ServiceInformation)
If ServiceCollection.Contains(serviceinfo.Service.ServiceName) Then Exit Sub
Dim info As ServiceInformation = serviceinfo.Clone
With info.Channel
.PortName = String.Concat(.PortName, "Client")
.Port = "0"
End With
ServicesFactory.ServiceChannelRegister(info)
ServiceCollection.Add(serviceinfo.Service.ServiceName, ServicesFactory.ActivatorGetObject(serviceinfo))
End Sub
Public Sub ServiceRemove(ByVal serviceName As String)
ServiceEventDisconnect(serviceName)
If ServiceCollection.Contains(serviceName) Then ServiceCollection.Remove(serviceName)
End Sub
Public Sub ServiceEventConnect(ByVal serviceName As String)
If ListenerCollection.Contains(serviceName) Then Exit Sub
Dim mListener As New ServicesListener
AddHandler mListener.SendServiceMessageToClient, AddressOf OnServiceMessageArrived
mListener.AddListener(CType(ServiceCollection.Item(serviceName), IService))
ListenerCollection.Add(serviceName, mListener)
End Sub
Public Sub ServiceEventDisconnect(ByVal serviceName As String)
If Not ListenerCollection.Contains(serviceName) Then Exit Sub
CType(ListenerCollection.Item(serviceName), ServicesListener).RemoveListenner(CType(ServiceCollection.Item(serviceName), IService))
RemoveHandler CType(ListenerCollection.Item(serviceName), ServicesListener).SendServiceMessageToClient, AddressOf OnServiceMessageArrived
ListenerCollection.Remove(serviceName)
End Sub
Public Sub MessageSendToServer(ByVal serviceName As String, ByVal sender As Object, ByVal arg As IMessage)
If Service(serviceName) Is Nothing Then Exit Sub
arg.ID = _ID
Service(serviceName).OnSendMessage(sender, arg)
End Sub
Public Function Service(ByVal serviceName As String) As IService
Return CType(ServiceCollection.Item(serviceName), IService)
End Function
Sub New()
_ID = System.Guid.NewGuid.ToString
End Sub
End Class
ServicesListener.vb
Public Class ServicesListener
Inherits MarshalByRefObject
Public Event SendServiceMessageToClient(ByVal sender As Object, ByVal arg As IMessage)
Private _Machine As String
Public ReadOnly Property Machine() As String
Get
Return System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(0).ToString
End Get
End Property
Public Sub SendServiceMessageToClientArrived(ByVal sender As Object, ByVal arg As IMessage)
If Not SendServiceMessageToClientEvent Is Nothing Then
For Each del As [Delegate] In SendServiceMessageToClientEvent.GetInvocationList
Try
If TypeOf del.Target Is ClientFactory Then
If Not CType(del.Target, ClientFactory).ID.Equals(arg.ID) Then
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Else
del.Method.Invoke(del.Target, New Object() {sender, arg})
End If
Catch ex As Exception
SendServiceMessageToClientEvent = [Delegate].Remove(SendServiceMessageToClientEvent, del)
End Try
Next
End If
End Sub
Public Sub AddListener(ByVal service As IService)
AddHandler service.SendMessage, AddressOf Me.SendServiceMessageToClientArrived
End Sub
Public Sub RemoveListenner(ByVal service As IService)
RemoveHandler service.SendMessage, AddressOf Me.SendServiceMessageToClientArrived
End Sub
End Class
Serialize.vb
Public Class Serialize
Private Shared Function GetXML(ByVal obj As Object) As String
Dim mSerializer As New System.Xml.Serialization.XmlSerializer(obj.GetType)
Dim mStringWriter As New System.IO.StringWriter
mSerializer.Serialize(mStringWriter, obj)
Return mStringWriter.ToString
End Function
Private Shared Function GetObj(ByVal objtype As Type, ByVal xml As String) As Object
Dim mSerializer As New System.Xml.Serialization.XmlSerializer(objtype)
Dim mStringReader As New System.IO.StringReader(xml)
Return mSerializer.Deserialize(mStringReader)
End Function
Public Shared Sub SaveXmlFile(ByVal filename As String, ByVal obj As Object)
Dim XmlWriter As New System.IO.StreamWriter(filename, False)
XmlWriter.Write(GetXML(obj))
XmlWriter.Close()
End Sub
Public Shared Function LoadXmlFile(ByVal filename As String, ByVal objtype As Type) As Object
Dim XmlReader As New System.IO.StreamReader(filename, System.Text.Encoding.Default)
Dim mObj As Object
mObj = GetObj(objtype, XmlReader.ReadToEnd)
XmlReader.Close()
Return mObj
End Function
Public Shared Sub SaveSerializerFile(ByVal filename As String, ByVal formatter As System.Runtime.Serialization.IFormatter, ByVal obj As Object)
Dim mFileStream As System.IO.Stream = System.IO.File.Open(filename, System.IO.FileMode.Create)
formatter.Serialize(mFileStream, obj)
mFileStream.Close()
End Sub
Public Shared Function LoadDeSerializeFile(ByVal FileName As String, ByVal formatter As System.Runtime.Serialization.IFormatter) As Object
Dim mFileStream As System.IO.Stream = System.IO.File.Open(FileName, System.IO.FileMode.Open)
Dim mObj As Object
mObj = formatter.Deserialize(mFileStream)
mFileStream.Close()
Return mObj
End Function
Public Shared Function Clone(ByVal obj As Object) As Object
Dim mFormatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mMemoryStream As New System.IO.MemoryStream
mFormatter.Serialize(mMemoryStream, obj)
mMemoryStream.Position = 0
Return mFormatter.Deserialize(mMemoryStream)
End Function
End Class