Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Network.VB into 7 Files from PR #11863 #12226

Merged
merged 31 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6245505
Split Network.VB into 7 Files
paul1956 Sep 25, 2024
0b2f79a
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Sep 26, 2024
eeef489
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 2, 2024
c80157c
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 4, 2024
13e32df
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 7, 2024
70c216e
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 9, 2024
d1c86ca
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 10, 2024
1a66169
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 11, 2024
2d151be
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 11, 2024
4bf1514
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 12, 2024
0b76086
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 14, 2024
3cc1f00
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 16, 2024
afd3a73
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 17, 2024
3c4059b
Minor Cleanup
paul1956 Oct 17, 2024
5f923e2
Remove m_ from private variable names and fix up event handler names …
paul1956 Oct 17, 2024
35da609
Remove unused imports
paul1956 Oct 17, 2024
9f3f3bd
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 19, 2024
6d18fe8
Fix Debug.Assert in AssemblyInfo.vb
paul1956 Oct 20, 2024
ca2e690
Cleanup Event Handler names
paul1956 Oct 20, 2024
420f759
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 23, 2024
ae3ad18
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 23, 2024
a5d420e
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 23, 2024
98dafbd
Merge
paul1956 Oct 26, 2024
c78783d
Fix incorrect naming of Private variables
paul1956 Oct 27, 2024
1c5bdb6
Cleanup line length and spelling
paul1956 Oct 27, 2024
203002b
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 28, 2024
88e65c2
Add missing CrLf
paul1956 Oct 28, 2024
875aac1
Simplify dialog.BeginInvoke
paul1956 Oct 28, 2024
93c466a
PR Feedback
paul1956 Oct 28, 2024
fd8f42f
PR feedback
paul1956 Oct 29, 2024
72a8f30
Merge branch 'master' into Split-Network.vb-inlo-logical-parts
paul1956 Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.

Imports System.ComponentModel
Imports System.Security
Imports System.Threading

Imports NetInfoAlias = System.Net.NetworkInformation

Namespace Microsoft.VisualBasic.Devices

<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Delegate Sub NetworkAvailableEventHandler(sender As Object, e As NetworkAvailableEventArgs)

Partial Public Class Network

' Object for syncing
Private ReadOnly _syncObject As New Object()

' Indicates last known connection state
Private _connected As Boolean

' Used for marshalling the network address changed event to the foreground thread
Private _networkAvailabilityChangedCallback As SendOrPostCallback

' Holds the listeners to our NetworkAvailability changed event
Private _networkAvailabilityEventHandlers As List(Of NetworkAvailableEventHandler)

Private _synchronizationContext As SynchronizationContext

''' <summary>
''' Indicates whether or not the local machine is connected to an IP network.
''' </summary>
''' <value>
''' <see langword="True"/> if connected,
''' otherwise <see langword="False"/>.
''' </value>
Public ReadOnly Property IsAvailable() As Boolean
Get
Return NetInfoAlias.NetworkInterface.GetIsNetworkAvailable()
End Get
End Property

''' <summary>
''' <see langword="Event"/> fired when connected to the network.
''' </summary>
''' <param name="Sender">Has no meaning for this event.</param>
''' <param name="e">Has no meaning for this event.</param>
Public Custom Event NetworkAvailabilityChanged As NetworkAvailableEventHandler
' This is a custom event because we want to hook up
' the NetworkAvailabilityChanged event only if the user writes a handler for it.
' The reason being that it is very expensive to handle and kills our application startup perf.
AddHandler(handler As NetworkAvailableEventHandler)

' Set the current state of connectedness, swallow known exceptions
' since user won't be able to correct problem
Try
_connected = IsAvailable
Catch ex As SecurityException
Return
Catch ex As PlatformNotSupportedException
Return
End Try
' We don't want our event firing before we've finished setting up the infrastructure.
' Also, need to assure there are no races in here so we don't hook up the OS listener twice, etc.
SyncLock _syncObject
If _networkAvailabilityEventHandlers Is Nothing Then
_networkAvailabilityEventHandlers = New List(Of NetworkAvailableEventHandler)
End If
_networkAvailabilityEventHandlers.Add(handler)

' Only setup the event Marshalling infrastructure once
If _networkAvailabilityEventHandlers.Count = 1 Then
' The async operation posts to this delegate
_networkAvailabilityChangedCallback = New SendOrPostCallback(
AddressOf NetworkAvailabilityChangedHandler)

If AsyncOperationManager.SynchronizationContext IsNot Nothing Then
' We need to hang on to the synchronization context associated
' with the thread the network object is created on
_synchronizationContext = AsyncOperationManager.SynchronizationContext
Try
' Exceptions are thrown if the user isn't an admin.
' Listen to the OS event
AddHandler NetInfoAlias.NetworkChange.NetworkAddressChanged,
New NetInfoAlias.NetworkAddressChangedEventHandler(
AddressOf OS_NetworkAvailabilityChangedListener)
Catch ex As PlatformNotSupportedException
Catch ex As NetInfoAlias.NetworkInformationException
End Try
End If
End If
End SyncLock
End AddHandler

RemoveHandler(handler As NetworkAvailableEventHandler)
If _networkAvailabilityEventHandlers IsNot Nothing AndAlso
_networkAvailabilityEventHandlers.Count > 0 Then

_networkAvailabilityEventHandlers.Remove(handler)
' Last one to leave, turn out the lights...
If _networkAvailabilityEventHandlers.Count = 0 Then
' Listen to the OS event
RemoveHandler NetInfoAlias.NetworkChange.NetworkAddressChanged,
New NetInfoAlias.NetworkAddressChangedEventHandler(
AddressOf OS_NetworkAvailabilityChangedListener)

' Stop listening to network change events since nobody is listening to us anymore
DisconnectListener()
End If
End If
End RemoveHandler

RaiseEvent(sender As Object, e As NetworkAvailableEventArgs)
If _networkAvailabilityEventHandlers IsNot Nothing Then
For Each handler As NetworkAvailableEventHandler In _networkAvailabilityEventHandlers
If handler IsNot Nothing Then handler.Invoke(sender, e)
Next
End If
End RaiseEvent
End Event

' Listens to the AddressChanged event which will come on the same thread that this
' class was created on (AsyncEventManager is responsible for getting the event here)
Private Sub NetworkAvailabilityChangedHandler(state As Object)
Dim connected As Boolean = IsAvailable
' Fire an event only if the connected state has changed
If _connected <> connected Then
_connected = connected
RaiseEvent NetworkAvailabilityChanged(Me, New NetworkAvailableEventArgs(connected))
End If
End Sub

' Listens to the AddressChanged event from the OS which comes in on an arbitrary thread
Private Sub OS_NetworkAvailabilityChangedListener(sender As Object, e As EventArgs)
SyncLock _syncObject
' Ensure we don't handle events until after we've finished setting up the event
' marshalling infrastructure. Don't call AsyncOperationManager.OperationSynchronizationContext.Post.
' The reason we want to go through _synchronizationContext is that the
' OperationSynchronizationContext is thread static. Since we are getting called on some random thread,
' the context that was in place when the Network object was created won't be available
' (it is on the original thread). To hang on to the original context associated with the thread
' that the network object is created on, I use _synchronizationContext.
_synchronizationContext.Post(_networkAvailabilityChangedCallback, Nothing)
End SyncLock
End Sub

Friend Sub DisconnectListener()
RemoveHandler NetInfoAlias.NetworkChange.NetworkAddressChanged,
New NetInfoAlias.NetworkAddressChangedEventHandler(
AddressOf OS_NetworkAvailabilityChangedListener)
End Sub

End Class
End Namespace
Loading
Loading