Skip to content

Commit 2308ff0

Browse files
committed
Adding ability to specify whitespace keep-alive character
1 parent 178e23b commit 2308ff0

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

Core/XMPPStream.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
7575
NSTimeInterval keepAliveInterval;
7676
dispatch_source_t keepAliveTimer;
7777
NSTimeInterval lastSendReceiveTime;
78+
NSData *keepAliveData;
7879

7980
DDList *registeredModules;
8081
NSMutableDictionary *autoDelegateDict;
@@ -211,6 +212,17 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
211212
**/
212213
@property (readwrite, assign) NSTimeInterval keepAliveInterval;
213214

215+
/**
216+
* The keep-alive mechanism sends whitespace which is ignored by the xmpp protocol.
217+
* The default whitespace character is a space (' ').
218+
*
219+
* This can be changed, for whatever reason, to another whitespace character.
220+
* Valid whitespace characters are space(' '), tab('\t') and newline('\n').
221+
*
222+
* If you attempt to set the character to any non-whitespace character, the attempt is ignored.
223+
**/
224+
@property (readwrite, assign) char keepAliveWhitespaceCharacter;
225+
214226
/**
215227
* Represents the last sent presence element concerning the presence of myJID on the server.
216228
* In other words, it represents the presence as others see us.

Core/XMPPStream.m

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ - (void)commonInit
115115

116116
hostPort = 5222;
117117
keepAliveInterval = DEFAULT_KEEPALIVE_INTERVAL;
118+
keepAliveData = [@" " dataUsingEncoding:NSUTF8StringEncoding];
118119

119120
registeredModules = [[DDList alloc] init];
120121
autoDelegateDict = [[NSMutableDictionary alloc] init];
@@ -403,20 +404,18 @@ - (XMPPPresence *)myPresence
403404

404405
- (NSTimeInterval)keepAliveInterval
405406
{
407+
__block NSTimeInterval result = 0.0;
408+
409+
dispatch_block_t block = ^{
410+
result = keepAliveInterval;
411+
};
412+
406413
if (dispatch_get_current_queue() == xmppQueue)
407-
{
408-
return keepAliveInterval;
409-
}
414+
block();
410415
else
411-
{
412-
__block NSTimeInterval result;
413-
414-
dispatch_sync(xmppQueue, ^{
415-
result = keepAliveInterval;
416-
});
417-
418-
return result;
419-
}
416+
dispatch_sync(xmppQueue, block);
417+
418+
return result;
420419
}
421420

422421
- (void)setKeepAliveInterval:(NSTimeInterval)interval
@@ -440,6 +439,43 @@ - (void)setKeepAliveInterval:(NSTimeInterval)interval
440439
dispatch_async(xmppQueue, block);
441440
}
442441

442+
- (char)keepAliveWhitespaceCharacter
443+
{
444+
__block char keepAliveChar = ' ';
445+
446+
dispatch_block_t block = ^{
447+
448+
NSString *keepAliveString = [[NSString alloc] initWithData:keepAliveData encoding:NSUTF8StringEncoding];
449+
if ([keepAliveString length] > 0)
450+
{
451+
keepAliveChar = (char)[keepAliveString characterAtIndex:0];
452+
}
453+
};
454+
455+
if (dispatch_get_current_queue() == xmppQueue)
456+
block();
457+
else
458+
dispatch_sync(xmppQueue, block);
459+
460+
return keepAliveChar;
461+
}
462+
463+
- (void)setKeepAliveWhitespaceCharacter:(char)keepAliveChar
464+
{
465+
dispatch_block_t block = ^{
466+
467+
if (keepAliveChar == ' ' || keepAliveChar == '\n' || keepAliveChar == '\t')
468+
{
469+
keepAliveData = [[NSString stringWithFormat:@"%c", keepAliveChar] dataUsingEncoding:NSUTF8StringEncoding];
470+
}
471+
};
472+
473+
if (dispatch_get_current_queue() == xmppQueue)
474+
block();
475+
else
476+
dispatch_async(xmppQueue, block);
477+
}
478+
443479
- (UInt64)numberOfBytesSent
444480
{
445481
if (dispatch_get_current_queue() == xmppQueue)
@@ -3349,11 +3385,9 @@ - (void)keepAlive
33493385

33503386
if (elapsed < 0 || elapsed >= keepAliveInterval)
33513387
{
3352-
NSData *outgoingData = [@" " dataUsingEncoding:NSUTF8StringEncoding];
3388+
numberOfBytesSent += [keepAliveData length];
33533389

3354-
numberOfBytesSent += [outgoingData length];
3355-
3356-
[asyncSocket writeData:outgoingData
3390+
[asyncSocket writeData:keepAliveData
33573391
withTimeout:TIMEOUT_XMPP_WRITE
33583392
tag:TAG_XMPP_WRITE_STREAM];
33593393

0 commit comments

Comments
 (0)