Skip to content

Unable to detect closed socket #213

@Joebayld

Description

@Joebayld

I'm not reciving zero bytes when the socket loses connection. Could something have changed regarding this?

It used to return bytesRead = 0 when the connection was closed.

            socketQueue.async { [weak self] in
                var readData = Data()
                var shouldKeepRunning = true
                do {
                    repeat {
                        let bytesRead = try socket.read(into: &readData)
                        if bytesRead > 0 {
                            let readString = String(data: readData, encoding: .utf8) ?? ""
                            self?.gotInput(string: readString)
                            readData = Data()
                        } else {
                            shouldKeepRunning = false
                            break
                        }
                    } while shouldKeepRunning

                    // close socket when bytesRead is zero
                    socket.close()
                    self?.connectionClosed()
                } catch {
                    self?.status = .error(message: error.localizedDescription)
                    log.error("Read error: \(error). Read data: \(readData)")
                }
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions