/CFSockets/CFStreamPair.h

http://github.com/royratcliffe/CFSockets · C Header · 100 lines · 25 code · 17 blank · 58 comment · 0 complexity · d04c7264dbf4093d17a3c7451748cd5a MD5 · raw file

  1. // CFSockets CFStreamPair.h
  2. //
  3. // Copyright © 2012, 2013, Roy Ratcliffe, Pioneering Software, United Kingdom
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the “Software”), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
  16. // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
  18. // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. // DEALINGS IN THE SOFTWARE.
  22. //
  23. //------------------------------------------------------------------------------
  24. #import <Foundation/Foundation.h>
  25. @class CFStreamPair;
  26. @protocol CFStreamPairDelegate<NSObject>
  27. @optional
  28. - (void)streamPair:(CFStreamPair *)streamPair hasBytesAvailable:(NSUInteger)bytesAvailable;
  29. - (void)streamPair:(CFStreamPair *)streamPair handleRequestEvent:(NSStreamEvent)eventCode;
  30. - (void)streamPair:(CFStreamPair *)streamPair handleResponseEvent:(NSStreamEvent)eventCode;
  31. @end
  32. /**
  33. * Adds request-response semantics to a socket by encapsulating it with
  34. * an input-output stream pair.
  35. *
  36. * For TCP connections, the request input stream corresponds to the
  37. * request; _receive_ the request bytes from the request input, _send_ the
  38. * response to the response output stream.
  39. */
  40. @interface CFStreamPair : NSObject<NSStreamDelegate>
  41. @property(weak, NS_NONATOMIC_IOSONLY) id<CFStreamPairDelegate> delegate;
  42. @property(strong, NS_NONATOMIC_IOSONLY) NSInputStream *requestStream;
  43. @property(strong, NS_NONATOMIC_IOSONLY) NSOutputStream *responseStream;
  44. - (id)initWithRequestStream:(NSInputStream *)requestStream responseStream:(NSOutputStream *)responseStream;
  45. - (id)initWithSocketNativeHandle:(NSSocketNativeHandle)socketNativeHandle;
  46. /**
  47. * Opens the request and response streams, scheduling them for service within
  48. * the current run loop. You cannot reopen the streams.
  49. *
  50. * This method assumes that you have not already delegated, scheduled
  51. * or opened the underlying request-response stream pair.
  52. */
  53. - (void)open;
  54. /**
  55. * Reverses the opening. Closes the request and response streams, removes them
  56. * from the current run loop. This assumes that you send `-close` from the same
  57. * thread as you sent the original `-open`.
  58. */
  59. - (void)close;
  60. /**
  61. * Destructively receives bytes from the request buffer.
  62. */
  63. - (NSData *)receiveAvailableBytes;
  64. /**
  65. * Special convenience method for receiving lines of text from the
  66. * request stream based on a given string encoding.
  67. *
  68. * The result includes any line termination characters. There could be
  69. * more than one termination character at the end of the line since some line
  70. * termination sequences span multiple characters.
  71. *
  72. * @result Answers `nil` if the request buffer does not yet contain a complete
  73. * line. Try again later.
  74. */
  75. - (NSString *)receiveLineUsingEncoding:(NSStringEncoding)encoding;
  76. - (void)sendBytes:(NSData *)outputBytes;
  77. //-------------------------------------------------------------------- Overrides
  78. - (void)hasBytesAvailable;
  79. - (void)hasSpaceAvailable;
  80. - (void)sendBytes;
  81. - (void)handleRequestEvent:(NSStreamEvent)eventCode;
  82. - (void)handleResponseEvent:(NSStreamEvent)eventCode;
  83. @end