• Stars
    star
    179
  • Rank 214,039 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MessagePack implementation for Objective-C / msgpack.org[Objective-C]

MPMessagePack

MessagePack framework.

MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller.

Included Open Source Libraries

  • CMP is a C implementation of the MessagePack serialization format.

Swift Package Manager

.package(url: "https://github.com/gabriel/MPMessagePack", from: "1.6.0"),

Podfile

pod "MPMessagePack"

Cartfile

github "gabriel/MPMessagePack"

MPMessagePack

Writing

#import <MPMessagePack/MPMessagePack.h>

NSDictionary *dict =
  @{
    @"n": @(32134123),
    @"bool": @(YES),
    @"array": @[@(1.1f), @(2.1)],
    @"body": [NSData data],
  };

NSData *data = [dict mp_messagePack];

Or via MPMessagePackWriter.

NSError *error = nil;
NSData *data = [MPMessagePackWriter writeObject:dict error:&error];

If you need to use an ordered dictionary.

MPOrderedDictionary *dict = [[MPOrderedDictionary alloc] init];
[dict addEntriesFromDictionary:@{@"c": @(1), @"b": @(2), @"a": @(3)}];
[dict sortKeysUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSData *data = [dict mp_messagePack];

Reading

id obj = [MPMessagePackReader readData:data error:&error];
MPMessagePackReader *reader = [[MPMessagePackReader alloc] initWithData:data];
id obj1 = [reader read:&error]; // Read an object
id obj2 = [reader read:&error]; // Read another object

RPC

See msgpack-rpc.

It also supports a framing option where it will prefix the rpc message with the number of bytes (as a msgpack'ed number).

Client

Request with completion block:

MPMessagePackClient *client = [[MPMessagePackClient alloc] init];
[client openWithHost:@"localhost" port:93434 completion:^(NSError *error) {
  // If error we failed
  [client sendRequestWithMethod:@"test" params:@[@{@"arg": @(1)}] completion:^(NSError *error, id result) {
    // If error we failed
    // Otherwise the result
  }];
}];

You can also request synchronously:

NSError *error = nil;
id result = [client sendRequestWithMethod:@"test" params:@[@{@"arg": @(1)}] messageId:3 timeout:5.0 error:&error];
// error.code == MPRPCErrorRequestTimeout on timeout

And cancel in progress requests:

BOOL cancelled = [client cancelRequestWithMessageId:3];
// cancelled == YES, if the request was in progress and we cancelled it

Server

MPMessagePackServer *server = [[MPMessagePackServer alloc] initWithOptions:MPMessagePackOptionsFramed];

server.requestHandler = ^(NSString *method, id params, MPRequestCompletion completion) {
  if ([method isEqualToString:@"echo"]) {
    completion(nil, params);
  } else {
    completion(@{@"error": {@"description": @"Method not found"}}, nil);
  }
};

NSError *error = nil;
if (![server openWithPort:93434 error:&error]) {
  // Failed to open
}

Mantle Encoding

If you are using Mantle to encode objects to JSON (and then msgpack), you can specify a coder for the MPMessagePackClient:

@interface KBMantleCoder : NSObject <MPMessagePackCoder>
@end

@implementation KBMantleCoder
- (NSDictionary *)encodeModel:(id)obj {
  return [obj conformsToProtocol:@protocol(MTLJSONSerializing)] ? [MTLJSONAdapter JSONDictionaryFromModel:obj] : obj;
}
@end

Then in the client:

MPMessagePackClient *client = [[MPMessagePackClient alloc] init];
client.coder = [[KBMantleCoder alloc] init];

XPC

There is an experimental, but functional msgpack-rpc over XPC (see XPC directory). More details soon.

More Repositories

1

yajl-objc

Objective-C bindings for YAJL (Yet Another JSON Library) C library
C
670
star
2

CaptureRecord

User + Screen Recording iOS SDK
Objective-C
342
star
3

GHKit

Utilities and categories for Objective-C
Objective-C
260
star
4

as3httpclient

HTTP Client for AS3
ActionScript
243
star
5

ffmpeg-iphone-build

Build scripts for building ffmpeg on iPhone
Shell
221
star
6

capitate

Capistrano recipes, plugins and templates.
Ruby
78
star
7

NAChloride

Libsodium for Objective-C
Objective-C
67
star
8

font-detect-js

Font detection using Javascript and Flash
HTML
64
star
9

xcode-themes

XCode Themes
49
star
10

shrub

S3 Proxy for Google App Engine
Python
45
star
11

airake

Rake tasks and generators for Adobe AIR
Ruby
14
star
12

GHGLUtils

OpenGL Utilities for OSX and iOS
Objective-C
14
star
13

GHODictionary

Ordered dictionary
Objective-C
13
star
14

GRUnit

Unit testing for iOS
Objective-C
12
star
15

json-objc-perf

Basic perf tests for Objective-C JSON frameworks
Objective-C
12
star
16

turing-fluid-osx

OSX version of Turing Fluid demo http://cake23.de/turing-fluid.html
Objective-C
10
star
17

KBPGP

PGP for iOS/OSX using kbpgp.js and JavaScriptCore
JavaScript
10
star
18

TSTripleSec

Objective-C implementation of TripleSec
Objective-C
10
star
19

NACrypto

Advanced crypto library. You may want to use NAChloride (libsodium/NaCl) instead.
C
6
star
20

KBKeybaseAPI

Keybase.io API client for iOS/OSX
Objective-C
6
star
21

PointSpriteShader

Example OS X OpenGL project for displaying variable size point sprites with shaders
Objective-C
4
star
22

FFProcessing

C++
3
star
23

GHUITable

Extensions for UITableView and UICollectionView
Objective-C
3
star
24

YOCView

Simplifying UIView and UIViewController interactions.
Objective-C
3
star
25

gabriel.github.com

2
star
26

GHBigNum

Bignum for Obj-C (OpenSSL)
Objective-C
2
star
27

pre-commit-golang

Shell
2
star
28

status-back

For debugging connections
Python
1
star