ZitiConnection
@objc
public class ZitiConnection : NSObject, ZitiUnretained
Create ZitiConnection
s from an instance of Ziti
and use to commuicate with services over a Ziti network
-
Connection callback
This callback is invoked after
dial(_:_:_:)
oraccept(_:_:)
is completed. The result of the function may be an error condition so it is important to verify the status code in this callback. If successful the status will be set toZITI_OK
Declaration
Swift
public typealias ConnCallback = (_ conn: ZitiConnection, _ status: Int32) -> Void
Parameters
conn
reference to
ZitiConnection
status
ZITI_OK
on success, else Ziti error code -
Listen callback
This callback is invoked after
listen(_:_:_:)
and is aliased toConnCallback
as a convenience for human readabilityDeclaration
Swift
public typealias ListenCallback = ConnCallback
-
Data callback
This callback is invoked when data arrives from
Ziti
, either as a response fromdial(_:_:_:)
or fromaccept(_:_:)
Return value should indicate how much data was consumed by the application. This callback will be called again at some later time and as many times as needed for application to accept the all of the dataDeclaration
Swift
public typealias DataCallback = (_ conn: ZitiConnection, _ data: Data?, _ status: Int) -> Int
Parameters
conn
the connection
data
the incoming data
status
size of the data or a Ziti error code,
ZITI_EOF
when connection has closed -
Client callback
This callback is invoked when a client connects to the service specified in
listen(_:_:_:)
call. The result of the status may be an error condition so it is important to verify the status code in this callback. If successful the status will be set toZITI_OK
Generally this callback is used for any preparations necessary before accepting incoming data from the Ziti network.
Declaration
Swift
public typealias ClientCallback = (_ server: ZitiConnection, _ client: ZitiConnection, _ status: Int32) -> Void
Parameters
server
server connection
client
client connection, generally used to
accept()
the connection in this callbackstatus
ZITI_OK
or error code -
Write callback
This callback is invoked after a call the
write(_:_:)
completes. The result of thewrite(_:_:)
may be an error condition so it is important to verify the provided status code in this callback.This callback is often used to free or reinitialize the buffer associated with the
write(_:_:)
. It is important to not free this memory until after data has been written to the wire else the results of the write operation may be unexpected.Declaration
Swift
public typealias WriteCallback = (_ conn: ZitiConnection, _ status: Int) -> Void
Parameters
status
amount of data written or Ziti error code
-
Close callback
This callback is invoked after a call the
close(_:)
completes.Declaration
Swift
public typealias CloseCallback = () -> Void
Parameters
status
amount of data written or Ziti error code
-
Established a connection to a
Ziti
service.Before any bytes can be sent over a
Ziti
service a connection must be established. This method will attempt to establish a connection by dialiing the service with the given name. The result of thedial()
attempt is included in the specifiedConnCallback
.If the
dial
succeeds, the providedDataCallback
is invoked to handle data returned from the service. If thedial
fails, only theConnCallback
will be invoked with the corresponding error codeDeclaration
Swift
@objc public func dial(_ service: String, _ onConn: @escaping ConnCallback, _ onData: @escaping DataCallback)
Parameters
service
name of the service to dial
onConn
callback invoked after
dial
attempt completesonData
callback invoked after a successful
dial
attempt with data received over the connection -
Start accepting
Ziti
client connectionsThis function is invoked to tell the
Ziti
to accept connections from otherZiti
clients for the provided service name.Declaration
Swift
@objc public func listen(_ service: String, _ onListen: @escaping ListenCallback, _ onClient: @escaping ClientCallback)
Parameters
service
name of the service to be hosted
onListen
callback invoked indicating success or failure of
listen
attemptonClient
callback invoked when client attempts to dial this service
-
Completes a client connection
After a client connects to a hosted Ziti service this method is invoked to finish connection establishment, establishing the callbacks necessary to send data to the connecting client or to process data sent by the client.
Declaration
Swift
@objc public func accept(_ onConn: @escaping ConnCallback, _ onData: @escaping DataCallback)
Parameters
onConn
invoked when
accept
completes indicating status of the attemptonData
invoked each time the client sends data
-
Get the identity of the client that initiated the Ziti connection
Declaration
Swift
@objc public func getSourceIdentity() -> String
Return Value
Source ID or empty String
-
Send data to the connection peer
Declaration
Swift
@objc public func write(_ data: Data, _ onWrite: @escaping WriteCallback)
Parameters
data
the data to send
onWrite
callback invoked after the
write
completes -
Close this connection
When no longer needed the connection should be closed to gracefully disconnect. This method should be invoked after any status is returned which indicates an error situation.
Declaration
Swift
@objc public func close(_ onClose: CloseCallback? = nil)
Parameters
onClose
called when connection is completely closed