/* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0 or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #import #import #import "AWSCocoaLumberjack.h" #import "AWSIoTWebSocketOutputStream.h" #import @interface AWSIoTWebSocketOutputStreamFactory() @property (nonatomic, strong) NSOutputStream *actualDelegate; @property (nonatomic, strong) AWSSRWebSocket *webSocket; @end #pragma mark create instances @implementation AWSIoTWebSocketOutputStreamFactory + (AWSIoTWebSocketOutputStream *)createAWSIoTWebSocketOutputStreamWithWebSocket:(AWSSRWebSocket *)webSocket { return (AWSIoTWebSocketOutputStream *) [[self alloc] initToMemoryWithWebSocket:webSocket]; } #pragma mark initialization - (instancetype)initToMemoryWithWebSocket:(AWSSRWebSocket *)webSocket { if (self = [super init]) { self.actualDelegate = [NSOutputStream outputStreamToMemory]; self.webSocket = webSocket; } return self; } #pragma mark override write method - (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)limit { [self.webSocket send:[NSData dataWithBytes:(void *)buffer length:limit]]; AWSDDLogVerbose(@"sending %lu bytes", (unsigned long)limit); return limit; // writes always succeed } #pragma mark forward all other messages to actualDelegate - (id)forwardingTargetForSelector:(SEL)aSelector { if (class_respondsToSelector([self class], aSelector)) { return self; } if ([self.actualDelegate respondsToSelector:aSelector]) { return self.actualDelegate; } return [super forwardingTargetForSelector:aSelector]; } - (BOOL)respondsToSelector:(SEL)aSelector { if (class_respondsToSelector([self class], aSelector)) { return YES; } if ([self.actualDelegate respondsToSelector:aSelector]) { return YES; } return [super respondsToSelector:aSelector]; } @end