Page 1 of 1

Reading data received via UDP

Posted: Fri Oct 13, 2023 6:32 pm
by bsilver
I think I'm missing something really BASIC in trying to read a UDP message after a network event.

The loop that is supposed to be handling this:

Code: Select all

        
        ; Check for a UDP server event
        LockMutex(mutNetUDPReference)
        intEventNet = NetworkServerEvent(structNetUDPListener\intConnectionID)
        intConnectionID = structNetUDPListener\intConnectionID
        UnlockMutex(mutNetUDPReference)
        If intEventNet = #PB_NetworkEvent_Data
          
          Repeat
            NetBuffer = AllocateMemory(#ConstantIncomingNetBufferSize)
            intNetReadLen = ReceiveNetworkData(intConnectionID,
                                               NetBuffer,
                                               #ConstantIncomingNetBufferSize) ;; THIS IS THE LINE THAT APPEARS TO ERROR
            strNetMessageReceived = strNetMessageReceived + PeekS(NetBuffer, 
                                                                  -1, 
                                                                  #PB_UTF8)
            FreeMemory(NetBuffer)
          Until intNetReadLen <> #ConstantIncomingNetBufferSize
          
          ;; DEBUG for now send this to debug
          If CheckTreeEntries(intActiveMenuItem) = #TreeDebugWindow
            AddGadgetItem(String_Debug_Receive_Box, 
                          -1, 
                          RTrim(strNetMessageReceived, #LF$))
            strNetMessageReceived = ""
          EndIf
        EndIf
        
It doesn't crash right away; it seems to happen when I suspect the UDP packet comes in and it does try to read the message from the buffer. I marked in the source where my trace shows the error (it's translating to an "Abort signal", error code 6, and I don't know if by that line number it means that parameter is where it is dying or the ReceiveNetworkData() call is the crash and it just flagged it at the last parameter.

Can someone offer some insight to what's going on here?

Re: Reading data received via UDP

Posted: Fri Oct 13, 2023 7:42 pm
by HeX0R
NetworkServerEvent() uses the Servers ID, whereas ReceiveNetworkData() needs the CLIENTS ID (from EventClient()).
It's also not very good to allocate and free a memory buffer all the time, you might have to rethink about that approach.

Re: Reading data received via UDP

Posted: Fri Oct 13, 2023 7:59 pm
by bsilver
That seems to be the fix! Thanks, HeX0R! I replaced intConnectionID in ReceiveNetworkData() with EventID() as the parameter and I started getting the UDP broadcast message!

Regarding the buffer part, could you elaborate on that? I was freeing it to prevent having garbage data being re-read or wasting memory, but you're saying this could cause problems?

Re: Reading data received via UDP

Posted: Fri Oct 13, 2023 8:08 pm
by HeX0R
Not problems, but it is ineffective.
You are always allocating the very same size, so why not do it once outside the loop and free it at the end?
Then get the string by its received length, via:

Code: Select all

PeekS(NetBuffer, intNetReadLen , #PB_UTF8 | #PB_ByteLength)