sockets - How to use CFStreamCreatePairWithSocket using CFSocketNativeHandle -
i trying write socket xmpp, after connection established socket should reestablish connection updated tls settings. problem delegate methods stream.event.opencompleted called on both outputstream , inputstream stream.event.hasspaceavailable not getting called after cfstreamcreatepairwithsocket.
code:-
if tls{ var readstream :unmanaged<cfreadstream>? var writestream:unmanaged<cfwritestream>? reset() print("establish tls ") tlsenabled = true let socketdata = cfwritestreamcopyproperty(self.outputstream cfwritestream, cfstreampropertykey.socketnativehandle) as! cfdata let native = cfsocketnativehandle(cfdatagetbyteptr(socketdata).pointee) inputstream.close() outputstream.close() inputstream.remove(from: runloop.current, formode: .defaultrunloopmode) outputstream.remove(from: runloop.current, formode: .defaultrunloopmode) inputstream = nil outputstream = nil cfstreamcreatepairwithsocket(kcfallocatordefault, native, &readstream, &writestream) instream = readstream!.takeretainedvalue() outstream = writestream!.takeretainedvalue() instream.delegate = self outstream.delegate = self instream.setproperty(kcfbooleantrue, forkey: kcfstreampropertyshouldclosenativesocket stream.propertykey) outstream.setproperty(kcfbooleantrue, forkey: kcfstreampropertyshouldclosenativesocket stream.propertykey) instream.setproperty(streamsocketsecuritylevel.negotiatedssl anyobject, forkey: stream.propertykey.socketsecuritylevelkey) outstream.setproperty(streamsocketsecuritylevel.negotiatedssl anyobject, forkey: stream.propertykey.socketsecuritylevelkey) let settings: [nsobject: nsobject] = [kcfstreamsslvalidatescertificatechain: nsnumber(value: false), kcfstreamsslpeername: kcfnull] instream.setproperty(settings, forkey: kcfstreampropertysslsettings stream.propertykey) outstream.setproperty(settings, forkey: kcfstreampropertysslsettings stream.propertykey) instream.schedule(in: runloop.current, formode:.defaultrunloopmode) outstream.schedule(in:runloop.current, formode:.defaultrunloopmode) instream.open() outstream.open()
}
the issue seems creating socket using cfsocket instead of creating native socket handle directly.
modifying code create socket directly using socket(_:_:_)
instead of using cfsocket , cfsocketnativehandle results in stream delegate methods being called me.
/** create sockaddr_in (ipv4) address google.com note: if need resolve dns hostname, use cfhost */ var remoteaddress = sockaddr_in() inet_pton(pf_inet, "207.47.131.217".cstring(using: .utf8), &sockaddress.sin_addr) sockaddress.sin_port = uint16(443).bigendian sockaddress.sin_family = sa_family_t(af_inet) // create native socket handle, if sockhandle < 0, error occurred, (check errno) let sockhandle = socket(pf_inet, sock_stream, ipproto_tcp) // if connectstatus < 0, error occurred (check errno) let connectstatus = connect(sockhandle, &remoteaddress, socklen_t(memorylayout<sockaddr_in>.size)) var readstream :unmanaged<cfreadstream>? var writestream:unmanaged<cfwritestream>? cfstreamcreatepairwithsocket(kcfallocatordefault, sockhandle, &readstream, &writestream)
Comments
Post a Comment