2016-05-26 59 views
1

我試圖實現到C++庫SecureTransport的快速橋接。我'認爲'只要我知道如何在sslReadCallback/sslWriteCallback實現中讀取其中的內容,就可以傳入任何連接。這是我正在與之合作的假設。這似乎是這種情況時,我認爲SSLConnectionRef的定義:如何將我的swift類轉換爲UnsafePointer,如__bridge in objective-c

/* Opaque reference to an I/O connection (socket, endpoint, etc.) */ 
public typealias SSLConnectionRef = UnsafePointer<Void> 

所以我只需要打開我的課變成UnsafePointer。不幸的是編譯器不喜歡我的嘗試。任何人都可以給我提示嗎?

func startSSLProcess() 
{ 
    self.sslContext = SSLCreateContext(kCFAllocatorDefault, SSLProtocolSide.ClientSide, SSLConnectionType.StreamType) 
    if let sslContext = self.sslContext 
    { 
     SSLSetIOFuncs(sslContext, sslReadCallback, sslWriteCallback) 
     SSLSetConnection(sslContext, UnsafePointer(self)) // <-- error 
     SSLSetSessionOption(sslContext, SSLSessionOption.BreakOnClientAuth, true) 
     SSLHandshake(sslContext) 
    } 
} 

在GCDAsyncSocket,它這樣做:

status = SSLSetConnection(sslContext, (__bridge SSLConnectionRef)self); 

SubZeroGCDAsyncSocket *asyncSocket = (__bridge SubZeroGCDAsyncSocket *)connection; 

..解開。這是什麼快速等值?

非常感謝!

回答

0

答案在這裏:所以現在

How to cast self to UnsafeMutablePointer<Void> type in swift

我做到以下幾點:

func startSSLProcess() 
{ 
    self.sslContext = SSLCreateContext(kCFAllocatorDefault, SSLProtocolSide.ClientSide, SSLConnectionType.StreamType) 
    if let sslContext = self.sslContext 
    { 
     SSLSetIOFuncs(sslContext, sslReadCallback, sslWriteCallback) 
     SSLSetConnection(sslContext, UnsafePointer(Unmanaged.passUnretained(self).toOpaque())) 
     SSLSetSessionOption(sslContext, SSLSessionOption.BreakOnClientAuth, true) 
     SSLHandshake(sslContext) 
    } 
} 

我解開這樣的:

let transportWrapper:SecureTransportWrapper = Unmanaged<SecureTransportWrapper>.fromOpaque(COpaquePointer(connection)).takeUnretainedValue() 

用您自己的類型SecureTransportWrapper。

相關問題