Bevor du dich mit NSConnection rumschlägst und graue Haare bekommst…
Alles anzeigen
Quellcode
- class Connection: NSObject, GCDAsyncSocketDelegate {
- private var asyncSocket: GCDAsyncSocket? = nil
- private let delegateQueue = DispatchQueue(label: "delegateQueue")
- private let timeout: TimeInterval = 40.0
- init() {
- super.init()
- }
- func connect (ipAdress: String, port: Int16) {
- self.asyncSocket = GCDAsyncSocket.init(delegate: self, delegateQueue: self.delegateQueue)
- self.asyncSocket?.isIPv6Enabled = false
- do
- {
- try self.asyncSocket?.connect(toHost: ipAdress, onPort: UInt16(port), withTimeout: 30)
- }
- catch {
- LogInfo("error: \(error)")
- }
- }
- func disconnect() {
- self.asyncSocket?.disconnect()
- self.asyncSocket = nil
- }
- func writeData(data: Data, tag: Int = 10) {
- self.asyncSocket?.write(data, withTimeout: timeout, tag: tag)
- LogInfo("Write")
- }
- // MARK: - GCDAsyncSocketDelegate
- /**
- * Called when a socket connects and is ready for reading and writing.
- * The host parameter will be an IP address, not a DNS name.
- **/
- func socket(_ sock: GCDAsyncSocket, didConnectToHost host: String, port: UInt16) {
- print("connected to host: \(host)")
- self.asyncSocket?.readData(withTimeout: timeout, tag: 1)
- }
- /**
- * Called when a socket has completed reading the requested data into memory.
- * Not called if there is an error.
- **/
- func socket(_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int) {
- LogInfo("Read")
- logHexDumpForBytes(bytes: Array<UInt8>(data))
- self.asyncSocket?.readData(withTimeout: timeout, tag: 1)
- }
- /**
- * Called when a socket has completed writing the requested data. Not called if there is an error.
- **/
- func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
- }
- /**
- * Called if a read operation has reached its timeout without completing.
- * This method allows you to optionally extend the timeout.
- * If you return a positive time interval (> 0) the read's timeout will be extended by the given amount.
- * If you don't implement this method, or return a non-positive time interval (<= 0) the read will timeout as usual.
- *
- * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
- * The length parameter is the number of bytes that have been read so far for the read operation.
- *
- * Note that this method may be called multiple times for a single read if you return positive numbers.
- **/
- func socket(_ sock: GCDAsyncSocket, shouldTimeoutReadWithTag tag: Int, elapsed: TimeInterval, bytesDone length: UInt) -> TimeInterval {
- print("Read timeout")
- return timeout
- }
- /**
- * Called if a write operation has reached its timeout without completing.
- * This method allows you to optionally extend the timeout.
- * If you return a positive time interval (> 0) the write's timeout will be extended by the given amount.
- * If you don't implement this method, or return a non-positive time interval (<= 0) the write will timeout as usual.
- *
- * The elapsed parameter is the sum of the original timeout, plus any additions previously added via this method.
- * The length parameter is the number of bytes that have been written so far for the write operation.
- *
- * Note that this method may be called multiple times for a single write if you return positive numbers.
- **/
- func socket(_ sock: GCDAsyncSocket, shouldTimeoutWriteWithTag tag: Int, elapsed: TimeInterval, bytesDone length: UInt) -> TimeInterval {
- print("Write timeout")
- return timeout
- }
- /**
- * Called when a socket disconnects with or without error.
- *
- * If you call the disconnect method, and the socket wasn't already disconnected,
- * then an invocation of this delegate method will be enqueued on the delegateQueue
- * before the disconnect method returns.
- *
- * Note: If the GCDAsyncSocket instance is deallocated while it is still connected,
- * and the delegate is not also deallocated, then this method will be invoked,
- * but the sock parameter will be nil. (It must necessarily be nil since it is no longer available.)
- * This is a generally rare, but is possible if one writes code like this:
- *
- * asyncSocket = nil; // I'm implicitly disconnecting the socket
- *
- * In this case it may preferrable to nil the delegate beforehand, like this:
- *
- * asyncSocket.delegate = nil; // Don't invoke my delegate method
- * asyncSocket = nil; // I'm implicitly disconnecting the socket
- *
- * Of course, this depends on how your state machine is configured.
- **/
- func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
- LogInfo("disconnected: \(String(describing: err))")
- }
- }
Man macht einfach solange irgendwelche Dinge, bis man tot ist.
Und dann bekommen die anderen Kuchen.
Und dann bekommen die anderen Kuchen.