Skip to content

Commit bd1bc68

Browse files
committed
Adding userData to GCDAsyncSocket so that arbitrary information may be associated with the socket.
1 parent 3e4d1e1 commit bd1bc68

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

GCD/GCDAsyncSocket.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
8484
NSMutableData *sslReadBuffer;
8585
size_t sslWriteCachedLength;
8686
#endif
87+
88+
id userData;
8789
}
8890

8991
/**
@@ -170,6 +172,13 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
170172
- (BOOL)isIPv4PreferredOverIPv6;
171173
- (void)setPreferIPv4OverIPv6:(BOOL)flag;
172174

175+
/**
176+
* User data allows you to associate arbitrary information with the socket.
177+
* This data is not used internally by socket in any way.
178+
**/
179+
- (id)userData;
180+
- (void)setUserData:(id)arbitraryUserData;
181+
173182
#pragma mark Accepting
174183

175184
/**

GCD/GCDAsyncSocket.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,35 @@ - (void)setPreferIPv4OverIPv6:(BOOL)flag
11821182
}
11831183
}
11841184

1185+
- (id)userData
1186+
{
1187+
__block id result;
1188+
1189+
dispatch_block_t block = ^{
1190+
1191+
result = [userData retain];
1192+
};
1193+
1194+
if (dispatch_get_current_queue() == socketQueue)
1195+
block();
1196+
else
1197+
dispatch_sync(socketQueue, block);
1198+
1199+
return [result autorelease];
1200+
}
1201+
1202+
- (void)setUserData:(id)arbitraryUserData
1203+
{
1204+
dispatch_async(socketQueue, ^{
1205+
1206+
if (userData != arbitraryUserData)
1207+
{
1208+
[userData release];
1209+
userData = [arbitraryUserData retain];
1210+
}
1211+
});
1212+
}
1213+
11851214
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11861215
#pragma mark Accepting
11871216
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)