1 //
2 //  MPMessagePackRPClient.h
3 //  MPMessagePack
4 //
5 //  Created by Gabriel on 12/12/14.
6 //  Copyright (c) 2014 Gabriel Handford. All rights reserved.
7 //
8 
9 #import <Foundation/Foundation.h>
10 
11 #import "MPDefines.h"
12 #import "MPRPCProtocol.h"
13 
14 typedef NS_ENUM (NSInteger, MPMessagePackClientStatus) {
15   MPMessagePackClientStatusNone = 0,
16   MPMessagePackClientStatusClosed = 1,
17   MPMessagePackClientStatusOpening,
18   MPMessagePackClientStatusOpen,
19 };
20 
21 typedef NS_OPTIONS (NSInteger, MPMessagePackOptions) {
22   MPMessagePackOptionsNone = 0,
23   // If true, the message is wrapped in a frame
24   MPMessagePackOptionsFramed = 1 << 0,
25 };
26 
27 @protocol MPMessagePackCoder
28 - (nonnull id)encodeObject:(nonnull id)obj;
29 @end
30 
31 @class MPMessagePackClient;
32 
33 @protocol MPMessagePackClientDelegate <NSObject>
34 - (void)client:(nonnull MPMessagePackClient *)client didError:(nonnull NSError *)error fatal:(BOOL)fatal;
35 - (void)client:(nonnull MPMessagePackClient *)client didChangeStatus:(MPMessagePackClientStatus)status;
36 - (void)client:(nonnull MPMessagePackClient *)client didReceiveNotificationWithMethod:(nonnull NSString *)method params:(nonnull NSArray *)params;
37 @end
38 
39 @interface MPMessagePackClient : NSObject
40 
41 @property (nullable, weak) id<MPMessagePackClientDelegate> delegate;
42 @property (nullable, copy) MPRequestHandler requestHandler;
43 @property (readonly, nonatomic) MPMessagePackClientStatus status;
44 @property (nullable) id<MPMessagePackCoder> coder;
45 
46 - (nonnull instancetype)initWithName:(nonnull NSString *)name options:(MPMessagePackOptions)options;
47 
48 - (void)openWithHost:(nonnull NSString *)host port:(UInt32)port completion:(nonnull MPCompletion)completion;
49 
50 - (BOOL)openWithSocket:(nonnull NSString *)unixSocket completion:(nonnull MPCompletion)completion;
51 
52 - (void)setInputStream:(nonnull NSInputStream *)inputStream outputStream:(nonnull NSOutputStream *)outputStream;
53 
54 - (void)close;
55 
56 /*!
57  Send RPC request asyncronously with completion block.
58 
59  @param method Method name
60  @param params Method args. If coder is set on client, we will use it to encode.
61  @param messageId Unique message identifier. Responses will use this message ID.
62  @param completion Response
63  */
64 - (void)sendRequestWithMethod:(nonnull NSString *)method params:(nonnull NSArray *)params messageId:(NSInteger)messageId completion:(nonnull MPRequestCompletion)completion;
65 
66 /*!
67  Send a response.
68 
69  @param result Result
70  @param error Error
71  @param messageId Message ID (will match request message ID)
72  */
73 - (void)sendResponseWithResult:(nullable id)result error:(nullable id)error messageId:(NSInteger)messageId;
74 
75 /*!
76  Send request synchronously.
77 
78  @param method Method name
79  @param params Method args. If coder is set on client, we will use it to encode.
80  @param messageId Unique message identifier. Responses will use this message ID.
81  @param timeout Timeout
82  @param error Out error
83  @result Result of method invocation
84  */
85 - (nullable id)sendRequestWithMethod:(nonnull NSString *)method params:(nonnull NSArray *)params messageId:(NSInteger)messageId timeout:(NSTimeInterval)timeout error:(NSError * _Nonnull * _Nonnull)error;
86 
87 /*!
88  Cancel request.
89 
90  @param messageId Message id
91  @result Return YES if cancelled
92  */
93 - (BOOL)cancelRequestWithMessageId:(NSInteger)messageId;
94 
95 @end
96 
97 
98