RocketBT-简单方便RocketBootstrap的封装

RocketBT

 一个兼具Sever和Client角色的RocketBootstarp封装,用起来非常非常简单。

1.代码

1.1 server code

遵循三步走:

  1. sharedSingleton 先初始化
  2. addDispatchHandler注册回调函数
  3. startServer 开启server,完事了。
RocketBT* rbt = [RocketBT sharedSingleton];
[rbt addDispatchHandler:@"com.xx.ping" dispatch_handler:^(NSMutableDictionary* info) {
	return [[NSMutableDictionary alloc] init];
}];
[rbt startServer];

1.2 client code

遵循两步走:

  1. sharedSingleton 先初始化
  2. sendMessageWithName发消息
NSMutableDictionary* info = [[NSMutableDictionary alloc] init];
NSDictionary* ret = [[RocketBT sharedSingleton] sendMessageWithName:@"com.xx.ping" userInfo:info];
NSLog(@"rbt client send %@ recv %@", info, ret);

1.3 ping pong sample

// server code
RocketBT* rbt = [RocketBT sharedSingleton];
[rbt addDispatchHandler:@"com.xx.ping" dispatch_handler:^(NSMutableDictionary* info) {
	NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
    
    // 这里你可以做你想做的任何事情
    [ret setObject:@"pong" forKey:@"msg"];
    
	return ret;
}];
[rbt startServer];
// client code

NSMutableDictionary* info = [[NSMutableDictionary alloc] init];
[info setObject:@"ping" forKey:@"msg"];

NSDictionary* ret = [[RocketBT sharedSingleton] sendMessageWithName:@"com.xx.ping" userInfo:info];

NSLog(@"rbt client send %@ recv %@", info, ret);

2. 定制部分

  1. kTTKCenterName 为centerName 修改为自己的即可
  2. 自己RocketBT addDispatchHandler添加自己的对应命令处理即可。

3. 注意事项:

  1. 使用前必须安装RocketBootstrap
  2. Makefile 中添加RocketBT.m 和动态库链接, 如下:

SRC = src
RocketBT = $(SRC)/RocketBT
TTouchTK_FILES = $(RocketBT)/RocketBT.m


TTouchTK_LIBRARIES += rocketbootstrap

4. 源码:

/*RocketBT.h*/
#import <Foundation/Foundation.h>
#import <AppSupport/CPDistributedMessagingCenter.h>
#import <rocketbootstrap/rocketbootstrap.h>
#import <objc/objc-runtime.h>
#import <dlfcn.h>
        

#pragma mark - macros


NS_ASSUME_NONNULL_BEGIN


typedef NSMutableDictionary*_Nonnull (^rbt_message_handler)(NSDictionary* user_info); // rbt message handler
@interface RocketBT : NSObject

#pragma mark - server methods

+ (instancetype) sharedSingleton;
+ (void) addDispatchHandler:(NSString *) name dispatchHandler:(rbt_message_handler) dispatch_handler;
- (void) startServer;


#pragma mark - client methods
-(NSDictionary*) sendMessageWithName:(NSString*) name userInfo:(NSMutableDictionary*)info;

@end
NS_ASSUME_NONNULL_END


```

```
/*RocketBT.mm*/
#import "RocketBT.h"

@implementation RocketBT



#pragma mark - public methods
NSString* kRBTCenterName = @"com.dbgmebeez.rbt.ttk";

static NSMutableDictionary* messageNameHandler = nil;

// name -> selector ->
// sharedSingleton -> register<name,block>  addDispatcherHandler -> startServer 

+ (instancetype)sharedSingleton {
    static RocketBT *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedSingleton = [[self alloc] init];
        NSLog(@" +++++++++++++++++++ RocketBT init");
        messageNameHandler = [[NSMutableDictionary alloc] init];

    });
    return _sharedSingleton;
}
+ (void) addDispatchHandler:(NSString *) name dispatchHandler:(rbt_message_handler) dispatch_handler{
    [messageNameHandler setObject:dispatch_handler forKey:name];
}

- (void) startServer{
    CPDistributedMessagingCenter *messageCenter = [objc_getClass("CPDistributedMessagingCenter") centerNamed:kRBTCenterName];
    rocketbootstrap_distributedmessagingcenter_apply(messageCenter);
    [messageCenter runServerOnCurrentThread];
    NSArray* centerNames = [messageNameHandler allKeys];
    for(NSString* centerName in centerNames){
        [messageCenter registerForMessageName:centerName target:self selector:@selector(interfaceHandleMessage:userInfo:)];
    }

}

- (NSDictionary *)interfaceHandleMessage:(NSString *)name userInfo:(NSDictionary *)info{
    
    if(!messageNameHandler[name]){
        NSLog(@" +++++++++++++++++++ RocketBT unknown name: %@", name);
        return nil;
    }
    rbt_message_handler rbt_handler = messageNameHandler[name];
    return rbt_handler(info);
}



-(NSDictionary*) sendMessageWithName:(NSString*) name userInfo:(NSMutableDictionary*)info{
    CPDistributedMessagingCenter *messageCenter = [objc_getClass("CPDistributedMessagingCenter") centerNamed:kRBTCenterName];
    rocketbootstrap_distributedmessagingcenter_apply(messageCenter);
    NSError *error;
    return [messageCenter sendMessageAndReceiveReplyName:name userInfo:info error:&error];
}
@end

```
2 Likes

我想不要脸的发下我的公众号:makdbb 平时自己记笔记,发一些服务端+逆向的东西,有兴趣的可以关注下。