This is what I am doing inside the class. There is probably some other way
to do this better, but I need a threadsafe way to get to
"MyServerConnections". I am using the PublicServerStruct as a "key" to look
up a connection. I thought an array pointer would be easy, but I cant
figure it out. So, below is a class that I am working on.
I will paste the class below:
Option Explicit On
'Option Strict On
Public Class COMMON_ServerConnections
Public Structure PublicServerStruct
Dim Remote_port_num As Integer
Dim Remote_host_name As String
End Structure
Private Structure PrivateServerConnectionStruct
Dim client As System.Net.Sockets.TcpClient
Dim Connected_To_Server As Boolean
Dim Connected_User_Name As String
Dim Remote_port_num As Integer
Dim Remote_host_name As String
Dim MessageQueue As Queue
End Structure
shared MyServerConnections As New ArrayList
#Region " Properties "
Public Property Client(ByVal PublicServerConnection As
PublicServerStruct) As System.Net.Sockets.TcpClient
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).client
Else
Return Nothing
End If
End Get
Set(ByVal Value As System.Net.Sockets.TcpClient)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
'Cant figure out how to get around late binding.
MyServerConnections(Myindex).client = Value
End If
End Set
End Property
Public Property Connected_To_Server(ByVal PublicServerConnection As
PublicServerStruct) As Boolean
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).Connected_To_Server
Else
Return Nothing
End If
End Get
Set(ByVal Value As Boolean)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
'Cant figure out how to get around late binding.
MyServerConnections(Myindex).Connected_To_Server = Value
End If
End Set
End Property
Public Property Connected_User_Name(ByVal PublicServerConnection As
PublicServerStruct) As String
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).Connected_User_Name
Else
Return Nothing
End If
End Get
Set(ByVal Value As String)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
'Cant figure out how to get around late binding.
MyServerConnections(Myindex).Connected_User_Name = Value
End If
End Set
End Property
Public Property MessageQueue(ByVal PublicServerConnection As
PublicServerStruct) As String
Get
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
Return CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).MessageQueue.Dequeue
Else
Return Nothing
End If
End Get
Set(ByVal Value As String)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
CType(MyServerConnections(Myindex),
PrivateServerConnectionStruct).MessageQueue.Enqueue(Value)
End If
End Set
End Property
#End Region
Public Function Add(ByVal RemoteHostname As String, ByVal
RemotePortNumber As Integer) As PublicServerStruct
Dim ServerConnection As New PublicServerStruct
ServerConnection.Remote_host_name = RemoteHostname
ServerConnection.Remote_port_num = RemotePortNumber
If ReturnArrayIndexOfClient(ServerConnection) = -1 Then
Dim NewServerConnection As New PrivateServerConnectionStruct
NewServerConnection.Remote_host_name = RemoteHostname
NewServerConnection.Remote_port_num = RemotePortNumber
NewServerConnection.MessageQueue = New Queue
MyServerConnections.Add(NewServerConnection)
End If
End Function
Public Sub Remove(ByVal PublicServerConnection As PublicServerStruct)
Dim Myindex As Integer =
ReturnArrayIndexOfClient(PublicServerConnection)
If Not IsNothing(Myindex) Then
MyServerConnections.Add(Myindex)
End If
End Sub
Public Function ReturnAllConnections() As ArrayList
Dim MyConnections As ArrayList
Dim PrivateConnection As PrivateServerConnectionStruct
For Each PrivateConnection In MyServerConnections
Dim ServerConnection As New PublicServerStruct
ServerConnection.Remote_host_name =
PrivateConnection.Remote_host_name
ServerConnection.Remote_port_num =
PrivateConnection.Remote_port_num
MyConnections.Add(ServerConnection)
Next
Return MyConnections
End Function
Private Function ReturnArrayIndexOfClient(ByVal PublicServerConnection
As PublicServerStruct) As Integer
Dim ServerConnection As New PrivateServerConnectionStruct
Dim MyIndex As Integer = 0
For Each ServerConnection In MyServerConnections
MyIndex += 1
If ServerConnection.Remote_host_name =
PublicServerConnection.Remote_host_name Then
If ServerConnection.Remote_port_num =
PublicServerConnection.Remote_port_num Then
Return MyIndex
End If
End If
Next
'Cant find anythnig, so return ... -1
Return -1
End Function
End Class