Skip to content

Commit

Permalink
fix(IoT): Fixing naming collision with SocketRocket (#5004)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruisebas authored Oct 23, 2023
1 parent a2bf4fa commit ec1cf57
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions AWSIoT/Internal/SocketRocket/AWSSRWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
static NSString *const AWSSRWebSocketAppendToSecKeyString = @"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

static inline int32_t validate_dispatch_data_partial_string(NSData *data);
static inline void SRFastLog(NSString *format, ...);
static inline void AWSSRFastLog(NSString *format, ...);

@interface NSData (AWSSRWebSocket)

Expand All @@ -98,7 +98,7 @@ @interface NSURL (AWSSRWebSocket)

// The origin isn't really applicable for a native application.
// So instead, just map ws -> http and wss -> https.
- (NSString *)SR_origin;
- (NSString *)AWSSR_origin;

@end

Expand Down Expand Up @@ -149,15 +149,15 @@ - (NSString *)stringBySHA1ThenBase64Encoding;
@end

NSString *const AWSSRWebSocketErrorDomain = @"AWSSRWebSocketErrorDomain";
NSString *const SRHTTPResponseErrorKey = @"HTTPResponseStatusCode";
NSString *const AWSSRHTTPResponseErrorKey = @"HTTPResponseStatusCode";

// Returns number of bytes consumed. Returning 0 means you didn't match.
// Sends bytes to callback handler;
typedef size_t (^stream_scanner)(NSData *collected_data);

typedef void (^data_callback)(AWSSRWebSocket *webSocket, NSData *data);

@interface SRIOConsumer : NSObject {
@interface AWSSRIOConsumer : NSObject {
stream_scanner _scanner;
data_callback _handler;
size_t _bytesNeeded;
Expand All @@ -173,12 +173,12 @@ @interface SRIOConsumer : NSObject {
@end

// This class is not thread-safe, and is expected to always be run on the same queue.
@interface SRIOConsumerPool : NSObject
@interface AWSSRIOConsumerPool : NSObject

- (id)initWithBufferCapacity:(NSUInteger)poolSize;

- (SRIOConsumer *)consumerWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
- (void)returnConsumer:(SRIOConsumer *)consumer;
- (AWSSRIOConsumer *)consumerWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
- (void)returnConsumer:(AWSSRIOConsumer *)consumer;

@end

Expand Down Expand Up @@ -252,7 +252,7 @@ @implementation AWSSRWebSocket {
__strong AWSSRWebSocket *_selfRetain;

NSArray *_requestedProtocols;
SRIOConsumerPool *_consumerPool;
AWSSRIOConsumerPool *_consumerPool;
}

@synthesize delegate = _delegate;
Expand Down Expand Up @@ -339,7 +339,7 @@ - (void)_SR_commonInit;

_consumers = [[NSMutableArray alloc] init];

_consumerPool = [[SRIOConsumerPool alloc] init];
_consumerPool = [[AWSSRIOConsumerPool alloc] init];

_scheduledRunloops = [[NSMutableSet alloc] init];

Expand Down Expand Up @@ -403,10 +403,10 @@ - (void)open;
- (void)_performDelegateBlock:(dispatch_block_t)block;
{
if (_delegateOperationQueue) {
SRFastLog(@"using _delegateOperationQueue.");
AWSSRFastLog(@"using _delegateOperationQueue.");
[_delegateOperationQueue addOperationWithBlock:block];
} else {
SRFastLog(@"using _delegateDispatchQueue.");
AWSSRFastLog(@"using _delegateDispatchQueue.");
assert(_delegateDispatchQueue);
dispatch_async(_delegateDispatchQueue, block);
}
Expand Down Expand Up @@ -444,8 +444,8 @@ - (void)_HTTPHeadersDidFinish;
NSInteger responseCode = CFHTTPMessageGetResponseStatusCode(_receivedHTTPHeaders);

if (responseCode >= 400) {
SRFastLog(@"Request failed with response code %d", responseCode);
[self _failWithError:[NSError errorWithDomain:AWSSRWebSocketErrorDomain code:2132 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"received bad response code from server %ld", (long)responseCode], SRHTTPResponseErrorKey:@(responseCode)}]];
AWSSRFastLog(@"Request failed with response code %d", responseCode);
[self _failWithError:[NSError errorWithDomain:AWSSRWebSocketErrorDomain code:2132 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"received bad response code from server %ld", (long)responseCode], AWSSRHTTPResponseErrorKey:@(responseCode)}]];
return;
}

Expand Down Expand Up @@ -489,7 +489,7 @@ - (void)_readHTTPHeader;
CFHTTPMessageAppendBytes(self->_receivedHTTPHeaders, (const UInt8 *)data.bytes, data.length);

if (CFHTTPMessageIsHeaderComplete(self->_receivedHTTPHeaders)) {
SRFastLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(self->_receivedHTTPHeaders)));
AWSSRFastLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(self->_receivedHTTPHeaders)));
[self _HTTPHeadersDidFinish];
} else {
[self _readHTTPHeader];
Expand All @@ -499,7 +499,7 @@ - (void)_readHTTPHeader;

- (void)didConnect;
{
SRFastLog(@"Connected");
AWSSRFastLog(@"Connected");
CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (__bridge CFURLRef)_url, kCFHTTPVersion1_1);

// Set host first so it defaults
Expand Down Expand Up @@ -535,7 +535,7 @@ - (void)didConnect;
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Connection"), CFSTR("Upgrade"));
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Key"), (__bridge CFStringRef)_secKey);
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Version"), (__bridge CFStringRef)[NSString stringWithFormat:@"%ld", (long)_webSocketVersion]);
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStringRef)_url.SR_origin);
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStringRef)_url.AWSSR_origin);

if (_requestedProtocols) {
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Sec-WebSocket-Protocol"), (__bridge CFStringRef)[_requestedProtocols componentsJoinedByString:@", "]);
Expand All @@ -547,7 +547,7 @@ - (void)didConnect;

NSData *message = CFBridgingRelease(CFHTTPMessageCopySerializedMessage(request));

SRFastLog(@"%@", [[NSString alloc]initWithData:message encoding:NSUTF8StringEncoding]);
AWSSRFastLog(@"%@", [[NSString alloc]initWithData:message encoding:NSUTF8StringEncoding]);
CFRelease(request);

[self _writeData:message];
Expand Down Expand Up @@ -601,7 +601,7 @@ - (void)_updateSecureStreamOptions;

if (self.allowsUntrustedSSLCertificates) {
[SSLOptions setValue:@NO forKey:(__bridge id)kCFStreamSSLValidatesCertificateChain];
SRFastLog(@"Allowing connection to any root cert");
AWSSRFastLog(@"Allowing connection to any root cert");
}

[_outputStream setProperty:SSLOptions
Expand Down Expand Up @@ -702,7 +702,7 @@ - (void)closeWithCode:(NSInteger)code reason:(NSString *)reason;

self.readyState = AWSSR_CLOSING;

SRFastLog(@"Closing with code %d reason %@", code, reason);
AWSSRFastLog(@"Closing with code %d reason %@", code, reason);

if (wasConnecting) {
[self closeConnection];
Expand Down Expand Up @@ -760,7 +760,7 @@ - (void)_failWithError:(NSError *)error;

self.readyState = AWSSR_CLOSED;

SRFastLog(@"Failing with error %@", error.localizedDescription);
AWSSRFastLog(@"Failing with error %@", error.localizedDescription);

[self closeConnection];
[self _scheduleCleanup];
Expand Down Expand Up @@ -819,7 +819,7 @@ - (void)handlePing:(NSData *)pingData;

- (void)handlePong:(NSData *)pongData;
{
SRFastLog(@"Received pong");
AWSSRFastLog(@"Received pong");
[self _performDelegateBlock:^{
if ([self.delegate respondsToSelector:@selector(webSocket:didReceivePong:)]) {
[self.delegate webSocket:self didReceivePong:pongData];
Expand All @@ -829,7 +829,7 @@ - (void)handlePong:(NSData *)pongData;

- (void)_handleMessage:(id)message
{
SRFastLog(@"Received message");
AWSSRFastLog(@"Received message");
[self _performDelegateBlock:^{
[self.delegate webSocket:self didReceiveMessage:message];
}];
Expand Down Expand Up @@ -875,7 +875,7 @@ - (void)handleCloseWithData:(NSData *)data;
size_t dataSize = data.length;
__block uint16_t closeCode = 0;

SRFastLog(@"Received close frame");
AWSSRFastLog(@"Received close frame");

if (dataSize == 1) {
// TODO handle error
Expand Down Expand Up @@ -912,7 +912,7 @@ - (void)handleCloseWithData:(NSData *)data;
- (void)closeConnection;
{
[self assertOnWorkQueue];
SRFastLog(@"Trying to disconnect");
AWSSRFastLog(@"Trying to disconnect");
_closeWhenFinishedWriting = YES;
[self _pumpWriting];
}
Expand Down Expand Up @@ -1301,7 +1301,7 @@ - (BOOL)_innerPumpScanner {
return didWork;
}

SRIOConsumer *consumer = [_consumers objectAtIndex:0];
AWSSRIOConsumer *consumer = [_consumers objectAtIndex:0];

size_t bytesNeeded = consumer.bytesNeeded;

Expand Down Expand Up @@ -1543,7 +1543,7 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
{
switch (eventCode) {
case NSStreamEventOpenCompleted: {
SRFastLog(@"NSStreamEventOpenCompleted %@", aStream);
AWSSRFastLog(@"NSStreamEventOpenCompleted %@", aStream);
if (self.readyState >= AWSSR_CLOSING) {
return;
}
Expand All @@ -1564,7 +1564,7 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
}

case NSStreamEventErrorOccurred: {
SRFastLog(@"NSStreamEventErrorOccurred %@ %@", aStream, [[aStream streamError] copy]);
AWSSRFastLog(@"NSStreamEventErrorOccurred %@ %@", aStream, [[aStream streamError] copy]);
/// TODO specify error better!
[self _failWithError:aStream.streamError];
self->_readBufferOffset = 0;
Expand All @@ -1575,7 +1575,7 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream

case NSStreamEventEndEncountered: {
[self _pumpScanner];
SRFastLog(@"NSStreamEventEndEncountered %@", aStream);
AWSSRFastLog(@"NSStreamEventEndEncountered %@", aStream);
if (aStream.streamError) {
[self _failWithError:aStream.streamError];
} else {
Expand All @@ -1601,7 +1601,7 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
}

case NSStreamEventHasBytesAvailable: {
SRFastLog(@"NSStreamEventHasBytesAvailable %@", aStream);
AWSSRFastLog(@"NSStreamEventHasBytesAvailable %@", aStream);
const int bufferSize = 2048;
uint8_t buffer[bufferSize];

Expand All @@ -1623,21 +1623,21 @@ - (void)safeHandleEvent:(NSStreamEvent)eventCode stream:(NSStream *)aStream
}

case NSStreamEventHasSpaceAvailable: {
SRFastLog(@"NSStreamEventHasSpaceAvailable %@", aStream);
AWSSRFastLog(@"NSStreamEventHasSpaceAvailable %@", aStream);
[self _pumpWriting];
break;
}

default:
SRFastLog(@"(default) %@", aStream);
AWSSRFastLog(@"(default) %@", aStream);
break;
}
}

@end


@implementation SRIOConsumer
@implementation AWSSRIOConsumer

@synthesize bytesNeeded = _bytesNeeded;
@synthesize consumer = _scanner;
Expand All @@ -1659,7 +1659,7 @@ - (void)setupWithScanner:(stream_scanner)scanner handler:(data_callback)handler
@end


@implementation SRIOConsumerPool {
@implementation AWSSRIOConsumerPool {
NSUInteger _poolSize;
NSMutableArray *_bufferedConsumers;
}
Expand All @@ -1678,22 +1678,22 @@ - (id)init
return [self initWithBufferCapacity:8];
}

- (SRIOConsumer *)consumerWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
- (AWSSRIOConsumer *)consumerWithScanner:(stream_scanner)scanner handler:(data_callback)handler bytesNeeded:(size_t)bytesNeeded readToCurrentFrame:(BOOL)readToCurrentFrame unmaskBytes:(BOOL)unmaskBytes;
{
SRIOConsumer *consumer = nil;
AWSSRIOConsumer *consumer = nil;
if (_bufferedConsumers.count) {
consumer = [_bufferedConsumers lastObject];
[_bufferedConsumers removeLastObject];
} else {
consumer = [[SRIOConsumer alloc] init];
consumer = [[AWSSRIOConsumer alloc] init];
}

[consumer setupWithScanner:scanner handler:handler bytesNeeded:bytesNeeded readToCurrentFrame:readToCurrentFrame unmaskBytes:unmaskBytes];

return consumer;
}

- (void)returnConsumer:(SRIOConsumer *)consumer;
- (void)returnConsumer:(AWSSRIOConsumer *)consumer;
{
if (_bufferedConsumers.count < _poolSize) {
[_bufferedConsumers addObject:consumer];
Expand Down Expand Up @@ -1728,7 +1728,7 @@ - (void)setAWSSR_SSLPinnedCertificates:(NSArray *)AWSSR_SSLPinnedCertificates;

@implementation NSURL (SRWebSocket)

- (NSString *)SR_origin;
- (NSString *)AWSSR_origin;
{
NSString *scheme = [self.scheme lowercaseString];

Expand All @@ -1753,7 +1753,7 @@ - (NSString *)SR_origin;

#define SR_ENABLE_LOG

static inline void SRFastLog(NSString *format, ...) {
static inline void AWSSRFastLog(NSString *format, ...) {
#ifdef SR_ENABLE_LOG
__block va_list arg_list;
va_start (arg_list, format);
Expand Down

0 comments on commit ec1cf57

Please sign in to comment.