GCDAsyncSocketDelegate Protocol Reference

Conforms to NSObject
Declared in GCDAsyncSocket.h

– newSocketQueueForConnectionFromAddress:onSocket:

This method is called immediately prior to socket:didAcceptNewSocket:. It optionally allows a listening socket to specify the socketQueue for a new accepted socket. If this method is not implemented, or returns NULL, the new accepted socket will create its own default queue.

- (nullable dispatch_queue_t)newSocketQueueForConnectionFromAddress:(NSData *)address onSocket:(GCDAsyncSocket *)sock

Discussion

This method is called immediately prior to socket:didAcceptNewSocket:. It optionally allows a listening socket to specify the socketQueue for a new accepted socket. If this method is not implemented, or returns NULL, the new accepted socket will create its own default queue.

Since you cannot autorelease a dispatch_queue, this method uses the “new” prefix in its name to specify that the returned queue has been retained.

Thus you could do something like this in the implementation: return dispatch_queue_create(“MyQueue”, NULL);

If you are placing multiple sockets on the same queue, then care should be taken to increment the retain count each time this method is invoked.

For example, your implementation might look something like this: dispatch_retain(myExistingQueue); return myExistingQueue;

Declared In

GCDAsyncSocket.h

– socket:didAcceptNewSocket:

Called when a socket accepts a connection. Another socket is automatically spawned to handle it.

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket

Discussion

Called when a socket accepts a connection. Another socket is automatically spawned to handle it.

You must retain the newSocket if you wish to handle the connection. Otherwise the newSocket instance will be released and the spawned connection will be closed.

By default the new socket will have the same delegate and delegateQueue. You may, of course, change this at any time.

Declared In

GCDAsyncSocket.h

– socket:didConnectToHost:port:

Called when a socket connects and is ready for reading and writing. The host parameter will be an IP address, not a DNS name.

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port

Discussion

Called when a socket connects and is ready for reading and writing. The host parameter will be an IP address, not a DNS name.

Declared In

GCDAsyncSocket.h

– socket:didConnectToUrl:

Called when a socket connects and is ready for reading and writing. The host parameter will be an IP address, not a DNS name.

- (void)socket:(GCDAsyncSocket *)sock didConnectToUrl:(NSURL *)url

Discussion

Called when a socket connects and is ready for reading and writing. The host parameter will be an IP address, not a DNS name.

Declared In

GCDAsyncSocket.h

– socket:didReadData:withTag:

Called when a socket has completed reading the requested data into memory. Not called if there is an error.

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag

Discussion

Called when a socket has completed reading the requested data into memory. Not called if there is an error.

Declared In

GCDAsyncSocket.h

– socket:didReadPartialDataOfLength:tag:

Called when a socket has read in data, but has not yet completed the read. This would occur if using readToData: or readToLength: methods. It may be used to for things such as updating progress bars.

- (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag

Discussion

Called when a socket has read in data, but has not yet completed the read. This would occur if using readToData: or readToLength: methods. It may be used to for things such as updating progress bars.

Declared In

GCDAsyncSocket.h

– socket:didWriteDataWithTag:

Called when a socket has completed writing the requested data. Not called if there is an error.

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag

Discussion

Called when a socket has completed writing the requested data. Not called if there is an error.

Declared In

GCDAsyncSocket.h

– socket:didWritePartialDataOfLength:tag:

Called when a socket has written some data, but has not yet completed the entire write. It may be used to for things such as updating progress bars.

- (void)socket:(GCDAsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag

Discussion

Called when a socket has written some data, but has not yet completed the entire write. It may be used to for things such as updating progress bars.

Declared In

GCDAsyncSocket.h

– socket:shouldTimeoutReadWithTag:elapsed:bytesDone:

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.

- (NSTimeInterval)socket:(GCDAsyncSocket *)sock shouldTimeoutReadWithTag:(long)tag elapsed:(NSTimeInterval)elapsed bytesDone:(NSUInteger)length

Discussion

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.

Declared In

GCDAsyncSocket.h

– socket:shouldTimeoutWriteWithTag:elapsed:bytesDone:

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.

- (NSTimeInterval)socket:(GCDAsyncSocket *)sock shouldTimeoutWriteWithTag:(long)tag elapsed:(NSTimeInterval)elapsed bytesDone:(NSUInteger)length

Discussion

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.

Declared In

GCDAsyncSocket.h

– socketDidCloseReadStream:

Conditionally called if the read stream closes, but the write stream may still be writeable.

- (void)socketDidCloseReadStream:(GCDAsyncSocket *)sock

Discussion

Conditionally called if the read stream closes, but the write stream may still be writeable.

This delegate method is only called if autoDisconnectOnClosedReadStream has been set to NO. See the discussion on the autoDisconnectOnClosedReadStream method for more information.

Declared In

GCDAsyncSocket.h

– socketDidDisconnect:withError:

Called when a socket disconnects with or without error.

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(nullable NSError *)err

Discussion

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.

Declared In

GCDAsyncSocket.h

– socketDidSecure:

Called after the socket has successfully completed SSL/TLS negotiation. This method is not called unless you use the provided startTLS method.

- (void)socketDidSecure:(GCDAsyncSocket *)sock

Discussion

Called after the socket has successfully completed SSL/TLS negotiation. This method is not called unless you use the provided startTLS method.

If a SSL/TLS negotiation fails (invalid certificate, etc) then the socket will immediately close, and the socketDidDisconnect:withError: delegate method will be called with the specific SSL error code.

Declared In

GCDAsyncSocket.h

– socket:didReceiveTrust:completionHandler:

Allows a socket delegate to hook into the TLS handshake and manually validate the peer it’s connecting to.

- (void)socket:(GCDAsyncSocket *)sock didReceiveTrust:(SecTrustRef)trust completionHandler:(void ( ^ ) ( BOOL shouldTrustPeer ))completionHandler

Discussion

Allows a socket delegate to hook into the TLS handshake and manually validate the peer it’s connecting to.

This is only called if startTLS is invoked with options that include: - GCDAsyncSocketManuallyEvaluateTrust == YES

Typically the delegate will use SecTrustEvaluate (and related functions) to properly validate the peer.

Note from Apple’s documentation: Because [SecTrustEvaluate] might look on the network for certificates in the certificate chain, [it] might block while attempting network access. You should never call it from your main thread; call it only from within a function running on a dispatch queue or on a separate thread.

Thus this method uses a completionHandler block rather than a normal return value. The completionHandler block is thread-safe, and may be invoked from a background queue/thread. It is safe to invoke the completionHandler block even if the socket has been closed.

Declared In

GCDAsyncSocket.h