(→Client) |
m (New How to use this library format.) |
||
Line 1: | Line 1: | ||
LightMessaging is an [[IPC|inter-process communication]] | LightMessaging is an [[IPC|inter-process communication]] header-only library by [[User:Rpetrich|Ryan Petrich]]. According to its [https://github.com/rpetrich/LightMessaging GitHub page], it is a "simple low-level replacement for CFMessagePort". | ||
== | == How to use this library == | ||
Headers are available from [https://github.com/rpetrich/LightMessaging LightMessaging's GitHub project]. If using Theos, place the headers in <code>$THEOS/include/LightMessaging</code>. | |||
== | === Include directive === | ||
<source lang="objc"> | |||
#import <LightMessaging/LightMessaging.h> | |||
</source> | |||
== | === RocketBootstrap === | ||
LightMessaging uses RocketBootstrap by default, so be sure to have it set up ([[RocketBootstrap#How_to_use_this_library|instructions here]]). To have LightMessaging **not** use RBS, add the following directive before including the LM header: | |||
<source lang="objc"> | <source lang="objc"> | ||
#define LIGHTMESSAGING_USE_ROCKETBOOTSTRAP 0 | |||
</source> | </source> | ||
== Server == | |||
It is recommended that this is done in somewhere better designed to act as a server, such as [[SpringBoard.app|SpringBoard]] or a [[Daemons|daemon]]. | |||
=== The Callback === | === The Callback === | ||
Line 28: | Line 31: | ||
void *info) | void *info) | ||
{ | { | ||
// get the reply port | |||
mach_port_t replyPort = request->head.msgh_remote_port; | |||
// sanity check | |||
if (size < sizeof(LMMessage)) { | |||
LMSendReply(replyPort, NULL, 0); | |||
LMResponseBufferFree((LMResponseBuffer *)request); | |||
return; | |||
} | |||
// get the data you recieved | |||
void *data = LMMessageGetData(request); | |||
// send some data back | |||
const char *msg = "lol"; | |||
LMSendReply(replyPort, msg, strlen(msg) + 1); | |||
// free the response buffer?? | |||
LMResponseBufferFree((LMResponseBuffer *)request); | |||
} | } | ||
</source> | |||
=== Starting the Server === | |||
<source lang="objc"> | |||
LMStartService("net.iphonedevwiki.some.server", CFRunLoopGetCurrent(), (CFMachPortCallBack)some_callback); | |||
</source> | </source> | ||
Line 56: | Line 66: | ||
// setup connection | // setup connection | ||
LMConnection connection = { | LMConnection connection = { | ||
MACH_PORT_NULL, | |||
"net.iphonedevwiki.some.server" | |||
}; | }; | ||
Line 74: | Line 84: | ||
There are some cool helper functions for if you want to send Images / PropertyLists / etc instead of just strings. Look at the [https://github.com/rpetrich/LightMessaging/blob/master/LightMessaging.h header file] or other examples to see how to use them. | There are some cool helper functions for if you want to send Images / PropertyLists / etc instead of just strings. Look at the [https://github.com/rpetrich/LightMessaging/blob/master/LightMessaging.h header file] or other examples to see how to use them. | ||
== Examples == | == Examples == |
Revision as of 21:46, 6 August 2016
LightMessaging is an inter-process communication header-only library by Ryan Petrich. According to its GitHub page, it is a "simple low-level replacement for CFMessagePort".
How to use this library
Headers are available from LightMessaging's GitHub project. If using Theos, place the headers in $THEOS/include/LightMessaging
.
Include directive
#import <LightMessaging/LightMessaging.h>
RocketBootstrap
LightMessaging uses RocketBootstrap by default, so be sure to have it set up (instructions here). To have LightMessaging **not** use RBS, add the following directive before including the LM header:
#define LIGHTMESSAGING_USE_ROCKETBOOTSTRAP 0
Server
It is recommended that this is done in somewhere better designed to act as a server, such as SpringBoard or a daemon.
The Callback
void some_callback(CFMachPortRef port,
LMMessage *request,
CFIndex size,
void *info)
{
// get the reply port
mach_port_t replyPort = request->head.msgh_remote_port;
// sanity check
if (size < sizeof(LMMessage)) {
LMSendReply(replyPort, NULL, 0);
LMResponseBufferFree((LMResponseBuffer *)request);
return;
}
// get the data you recieved
void *data = LMMessageGetData(request);
// send some data back
const char *msg = "lol";
LMSendReply(replyPort, msg, strlen(msg) + 1);
// free the response buffer??
LMResponseBufferFree((LMResponseBuffer *)request);
}
Starting the Server
LMStartService("net.iphonedevwiki.some.server", CFRunLoopGetCurrent(), (CFMachPortCallBack)some_callback);
Client
This can be run anywhere that has the appropriate permissions to send/receive data on a message port.
// setup connection
LMConnection connection = {
MACH_PORT_NULL,
"net.iphonedevwiki.some.server"
};
//send message
LMResponseBuffer buffer;
const char *msg = "lol wtf";
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);
Type helpers
There are some cool helper functions for if you want to send Images / PropertyLists / etc instead of just strings. Look at the header file or other examples to see how to use them.