Difference between revisions of "LightMessaging"

From iPhone Development Wiki
Jump to: navigation, search
(no)
(expand on a few things)
Line 1: Line 1:
LightMessaging is a sexy header-only [[IPC]] framework [[User:Rpetrich|Ryan Petrich]]. According to its [https://github.com/rpetrich/LightMessaging GitHub page], it is a "simple low-level replacement for CFMessagePort". It is usually used as an API to [[RocketBootstrap]].
+
LightMessaging is a header-only [[IPC]] framework [[User:Rpetrich|Ryan Petrich]]. According to its [https://github.com/rpetrich/LightMessaging GitHub page], it is a "simple low-level replacement for CFMessagePort". It is usually used as an API to [[RocketBootstrap]].
  
 
== Why should I use this? ==
 
== Why should I use this? ==
Line 8: Line 8:
 
== Including this in your project ==
 
== Including this in your project ==
  
Grab the headers from [https://github.com/rpetrich/LightMessaging rpetrich/LightMessaging], and <code>#import <LightMessaging/LightMessaging.h></code>. In most use cases, be sure to build your project with RocketBootstrap as a dependency.
+
Grab the headers from [https://github.com/rpetrich/LightMessaging rpetrich/LightMessaging], and <code>#import <LightMessaging/LightMessaging.h></code>. In most use cases, be sure to build your project with [[RocketBootstrap]] as a dependency.
  
 
== Server ==
 
== Server ==
  
It is recommended that this is only done in SpringBoard.
+
It is recommended that this is done in somewhere better designed to act as a server, such as SpringBoard or a daemon.
  
 
=== Starting the Server ===
 
=== Starting the Server ===
Line 51: Line 51:
 
== Client ==
 
== Client ==
  
This can be put literally anywhere.
+
This can be run anywhere that has the appropriate permissions to send/receive data on a message port.
  
 
<source lang="objc">
 
<source lang="objc">
Line 71: Line 71:
 
</source>
 
</source>
  
== ConsumeInteger / ConsumePropertyList / etc ==
+
== 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.
 
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.

Revision as of 01:56, 3 August 2016

LightMessaging is a header-only IPC framework Ryan Petrich. According to its GitHub page, it is a "simple low-level replacement for CFMessagePort". It is usually used as an API to RocketBootstrap.

Why should I use this?

  • Ryan Petrich.
  • Everyone else is using it.

Including this in your project

Grab the headers from rpetrich/LightMessaging, and #import <LightMessaging/LightMessaging.h>. In most use cases, be sure to build your project with RocketBootstrap as a dependency.

Server

It is recommended that this is done in somewhere better designed to act as a server, such as SpringBoard or a daemon.

Starting the Server

LMStartService("net.iphonedevwiki.some.server", CFRunLoopGetCurrent(), (CFMachPortCallBack)some_callback);

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);
}

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.


References

Working, Digestible Example Code

From rpetrich himself