FIRMessagingFakeSocket.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Example/Messaging/Tests/FIRMessagingFakeSocket.h"
  17. #import "Firebase/Messaging/FIRMessagingConstants.h"
  18. #import "Firebase/Messaging/FIRMessagingDefines.h"
  19. @interface FIRMessagingSecureSocket () <NSStreamDelegate>
  20. @property(nonatomic, readwrite, assign) FIRMessagingSecureSocketState state;
  21. @property(nonatomic, readwrite, strong) NSInputStream *inStream;
  22. @property(nonatomic, readwrite, strong) NSOutputStream *outStream;
  23. @property(nonatomic, readwrite, assign) BOOL isInStreamOpen;
  24. @property(nonatomic, readwrite, assign) BOOL isOutStreamOpen;
  25. @property(nonatomic, readwrite, strong) NSRunLoop *runLoop;
  26. @end
  27. @interface FIRMessagingFakeSocket ()
  28. @property(nonatomic, readwrite, assign) int8_t bufferSize;
  29. @end
  30. @implementation FIRMessagingFakeSocket
  31. - (instancetype)initWithBufferSize:(uint8_t)bufferSize {
  32. self = [super init];
  33. if (self) {
  34. _bufferSize = bufferSize;
  35. }
  36. return self;
  37. }
  38. - (void)connectToHost:(NSString *)host port:(NSUInteger)port onRunLoop:(NSRunLoop *)runLoop {
  39. self.state = kFIRMessagingSecureSocketOpening;
  40. self.runLoop = runLoop;
  41. CFReadStreamRef inputStreamRef = nil;
  42. CFWriteStreamRef outputStreamRef = nil;
  43. CFStreamCreateBoundPair(NULL, &inputStreamRef, &outputStreamRef, self.bufferSize);
  44. self.inStream = CFBridgingRelease(inputStreamRef);
  45. self.outStream = CFBridgingRelease(outputStreamRef);
  46. if (!self.inStream || !self.outStream) {
  47. NSAssert(NO, @"Cannot create a fake socket");
  48. return;
  49. }
  50. self.isInStreamOpen = NO;
  51. self.isOutStreamOpen = NO;
  52. [self openStream:self.outStream];
  53. [self openStream:self.inStream];
  54. }
  55. - (void)openStream:(NSStream *)stream {
  56. NSAssert(stream, @"Cannot open nil stream");
  57. if (stream) {
  58. stream.delegate = self;
  59. [stream scheduleInRunLoop:self.runLoop forMode:NSDefaultRunLoopMode];
  60. [stream open];
  61. }
  62. }
  63. @end