Skip to content

Commit fba2aa2

Browse files
committed
Optimizing ssl prebuffer
1 parent 38dea62 commit fba2aa2

File tree

1 file changed

+45
-53
lines changed

1 file changed

+45
-53
lines changed

GCD/GCDAsyncSocket.m

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,7 +2628,8 @@ - (void)closeWithError:(NSError *)error
26282628
#endif
26292629
#if SECURE_TRANSPORT_MAYBE_AVAILABLE
26302630
{
2631-
[sslReadBuffer setLength:0];
2631+
[sslPreBuffer reset];
2632+
26322633
if (sslContext)
26332634
{
26342635
// Getting a linker error here about the SSLx() functions?
@@ -4051,15 +4052,15 @@ - (void)flushSSLBuffers
40514052

40524053
// Figure out if there is any data available to be read
40534054
//
4054-
// socketFDBytesAvailable <- Number of encrypted bytes we haven't read from the bsd socket
4055-
// [sslReadBuffer length] <- Number of encrypted bytes we've buffered from bsd socket
4056-
// sslInternalBufSize <- Number of decrypted bytes SecureTransport has buffered
4055+
// socketFDBytesAvailable <- Number of encrypted bytes we haven't read from the bsd socket
4056+
// [sslPreBuffer availableBytes] <- Number of encrypted bytes we've buffered from bsd socket
4057+
// sslInternalBufSize <- Number of decrypted bytes SecureTransport has buffered
40574058
//
40584059
// We call the variable "estimated" because we don't know how many decrypted bytes we'll get
4059-
// from the encrypted bytes in the sslReadBuffer.
4060+
// from the encrypted bytes in the sslPreBuffer.
40604061
// However, we do know this is an upper bound on the estimation.
40614062

4062-
estimatedBytesAvailable = socketFDBytesAvailable + [sslReadBuffer length];
4063+
estimatedBytesAvailable = socketFDBytesAvailable + [sslPreBuffer availableBytes];
40634064

40644065
size_t sslInternalBufSize = 0;
40654066
SSLGetBufferedReadSize(sslContext, &sslInternalBufSize);
@@ -4095,7 +4096,7 @@ - (void)flushSSLBuffers
40954096
[preBuffer didWrite:bytesRead];
40964097
}
40974098

4098-
LogVerbose(@"%@ - prebuffer.length = %lu", THIS_METHOD, (unsigned long)[ringBuffer availableBytes]);
4099+
LogVerbose(@"%@ - prebuffer.length = %zu", THIS_METHOD, [preBuffer availableBytes]);
40994100

41004101
if (result != noErr)
41014102
{
@@ -4202,9 +4203,9 @@ - (void)doReadData
42024203
// This has to do with the encypted packets that are coming across the TCP stream.
42034204
// But it's non-optimal to do a bunch of small reads from the BSD socket.
42044205
// So our SSLReadFunction reads all available data from the socket (optimizing the sys call)
4205-
// and may store excess in the sslReadBuffer.
4206+
// and may store excess in the sslPreBuffer.
42064207

4207-
estimatedBytesAvailable += [sslReadBuffer length];
4208+
estimatedBytesAvailable += [sslPreBuffer availableBytes];
42084209

42094210
// The second buffer is within SecureTransport.
42104211
// As mentioned earlier, there are encrypted packets coming across the TCP stream.
@@ -4327,7 +4328,7 @@ - (void)doReadData
43274328
// Remove the copied bytes from the preBuffer
43284329
[preBuffer didRead:bytesToCopy];
43294330

4330-
LogVerbose(@"copied(%lu) preBufferLength(%lu)", bytesToCopy, [preBuffer availableBytes]);
4331+
LogVerbose(@"copied(%lu) preBufferLength(%zu)", (unsigned long)bytesToCopy, [preBuffer availableBytes]);
43314332

43324333
// Update totals
43334334

@@ -4607,10 +4608,12 @@ - (void)doReadData
46074608
// We just read a big chunk of data into the preBuffer
46084609

46094610
[preBuffer didWrite:bytesRead];
4611+
LogVerbose(@"read data into preBuffer - preBuffer.length = %zu", [preBuffer availableBytes]);
46104612

46114613
// Search for the terminating sequence
46124614

46134615
bytesToRead = [currentRead readLengthForTermWithPreBuffer:preBuffer found:&done];
4616+
LogVerbose(@"copying %lu bytes from preBuffer", (unsigned long)bytesToRead);
46144617

46154618
// Ensure there's room on the read packet's buffer
46164619

@@ -4625,6 +4628,7 @@ - (void)doReadData
46254628

46264629
// Remove the copied bytes from the prebuffer
46274630
[preBuffer didRead:bytesToRead];
4631+
LogVerbose(@"preBuffer.length = %zu", [preBuffer availableBytes]);
46284632

46294633
// Update totals
46304634
currentRead->bytesDone += bytesToRead;
@@ -4659,12 +4663,14 @@ - (void)doReadData
46594663

46604664
// Copy excess data into preBuffer
46614665

4666+
LogVerbose(@"copying %ld overflow bytes into preBuffer", (long)overflow);
46624667
[preBuffer ensureCapacityForWrite:overflow];
46634668

46644669
uint8_t *overflowBuffer = buffer + underflow;
46654670
memcpy([preBuffer writeBuffer], overflowBuffer, overflow);
46664671

46674672
[preBuffer didWrite:overflow];
4673+
LogVerbose(@"preBuffer.length = %zu", [preBuffer availableBytes]);
46684674

46694675
// Note: The completeCurrentRead method will trim the buffer for us.
46704676

@@ -5794,7 +5800,7 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
57945800
{
57955801
LogVerbose(@"sslReadWithBuffer:%p length:%lu", buffer, (unsigned long)*bufferLength);
57965802

5797-
if ((socketFDBytesAvailable == 0) && ([sslReadBuffer length] == 0))
5803+
if ((socketFDBytesAvailable == 0) && ([sslPreBuffer availableBytes] == 0))
57985804
{
57995805
LogVerbose(@"%@ - No data available to read...", THIS_METHOD);
58005806

@@ -5819,25 +5825,24 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
58195825
// STEP 1 : READ FROM SSL PRE BUFFER
58205826
//
58215827

5822-
NSUInteger sslReadBufferLength = [sslReadBuffer length];
5828+
size_t sslPreBufferLength = [sslPreBuffer availableBytes];
58235829

5824-
if (sslReadBufferLength > 0)
5830+
if (sslPreBufferLength > 0)
58255831
{
58265832
LogVerbose(@"%@: Reading from SSL pre buffer...", THIS_METHOD);
58275833

58285834
size_t bytesToCopy;
5829-
if (sslReadBufferLength > totalBytesLeftToBeRead)
5835+
if (sslPreBufferLength > totalBytesLeftToBeRead)
58305836
bytesToCopy = totalBytesLeftToBeRead;
58315837
else
5832-
bytesToCopy = (size_t)sslReadBufferLength;
5833-
5834-
LogVerbose(@"%@: Copying %zu bytes from sslReadBuffer", THIS_METHOD, bytesToCopy);
5838+
bytesToCopy = sslPreBufferLength;
58355839

5836-
memcpy(buffer, [sslReadBuffer mutableBytes], bytesToCopy);
5840+
LogVerbose(@"%@: Copying %zu bytes from sslPreBuffer", THIS_METHOD, bytesToCopy);
58375841

5838-
[sslReadBuffer replaceBytesInRange:NSMakeRange(0, bytesToCopy) withBytes:NULL length:0];
5842+
memcpy(buffer, [sslPreBuffer readBuffer], bytesToCopy);
5843+
[sslPreBuffer didRead:bytesToCopy];
58395844

5840-
LogVerbose(@"%@: sslReadBuffer.length = %lu", THIS_METHOD, (unsigned long)[sslReadBuffer length]);
5845+
LogVerbose(@"%@: sslPreBuffer.length = %zu", THIS_METHOD, [sslPreBuffer availableBytes]);
58415846

58425847
totalBytesRead += bytesToCopy;
58435848
totalBytesLeftToBeRead -= bytesToCopy;
@@ -5863,19 +5868,16 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
58635868

58645869
if (socketFDBytesAvailable > totalBytesLeftToBeRead)
58655870
{
5866-
// Read all available data from socket into sslReadBuffer.
5871+
// Read all available data from socket into sslPreBuffer.
58675872
// Then copy requested amount into dataBuffer.
58685873

5869-
LogVerbose(@"%@: Reading into sslReadBuffer...", THIS_METHOD);
5874+
LogVerbose(@"%@: Reading into sslPreBuffer...", THIS_METHOD);
58705875

5871-
if ([sslReadBuffer length] < socketFDBytesAvailable)
5872-
{
5873-
[sslReadBuffer setLength:socketFDBytesAvailable];
5874-
}
5876+
[sslPreBuffer ensureCapacityForWrite:socketFDBytesAvailable];
58755877

58765878
readIntoPreBuffer = YES;
58775879
bytesToRead = (size_t)socketFDBytesAvailable;
5878-
buf = [sslReadBuffer mutableBytes];
5880+
buf = [sslPreBuffer writeBuffer];
58795881
}
58805882
else
58815883
{
@@ -5901,23 +5903,13 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
59015903
}
59025904

59035905
socketFDBytesAvailable = 0;
5904-
5905-
if (readIntoPreBuffer)
5906-
{
5907-
[sslReadBuffer setLength:0];
5908-
}
59095906
}
59105907
else if (result == 0)
59115908
{
59125909
LogVerbose(@"%@: read EOF", THIS_METHOD);
59135910

59145911
socketError = YES;
59155912
socketFDBytesAvailable = 0;
5916-
5917-
if (readIntoPreBuffer)
5918-
{
5919-
[sslReadBuffer setLength:0];
5920-
}
59215913
}
59225914
else
59235915
{
@@ -5930,19 +5922,19 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
59305922

59315923
if (readIntoPreBuffer)
59325924
{
5933-
size_t bytesToCopy = MIN(totalBytesLeftToBeRead, bytesReadFromSocket);
5925+
[sslPreBuffer didWrite:bytesReadFromSocket];
59345926

5935-
LogVerbose(@"%@: Copying %zu bytes out of sslReadBuffer", THIS_METHOD, bytesToCopy);
5927+
size_t bytesToCopy = MIN(totalBytesLeftToBeRead, bytesReadFromSocket);
59365928

5937-
memcpy((uint8_t *)buffer + totalBytesRead, [sslReadBuffer bytes], bytesToCopy);
5929+
LogVerbose(@"%@: Copying %zu bytes out of sslPreBuffer", THIS_METHOD, bytesToCopy);
59385930

5939-
[sslReadBuffer setLength:bytesReadFromSocket];
5940-
[sslReadBuffer replaceBytesInRange:NSMakeRange(0, bytesToCopy) withBytes:NULL length:0];
5931+
memcpy((uint8_t *)buffer + totalBytesRead, [sslPreBuffer readBuffer], bytesToCopy);
5932+
[sslPreBuffer didRead:bytesToCopy];
59415933

59425934
totalBytesRead += bytesToCopy;
59435935
totalBytesLeftToBeRead -= bytesToCopy;
59445936

5945-
LogVerbose(@"%@: sslReadBuffer.length = %lu", THIS_METHOD, (unsigned long)[sslReadBuffer length]);
5937+
LogVerbose(@"%@: sslPreBuffer.length = %zu", THIS_METHOD, [sslPreBuffer availableBytes]);
59465938
}
59475939
else
59485940
{
@@ -6392,22 +6384,22 @@ - (void)ssl_startTLS
63926384
}
63936385
#endif
63946386

6395-
// Setup the sslReadBuffer
6387+
// Setup the sslPreBuffer
63966388
//
6397-
// Any data in the preBuffer needs to be moved into the sslReadBuffer,
6389+
// Any data in the preBuffer needs to be moved into the sslPreBuffer,
63986390
// as this data is now part of the secure read stream.
63996391

6400-
sslReadBuffer = [[NSMutableData alloc] init];
6392+
sslPreBuffer = [[GCDAsyncSocketPreBuffer alloc] initWithCapacity:(1024 * 4)];
64016393

6402-
uint8_t *preBuf;
6403-
size_t preBufLen;
6394+
size_t preBufferLength = [preBuffer availableBytes];
64046395

6405-
[preBuffer getReadBuffer:&preBuf availableBytes:&preBufLen];
6406-
6407-
if (preBufLen > 0)
6396+
if (preBufferLength > 0)
64086397
{
6409-
[sslReadBuffer appendBytes:preBuf length:preBufLen];
6410-
[preBuffer didRead:preBufLen];
6398+
[sslPreBuffer ensureCapacityForWrite:preBufferLength];
6399+
6400+
memcpy([sslPreBuffer writeBuffer], [preBuffer readBuffer], preBufferLength);
6401+
[preBuffer didRead:preBufferLength];
6402+
[sslPreBuffer didWrite:preBufferLength];
64116403
}
64126404

64136405
// Start the SSL Handshake process

0 commit comments

Comments
 (0)