gunterhager / UDPBroadcastConnection

Framework to send UDP broadcast messages and listen to responses using a Dispatch source.
MIT License
84 stars 28 forks source link

Remove Null pointer and deprecated pointer method #24

Closed victis23 closed 3 years ago

victis23 commented 3 years ago

Removed Null pointer made required changes to codebase. This first portion could also just be simplified to

let bitCount = 4096
let response = malloc(4096)
defer { response?.deallocate() }

let UDPSocket = Int32(source.handle)

let bytesRead = withUnsafeMutablePointer(to: &socketAddress) {
                recvfrom(UDPSocket, response, bitCount, 0, UnsafeMutableRawPointer($0).bindMemory(to: sockaddr.self, capacity: 1), &socketAddressLength)
            }

Line 166 must be changed to :

var responseBytes = Data()

                if let response = response {
                    responseBytes = Data(bytes: response, count: bytesRead)
                }

We actually took this approach in our project and it worked fine on iOS14.

gunterhager commented 3 years ago

I'm not 100% sure why you would want to change the [UInt8] array into a malloc memory. I'm not particularly fond of using low level C methods to handle memory when there are perfectly working Swift methods available. Furthermore I simplified and cleaned up the receive handler code a bit and tested it with iOS 14. So please take a look at the latest version and see if it creates any problems for you.