Skip to content

InputStream/OutputStream implemented delegate and getBuffer #1667

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

Closed
wants to merge 6 commits into from
Closed

InputStream/OutputStream implemented delegate and getBuffer #1667

wants to merge 6 commits into from

Conversation

amosavian
Copy link
Contributor

This PR implements Stream's missing methods including delegate, scheduleInRunLoop and getBuffer methods.

@spevans
Copy link
Contributor

spevans commented Aug 24, 2018

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor

spevans commented Aug 24, 2018

@swift-ci please test

@amosavian
Copy link
Contributor Author

amosavian commented Aug 24, 2018

@spevans would you mind testing again?

@spevans
Copy link
Contributor

spevans commented Aug 24, 2018

@swift-ci please test

@parkera
Copy link
Contributor

parkera commented Aug 24, 2018

Awesome! Maybe a couple simple smoke tests to verify that it's all hooked up correctly?

@amosavian
Copy link
Contributor Author

@parkera Please find tests. I'm not sure they would work.

@ianpartridge
Copy link
Contributor

@swift-ci please test

@parkera
Copy link
Contributor

parkera commented Aug 27, 2018

Not sure what went wrong there.

@parkera
Copy link
Contributor

parkera commented Aug 27, 2018

@swift-ci clean test

@amosavian
Copy link
Contributor Author

@parkera I've implemented getStreamsToHost(withName:port:inputStream:outputStream:) and getBoundStreams(withBufferSize:inputStream:outputStream:) functions but I did not push them due to a comment about autoreleasing pointers in file. Is this issue fixed? Or should I push their implementation and keep it disable?

@parkera
Copy link
Contributor

parkera commented Aug 28, 2018

Let's keep them disabled for now. The problem is that the "Autoreleasing" style of pointer is unavailable on Linux.

@amosavian
Copy link
Contributor Author

amosavian commented Aug 28, 2018

@parkera Would you mind mentioning CI to test?
Other APIs which had auto releaseing parameters like NSCalendar and Scanner had replaced these functions with other ones, or used UnsafeMutablePointer instead.

@amosavian
Copy link
Contributor Author

I still wonder why ClangImporter does not import Autoreleasing pointers as simple inout variables

@spevans
Copy link
Contributor

spevans commented Aug 28, 2018

@swift-ci test

@parkera
Copy link
Contributor

parkera commented Aug 28, 2018

Autoreleasing has a certain memory management connotation which I don't believe is possible to replicate with just inout.

@amosavian
Copy link
Contributor Author

Apple can change swift foundation overlay in a way that omit that autoreleasepool. Something like this pseudocode:

open class func getStreamsToHost(withName hostname: String, port: Int, inputStream: inout InputStream?, outputStream: inout OutputStream?) {
    @autoreleasepool {
        var _inputStream: AutoreleasingUnsafeMutablePointer<InputStream?>?
        var _outputStream: AutoreleasingUnsafeMutablePointer<InputStream?>?
        self._getStreamsToHost(withName: hostname, port: port, inputStream: _inputStream, outputStream: _outputStream)
        inputStream = _inputStream.pointee.retain()
        outputStreeam = _outputStream.pointee.retain()
    }
}

@amosavian
Copy link
Contributor Author

I don’t understand why test failed

@spevans
Copy link
Contributor

spevans commented Aug 29, 2018

@swift-ci test

@spevans
Copy link
Contributor

spevans commented Aug 29, 2018

Looks like the last test ran really slowly for some reason

@amosavian
Copy link
Contributor Author

It seems XCTestExpectation.init method signature is different in linux and darwin. I’ll fix it.

@parkera
Copy link
Contributor

parkera commented Aug 29, 2018

@amosavian Agreed that we can improve that API. It's on our list.

@amosavian
Copy link
Contributor Author

@spevans Sorry, would you mind re-test again?

@xwu
Copy link
Contributor

xwu commented Sep 2, 2018

@swift-ci please test

@xwu
Copy link
Contributor

xwu commented Sep 2, 2018

Hmm...
@swift-ci please test

@amosavian
Copy link
Contributor Author

@parkera linux’s XCTest is unable to test asynchronous with multiple fulfilling. Would you if I disable tests until xctest team implement missing features?

@amosavian
Copy link
Contributor Author

@spevans would you mind retest. I've disabled delegates' test until linux's XCTest can handle expectations with multiple fulfills. This should also resolve compiling issue for now.

@spevans
Copy link
Contributor

spevans commented Sep 7, 2018

@swift-ci test

@spevans
Copy link
Contributor

spevans commented Sep 7, 2018

@amosavian I ran the test you added on macOS against the native Foundation library and it failed there (it timed out).
Is it possible to rework the test to work correctly on macOS? Also is it possible to come up with an alternative way of testing the delegate without using the wait function so it can be run on Linux? Maybe setting some state and just sleeping for 1 second (I assume on a file stream the delegate should fire straight away)?

@amosavian
Copy link
Contributor Author

@spevans You are right. Indeed delegate handle is never triggered. It works in playground when needsIndefiniteExecution is set to true. I tried to use your approach using sleep or Thread.sleep() with no success. Even I used DispatchQueue and Thread but test is failing.

@amosavian
Copy link
Contributor Author

Changes:

  • Get delegate object from CFStream instead of keeping it in variable
  • Added isEqua()l and hash methods for streams
  • Added test for get/set delegate property
  • Unified schedule/delegate test for input and output stream [disabled with comments]

@amosavian
Copy link
Contributor Author

amosavian commented Sep 11, 2018

@compnerd I've made InputStream bridgable to CFReadStream and OutputStream to CFWriteStream. But stream as CFReadStream emits compile error. Can you help what I am missing?

Implementing _CFSwiftBridge function pointers' section for streams remained todo as I don't know how to implement and which functions should be listed in.

@millenomi
Copy link
Contributor

@amosavian There is a conflict on this patch now that the Mojave merge has landed. Can you fix that?

@amosavian
Copy link
Contributor Author

@millenomi done

@millenomi
Copy link
Contributor

@swift-ci please test

func test_streamDelegate() {
// TODO: macOS/iOS: Fix CFNetwork's dynamic bindings for `callBacks` field.
// TODO: Linux: Enabling delegate/scheduling tests when `CFStreamCreatePairWithSocketToHost()` implemented.
#if false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this point to a specific SR and/or Radar # for follow-up work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is a bug or intended behavior. This code crashed in my Xcode. I guess SwiftFoundation fails to load CFNetwork and fill callback pointers in CFStream socket object. (Regarding SwiftFoundation target is primarily Linux and macOS Foundation works fine.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't cause SCF compiled on macOS to crash, because that's a development environment we would like contributors to use. And we should not be picking anything up from CFNetwork in that case either. :(

So something is amiss.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m novice in C and unable to resolve this (don’t know how to fix dynamic loading), unfortunately. Thus I decided to disable the code and hope an expert would fix it later.
Interestingly, afaik no other member of CFReadStream cluster classes are able to use scheduling and I have to stick to socket.

@shahmishal shahmishal closed this Oct 6, 2020
@shahmishal
Copy link
Member

The Swift project moved the default branch to main and deleted master branch, so GitHub automatically closed the PR. Please re-create pull request with main branch.

More detail about the branch update - https://forums.swift.org/t/updating-branch-names/40412

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants