Code Monkey home page Code Monkey logo

mpmessagepack's Introduction

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.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.