Lookin_PTChannel.h 5.2 KB

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