Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Data Stream example #3134

Closed
jasperaelvoet opened this issue Dec 21, 2024 · 4 comments
Closed

iOS Data Stream example #3134

jasperaelvoet opened this issue Dec 21, 2024 · 4 comments
Labels
needs-triage Issue still needs to be assigned, labeled and deduplicated type: question General questions (we may not have time to provide an answer)

Comments

@jasperaelvoet
Copy link

Hi, I am trying to create two data streams between two iOS devices. However, I have unable to get it to work, and there seems to be no clear example in the documentation.

Here is how I create the Stream objects (this runs when a connection has been successfully established):

var inputStream: InputStream?
var outputStream: OutputStream?

Stream
  .getBoundStreams(
    withBufferSize: 1024,
    inputStream: &inputStream,
    outputStream: &outputStream)

guard let inputStream = inputStream, let outputStream = outputStream
else
{
  print("Failed to create input/output streams")
  return
}

nearbyConnectionManager
  .startStream(
    inputStream,
    to: [endpointID])

self.connectedPeers[connectedIndex].audioStream = outputStream
self.connectedPeers[connectedIndex].audioStream!.schedule(in: .main, forMode: .default)
self.connectedPeers[connectedIndex].audioStream!.open()

And then the receiving of the streams:

public func connectionManager(
  _ connectionManager: ConnectionManager, didReceive stream: InputStream,
  withID payloadID: PayloadID, from endpointID: EndpointID,
  cancellationToken token: CancellationToken)
{
  print("Received stream from \(endpointID)")

  stream.open()
  stream.delegate = self
  stream.schedule(in: .current, forMode: .default)
}

For testing purposes, I send 10 bytes 4x per second.

On the receiving end, I get this error:

Received stream from ZCUF
TRANSFER UPDATE: progress(<NSProgress: 0x303c06400> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 10 of -1  )
E0000 00:00:1734809808.195350 3542100 payload_manager.cc:1424] ProcessDataPacket: [data: error] endpoint_id=ZCUF; payload_id=3260928757190108707
I0000 00:00:1734809808.195654 3542213 client_proxy.cc:1061] ClientProxy [reporting onPayloadProgress]: client=11369573624546782; endpoint_id=ZCUF; payload_id=3260928757190108707, payload_status=Failure
TRANSFER UPDATE: failure

I tried modifying feature flags to see if that would resolve the issue, but was unsuccessful.

Would be great if someone could share a working example!

Thanks in advance

@jasperaelvoet jasperaelvoet added needs-triage Issue still needs to be assigned, labeled and deduplicated type: question General questions (we may not have time to provide an answer) labels Dec 21, 2024
@jasperaelvoet
Copy link
Author

So, not sure what I changed but receiving a stream seems to be working now.

 public func connectionManager(
    _ connectionManager: ConnectionManager, didReceive stream: InputStream,
    withID payloadID: PayloadID, from endpointID: EndpointID,
    cancellationToken token: CancellationToken)
  {
    print("Received stream from \(endpointID)")

    self.stream = stream

    let delegate = MPStreamDelegates()
    streamDelegate = delegate

    stream.delegate = delegate

    if stream.delegate === delegate
    {
      print("Delegate assigned successfully.")
    }
    else
    {
      print("Delegate assignment failed.")
    }

    stream.schedule(in: .main, forMode: .default)
    stream.open()
}

class MPStreamDelegates: NSObject, StreamDelegate {
  public func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
    print("Stream event: \(eventCode)")

    switch eventCode {
    case .endEncountered:
      print("endEncountered")
    case .hasBytesAvailable:
      if let inputStream = aStream as? InputStream {
        let bufferSize = 512
        var buffer = [UInt8](repeating: 0, count: bufferSize)
        let bytesRead = inputStream.read(&buffer, maxLength: bufferSize)

        if bytesRead > 0 {
          print("read \(bytesRead)")
        } else {
          print("No new data available or end of stream.")
          break
        }
      }
    case .endEncountered:
      print("Stream ended")
      aStream.close()
      aStream.remove(from: .current, forMode: .default)
    case .errorOccurred:
      print("Stream error: \(aStream.streamError?.localizedDescription ?? "Unknown error")")
    case .openCompleted:
      print("Stream opened")
    case .hasSpaceAvailable:
      print("Stream has space available")
    default:
      print("Other stream event")
    }
  }
}

logs:

Received stream from UCIG
Delegate assigned successfully.
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 2 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 4 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 6 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 8 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 10 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 12 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 14 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 16 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 18 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 20 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 22 of -1  )
TRANSFER UPDATE: progress(<NSProgress: 0x300436f80> : Parent: 0x0 (portion: 0) / Fraction completed: 0.0000 / Completed: 24 of -1  )

However, I have been unsuccessful actually reading the stream. Not sure what's going on here, on the Multipeer Connectivity framework this stream delegate worked fine.

@jasperaelvoet
Copy link
Author

So, I was able to fix it. Instead of scheduling the stream when receiving it just open it. and then when receiving a progress update of the stream, manually read it.

@jasperaelvoet
Copy link
Author

jasperaelvoet commented Jan 2, 2025

So, turns it isn't fixed, it is now just reading all the data in the beginning of the stream each time. Instead of actually advancing, through the stream. I also see the memory usage continuously increasing over time.

@jasperaelvoet jasperaelvoet reopened this Jan 2, 2025
@jasperaelvoet
Copy link
Author

jasperaelvoet commented Jan 3, 2025

Okay, so I fixed the repeating data part, but there is still the issue where the memory continues to increase. This is probably related to #2147

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Issue still needs to be assigned, labeled and deduplicated type: question General questions (we may not have time to provide an answer)
Projects
None yet
Development

No branches or pull requests

1 participant