PageRenderTime 336ms CodeModel.GetById 47ms RepoModel.GetById 2ms app.codeStats 0ms

/Frameworks/Foundation/NSOutputStream_socket.mm

https://gitlab.com/goolic/WinObjC
Objective C++ | 184 lines | 134 code | 43 blank | 7 comment | 24 complexity | 11a450fefe3dc8b9aea452add49d6dbe MD5 | raw file
  1. /* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>
  2. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  3. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  4. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  5. #include "Starboard.h"
  6. #include "Foundation/NSStream.h"
  7. #include "Foundation/NSDate.h"
  8. #include "NSSSLHandler.h"
  9. #include "NSOutputStream_socket.h"
  10. #include "NSSelectInputSource.h"
  11. #include "NSSelectSet.h"
  12. static BOOL socketHasSpaceAvailable(id socket) {
  13. NSSelectSet* selectSet = [[[NSSelectSet alloc] init] autorelease];
  14. NSSelectSet* outputSet;
  15. [selectSet addObjectForWrite:socket];
  16. if ( [selectSet waitForSelectWithOutputSet:&outputSet beforeDate:[NSDate date]] == nil )
  17. return [outputSet containsObjectForWrite:socket];
  18. return NO;
  19. }
  20. @implementation NSOutputStream_socket : NSStream
  21. /* annotate with type */ -(id) initWithSocket:(id)socket streamStatus:(DWORD)status {
  22. _delegate=self;
  23. _error=nil;
  24. _status=status;
  25. _socket=[socket retain];
  26. _inputSource=nil;
  27. return self;
  28. }
  29. /* annotate with type */ -(id) setDelegate:(id)delegate {
  30. _delegate=delegate;
  31. if(_delegate==nil)
  32. _delegate=self;
  33. return self;
  34. }
  35. /* annotate with type */ -(id) scheduleInRunLoop:(id)runLoop forMode:(id)mode {
  36. if(_inputSource==nil) {
  37. _inputSource=[[NSSelectInputSource alloc] initWithSocket:_socket];
  38. [_inputSource setDelegate:self];
  39. [_inputSource setSelectEventMask:NSSelectWriteEvent];
  40. }
  41. [runLoop addInputSource:_inputSource forMode:mode];
  42. return self;
  43. }
  44. /* annotate with type */ -(id) open {
  45. if(_status==NSStreamStatusNotOpen){
  46. _status=NSStreamStatusOpening;
  47. }
  48. return self;
  49. }
  50. /* annotate with type */ -(id) close {
  51. [_socket close];
  52. return self;
  53. }
  54. /* annotate with type */ -(id) _triggerWrite {
  55. [_inputSource setSelectEventMask:[_inputSource selectEventMask] | NSSelectWriteEvent];
  56. return self;
  57. }
  58. /* annotate with type */ -(id) selectInputSource:(id)inputSource selectEvent:(DWORD)selectEvent {
  59. NSStreamEvent event;
  60. switch(_status) {
  61. case NSStreamStatusOpening:
  62. _status=NSStreamStatusOpen;
  63. event=NSStreamEventOpenCompleted;
  64. break;
  65. case NSStreamStatusOpen: {
  66. id sslHandler = [_socket sslHandler];
  67. bool done = true;
  68. if (sslHandler != nil) {
  69. [sslHandler setOutputStream:self];
  70. [sslHandler runHandshakeIfNeeded:_socket];
  71. done = ![sslHandler isHandshaking];
  72. }
  73. if ( ![self hasSpaceAvailable] || !done )
  74. event=NSStreamEventNone;
  75. else
  76. event=NSStreamEventHasSpaceAvailable;
  77. [_inputSource setSelectEventMask:[_inputSource selectEventMask] & ~NSSelectWriteEvent];
  78. }
  79. break;
  80. case NSStreamStatusAtEnd:
  81. event=NSStreamEventEndEncountered;
  82. break;
  83. default:
  84. event=NSStreamEventNone;
  85. break;
  86. }
  87. if(event!=NSStreamEventNone && [_delegate respondsToSelector:@selector(stream:handleEvent:)])
  88. [_delegate stream:self handleEvent:event];
  89. return self;
  90. }
  91. /* annotate with type */ -(id) removeFromRunLoop:(id)runLoop forMode:(id)mode {
  92. if(_inputSource!=nil)
  93. [runLoop removeInputSource:_inputSource forMode:mode];
  94. return self;
  95. }
  96. -(BOOL) hasSpaceAvailable {
  97. if(_status==NSStreamStatusOpen) {
  98. id sslHandler = [_socket sslHandler];
  99. if(sslHandler==nil)
  100. return socketHasSpaceAvailable(_socket);
  101. else {
  102. if ( [sslHandler writeBytesAvailable] ==0 )
  103. return YES;
  104. else if ( socketHasSpaceAvailable(_socket)) {
  105. [sslHandler transferOneBufferFromSSLToSocket:_socket];
  106. }
  107. }
  108. }
  109. return NO;
  110. }
  111. -(int) write:(uint8_t *)buffer maxLength:(unsigned)length {
  112. if(_status!=NSStreamStatusOpen && _status!=NSStreamStatusOpening)
  113. return -1;
  114. NSSSLHandler* sslHandler = [_socket sslHandler];
  115. if(sslHandler==nil) {
  116. [_inputSource setSelectEventMask:[_inputSource selectEventMask] | NSSelectWriteEvent];
  117. return [_socket write:buffer maxLength:length];
  118. } else {
  119. [sslHandler runHandshakeIfNeeded:_socket];
  120. NSInteger check = [sslHandler writePlaintext:buffer maxLength:length];
  121. if(check!=length)
  122. EbrDebugLog("failure writePlaintext:%d=%d",length,check);
  123. [sslHandler runWithSocket:_socket];
  124. [_inputSource setSelectEventMask:[_inputSource selectEventMask] | NSSelectWriteEvent];
  125. return check;
  126. }
  127. }
  128. /* annotate with type */ -(id) setProperty:(id)prop forKey:(id)key {
  129. if ( [key isEqualToString:(NSString*) kCFStreamPropertySSLSettings])
  130. return [_socket setSSLProperties:prop];
  131. assert(0);
  132. return NO;
  133. }
  134. -(void) dealloc {
  135. [_inputSource setDelegate:nil];
  136. [super dealloc];
  137. }
  138. @end