Lookin_PTChannel.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Represents a communication channel between two endpoints talking the same
  3. // Lookin_PTProtocol.
  4. //
  5. #import <Foundation/Foundation.h>
  6. #import <dispatch/dispatch.h>
  7. #import <netinet/in.h>
  8. #import <sys/socket.h>
  9. #import "Lookin_PTProtocol.h"
  10. #import "Lookin_PTUSBHub.h"
  11. @class Lookin_PTData, Lookin_PTAddress;
  12. @protocol Lookin_PTChannelDelegate;
  13. @interface Lookin_PTChannel : NSObject
  14. // Delegate
  15. @property (strong) id<Lookin_PTChannelDelegate> delegate;
  16. // Communication protocol. Must not be nil.
  17. @property Lookin_PTProtocol *protocol;
  18. // YES if this channel is a listening server
  19. @property (readonly) BOOL isListening;
  20. // YES if this channel is a connected peer
  21. @property (readonly) BOOL isConnected;
  22. // Arbitrary attachment. Note that if you set this, the object will grow by
  23. // 8 bytes (64 bits).
  24. @property (strong) id userInfo;
  25. // Create a new channel using the shared Lookin_PTProtocol for the current dispatch
  26. // queue, with *delegate*.
  27. + (Lookin_PTChannel*)channelWithDelegate:(id<Lookin_PTChannelDelegate>)delegate;
  28. // Initialize a new frame channel, configuring it to use the calling queue's
  29. // protocol instance (as returned by [Lookin_PTProtocol sharedProtocolForQueue:
  30. // dispatch_get_current_queue()])
  31. - (id)init;
  32. // Initialize a new frame channel with a specific protocol.
  33. - (id)initWithProtocol:(Lookin_PTProtocol*)protocol;
  34. // Initialize a new frame channel with a specific protocol and delegate.
  35. - (id)initWithProtocol:(Lookin_PTProtocol*)protocol delegate:(id<Lookin_PTChannelDelegate>)delegate;
  36. // Connect to a TCP port on a device connected over USB
  37. - (void)connectToPort:(int)port overUSBHub:(Lookin_PTUSBHub*)usbHub deviceID:(NSNumber*)deviceID callback:(void(^)(NSError *error))callback;
  38. // Connect to a TCP port at IPv4 address. Provided port must NOT be in network
  39. // byte order. Provided in_addr_t must NOT be in network byte order. A value returned
  40. // from inet_aton() will be in network byte order. You can use a value of inet_aton()
  41. // as the address parameter here, but you must flip the byte order before passing the
  42. // in_addr_t to this function.
  43. - (void)connectToPort:(in_port_t)port IPv4Address:(in_addr_t)address callback:(void(^)(NSError *error, Lookin_PTAddress *address))callback;
  44. // Listen for connections on port and address, effectively starting a socket
  45. // server. Provided port must NOT be in network byte order. Provided in_addr_t
  46. // must NOT be in network byte order.
  47. // For this to make sense, you should provide a onAccept block handler
  48. // or a delegate implementing ioFrameChannel:didAcceptConnection:.
  49. - (void)listenOnPort:(in_port_t)port IPv4Address:(in_addr_t)address callback:(void(^)(NSError *error))callback;
  50. // Send a frame with an optional payload and optional callback.
  51. // If *callback* is not NULL, the block is invoked when either an error occured
  52. // or when the frame (and payload, if any) has been completely sent.
  53. - (void)sendFrameOfType:(uint32_t)frameType tag:(uint32_t)tag withPayload:(dispatch_data_t)payload callback:(void(^)(NSError *error))callback;
  54. // Lower-level method to assign a connected dispatch IO channel to this channel
  55. - (BOOL)startReadingFromConnectedChannel:(dispatch_io_t)channel error:(__autoreleasing NSError**)error;
  56. // Close the channel, preventing further reading and writing. Any ongoing and
  57. // queued reads and writes will be aborted.
  58. - (void)close;
  59. // "graceful" close -- any ongoing and queued reads and writes will complete
  60. // before the channel ends.
  61. - (void)cancel;
  62. @end
  63. // Wraps a mapped dispatch_data_t object. The memory pointed to by *data* is
  64. // valid until *dispatchData* is deallocated (normally when the receiver is
  65. // deallocated).
  66. @interface Lookin_PTData : NSObject
  67. @property (readonly) dispatch_data_t dispatchData;
  68. @property (readonly) void *data;
  69. @property (readonly) size_t length;
  70. @end
  71. // Represents a peer's address
  72. @interface Lookin_PTAddress : NSObject
  73. // For network addresses, this is the IP address in textual format
  74. @property (readonly) NSString *name;
  75. // For network addresses, this is the port number. Otherwise 0 (zero).
  76. @property (readonly) NSInteger port;
  77. @end
  78. // Protocol for Lookin_PTChannel delegates
  79. @protocol Lookin_PTChannelDelegate <NSObject>
  80. @required
  81. // Invoked when a new frame has arrived on a channel.
  82. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(Lookin_PTData*)payload;
  83. @optional
  84. // Invoked to accept an incoming frame on a channel. Reply NO ignore the
  85. // incoming frame. If not implemented by the delegate, all frames are accepted.
  86. - (BOOL)ioFrameChannel:(Lookin_PTChannel*)channel shouldAcceptFrameOfType:(uint32_t)type tag:(uint32_t)tag payloadSize:(uint32_t)payloadSize;
  87. // Invoked when the channel closed. If it closed because of an error, *error* is
  88. // a non-nil NSError object.
  89. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didEndWithError:(NSError*)error;
  90. // For listening channels, this method is invoked when a new connection has been
  91. // accepted.
  92. - (void)ioFrameChannel:(Lookin_PTChannel*)channel didAcceptConnection:(Lookin_PTChannel*)otherChannel fromAddress:(Lookin_PTAddress*)address;
  93. @end