Skip to content

Commit 9a66ef0

Browse files
committed
Adding method to XMPPStream: getNumberOfBytesSent:numberOfBytesReceived: (simple atomic operation to fetch 2 related properties at the same time). Also updated XMPPBandwidthMonitor to use the new method.
1 parent cc35901 commit 9a66ef0

File tree

3 files changed

+67
-36
lines changed

3 files changed

+67
-36
lines changed

Core/XMPPStream.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,16 @@ extern const NSTimeInterval XMPPStreamTimeoutNone;
208208
* the count will be the summation of all connections.
209209
*
210210
* The functionality may optionaly be changed to count only the current socket connection.
211-
* See the resetByteCountPerConnection property.
211+
* @see resetByteCountPerConnection
212212
**/
213-
@property (readonly) UInt64 numberOfBytesSent;
214-
@property (readonly) UInt64 numberOfBytesReceived;
213+
@property (readonly) uint64_t numberOfBytesSent;
214+
@property (readonly) uint64_t numberOfBytesReceived;
215+
216+
/**
217+
* Same as the individual properties,
218+
* but provides a way to fetch them in one atomic operation.
219+
**/
220+
- (void)getNumberOfBytesSent:(uint64_t *)bytesSentPtr numberOfBytesReceived:(uint64_t *)bytesReceivedPtr;
215221

216222
/**
217223
* Affects the funtionality of the byte counter.
@@ -320,7 +326,10 @@ extern const NSTimeInterval XMPPStreamTimeoutNone;
320326
* The given address is specified as a sockaddr structure wrapped in a NSData object.
321327
* For example, a NSData object returned from NSNetservice's addresses method.
322328
**/
323-
- (BOOL)connectTo:(XMPPJID *)remoteJID withAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr;
329+
- (BOOL)connectTo:(XMPPJID *)remoteJID
330+
withAddress:(NSData *)remoteAddr
331+
withTimeout:(NSTimeInterval)timeout
332+
error:(NSError **)errPtr;
324333

325334
/**
326335
* Starts a P2P connection with the given accepted socket.

Core/XMPPStream.m

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ @interface XMPPStream ()
9797

9898
GCDAsyncSocket *asyncSocket;
9999

100-
UInt64 numberOfBytesSent;
101-
UInt64 numberOfBytesReceived;
100+
uint64_t numberOfBytesSent;
101+
uint64_t numberOfBytesReceived;
102102

103103
XMPPParser *parser;
104104
NSError *parserError;
@@ -585,40 +585,55 @@ - (void)setKeepAliveWhitespaceCharacter:(char)keepAliveChar
585585
dispatch_async(xmppQueue, block);
586586
}
587587

588-
- (UInt64)numberOfBytesSent
588+
- (uint64_t)numberOfBytesSent
589589
{
590+
__block uint64_t result = 0;
591+
592+
dispatch_block_t block = ^{
593+
result = numberOfBytesSent;
594+
};
595+
590596
if (dispatch_get_specific(xmppQueueTag))
591-
{
592-
return numberOfBytesSent;
593-
}
597+
block();
594598
else
595-
{
596-
__block UInt64 result;
597-
598-
dispatch_sync(xmppQueue, ^{
599-
result = numberOfBytesSent;
600-
});
601-
602-
return result;
603-
}
599+
dispatch_sync(xmppQueue, block);
600+
601+
return result;
604602
}
605603

606-
- (UInt64)numberOfBytesReceived
604+
- (uint64_t)numberOfBytesReceived
607605
{
606+
__block uint64_t result = 0;
607+
608+
dispatch_block_t block = ^{
609+
result = numberOfBytesReceived;
610+
};
611+
608612
if (dispatch_get_specific(xmppQueueTag))
609-
{
610-
return numberOfBytesReceived;
611-
}
613+
block();
612614
else
613-
{
614-
__block UInt64 result;
615-
616-
dispatch_sync(xmppQueue, ^{
617-
result = numberOfBytesReceived;
618-
});
619-
620-
return result;
621-
}
615+
dispatch_sync(xmppQueue, block);
616+
617+
return result;
618+
}
619+
620+
- (void)getNumberOfBytesSent:(uint64_t *)bytesSentPtr numberOfBytesReceived:(uint64_t *)bytesReceivedPtr
621+
{
622+
__block uint64_t bytesSent = 0;
623+
__block uint64_t bytesReceived = 0;
624+
625+
dispatch_block_t block = ^{
626+
bytesSent = numberOfBytesSent;
627+
bytesReceived = numberOfBytesReceived;
628+
};
629+
630+
if (dispatch_get_specific(xmppQueueTag))
631+
block();
632+
else
633+
dispatch_sync(xmppQueue, block);
634+
635+
if (bytesSentPtr) *bytesSentPtr = bytesSent;
636+
if (bytesReceivedPtr) *bytesReceivedPtr = bytesReceived;
622637
}
623638

624639
- (BOOL)resetByteCountPerConnection

Extensions/BandwidthMonitor/XMPPBandwidthMonitor.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ - (double)incomingBandwidth
5959

6060
- (void)updateBandwidth
6161
{
62-
uint64_t currentNumberOfBytesSent = xmppStream.numberOfBytesSent;
63-
uint64_t currentNumberOfBytesReceived = xmppStream.numberOfBytesReceived;
62+
uint64_t currentNumberOfBytesSent = 0;
63+
uint64_t currentNumberOfBytesReceived = 0;
64+
65+
[xmppStream getNumberOfBytesSent:&currentNumberOfBytesSent numberOfBytesReceived:&currentNumberOfBytesReceived];
6466

6567
double currentOutgoingBandwidth = currentNumberOfBytesSent - lastNumberOfBytesSent; // Bytes per second
6668
double currentIncomingBandwidth = currentNumberOfBytesReceived - lastNumberOfBytesReceived; // Bytes per second
@@ -97,8 +99,13 @@ - (void)startTimer
9799
{
98100
if (timer == NULL)
99101
{
100-
lastNumberOfBytesSent = xmppStream.numberOfBytesSent;
101-
lastNumberOfBytesReceived = xmppStream.numberOfBytesReceived;
102+
uint64_t numberOfBytesSent = 0;
103+
uint64_t numberOfBytesReceived = 0;
104+
105+
[xmppStream getNumberOfBytesSent:&numberOfBytesSent numberOfBytesReceived:&numberOfBytesReceived];
106+
107+
lastNumberOfBytesSent = numberOfBytesSent;
108+
lastNumberOfBytesReceived = numberOfBytesReceived;
102109

103110
smoothedAverageOutgoingBandwidth = 0.0;
104111
smoothedAverageIncomingBandwidth = 0.0;

0 commit comments

Comments
 (0)