LightMessaging
LightMessaging copied to clipboard
when I use it in a sandboxed app. it stoped when send message ! my iOS verson is 10.0
when I use it in a sandboxed app. it stoped when send message ! my iOS verson is 10.0.
server code:
#include <stdio.h>
#import <rocketbootstrap/rocketbootstrap.h>
@interface MYMessagingCenter : NSObject
{
CPDistributedMessagingCenter * _messagingCenter;
}
@end
@implementation MYMessagingCenter
+ (void)load
{
[self sharedInstance];
}
+ (instancetype)sharedInstance
{
static dispatch_once_t once = 0;
__strong static id sharedInstance = nil;
dispatch_once(&once, ^{
sharedInstance = [self new];
});
return sharedInstance;
}
- (instancetype)init
{
SKFLY_TRACE
if ((self = [super init]))
{SKFLY_TRACE
_messagingCenter = [CPDistributedMessagingCenter centerNamed:@"com.mycompany.myCenter"];SKFLY_TRACE
// apply rocketbootstrap regardless of iOS version (via rpetrich)
rocketbootstrap_distributedmessagingcenter_apply(_messagingCenter);SKFLY_TRACE
[_messagingCenter runServerOnCurrentThread];SKFLY_TRACE
[_messagingCenter registerForMessageName:@"myMessageName" target:self selector:@selector(handleMessageNamed:withUserInfo:)];SKFLY_TRACE
}
return self;
}
- (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userInfo
{
NSLog(@"收到消息: userInfo=[%@]",userInfo);SKFLY_TRACE
return [NSDictionary dictionaryWithObjectsAndKeys:@"key", @"object", nil];
}
@end
void some_callback(CFMachPortRef port, LMMessage *message, CFIndex size, void *info)
{
NSLog(@"接收到请求!");
// get the reply port
mach_port_t replyPort = message->head.msgh_remote_port;
// Check validity of message
if (!LMDataWithSizeIsValidMessage(message, size))
{
LMSendReply(replyPort, NULL, 0);
LMResponseBufferFree((LMResponseBuffer *)message);
return;
}
// Get the data you received
void *data = LMMessageGetData(message);
NSLog(@"%s",data);
size_t length = LMMessageGetDataLength(message);
// Make it into a CFDataRef object
CFDataRef cfdata = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)data ?: (const UInt8 *)&data, length, kCFAllocatorNull);
// Send some data back
const char *msg = "lol";
LMSendReply(replyPort, msg, strlen(msg) + 1);
// Free the CFDataRef object
if (cfdata)
{
CFRelease(cfdata);
}
// Free the response buffer
LMResponseBufferFree((LMResponseBuffer *)message);
}
int main(int argc, char *argv[], char *envp[])
{
NSLog(@"开始执行\n");
LMStartService("net.iphonedevwiki.some.server", CFRunLoopGetCurrent(), (CFMachPortCallBack)some_callback);
[MYMessagingCenter load];
CFRunLoopRun();
NSLog(@"程序结束!");
return 0;
}
client code:
//
// ViewController.m
// RocketBootstrapTest
//
// Created by skfly on 2020/3/21.
// Copyright © 2020 skfly. All rights reserved.
//
#import "ViewController.h"
#import "rocketbootstrap.h"
#import "LightMessaging.h"
#import <AppSupport/CPDistributedMessagingCenter.h>
static CPDistributedMessagingCenter *c = nil;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
c = [CPDistributedMessagingCenter centerNamed:@"com.mycompany.myCenter"];
//rocketbootstrap_distributedmessagingcenter_apply(c);
// Send a message with no dictionary
[c sendMessageName:@"myMessageName" userInfo:nil];
// Send a message with a dictionary
NSDictionary * message = [NSDictionary dictionaryWithObjectsAndKeys: @"key", @"object", nil];
//[c sendMessageName:@"myMessageName" userInfo:message];
// Send a message with no dictionary and receive a reply dictionary
//NSDictionary * reply = [c sendMessageAndReceiveReplyName:@"myMessageName" userInfo:nil];
// Send a message with a dictionary and receive a reply dictionary
NSDictionary * replyWithMessage = [c sendMessageAndReceiveReplyName:@"myMessageName" userInfo:message];
NSLog(@"%@",replyWithMessage);
// setup connection
LMConnection connection =
{
MACH_PORT_NULL,
"net.iphonedevwiki.some.server"
};
//send message
LMResponseBuffer buffer;
const char *msg = "lol wtf222";
SInt32 messageId = 0x1111; // this is arbitrary i think
LMConnectionSendTwoWay(&connection, messageId, msg, strlen(msg) + 1, &buffer);
LMMessage *response = &(buffer.message);
// do whatever you want !!!!
const char *data = LMMessageGetData(response);
NSLog(@"%s",data);
//cleanup
LMResponseBufferFree(&buffer);
}
@end