Giter VIP home page Giter VIP logo

Comments (2)

hodlhero443 avatar hodlhero443 commented on September 25, 2024

Pasting the code of OnReceiveData also.
I've tried adding code that disposes the socket in multple places to see where it could fall through, but the exceptions always gets caught corrently when I try.

private void OnReceiveData(IAsyncResult asyncResult)
{

        var connection = (UdpClient)asyncResult.AsyncState;

        try
        {
            IPEndPoint ep = null;
            byte[] receiveBuffer;

            try
            {
                receiveBuffer = connection.EndReceive(asyncResult, ref ep);
            }
            finally
            {
                if (connection.Client != null && !_disposing)
                {
                    try
                    {
                        // BeginReceive ASAP to enable parallel processing (e.g. query additional data while processing a notification)
                        connection.BeginReceive(OnReceiveData, connection);
                    }
                    catch (Exception ex)
                    {
                        Log.Error($"Failed to restart data receive. {ex}");
                    }
                }
            }

            var receiveBufferHex = ConvertToHex(receiveBuffer);
            var receivedLength = receiveBuffer.Length;

            if (receivedLength == 0) // Empty frame : port scanner maybe
                return;

            //verify message
            Convert(ep, out var remoteAddress);

            if (receivedLength < BVLC.BVLC_HEADER_LENGTH)
            {
                Log.Warn($"Some garbage data got in: {receiveBufferHex}");
                return;
            }

            // Basic Header lenght
            var headerLength = Bvlc.Decode(receiveBuffer, 0, out var function, out var _, ep);

            if (headerLength == -1)
            {
                Log.Warn($"Unknow BVLC Header in: {receiveBufferHex}");
                return;
            }

            switch (function)
            {
                case BacnetBvlcFunctions.BVLC_RESULT:
                    // response to BVLC_REGISTER_FOREIGN_DEVICE, could be BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK
                    // but we are not a BBMD, we don't care
                    Log.Debug("Receive Register as Foreign Device Response");
                    break;

                case BacnetBvlcFunctions.BVLC_FORWARDED_NPDU:
                    // BVLC_FORWARDED_NPDU frame by a BBMD, change the remote_address to the original one
                    // stored in the BVLC header, we don't care about the BBMD address
                    var ip = ((long) receiveBuffer[7] << 24) + ((long) receiveBuffer[6] << 16) +
                             ((long) receiveBuffer[5] << 8) + receiveBuffer[4];

                    var port = (receiveBuffer[8] << 8) + receiveBuffer[9]; // 0xbac0 maybe
                    Convert(new IPEndPoint(ip, port), out remoteAddress);
                    break;
            }

            if (function != BacnetBvlcFunctions.BVLC_ORIGINAL_UNICAST_NPDU &&
                function != BacnetBvlcFunctions.BVLC_ORIGINAL_BROADCAST_NPDU &&
                function != BacnetBvlcFunctions.BVLC_FORWARDED_NPDU)
            {
                Log.Debug($"{function} - ignoring");
                return;
            }

            if (receivedLength <= headerLength)
            {
                Log.Warn($"Missing data, only header received: {receiveBufferHex}");
                return;
            }

            InvokeMessageRecieved(receiveBuffer, headerLength, receivedLength - headerLength, remoteAddress);
        }
        catch (ObjectDisposedException)
        {
            Log.Debug("Connection has been disposed");
        }
        catch (Exception e)
        {
            if (connection.Client == null || _disposing)
                return;

            Log.Error("Exception in OnRecieveData", e);
        }
    }

from bacnet.

hodlhero443 avatar hodlhero443 commented on September 25, 2024

I might have found the issue. Disassembling the libbacnet.dll we used showed that the code didn't match what was in 1.1.0 and what I pasted here. We where using 1.0.0, where it actually looks like the the exception could be found without being caught.

if finishes like this:
catch (Exception ex)
{
if (asyncState.Client == null)
return;
Trace.TraceError("Exception in Ip OnRecieveData: " + ex.Message);
asyncState.BeginReceive(new AsyncCallback(this.OnReceiveData), (object) asyncState);
}

from bacnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.