3
\$\begingroup\$

I wrote this wrapper around the UdpClient,using events for new datagrams:

Class EventedUDP
        Implements IDisposable
        Dim Client As Net.Sockets.UdpClient
        Dim Endpoint As Net.IPEndPoint
        Public Event RawDatagram(sender As EventedUDP, datagram As Byte(), ByRef handled As Boolean)
        Public Event NewDatagram(sender As EventedUDP, datagram As String)
        Property Active As Boolean = False
        Property Encoding As System.Text.Encoding = New System.Text.UnicodeEncoding(False, False)
        ReadOnly Property Port As Integer
            Get
                Return Endpoint.Port
            End Get
        End Property
        ReadOnly Property LocalEndpoint As Net.IPEndPoint
            Get
                Return Endpoint
            End Get
        End Property
        ReadOnly Property LocalIP As String
            Get
                Return Endpoint.Address.ToString
            End Get
        End Property
        Sub New(ip As String, port As Integer)
            Try
                Endpoint = New Net.IPEndPoint(Net.IPAddress.Parse(ip), port)
                Client = New Net.Sockets.UdpClient(Endpoint)
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Throw ex
            End Try
        End Sub
        Sub New(port As Integer)
            Try
                Endpoint = New Net.IPEndPoint(Net.IPAddress.Any, port)
                Client = New Net.Sockets.UdpClient(Endpoint)
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Throw ex
            End Try
        End Sub
        Sub New(ip As String)
            Try
                Endpoint = New Net.IPEndPoint(Net.IPAddress.Parse(ip), 0)
                Client = New Net.Sockets.UdpClient(Endpoint)
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Throw ex
            End Try
        End Sub
        Sub New()
            Try
                Client = New Net.Sockets.UdpClient
                Endpoint = Client.Client.LocalEndPoint
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
                Throw ex
            End Try
        End Sub
        Sub Start()
            If Not Active Then
                Active = True
                BeginReceive()
            End If
        End Sub
        Sub ReceiveCallback(ar As IAsyncResult)
            Dim datagram() As Byte = Nothing
            Try
                datagram = Client.EndReceive(ar, New Net.IPEndPoint(Net.IPAddress.Any, 0))
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
            If datagram IsNot Nothing Then Task.Factory.StartNew(Sub() RelayDatagram(datagram))
            If Active = True Then BeginReceive()
        End Sub
        Sub RelayDatagram(datagram As Byte())
            Dim handled As Boolean = False
            Try
                RaiseEvent RawDatagram(Me, datagram, handled)
                If handled = True Then Return
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
            Try
                Dim strData As String = Encoding.GetString(datagram)
                RaiseEvent NewDatagram(Me, strdata)
            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
        End Sub
        Private Sub BeginReceive()
            Client.BeginReceive(AddressOf ReceiveCallback, "NO STATE")
        End Sub
#Region "IDisposable Support"
        Private disposedValue As Boolean
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    Active = False
                    Try
                        Client.Close()
                    Catch ex As Exception
                        Console.WriteLine(ex.ToString)
                    End Try
                End If
                Me.Endpoint = Nothing
            End If
            Me.disposedValue = True
        End Sub
        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
        End Sub
#End Region
    End Class

Usage Example:

Dim rec As New EventedUDP("127.0.0.1", 12345)
AddHandler rec.NewDatagram, Sub(sender, datagram) Console.WriteLine(datagram)
rec.Start()

Do you see any problem with this class, regarding possible points of failure?

This class is going to be used in different projects: for IPC with a Java program (see this, too), for receiving real time measurements from a sensor, and to act as a generic receiver. I may add datagram sending capability in the future.

Is using events (NewDatagram) combined with Begin/End bad practice?

Any tips to make it better?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.