pointers - Swift2, Call swift function in CFSocketCallBack - EXEC_BAD_ACCESS -
i'm trying write socket server app mac osx xcode:7.2.1 in swift2.1.1. referring cocoaecho sample code. cannot call swift function in socketcallback function.
code here. i'm passing self based on answer @ swift 2 - unsafemutablepointer object. , think part of code working ok.
class myserver: nsobject { // sockets private var socketipv4: cfsocket! private var socketipv6: cfsocket! // connections var connections = set<spfconnection>() func start(address: string) -> bool { var sockctxt = cfsocketcontext(version: 0, info: nil, retain: nil, release: nil, copydescription: nil) sockctxt.info = unsafemutablepointer(unsafeaddressof(self)) // create socket cfsocketcreate socketipv4 = cfsocketcreate( kcfallocatordefault, pf_inet, sock_stream, ipproto_tcp, kcfsocketautomaticallyreenableacceptcallback, socketcallback, &sockctxt) // ipv4 var sin = sockaddr_in() // = initstruct() let server_addr_size = socklen_t(inet_addrstrlen) sin.sin_len = uint8(server_addr_size) sin.sin_family = sa_family_t(af_inet) sin.sin_port = uint16(9999).bigendian sin.sin_addr.s_addr = inet_addr(address) let sindata = nsdata(bytes: &sin, length: sizeof(sockaddr_in)) let ptr = unsafepointer<uint8>(sindata.bytes) let sincfd = cfdatacreate(kcfallocatordefault, ptr, sizeof(sockaddr_in)) let ipv4socketerror: cfsocketerror = cfsocketsetaddress(socketipv4, sincfd) switch ipv4socketerror { case .success: print("ipv4 success") default: print("ipv4 error = \(ipv4socketerror.rawvalue)") return false } let socketsource = cfsocketcreaterunloopsource(kcfallocatordefault, socketipv4, 0) cfrunloopaddsource(cfrunloopgetcurrent(), socketsource, kcfrunloopdefaultmode) return true } // cfsocket call var socketcallback : @convention(c)(cfsocket!, cfsocketcallbacktype, cfdata!, unsafepointer<void>, unsafemutablepointer<void>) -> void = { (socketref, callbacktype, address, data, info) in print("acceptconnection callback-ed") // \(socketref), \(callbacktype), \(address), \(data),\(info)") var tempdata: cfsocketnativehandle = 0 var annsdata:nsdata = nsdata(bytes: data, length: sizeofvalue(data)) annsdata.getbytes(&tempdata, length: sizeof(cfsocketnativehandle)) var tempary = [unsafemutablepointer<void>]() tempary.append(info) if callbacktype == cfsocketcallbacktype.acceptcallback { let server = unsafebitcast(info, myserver.self) // **** exec_bad_access, code=2 ***** // server.acceptconnection(tempdata) } else { print("callbacktype = \(callbacktype.rawvalue)") } } func acceptconnection(data: cfsocketnativehandle) { print("acceptconnection called") var readstream: unmanaged<cfreadstream>? var writestream: unmanaged<cfwritestream>? cfstreamcreatepairwithsocket(kcfallocatordefault, data, &readstream, &writestream) if readstream != nil && writestream != nil { cfreadstreamsetproperty(readstream!.takeunretainedvalue(), kcfstreampropertyshouldclosenativesocket, kcfbooleantrue) cfwritestreamsetproperty(writestream!.takeunretainedvalue(), kcfstreampropertyshouldclosenativesocket, kcfbooleantrue) let connection = spfconnection() connection.inputstream = readstream!.takeretainedvalue() connection.outputstream = writestream!.takeretainedvalue() if connection.open() { connections.insert(connection) } } } }
i'm getting exec_bad_access code=2 @ code server.acceptconnection(tempdata).
debugger shows same pointer both info , server, should mean info(self) assigned server. exec_bad_access seems mean self no longer available. i'm struggling find solution. if give me advise, it'd appreciated. in advance help.
i'll admit don't know using these lower level c conventions, since callback function part of class, can't self.acceptconnection(tempdata)?
Comments
Post a Comment