You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2015/10/09 22:06:00 UTC

[3/7] thrift git commit: THRIFT-2905 Cocoa compiler should have option to produce "modern" Objective-C Client: Cocoa (ObjectiveC & Swift) Author: Kevin Wooten

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TCompactProtocol.m
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TCompactProtocol.m b/lib/cocoa/src/protocol/TCompactProtocol.m
index 45b0ef3..9b0ebb2 100644
--- a/lib/cocoa/src/protocol/TCompactProtocol.m
+++ b/lib/cocoa/src/protocol/TCompactProtocol.m
@@ -18,14 +18,13 @@
  */
 
 #import "TCompactProtocol.h"
-#import "TObjective-C.h"
-#import "TProtocolException.h"
-
-static const uint8_t COMPACT_PROTOCOL_ID = 0x82;
-static const uint8_t COMPACT_VERSION = 1;
-static const uint8_t COMPACT_VERSION_MASK = 0x1F; // 0001 1111
-static const uint8_t COMPACT_TYPE_MASK = 0xE0; // 1110 0000
-static const uint8_t COMPACT_TYPE_BITS = 0x07; // 0000 0111
+#import "TProtocolError.h"
+
+static const UInt8 COMPACT_PROTOCOL_ID = 0x82;
+static const UInt8 COMPACT_VERSION = 1;
+static const UInt8 COMPACT_VERSION_MASK = 0x1F; // 0001 1111
+static const UInt8 COMPACT_TYPE_MASK = 0xE0; // 1110 0000
+static const UInt8 COMPACT_TYPE_BITS = 0x07; // 0000 0111
 static const int COMPACT_TYPE_SHIFT_AMOUNT = 5;
 
 enum {
@@ -46,322 +45,442 @@ enum {
 
 @implementation TCompactProtocolFactory
 
-+ (TCompactProtocolFactory *) sharedFactory
++(TCompactProtocolFactory *) sharedFactory
 {
-  static TCompactProtocolFactory * gSharedFactory = nil;
+  static TCompactProtocolFactory *gSharedFactory = nil;
   if (gSharedFactory == nil) {
     gSharedFactory = [[TCompactProtocolFactory alloc] init];
   }
-  
+
   return gSharedFactory;
 }
 
-- (TCompactProtocol *) newProtocolOnTransport: (id <TTransport>) transport
+-(NSString *) protocolName
 {
-  return [[TCompactProtocol alloc] initWithTransport: transport];
+  return @"compact";
+}
+
+-(TCompactProtocol *) newProtocolOnTransport:(id <TTransport>)transport
+{
+  return [[TCompactProtocol alloc] initWithTransport:transport];
 }
 
 @end
 
-@implementation TCompactProtocol {
-  NSMutableArray * lastField;
-  short lastFieldId;
-  id <TTransport> mTransport;
-  
-  NSString * boolFieldName;
-  NSNumber * boolFieldType;
-  NSNumber * boolFieldId;
-  NSNumber * booleanValue;
-}
 
-- (id) init
+@interface TCompactProtocol ()
+
+@property(strong, nonatomic) id <TTransport> transport;
+
+@property(strong, nonatomic) NSMutableArray *lastField;
+@property(assign, nonatomic) short lastFieldId;
+
+@property(strong, nonatomic) NSString *boolFieldName;
+@property(strong, nonatomic) NSNumber *boolFieldType;
+@property(strong, nonatomic) NSNumber *boolFieldId;
+@property(strong, nonatomic) NSNumber *booleanValue;
+
+@property(strong, nonatomic) NSString *currentMessageName;
+
+@end
+
+
+@implementation TCompactProtocol
+
+-(id) init
 {
   self = [super init];
-  
+
   if (self != nil) {
-    lastField = [[NSMutableArray alloc] init];
+    _lastField = [[NSMutableArray alloc] init];
   }
-  
+
   return self;
 }
 
-- (id) initWithTransport: (id <TTransport>) transport
+-(id) initWithTransport:(id <TTransport>)aTransport
 {
   self = [self init];
-  
+
   if (self != nil) {
-    mTransport = [transport retain_stub];
+    _transport = aTransport;
   }
-  
+
   return self;
 }
 
-- (void) dealloc
+-(id <TTransport>) transport
 {
-  [lastField release_stub];
-  [mTransport release_stub];
-  [boolFieldName release_stub];
-  [boolFieldType release_stub];
-  [boolFieldId release_stub];
-  [booleanValue release_stub];
-  
-  [super dealloc_stub];
+  return _transport;
 }
 
-- (id <TTransport>) transport
+-(BOOL) writeByteDirect:(UInt8)n error:(NSError *__autoreleasing *)error
 {
-  return mTransport;
+  if (![_transport write:(UInt8 *)&n offset:0 length:1 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+  return YES;
 }
 
-- (void) writeByteDirect: (int8_t) n
+-(BOOL) writeVarint32:(UInt32)n error:(NSError *__autoreleasing *)error
 {
-  [mTransport write: (uint8_t *)&n offset: 0 length: 1];
-}
+  UInt8 i32buf[5] = {0};
+  UInt32 idx = 0;
 
-- (void)writeVarint32: (uint32_t) n
-{
-  uint8_t i32buf[5] = {0};
-  uint32_t idx = 0;
-  
   while (true) {
     if ((n & ~0x7F) == 0) {
-      i32buf[idx++] = (uint8_t)n;
+      i32buf[idx++] = (UInt8)n;
       break;
-    } else {
-      i32buf[idx++] = (uint8_t)((n & 0x7F) | 0x80);
+    }
+    else {
+      i32buf[idx++] = (UInt8)((n & 0x7F) | 0x80);
       n >>= 7;
     }
   }
-  
-  [mTransport write: i32buf offset: 0 length: idx];
+
+  if (![_transport write:i32buf offset:0 length:idx error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error
 {
-  [self writeByteDirect: COMPACT_PROTOCOL_ID];
-  [self writeByteDirect: (uint8_t)((COMPACT_VERSION & COMPACT_VERSION_MASK) |
-                                   ((((uint32_t)messageType) << COMPACT_TYPE_SHIFT_AMOUNT) & COMPACT_TYPE_MASK))];
-  [self writeVarint32: (uint32_t)sequenceID];
-  [self writeString: name];
+  if (![self writeByteDirect:COMPACT_PROTOCOL_ID error:error]) {
+    return NO;
+  }
+  if (![self writeByteDirect:(UInt8)((COMPACT_VERSION & COMPACT_VERSION_MASK) |
+                                     ((((UInt32)messageType) << COMPACT_TYPE_SHIFT_AMOUNT) & COMPACT_TYPE_MASK)) error:error])
+  {
+    return NO;
+  }
+  if (![self writeVarint32:(UInt32)sequenceID error:error]) {
+    return NO;
+  }
+  if (![self writeString:name error:error]) {
+    return NO;
+  }
+
+  _currentMessageName = name;
+
+  return YES;
 }
 
-- (void) writeStructBeginWithName: (NSString *) name
+-(BOOL) writeStructBeginWithName:(NSString *)name error:(NSError *__autoreleasing *)error
 {
-  [lastField addObject: [NSNumber numberWithShort: lastFieldId]];
-  lastFieldId = 0;
+  [_lastField addObject:@(_lastFieldId)];
+  _lastFieldId = 0;
+  return YES;
 }
 
-- (void) writeStructEnd
+-(BOOL) writeStructEnd:(NSError *__autoreleasing *)error
 {
-  lastFieldId = [[lastField lastObject] shortValue];
-  [lastField removeLastObject];
+  _lastFieldId = [_lastField.lastObject shortValue];
+  [_lastField removeLastObject];
+  return YES;
 }
 
-- (void) writeFieldBeginWithName: (NSString *) name
-                            type: (int) fieldType
-                         fieldID: (int) fieldID
+-(BOOL) writeFieldBeginWithName:(NSString *)name
+                           type:(SInt32)fieldType
+                        fieldID:(SInt32)fieldID
+                          error:(NSError *__autoreleasing *)error
 {
-  if (fieldType == TType_BOOL) {
-    boolFieldName = [name copy];
-    boolFieldType = [[NSNumber numberWithInt: fieldType] retain_stub];
-    boolFieldId = [[NSNumber numberWithInt: fieldID] retain_stub];
-  } else {
-    [self writeFieldBeginInternalWithName: name
-                                     type: fieldType
-                                  fieldID: fieldID
-                             typeOverride: 0xFF];
+  if (fieldType == TTypeBOOL) {
+    _boolFieldName = [name copy];
+    _boolFieldType = @(fieldType);
+    _boolFieldId = @(fieldID);
+    return YES;
+  }
+  else {
+    return [self writeFieldBeginInternalWithName:name
+                                            type:fieldType
+                                         fieldID:fieldID
+                                    typeOverride:0xFF
+                                           error:error];
   }
 }
 
-- (void) writeFieldBeginInternalWithName: (NSString *) name
-                                    type: (int) fieldType
-                                 fieldID: (int) fieldID
-                            typeOverride: (uint8_t) typeOverride
+-(BOOL) writeFieldBeginInternalWithName:(NSString *)name
+                                   type:(SInt32)fieldType
+                                fieldID:(SInt32)fieldID
+                           typeOverride:(UInt8)typeOverride
+                                  error:(NSError *__autoreleasing *)error
 {
-  uint8_t typeToWrite = typeOverride == 0xFF ? [self compactTypeForTType: fieldType] : typeOverride;
-  
+  UInt8 typeToWrite = typeOverride == 0xFF ? [self compactTypeForTType:fieldType] : typeOverride;
+
   // check if we can use delta encoding for the field id
-  if (fieldID > lastFieldId && fieldID - lastFieldId <= 15) {
+  if (fieldID > _lastFieldId && fieldID - _lastFieldId <= 15) {
     // Write them together
-    [self writeByteDirect: (fieldID - lastFieldId) << 4 | typeToWrite];
-  } else {
+    if (![self writeByteDirect:(fieldID - _lastFieldId) << 4 | typeToWrite error:error]) {
+      return NO;
+    }
+  }
+  else {
     // Write them separate
-    [self writeByteDirect: typeToWrite];
-    [self writeI16: fieldID];
+    if (![self writeByteDirect:typeToWrite error:error]) {
+      return NO;
+    }
+    if (![self writeI16:fieldID error:error]) {
+      return NO;
+    }
   }
-  
-  lastFieldId = fieldID;
+
+  _lastFieldId = fieldID;
+
+  return YES;
 }
 
-- (void) writeFieldStop
+-(BOOL) writeFieldStop:(NSError *__autoreleasing *)error
 {
-  [self writeByteDirect: TCType_STOP];
+  return [self writeByteDirect:TCType_STOP error:error];
 }
 
-- (void) writeMapBeginWithKeyType: (int) keyType
-                        valueType: (int) valueType
-                             size: (int) size
+-(BOOL) writeMapBeginWithKeyType:(SInt32)keyType
+                       valueType:(SInt32)valueType
+                            size:(SInt32)size
+                           error:(NSError *__autoreleasing *)error
 {
   if (size == 0) {
-    [self writeByteDirect: 0];
-  } else {
-    [self writeVarint32: (uint32_t)size];
-    [self writeByteDirect: [self compactTypeForTType: keyType] << 4 | [self compactTypeForTType: valueType]];
+    if (![self writeByteDirect:0 error:error]) {
+      return NO;
+    }
   }
+  else {
+    if (![self writeVarint32:(UInt32)size error:error]) {
+      return NO;
+    }
+    if (![self writeByteDirect:[self compactTypeForTType:keyType] << 4 | [self compactTypeForTType:valueType] error:error]) {
+      return NO;
+    }
+  }
+  return YES;
 }
 
-- (void) writeListBeginWithElementType: (int) elementType
-                                  size: (int) size
+-(BOOL) writeListBeginWithElementType:(SInt32)elementType
+                                 size:(SInt32)size
+                                error:(NSError *__autoreleasing *)error
 {
-  [self writeCollectionBeginWithElementType: elementType size: size];
+  return [self writeCollectionBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) writeSetBeginWithElementType: (int) elementType
-                                 size: (int) size
+-(BOOL) writeSetBeginWithElementType:(SInt32)elementType
+                                size:(SInt32)size
+                               error:(NSError *__autoreleasing *)error
 {
-  [self writeCollectionBeginWithElementType: elementType size: size];
+  return [self writeCollectionBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) writeBool: (BOOL) b
+-(BOOL) writeBool:(BOOL)b error:(NSError *__autoreleasing *)error
 {
-  if (boolFieldId != nil && boolFieldName != nil && boolFieldType != nil) {
+  BOOL result;
+  if (_boolFieldId != nil && _boolFieldName != nil && _boolFieldType != nil) {
     // we haven't written the field header yet
-    [self writeFieldBeginInternalWithName: boolFieldName
-                                     type: [boolFieldType intValue]
-                                  fieldID: [boolFieldId intValue]
-                             typeOverride: b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE];
-    
-    [boolFieldId release_stub];
-    [boolFieldName release_stub];
-    [boolFieldType release_stub];
-    
-    boolFieldId = nil;
-    boolFieldName = nil;
-    boolFieldType = nil;
-  } else {
+    result = [self writeFieldBeginInternalWithName:_boolFieldName
+                                              type:_boolFieldType.intValue
+                                           fieldID:_boolFieldId.intValue
+                                      typeOverride:b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE
+                                             error:error];
+    _boolFieldId = nil;
+    _boolFieldName = nil;
+    _boolFieldType = nil;
+  }
+  else {
     // we're not part of a field, so just Write the value.
-    [self writeByteDirect: b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE];
+    result = [self writeByteDirect:b ? TCType_BOOLEAN_TRUE : TCType_BOOLEAN_FALSE error:error];
   }
+  return result;
 }
 
-- (void) writeByte: (uint8_t) value
+-(BOOL) writeByte:(UInt8)value error:(NSError *__autoreleasing *)error
 {
-  [self writeByteDirect: value];
+  return [self writeByteDirect:value error:error];
 }
 
-- (void) writeI16: (int16_t) value
+-(BOOL) writeI16:(SInt16)value error:(NSError *__autoreleasing *)error
 {
-  [self writeVarint32: [self i32ToZigZag: value]];
+  return [self writeVarint32:[self i32ToZigZag:value] error:error];
 }
 
-- (void) writeI32: (int32_t) value
+-(BOOL) writeI32:(SInt32)value error:(NSError *__autoreleasing *)error
 {
-  [self writeVarint32: [self i32ToZigZag: value]];
+  return [self writeVarint32:[self i32ToZigZag:value] error:error];
 }
 
-- (void) writeI64: (int64_t) value
+-(BOOL) writeI64:(SInt64)value error:(NSError *__autoreleasing *)error
 {
-  [self writeVarint64: [self i64ToZigZag: value]];
+  return [self writeVarint64:[self i64ToZigZag:value] error:error];
 }
 
-- (void) writeDouble: (double) value
+-(BOOL) writeDouble:(double)value error:(NSError *__autoreleasing *)error
 {
-  //Safe bit-casting double->uint64
-  
-  uint64_t bits = 0;
+  // Safe bit-casting double->uint64
+
+  UInt64 bits = 0;
   memcpy(&bits, &value, 8);
-  
+
   bits = OSSwapHostToLittleInt64(bits);
-  
-  [mTransport write: (uint8_t *)&bits offset: 0 length: 8];
+
+  if (![_transport write:(UInt8 *)&bits offset:0 length:8 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
-- (void) writeString: (NSString *) value
+-(BOOL) writeString:(NSString *)value error:(NSError *__autoreleasing *)error
 {
-  [self writeBinary: [value dataUsingEncoding: NSUTF8StringEncoding]];
+  return [self writeBinary:[value dataUsingEncoding:NSUTF8StringEncoding] error:error];
 }
 
-- (void) writeBinary: (NSData *) data
+-(BOOL) writeBinary:(NSData *)data error:(NSError *__autoreleasing *)error
 {
-  [self writeVarint32: (uint32_t)data.length];
-  [mTransport write: data.bytes offset: 0 length: data.length];
+  if (![self writeVarint32:(UInt32)data.length error:error]) {
+    return NO;
+  }
+  if (![_transport write:data.bytes offset:0 length:(UInt32)data.length error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+  return YES;
 }
 
-- (void) writeMessageEnd {}
-- (void) writeMapEnd {}
-- (void) writeListEnd {}
-- (void) writeSetEnd {}
-- (void) writeFieldEnd {}
+-(BOOL) writeMessageEnd:(NSError *__autoreleasing *)error
+{
+  _currentMessageName = nil;
+  return YES;
+}
 
-- (void) writeCollectionBeginWithElementType: (int) elementType
-                                        size: (int) size
+-(BOOL) writeMapEnd:(NSError *__autoreleasing *)error
 {
+  return YES;
+}
+
+-(BOOL) writeListEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) writeSetEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) writeFieldEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+
+-(BOOL) writeCollectionBeginWithElementType:(SInt32)elementType
+                                       size:(SInt32)size
+                                      error:(NSError *__autoreleasing *)error
+{
+  UInt8 ctypeElement = [self compactTypeForTType:elementType];
+
   if (size <= 14) {
-    [self writeByteDirect: size << 4 | [self compactTypeForTType: elementType]];
-  } else {
-    [self writeByteDirect: 0xf0 | [self compactTypeForTType: elementType]];
-    [self writeVarint32: (uint32_t)size];
+    if (![self writeByteDirect:size << 4 | ctypeElement error:error]) {
+      return NO;
+    }
   }
+  else {
+    if (![self writeByteDirect:0xf0 | ctypeElement error:error]) {
+      return NO;
+    }
+    if (![self writeVarint32:(UInt32)size error:error]) {
+      return NO;
+    }
+  }
+  return YES;
 }
 
-- (void) writeVarint64: (uint64_t) n
+-(BOOL) writeVarint64:(UInt64)n error:(NSError *__autoreleasing *)error
 {
-  uint8_t varint64out[10] = {0};
+  UInt8 varint64out[10] = {0};
   int idx = 0;
-  
+
   while (true) {
     if ((n & ~0x7FL) == 0) {
-      varint64out[idx++] = (uint8_t)n;
+      varint64out[idx++] = (UInt8)n;
       break;
-    } else {
-      varint64out[idx++] = (uint8_t)((n & 0x7F) | 0x80);
+    }
+    else {
+      varint64out[idx++] = (UInt8)((n & 0x7F) | 0x80);
       n >>= 7;
     }
   }
-  
-  [mTransport write: varint64out offset: 0 length: idx];
+
+  if (![_transport write:varint64out offset:0 length:idx error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport write failed");
+  }
+
+  return YES;
 }
 
-- (uint32_t) i32ToZigZag: (int32_t) n
+-(UInt32) i32ToZigZag:(SInt32)n
 {
   /*
-   ZigZag encoding maps signed integers to unsigned integers so that
-   numbers with a small absolute value (for instance, -1) have
-   a small varint encoded value too. It does this in a way that
-   "zig-zags" back and forth through the positive and negative integers,
-   so that -1 is encoded as 1, 1 is encoded as 2, -2 is encoded as 3, and so on
+     ZigZag encoding maps signed integers to unsigned integers so that
+     numbers with a small absolute value (for instance, -1) have
+     a small varint encoded value too. It does this in a way that
+     "zig-zags" back and forth through the positive and negative integers,
+     so that -1 is encoded as 1, 1 is encoded as 2, -2 is encoded as 3, and so
+         on
    */
-  return (uint32_t)(n << 1) ^ (uint32_t)(n >> 31);
+  return (UInt32)(n << 1) ^ (UInt32)(n >> 31);
 }
 
-- (uint64_t) i64ToZigZag: (int64_t) n
+-(UInt64) i64ToZigZag:(SInt64)n
 {
-  return (uint64_t)(n << 1) ^ (uint64_t)(n >> 63);
+  return (UInt64)(n << 1) ^ (UInt64)(n >> 63);
 }
 
-- (void) readMessageBeginReturningName: (NSString **) pname
-                                  type: (int *) ptype
-                            sequenceID: (int *) psequenceID
+-(BOOL) readMessageBeginReturningName:(NSString **)pname
+                                 type:(SInt32 *)ptype
+                           sequenceID:(SInt32 *)psequenceID
+                                error:(NSError *__autoreleasing *)error
 {
-  uint8_t protocolId = [self readByte];
+  UInt8 protocolId;
+  if (![self readByte:&protocolId error:error]) {
+    return NO;
+  }
+
   if (protocolId != COMPACT_PROTOCOL_ID) {
-    @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                          reason: [NSString stringWithFormat: @"Expected protocol id %X but got %X", COMPACT_PROTOCOL_ID, protocolId]];
+    if (error) {
+      *error = [NSError errorWithDomain:TProtocolErrorDomain
+                                   code:TProtocolErrorUnknown
+                               userInfo:@{TProtocolErrorExtendedErrorKey: @(TProtocolExtendedErrorMismatchedProtocol),
+                                          TProtocolErrorExpectedIdKey: @(COMPACT_PROTOCOL_ID)}];
+    }
+    return NO;
   }
-  
-  uint8_t versionAndType = [self readByte];
-  uint8_t version = versionAndType & COMPACT_VERSION_MASK;
+
+  UInt8 versionAndType;
+  if (![self readByte:&versionAndType error:error]) {
+    return NO;
+  }
+
+  UInt8 version = versionAndType & COMPACT_VERSION_MASK;
   if (version != COMPACT_VERSION) {
-    @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                          reason: [NSString stringWithFormat: @"Expected version %d but got %d", COMPACT_VERSION, version]];
+    if (error) {
+      *error = [NSError errorWithDomain:TProtocolErrorDomain
+                                   code:TProtocolErrorBadVersion
+                               userInfo:@{TProtocolErrorExpectedVersionKey: @(COMPACT_VERSION)}];
+    }
+    return NO;
   }
-  
+
   int type = (versionAndType >> COMPACT_TYPE_SHIFT_AMOUNT) & COMPACT_TYPE_BITS;
-  int sequenceID = (int)[self readVarint32];
-  NSString* name = [self readString];
-  
+  UInt32 sequenceID;
+  if (![self readVarint32:&sequenceID error:error]) {
+    return NO;
+  }
+  NSString *name;
+  if (![self readString:&name error:error]) {
+    return NO;
+  }
+
   if (ptype != NULL) {
     *ptype = type;
   }
@@ -371,59 +490,74 @@ enum {
   if (pname != NULL) {
     *pname = name;
   }
+  return YES;
 }
 
-- (void) readStructBeginReturningName: (NSString **) pname
+-(BOOL) readStructBeginReturningName:(NSString **)pname error:(NSError *__autoreleasing *)error
 {
-  [lastField addObject: [NSNumber numberWithShort: lastFieldId]];
-  lastFieldId = 0;
-  
+  [_lastField addObject:@(_lastFieldId)];
+  _lastFieldId = 0;
+
   if (pname != NULL) {
     *pname = @"";
   }
+
+  return YES;
 }
 
-- (void) readStructEnd
+-(BOOL) readStructEnd:(NSError *__autoreleasing *)error
 {
-  lastFieldId = [[lastField lastObject] shortValue];
-  [lastField removeLastObject];
+  _lastFieldId = [_lastField.lastObject shortValue];
+  [_lastField removeLastObject];
+  return YES;
 }
 
-- (void) readFieldBeginReturningName: (NSString **) pname
-                                type: (int *) pfieldType
-                             fieldID: (int *) pfieldID
+-(BOOL) readFieldBeginReturningName:(NSString **)pname
+                               type:(SInt32 *)pfieldType
+                            fieldID:(SInt32 *)pfieldID
+                              error:(NSError *__autoreleasing *)error
 {
-  uint8_t byte = [self readByte];
-  uint8_t type = byte & 0x0f;
-  
+  UInt8 byte;
+  if (![self readByte:&byte error:error]) {
+    return NO;
+  }
+
+  UInt8 type = byte & 0x0f;
+
   // if it's a stop, then we can return immediately, as the struct is over.
   if (type == TCType_STOP) {
     if (pname != NULL) {
       *pname = @"";
     }
     if (pfieldType != NULL) {
-      *pfieldType = TType_STOP;
+      *pfieldType = TTypeSTOP;
     }
     if (pfieldID != NULL) {
       *pfieldID = 0;
     }
-    return;
+    return YES;
   }
-  
+
   short fieldId = 0;
-  
+
   // mask off the 4 MSB of the type header. it could contain a field id delta.
   short modifier = (byte & 0xf0) >> 4;
   if (modifier == 0) {
     // not a delta. look ahead for the zigzag varint field id.
-    fieldId = [self readI16];
-  } else {
+    if (![self readI16:&fieldId error:error]) {
+      return NO;
+    }
+  }
+  else {
     // has a delta. add the delta to the last Read field id.
-    fieldId = lastFieldId + modifier;
+    fieldId = _lastFieldId + modifier;
   }
-  
-  int fieldType = [self ttypeForCompactType: type];
-  
+
+  UInt8 fieldType;
+  if (![self ttype:&fieldType forCompactType:type error:error]) {
+    return NO;
+  }
+
   if (pname != NULL) {
     *pname = @"";
   }
@@ -433,31 +567,47 @@ enum {
   if (pfieldID != NULL) {
     *pfieldID = fieldId;
   }
-  
+
   // if this happens to be a boolean field, the value is encoded in the type
   if (type == TCType_BOOLEAN_TRUE ||
-      type == TCType_BOOLEAN_FALSE) {
+      type == TCType_BOOLEAN_FALSE)
+  {
     // save the boolean value in a special instance variable.
-    booleanValue = [[NSNumber numberWithBool: type == TCType_BOOLEAN_TRUE] retain_stub];
+    _booleanValue = [NSNumber numberWithBool:type == TCType_BOOLEAN_TRUE];
   }
-  
+
   // push the new field onto the field stack so we can keep the deltas going.
-  lastFieldId = fieldId;
+  _lastFieldId = fieldId;
+
+  return YES;
 }
 
-- (void) readMapBeginReturningKeyType: (int *) pkeyType
-                            valueType: (int *) pvalueType
-                                 size: (int *) psize
+-(BOOL) readMapBeginReturningKeyType:(SInt32 *)pkeyType
+                           valueType:(SInt32 *)pvalueType
+                                size:(SInt32 *)psize
+                               error:(NSError *__autoreleasing *)error
 {
-  uint8_t keyAndValueType = 0;
-  int size = (int)[self readVarint32];
+  UInt8 keyAndValueType = 0;
+  UInt32 size;
+  if (![self readVarint32:&size error:error]) {
+    return NO;
+  }
   if (size != 0) {
-    keyAndValueType = [self readByte];
+    if (![self readByte:&keyAndValueType error:error]) {
+      return NO;
+    }
+  }
+
+  UInt8 keyType;
+  if (![self ttype:&keyType forCompactType:keyAndValueType >> 4 error:error]) {
+    return NO;
   }
-  
-  int keyType = [self ttypeForCompactType: keyAndValueType >> 4];
-  int valueType = [self ttypeForCompactType: keyAndValueType & 0xf];
-  
+
+  UInt8 valueType;
+  if (![self ttype:&valueType forCompactType:keyAndValueType & 0xf error:error]) {
+    return NO;
+  }
+
   if (pkeyType != NULL) {
     *pkeyType = keyType;
   }
@@ -467,220 +617,366 @@ enum {
   if (psize != NULL) {
     *psize = size;
   }
+
+  return YES;
 }
 
-- (void) readListBeginReturningElementType: (int *) pelementType
-                                      size: (int *) psize
+-(BOOL) readListBeginReturningElementType:(SInt32 *)pelementType
+                                     size:(SInt32 *)psize
+                                    error:(NSError *__autoreleasing *)error
 {
-  uint8_t size_and_type = [self readByte];
-  int size = (size_and_type >> 4) & 0x0f;
+  UInt8 sizeAndType;
+  if (![self readByte:&sizeAndType error:error]) {
+    return NO;
+  }
+
+  UInt32 size = (sizeAndType >> 4) & 0x0f;
   if (size == 15) {
-    size = (int)[self readVarint32];
+    if (![self readVarint32:&size error:error]) {
+      return NO;
+    }
   }
-  
-  int elementType = [self ttypeForCompactType: size_and_type & 0x0f];
-  
+
+  UInt8 elementType;
+  if (![self ttype:&elementType forCompactType:sizeAndType & 0x0f error:error]) {
+    return NO;
+  }
+
   if (pelementType != NULL) {
     *pelementType = elementType;
   }
   if (psize != NULL) {
     *psize = size;
   }
+
+  return YES;
 }
 
-- (void) readSetBeginReturningElementType: (int *) pelementType
-                                     size: (int *) psize
+-(BOOL) readSetBeginReturningElementType:(SInt32 *)pelementType
+                                    size:(SInt32 *)psize
+                                   error:(NSError *__autoreleasing *)error
 {
-  [self readListBeginReturningElementType: pelementType size: psize];
+  return [self readListBeginReturningElementType:pelementType size:psize error:error];
 }
 
-- (BOOL) readBool
+-(BOOL) readBool:(BOOL *)value error:(NSError *__autoreleasing *)error
 {
-  if (booleanValue != nil) {
-    BOOL result = [booleanValue boolValue];
-    [booleanValue release_stub];
-    booleanValue = nil;
-    return result;
-  } else {
-    return [self readByte] == TCType_BOOLEAN_TRUE;
+  if (_booleanValue != nil) {
+
+    BOOL result = _booleanValue.boolValue;
+    _booleanValue = nil;
+
+    *value = result;
+  }
+  else {
+
+    UInt8 result;
+    if (![self readByte:&result error:error]) {
+      return NO;
+    }
+
+    *value = result == TCType_BOOLEAN_TRUE;
   }
+
+  return YES;
 }
 
-- (uint8_t) readByte
+-(BOOL) readByte:(UInt8 *)value error:(NSError *__autoreleasing *)error
 {
-  uint8_t buf = 0;
-  [mTransport readAll: &buf offset: 0 length: 1];
-  return buf;
+  if (![_transport readAll:value offset:0 length:1 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+  return YES;
 }
 
-- (int16_t) readI16
+-(BOOL) readI16:(SInt16 *)value error:(NSError *__autoreleasing *)error
 {
-  return (int16_t)[self zigZagToi32: [self readVarint32]];
+  UInt32 v;
+  if (![self readVarint32:&v error:error]) {
+    return NO;
+  }
+
+  if (value) {
+    *value = (SInt16)[self zigZagToi32:v];
+  }
+
+  return YES;
 }
 
-- (int32_t) readI32
+-(BOOL) readI32:(SInt32 *)value error:(NSError *__autoreleasing *)error
 {
-  return [self zigZagToi32: [self readVarint32]];
+  UInt32 v;
+  if (![self readVarint32:&v error:error]) {
+    return NO;
+  }
+
+  if (value) {
+    *value = [self zigZagToi32:v];
+  }
+
+  return YES;
 }
 
-- (int64_t) readI64
+-(BOOL) readI64:(SInt64 *)value error:(NSError *__autoreleasing *)error
 {
-  return [self zigZagToi64: [self readVarint64]];
+  UInt64 v;
+  if (![self readVarint64:&v error:error]) {
+    return NO;
+  }
+
+  if (value) {
+    *value = [self zigZagToi64:v];
+  }
+
+  return YES;
 }
 
-- (double) readDouble
+-(BOOL) readDouble:(double *)value error:(NSError *__autoreleasing *)error
 {
-  uint64_t bits = 0;
-  [mTransport readAll: (uint8_t *)&bits offset: 0 length: 8];
+  UInt64 bits;
+  if (![_transport readAll:(UInt8 *)&bits offset:0 length:8 error:error]) {
+    PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+  }
+
   bits = OSSwapLittleToHostInt64(bits);
-  
-  double result = 0;
-  memcpy(&result, &bits, 8);
-  
-  return result;
+
+  if (value) {
+    memcpy(value, &bits, sizeof(bits));
+  }
+
+  return YES;
 }
 
-- (NSString *) readString
+-(BOOL) readString:(NSString *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-  int length = (int)[self readVarint32];
-  if (length == 0) {
-    return @"";
+  UInt32 length;
+  if (![self readVarint32:&length error:error]) {
+    return NO;
+  }
+
+  NSString *result;
+
+  if (length != 0) {
+
+    NSData *data;
+    if (![self readBinaryOfLength:length data:&data error:error]) {
+      return NO;
+    }
+
+    result = [[NSString alloc] initWithData:data
+                                   encoding:NSUTF8StringEncoding];
+  }
+  else {
+    result = @"";
   }
-  
-  return [[[NSString alloc] initWithData: [self readBinary: length]
-                                encoding: NSUTF8StringEncoding] autorelease_stub];
+
+  if (value) {
+    *value = result;
+  }
+
+  return YES;
 }
 
-- (NSData *) readBinary
+-(BOOL) readBinary:(NSData *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-  return [self readBinary: (int)[self readVarint32]];
+  UInt32 length;
+  if (![self readVarint32:&length error:error]) {
+    return NO;
+  }
+
+  return [self readBinaryOfLength:length data:value error:error];
 }
 
-- (NSData *) readBinary: (int) length
+-(BOOL) readBinaryOfLength:(UInt32)length data:(NSData *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-  if (length == 0) {
-    return [NSData data];
+  NSData *result;
+
+  if (length != 0) {
+
+    NSMutableData *buf = [NSMutableData dataWithLength:length];
+    if (![_transport readAll:buf.mutableBytes offset:0 length:length error:error]) {
+      PROTOCOL_TRANSPORT_ERROR(NO, error, @"Transport read failed");
+    }
+
+    result = buf;
+  }
+  else {
+
+    result = [NSData data];
+
+  }
+
+  if (value) {
+    *value = result;
   }
-  
-  NSMutableData* buf = [NSMutableData dataWithLength: length];
-  [mTransport readAll: buf.mutableBytes offset: 0 length: length];
-  return buf;
+
+  return YES;
 }
 
-- (void) readMessageEnd {}
-- (void) readFieldEnd {}
-- (void) readMapEnd {}
-- (void) readListEnd {}
-- (void) readSetEnd {}
+-(BOOL) readMessageEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readFieldEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readMapEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readListEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
+-(BOOL) readSetEnd:(NSError *__autoreleasing *)error
+{
+  return YES;
+}
 
-- (uint32_t) readVarint32
+-(BOOL) readVarint32:(UInt32 *)value error:(NSError *__autoreleasing *)error
 {
-  uint32_t result = 0;
+  UInt32 result = 0;
   int shift = 0;
-  
+
   while (true) {
-    uint8_t byte = [self readByte];
-    result |= (uint32_t)(byte & 0x7f) << shift;
+
+    UInt8 byte;
+    if (![self readByte:&byte error:error]) {
+      return NO;
+    }
+
+    result |= (UInt32)(byte & 0x7f) << shift;
     if (!(byte & 0x80)) {
       break;
     }
-    
+
     shift += 7;
   }
-  return result;
+
+  if (value) {
+    *value = result;
+  }
+
+  return YES;
 }
 
-- (uint64_t) readVarint64
+-(BOOL) readVarint64:(UInt64 *)value error:(NSError *__autoreleasing *)error
 {
   int shift = 0;
-  uint64_t result = 0;
-  
+  UInt64 result = 0;
+
   while (true) {
-    uint8_t byte = [self readByte];
-    result |= (uint64_t)(byte & 0x7f) << shift;
+
+    UInt8 byte;
+    if (![self readByte:&byte error:error]) {
+      return NO;
+    }
+
+    result |= (UInt64)(byte & 0x7f) << shift;
     if (!(byte & 0x80)) {
       break;
     }
-    
+
     shift += 7;
   }
-  
-  return result;
+
+  if (value) {
+    *value = result;
+  }
+
+  return YES;
+}
+
+-(SInt32) zigZagToi32:(UInt32)n
+{
+  return (SInt32)(n >> 1) ^ (-(SInt32)(n & 1));
+}
+
+-(SInt64) zigZagToi64:(UInt64)n
+{
+  return (SInt64)(n >> 1) ^ (-(SInt64)(n & 1));
 }
 
-- (int32_t) zigZagToi32: (uint32_t) n
-{
-  return (int32_t)(n >> 1) ^ (-(int32_t)(n & 1));
-}
-
-- (int64_t) zigZagToi64: (uint64_t) n
-{
-  return (int64_t)(n >> 1) ^ (-(int64_t)(n & 1));
-}
-
-- (uint8_t) ttypeForCompactType: (uint8_t) type
-{
-  switch (type & 0x0f) {
-    case TCType_STOP:
-      return TType_STOP;
-      
-    case TCType_BOOLEAN_FALSE:
-    case TCType_BOOLEAN_TRUE:
-      return TType_BOOL;
-      
-    case TCType_BYTE:
-      return TType_BYTE;
-      
-    case TCType_I16:
-      return TType_I16;
-      
-    case TCType_I32:
-      return TType_I32;
-      
-    case TCType_I64:
-      return TType_I64;
-      
-    case TCType_DOUBLE:
-      return TType_DOUBLE;
-      
-    case TCType_BINARY:
-      return TType_STRING;
-      
-    case TCType_LIST:
-      return TType_LIST;
-      
-    case TCType_SET:
-      return TType_SET;
-      
-    case TCType_MAP:
-      return TType_MAP;
-      
-    case TCType_STRUCT:
-      return TType_STRUCT;
-      
-    default:
-      @throw [TProtocolException exceptionWithName: @"TProtocolException"
-                                            reason: [NSString stringWithFormat: @"Don't know what type: %d", (uint8_t)(type & 0x0F)]];
-  }
-}
-
-- (uint8_t) compactTypeForTType: (uint8_t) ttype
-{
-  static uint8_t ttypeToCompactType[] = {
-    [TType_STOP] = TCType_STOP,
-    [TType_BOOL] = TCType_BOOLEAN_FALSE,
-    [TType_BYTE] = TCType_BYTE,
-    [TType_DOUBLE] = TCType_DOUBLE,
-    [TType_I16] = TCType_I16,
-    [TType_I32] = TCType_I32,
-    [TType_I64] = TCType_I64,
-    [TType_STRING] = TCType_BINARY,
-    [TType_STRUCT] = TCType_STRUCT,
-    [TType_MAP] = TCType_MAP,
-    [TType_SET] = TCType_SET,
-    [TType_LIST] = TCType_LIST
+-(BOOL) ttype:(UInt8 *)ttype forCompactType:(UInt8)ctype error:(NSError *__autoreleasing *)error
+{
+  switch (ctype & 0x0f) {
+  case TCType_STOP:
+    *ttype = TTypeSTOP;
+    return YES;
+
+  case TCType_BOOLEAN_FALSE:
+  case TCType_BOOLEAN_TRUE:
+    *ttype = TTypeBOOL;
+    return YES;
+
+  case TCType_BYTE:
+    *ttype = TTypeBYTE;
+    return YES;
+
+  case TCType_I16:
+    *ttype = TTypeI16;
+    return YES;
+
+  case TCType_I32:
+    *ttype = TTypeI32;
+    return YES;
+
+  case TCType_I64:
+    *ttype = TTypeI64;
+    return YES;
+
+  case TCType_DOUBLE:
+    *ttype = TTypeDOUBLE;
+    return YES;
+
+  case TCType_BINARY:
+    *ttype = TTypeSTRING;
+    return YES;
+
+  case TCType_LIST:
+    *ttype = TTypeLIST;
+    return YES;
+
+  case TCType_SET:
+    *ttype = TTypeSET;
+    return YES;
+
+  case TCType_MAP:
+    *ttype = TTypeMAP;
+    return YES;
+
+  case TCType_STRUCT:
+    *ttype = TTypeSTRUCT;
+    return YES;
+
+  default:
+    if (error) {
+      *error = [NSError errorWithDomain:TProtocolErrorDomain
+                                   code:TProtocolErrorUnknown
+                               userInfo:@{TProtocolErrorTypeKey: @((UInt8)(ctype & 0x0F))}];
+    }
+    return NO;
+  }
+}
+
+-(UInt8) compactTypeForTType:(UInt8)ttype
+{
+  static UInt8 ttypeToCompactType[] = {
+    [TTypeSTOP] = TCType_STOP,
+    [TTypeBOOL] = TCType_BOOLEAN_FALSE,
+    [TTypeBYTE] = TCType_BYTE,
+    [TTypeDOUBLE] = TCType_DOUBLE,
+    [TTypeI16] = TCType_I16,
+    [TTypeI32] = TCType_I32,
+    [TTypeI64] = TCType_I64,
+    [TTypeSTRING] = TCType_BINARY,
+    [TTypeSTRUCT] = TCType_STRUCT,
+    [TTypeMAP] = TCType_MAP,
+    [TTypeSET] = TCType_SET,
+    [TTypeLIST] = TCType_LIST
   };
-  
+
   return ttypeToCompactType[ttype];
 }
 

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TMultiplexedProtocol.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TMultiplexedProtocol.h b/lib/cocoa/src/protocol/TMultiplexedProtocol.h
index f298459..b8ce361 100644
--- a/lib/cocoa/src/protocol/TMultiplexedProtocol.h
+++ b/lib/cocoa/src/protocol/TMultiplexedProtocol.h
@@ -21,13 +21,18 @@
 
 #import "TProtocolDecorator.h"
 
-FOUNDATION_EXPORT NSString *const MULTIPLEXED_SERVICE_SEPERATOR;
+NS_ASSUME_NONNULL_BEGIN
 
-@interface TMultiplexedProtocol : TProtocolDecorator {
-    NSString * mServiceName;
-}
 
-- (id) initWithProtocol: (id <TProtocol>) protocol
-            serviceName: (NSString *) name;
+extern NSString *TMultiplexedProtocolSeperator;
+
+
+@interface TMultiplexedProtocol : TProtocolDecorator
+
+-(id) initWithProtocol:(id <TProtocol>)protocol
+           serviceName:(NSString *)name;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TMultiplexedProtocol.m
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TMultiplexedProtocol.m b/lib/cocoa/src/protocol/TMultiplexedProtocol.m
index 49095e3..5838c57 100644
--- a/lib/cocoa/src/protocol/TMultiplexedProtocol.m
+++ b/lib/cocoa/src/protocol/TMultiplexedProtocol.m
@@ -20,48 +20,47 @@
 #import "TMultiplexedProtocol.h"
 
 #import "TProtocol.h"
-#import "TObjective-C.h"
 
-NSString *const MULTIPLEXED_SERVICE_SEPERATOR = @":";
+NSString *TMultiplexedProtocolSeperator = @":";
 
-@implementation TMultiplexedProtocol
 
-- (id) initWithProtocol: (id <TProtocol>) protocol
-            serviceName: (NSString *) name
-{
-    self = [super initWithProtocol:protocol];
+@interface TMultiplexedProtocol ()
 
-    if (self) {
-        mServiceName = [name retain_stub];
-    }
-    return self;
-}
+@property(strong, nonatomic) NSString *serviceName;
+
+@end
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID
+
+@implementation TMultiplexedProtocol
+
+-(id) initWithProtocol:(id <TProtocol>)protocol
+           serviceName:(NSString *)name
 {
-    switch (messageType) {
-        case TMessageType_CALL:
-        case TMessageType_ONEWAY:
-            {
-                NSMutableString * serviceFunction = [[NSMutableString alloc] initWithString:mServiceName];
-                [serviceFunction appendString:MULTIPLEXED_SERVICE_SEPERATOR];
-                [serviceFunction appendString:name];
-                [super writeMessageBeginWithName:serviceFunction type:messageType sequenceID:sequenceID];
-                [serviceFunction release_stub];
-            }
-            break;
-        default:
-            [super writeMessageBeginWithName:name type:messageType sequenceID:sequenceID];
-            break;
-    }
+  self = [super initWithProtocol:protocol];
+  if (self) {
+    _serviceName = name;
+  }
+  return self;
 }
 
-- (void) dealloc
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error
 {
-    [mServiceName release_stub];
-    [super dealloc_stub];
+  switch (messageType) {
+  case TMessageTypeCALL:
+  case TMessageTypeONEWAY: {
+    NSMutableString *serviceFunction = [[NSMutableString alloc] initWithString:_serviceName];
+    [serviceFunction appendString:TMultiplexedProtocolSeperator];
+    [serviceFunction appendString:name];
+    return [super writeMessageBeginWithName:serviceFunction type:messageType sequenceID:sequenceID error:error];
+  }
+  break;
+
+  default:
+    return [super writeMessageBeginWithName:name type:messageType sequenceID:sequenceID error:error];
+  }
 }
 
 @end

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocol.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocol.h b/lib/cocoa/src/protocol/TProtocol.h
index 281239d..841059f 100644
--- a/lib/cocoa/src/protocol/TProtocol.h
+++ b/lib/cocoa/src/protocol/TProtocol.h
@@ -22,127 +22,143 @@
 #import "TTransport.h"
 
 
-enum {
-  TMessageType_CALL = 1,
-  TMessageType_REPLY = 2,
-  TMessageType_EXCEPTION = 3,
-  TMessageType_ONEWAY = 4
+NS_ASSUME_NONNULL_BEGIN
+
+
+typedef NS_ENUM (int, TMessageType) {
+  TMessageTypeCALL = 1,
+  TMessageTypeREPLY = 2,
+  TMessageTypeEXCEPTION = 3,
+  TMessageTypeONEWAY = 4
 };
 
-enum {
-  TType_STOP   = 0,
-  TType_VOID   = 1,
-  TType_BOOL   = 2,
-  TType_BYTE   = 3,
-  TType_DOUBLE = 4,
-  TType_I16    = 6,
-  TType_I32    = 8,
-  TType_I64    = 10,
-  TType_STRING = 11,
-  TType_STRUCT = 12,
-  TType_MAP    = 13,
-  TType_SET    = 14,
-  TType_LIST   = 15
+typedef NS_ENUM (int, TType) {
+  TTypeSTOP   = 0,
+  TTypeVOID   = 1,
+  TTypeBOOL   = 2,
+  TTypeBYTE   = 3,
+  TTypeDOUBLE = 4,
+  TTypeI16    = 6,
+  TTypeI32    = 8,
+  TTypeI64    = 10,
+  TTypeSTRING = 11,
+  TTypeSTRUCT = 12,
+  TTypeMAP    = 13,
+  TTypeSET    = 14,
+  TTypeLIST   = 15
 };
 
 
 @protocol TProtocol <NSObject>
 
-- (id <TTransport>) transport;
+-(id <TTransport>) transport;
 
-- (void) readMessageBeginReturningName: (NSString **) name
-                                  type: (int *) type
-                            sequenceID: (int *) sequenceID;
-- (void) readMessageEnd;
+-(BOOL) readMessageBeginReturningName:(NSString *__nullable __autoreleasing *__nullable)name
+                                 type:(nullable SInt32 *)type
+                           sequenceID:(nullable SInt32 *)sequenceID
+                                error:(NSError *__autoreleasing *)error;
+-(BOOL) readMessageEnd:(NSError *__autoreleasing *)error;
 
-- (void) readStructBeginReturningName: (NSString **) name;
-- (void) readStructEnd;
+-(BOOL) readStructBeginReturningName:(NSString *__nullable __autoreleasing *__nullable)name
+                               error:(NSError *__autoreleasing *)error;
+-(BOOL) readStructEnd:(NSError *__autoreleasing *)error;
 
-- (void) readFieldBeginReturningName: (NSString **) name
-                                type: (int *) fieldType
-                             fieldID: (int *) fieldID;
-- (void) readFieldEnd;
+-(BOOL) readFieldBeginReturningName:(NSString *__nullable __autoreleasing *__nullable)name
+                               type:(SInt32 *)fieldType
+                            fieldID:(nullable SInt32 *)fieldID
+                              error:(NSError *__autoreleasing *)error;
+-(BOOL) readFieldEnd:(NSError *__autoreleasing *)error;
 
-- (NSString *) readString;
+-(BOOL) readString:(NSString *__nonnull __autoreleasing *__nonnull)value error:(NSError **)error;
 
-- (BOOL) readBool;
+-(BOOL) readBool:(BOOL *)value error:(NSError *__autoreleasing *)error;
 
-- (unsigned char) readByte;
+-(BOOL) readByte:(UInt8 *)value error:(NSError *__autoreleasing *)error;
 
-- (short) readI16;
+-(BOOL) readI16:(SInt16 *)value error:(NSError *__autoreleasing *)error;
 
-- (int32_t) readI32;
+-(BOOL) readI32:(SInt32 *)value error:(NSError *__autoreleasing *)error;
 
-- (int64_t) readI64;
+-(BOOL) readI64:(SInt64 *)value error:(NSError *__autoreleasing *)error;
 
-- (double) readDouble;
+-(BOOL) readDouble:(double *)value error:(NSError *__autoreleasing *)error;
 
-- (NSData *) readBinary;
+-(BOOL) readBinary:(NSData *__nonnull __autoreleasing *__nonnull)value error:(NSError **)error;
 
-- (void) readMapBeginReturningKeyType: (int *) keyType
-                            valueType: (int *) valueType
-                                 size: (int *) size;
-- (void) readMapEnd;
+-(BOOL) readMapBeginReturningKeyType:(nullable SInt32 *)keyType
+                           valueType:(nullable SInt32 *)valueType
+                                size:(SInt32 *)size
+                               error:(NSError *__autoreleasing *)error;
+-(BOOL) readMapEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) readSetBeginReturningElementType: (int *) elementType
-                                     size: (int *) size;
-- (void) readSetEnd;
+-(BOOL) readSetBeginReturningElementType:(nullable SInt32 *)elementType
+                                    size:(SInt32 *)size
+                                   error:(NSError *__autoreleasing *)error;
+-(BOOL) readSetEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) readListBeginReturningElementType: (int *) elementType
-                                      size: (int *) size;
-- (void) readListEnd;
+-(BOOL) readListBeginReturningElementType:(nullable SInt32 *)elementType
+                                     size:(SInt32 *)size
+                                    error:(NSError *__autoreleasing *)error;
+-(BOOL) readListEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID;
-- (void) writeMessageEnd;
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error;
+-(BOOL) writeMessageEnd:(NSError *__autoreleasing *)error;
 
-- (void) writeStructBeginWithName: (NSString *) name;
-- (void) writeStructEnd;
+-(BOOL) writeStructBeginWithName:(NSString *)name error:(NSError **)error;
+-(BOOL) writeStructEnd:(NSError *__autoreleasing *)error;
 
-- (void) writeFieldBeginWithName: (NSString *) name
-                            type: (int) fieldType
-                         fieldID: (int) fieldID;
+-(BOOL) writeFieldBeginWithName:(NSString *)name
+                           type:(SInt32)fieldType
+                        fieldID:(SInt32)fieldID
+                          error:(NSError *__autoreleasing *)error;
 
-- (void) writeI32: (int32_t) value;
+-(BOOL) writeI32:(SInt32)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeI64: (int64_t) value;
+-(BOOL) writeI64:(SInt64)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeI16: (short) value;
+-(BOOL) writeI16:(short)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeByte: (uint8_t) value;
+-(BOOL) writeByte:(UInt8)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeString: (NSString *) value;
+-(BOOL) writeString:(NSString *)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeDouble: (double) value;
+-(BOOL) writeDouble:(double)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeBool: (BOOL) value;
+-(BOOL) writeBool:(BOOL)value error:(NSError *__autoreleasing *)error;
 
-- (void) writeBinary: (NSData *) data;
+-(BOOL) writeBinary:(NSData *)data error:(NSError *__autoreleasing *)error;
 
-- (void) writeFieldStop;
+-(BOOL) writeFieldStop:(NSError *__autoreleasing *)error;
 
-- (void) writeFieldEnd;
+-(BOOL) writeFieldEnd:(NSError *__autoreleasing *)error;
 
-- (void) writeMapBeginWithKeyType: (int) keyType
-                        valueType: (int) valueType
-                             size: (int) size;
-- (void) writeMapEnd;
+-(BOOL) writeMapBeginWithKeyType:(SInt32)keyType
+                       valueType:(SInt32)valueType
+                            size:(SInt32)size
+                           error:(NSError *__autoreleasing *)error;
+-(BOOL) writeMapEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) writeSetBeginWithElementType: (int) elementType
-                                 size: (int) size;
-- (void) writeSetEnd;
+-(BOOL) writeSetBeginWithElementType:(SInt32)elementType
+                                size:(SInt32)size
+                               error:(NSError *__autoreleasing *)error;
+-(BOOL) writeSetEnd:(NSError *__autoreleasing *)error;
 
 
-- (void) writeListBeginWithElementType: (int) elementType
-                                  size: (int) size;
+-(BOOL) writeListBeginWithElementType:(SInt32)elementType
+                                 size:(SInt32)size
+                                error:(NSError *__autoreleasing *)error;
 
-- (void) writeListEnd;
+-(BOOL) writeListEnd:(NSError *__autoreleasing *)error;
 
 
 @end
 
+
+NS_ASSUME_NONNULL_END

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolDecorator.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolDecorator.h b/lib/cocoa/src/protocol/TProtocolDecorator.h
index 829bed6..369b6a2 100644
--- a/lib/cocoa/src/protocol/TProtocolDecorator.h
+++ b/lib/cocoa/src/protocol/TProtocolDecorator.h
@@ -21,10 +21,14 @@
 
 #import "TProtocol.h"
 
-@interface TProtocolDecorator : NSObject <TProtocol> {
-    id<TProtocol> mConcreteProtocol;
-}
+NS_ASSUME_NONNULL_BEGIN
 
-- (id) initWithProtocol: (id <TProtocol>) protocol;
+
+@interface TProtocolDecorator : NSObject <TProtocol>
+
+-(id) initWithProtocol:(id <TProtocol>)protocol;
 
 @end
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolDecorator.m
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolDecorator.m b/lib/cocoa/src/protocol/TProtocolDecorator.m
index e5acb6c..218f900 100644
--- a/lib/cocoa/src/protocol/TProtocolDecorator.m
+++ b/lib/cocoa/src/protocol/TProtocolDecorator.m
@@ -18,257 +18,278 @@
  */
 
 #import "TProtocolDecorator.h"
-#import "TObjective-C.h"
+
+
+@interface TProtocolDecorator ()
+
+@property(strong, nonatomic) id<TProtocol> concreteProtocol;
+
+@end
+
 
 @implementation TProtocolDecorator
 
-- (id) initWithProtocol: (id <TProtocol>) protocol
+-(id) initWithProtocol:(id <TProtocol>)protocol
 {
-    self = [super init];
-    if (self) {
-        mConcreteProtocol = [protocol retain_stub];
-    }
-    return self;
+  self = [super init];
+  if (self) {
+    _concreteProtocol = protocol;
+  }
+  return self;
 }
 
-- (id <TTransport>) transport
+-(id <TTransport>) transport
 {
-    return [mConcreteProtocol transport];
+  return [_concreteProtocol transport];
 }
 
-- (void) readMessageBeginReturningName: (NSString **) name
-                                  type: (int *) type
-                            sequenceID: (int *) sequenceID
+-(BOOL) readMessageBeginReturningName:(NSString **)name
+                                 type:(SInt32 *)type
+                           sequenceID:(SInt32 *)sequenceID
+                                error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMessageBeginReturningName:name
-                                                type:type
-                                          sequenceID:sequenceID];
+  return [_concreteProtocol readMessageBeginReturningName:name
+                                                     type:type
+                                               sequenceID:sequenceID
+                                                    error:error];
 }
 
-- (void) readMessageEnd
+-(BOOL) readMessageEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMessageEnd];
+  return [_concreteProtocol readMessageEnd:error];
 }
 
-- (void) readStructBeginReturningName: (NSString **) name
+-(BOOL) readStructBeginReturningName:(NSString **)name
+                               error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readStructBeginReturningName:name];
+  return [_concreteProtocol readStructBeginReturningName:name error:error];
 }
 
-- (void) readStructEnd
+-(BOOL) readStructEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readStructEnd];
+  return [_concreteProtocol readStructEnd:error];
 }
 
-- (void) readFieldBeginReturningName: (NSString **) name
-                                type: (int *) fieldType
-                             fieldID: (int *) fieldID
+-(BOOL) readFieldBeginReturningName:(NSString **)name
+                               type:(SInt32 *)fieldType
+                            fieldID:(SInt32 *)fieldID
+                              error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readFieldBeginReturningName:name
-                                              type:fieldType
-                                           fieldID:fieldID];
+  return [_concreteProtocol readFieldBeginReturningName:name
+                                                   type:fieldType
+                                                fieldID:fieldID
+                                                  error:error];
 }
-- (void) readFieldEnd
+-(BOOL) readFieldEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readFieldEnd];
+  return [_concreteProtocol readFieldEnd:error];
 }
 
-- (NSString *) readString
+-(BOOL) readString:(NSString *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readString];
+  return [_concreteProtocol readString:value error:error];
 }
 
-- (BOOL) readBool
+-(BOOL) readBool:(BOOL *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readBool];
+  return [_concreteProtocol readBool:value error:error];
 }
 
-- (unsigned char) readByte
+-(BOOL) readByte:(UInt8 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readByte];
+  return [_concreteProtocol readByte:value error:error];
 }
 
-- (short) readI16
+-(BOOL) readI16:(SInt16 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readI16];
+  return [_concreteProtocol readI16:value error:error];
 }
 
-- (int32_t) readI32
+-(BOOL) readI32:(SInt32 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readI32];
+  return [_concreteProtocol readI32:value error:error];
 }
 
-- (int64_t) readI64
+-(BOOL) readI64:(SInt64 *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readI64];
+  return [_concreteProtocol readI64:value error:error];
 }
 
-- (double) readDouble
+-(BOOL) readDouble:(double *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readDouble];
+  return [_concreteProtocol readDouble:value error:error];
 }
 
-- (NSData *) readBinary
+-(BOOL) readBinary:(NSData *__autoreleasing *)value error:(NSError *__autoreleasing *)error
 {
-    return [mConcreteProtocol readBinary];
+  return [_concreteProtocol readBinary:value error:error];
 }
 
-- (void) readMapBeginReturningKeyType: (int *) keyType
-                            valueType: (int *) valueType
-                                 size: (int *) size
+-(BOOL) readMapBeginReturningKeyType:(SInt32 *)keyType
+                           valueType:(SInt32 *)valueType
+                                size:(SInt32 *)size
+                               error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMapBeginReturningKeyType:keyType
-                                          valueType:valueType
-                                               size:size];
+  return [_concreteProtocol readMapBeginReturningKeyType:keyType
+                                               valueType:valueType
+                                                    size:size
+                                                   error:error];
 }
-- (void) readMapEnd
+-(BOOL) readMapEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readMapEnd];
+  return [_concreteProtocol readMapEnd:error];
 }
 
 
-- (void) readSetBeginReturningElementType: (int *) elementType
-                                     size: (int *) size
+-(BOOL) readSetBeginReturningElementType:(SInt32 *)elementType
+                                    size:(SInt32 *)size
+                                   error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readSetBeginReturningElementType:elementType
-                                                   size:size];
+  return [_concreteProtocol readSetBeginReturningElementType:elementType
+                                                        size:size
+                                                       error:error];
 }
-- (void) readSetEnd
+-(BOOL) readSetEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readSetEnd];
+  return [_concreteProtocol readSetEnd:error];
 }
 
-- (void) readListBeginReturningElementType: (int *) elementType
-                                      size: (int *) size
+-(BOOL) readListBeginReturningElementType:(SInt32 *)elementType
+                                     size:(SInt32 *)size
+                                    error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readListBeginReturningElementType:elementType
-                                                    size:size];
+  return [_concreteProtocol readListBeginReturningElementType:elementType
+                                                         size:size
+                                                        error:error];
 }
-- (void) readListEnd
+-(BOOL) readListEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol readListEnd];
+  return [_concreteProtocol readListEnd:error];
 }
 
-- (void) writeMessageBeginWithName: (NSString *) name
-                              type: (int) messageType
-                        sequenceID: (int) sequenceID
+-(BOOL) writeMessageBeginWithName:(NSString *)name
+                             type:(SInt32)messageType
+                       sequenceID:(SInt32)sequenceID
+                            error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeMessageBeginWithName:name
-                                            type:messageType
-                                      sequenceID:sequenceID];
+  return [_concreteProtocol writeMessageBeginWithName:name
+                                                 type:messageType
+                                           sequenceID:sequenceID
+                                                error:error];
 }
-- (void) writeMessageEnd
+-(BOOL) writeMessageEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeMessageEnd];
+  return [_concreteProtocol writeMessageEnd:error];
 }
 
-- (void) writeStructBeginWithName: (NSString *) name
+-(BOOL) writeStructBeginWithName:(NSString *)name error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeStructBeginWithName:name];
+  return [_concreteProtocol writeStructBeginWithName:name error:error];
 }
-- (void) writeStructEnd
+-(BOOL) writeStructEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeStructEnd];
+  return [_concreteProtocol writeStructEnd:error];
 }
 
-- (void) writeFieldBeginWithName: (NSString *) name
-                            type: (int) fieldType
-                         fieldID: (int) fieldID
+-(BOOL) writeFieldBeginWithName:(NSString *)name
+                           type:(SInt32)fieldType
+                        fieldID:(SInt32)fieldID
+                          error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeFieldBeginWithName:name
-                                          type:fieldType
-                                       fieldID:fieldID];
+  return [_concreteProtocol writeFieldBeginWithName:name
+                                               type:fieldType
+                                            fieldID:fieldID
+                                              error:error];
 }
 
-- (void) writeI32: (int32_t) value
+-(BOOL) writeI32:(SInt32)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeI32:value];
+  return [_concreteProtocol writeI32:value error:error];
 }
 
-- (void) writeI64: (int64_t) value
+-(BOOL) writeI64:(SInt64)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeI64:value];
+  return [_concreteProtocol writeI64:value error:error];
 }
 
-- (void) writeI16: (short) value
+-(BOOL) writeI16:(SInt16)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeI16:value];
+  return [_concreteProtocol writeI16:value error:error];
 }
 
-- (void) writeByte: (uint8_t) value
+-(BOOL) writeByte:(UInt8)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeByte:value];
+  return [_concreteProtocol writeByte:value error:error];
 }
 
-- (void) writeString: (NSString *) value
+-(BOOL) writeString:(NSString *)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeString:value];
+  return [_concreteProtocol writeString:value error:error];
 }
 
-- (void) writeDouble: (double) value
+-(BOOL) writeDouble:(double)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeDouble:value];
+  return [_concreteProtocol writeDouble:value error:error];
 }
 
-- (void) writeBool: (BOOL) value
+-(BOOL) writeBool:(BOOL)value error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeBool:value];
+  return [_concreteProtocol writeBool:value error:error];
 }
 
-- (void) writeBinary: (NSData *) data
+-(BOOL) writeBinary:(NSData *)data error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeBinary:data];
+  return [_concreteProtocol writeBinary:data error:error];
 }
 
-- (void) writeFieldStop
+-(BOOL) writeFieldStop:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeFieldStop];
+  return [_concreteProtocol writeFieldStop:error];
 }
 
-- (void) writeFieldEnd
+-(BOOL) writeFieldEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeFieldEnd];
+  return [_concreteProtocol writeFieldEnd:error];
 }
 
-- (void) writeMapBeginWithKeyType: (int) keyType
-                        valueType: (int) valueType
-                             size: (int) size
-{
-    [mConcreteProtocol writeMapBeginWithKeyType:keyType
-                                      valueType:valueType
-                                           size:size];
-}
-- (void) writeMapEnd
+-(BOOL) writeMapBeginWithKeyType:(SInt32)keyType
+                       valueType:(SInt32)valueType
+                            size:(SInt32)size
+                           error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeMapEnd];
+  return [_concreteProtocol writeMapBeginWithKeyType:keyType
+                                           valueType:valueType
+                                                size:size
+                                               error:error];
 }
 
-- (void) writeSetBeginWithElementType: (int) elementType
-                                 size: (int) size
+-(BOOL) writeMapEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeSetBeginWithElementType:elementType size:size];
+  return [_concreteProtocol writeMapEnd:error];
 }
 
-- (void) writeSetEnd
+-(BOOL) writeSetBeginWithElementType:(SInt32)elementType
+                                size:(SInt32)size
+                               error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeSetEnd];
+  return [_concreteProtocol writeSetBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) writeListBeginWithElementType: (int) elementType
-                                  size: (int) size
+-(BOOL) writeSetEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeListBeginWithElementType:elementType size:size];
+  return [_concreteProtocol writeSetEnd:error];
 }
 
-- (void) writeListEnd
+-(BOOL) writeListBeginWithElementType:(SInt32)elementType
+                                 size:(SInt32)size
+                                error:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol writeListEnd];
+  return [_concreteProtocol writeListBeginWithElementType:elementType size:size error:error];
 }
 
-- (void) dealloc
+-(BOOL) writeListEnd:(NSError *__autoreleasing *)error
 {
-    [mConcreteProtocol release_stub];
-    [super dealloc_stub];
+  return [_concreteProtocol writeListEnd:error];
 }
 
 @end

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolError.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolError.h b/lib/cocoa/src/protocol/TProtocolError.h
new file mode 100644
index 0000000..ab0bc40
--- /dev/null
+++ b/lib/cocoa/src/protocol/TProtocolError.h
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TError.h"
+
+
+extern NSString *TProtocolErrorDomain;
+
+typedef NS_ENUM (int, TProtocolError) {
+  TProtocolErrorUnknown                   = 0,
+  TProtocolErrorInvalidData               = 1,
+  TProtocolErrorNegativeSize              = 2,
+  TProtocolErrorSizeLimit                 = 3,
+  TProtocolErrorBadVersion                = 4,
+  TProtocolErrorNotImplemented            = 5,
+  TProtocolErrorDepthLimit                = 6,
+};
+
+
+typedef NS_ENUM(int, TProtocolExtendedError) {
+  TProtocolExtendedErrorMissingRequiredField  = 1001,
+  TProtocolExtendedErrorUnexpectedType        = 1002,
+  TProtocolExtendedErrorMismatchedProtocol    = 1003,
+};
+
+extern NSString *TProtocolErrorExtendedErrorKey;
+extern NSString *TProtocolErrorFieldNameKey;
+extern NSString *TProtocolErrorExpectedIdKey;
+extern NSString *TProtocolErrorExpectedVersionKey;
+extern NSString *TProtocolErrorTypeKey;
+extern NSString *TProtocolErrorSourceLineKey;
+extern NSString *TProtocolErrorSourceFileKey;
+extern NSString *TProtocolErrorSourceMethodKey;
+extern NSString *TProtocolErrorMessageNameKey;
+
+
+#define PROTOCOL_ERROR(ret, err, ...) \
+  if (error) {  \
+    *error = [NSError errorWithDomain:TProtocolErrorDomain \
+                                 code:TProtocolError ## err \
+                             userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:__VA_ARGS__], \
+                                        @"SourceFile": [NSString stringWithUTF8String:__FILE__], \
+                                        @"SourceLine": @(__LINE__), \
+                                        @"SourceFunction": [NSString stringWithUTF8String:__PRETTY_FUNCTION__], \
+                                        @"Message": self.currentMessageName ? self.currentMessageName : @""}]; \
+  } \
+  return ret
+
+#define PROTOCOL_TRANSPORT_ERROR(ret, errorPtr, ...) \
+  if (errorPtr) { \
+    *error = [NSError errorWithDomain:TProtocolErrorDomain \
+                                 code:TProtocolErrorUnknown \
+                             userInfo:@{NSLocalizedDescriptionKey: [[NSString stringWithFormat:__VA_ARGS__] stringByAppendingFormat:@": %@", [(*errorPtr) localizedDescription]], \
+                                        TProtocolErrorSourceFileKey: [NSString stringWithUTF8String:__FILE__], \
+                                        TProtocolErrorSourceLineKey: @(__LINE__), \
+                                        TProtocolErrorSourceMethodKey: [NSString stringWithUTF8String:__PRETTY_FUNCTION__], \
+                                        TProtocolErrorMessageNameKey: self.currentMessageName ? self.currentMessageName : @"", \
+                                        NSUnderlyingErrorKey: *errorPtr}]; \
+  } \
+  return ret

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolError.m
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolError.m b/lib/cocoa/src/protocol/TProtocolError.m
new file mode 100644
index 0000000..953673b
--- /dev/null
+++ b/lib/cocoa/src/protocol/TProtocolError.m
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#import "TProtocolError.h"
+
+
+NSString *TProtocolErrorDomain = @"TProtocolErrorDomain";
+
+NSString *TProtocolErrorExtendedErrorKey = @"extendedError";
+NSString *TProtocolErrorFieldNameKey = @"field";
+NSString *TProtocolErrorExpectedIdKey = @"expectedId";
+NSString *TProtocolErrorExpectedVersionKey = @"expectedVersion";
+NSString *TProtocolErrorTypeKey = @"type";
+NSString *TProtocolErrorSourceLineKey = @"sourceLine";
+NSString *TProtocolErrorSourceFileKey = @"sourceFile";
+NSString *TProtocolErrorSourceMethodKey = @"sourceMethod";
+NSString *TProtocolErrorMessageNameKey = @"messageName";

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolException.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolException.h b/lib/cocoa/src/protocol/TProtocolException.h
deleted file mode 100644
index ad354fc..0000000
--- a/lib/cocoa/src/protocol/TProtocolException.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TException.h"
-
-@interface TProtocolException : TException {
-}
-
-@end

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolException.m
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolException.m b/lib/cocoa/src/protocol/TProtocolException.m
deleted file mode 100644
index 681487a..0000000
--- a/lib/cocoa/src/protocol/TProtocolException.m
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#import "TProtocolException.h"
-
-@implementation TProtocolException
-@end

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolFactory.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolFactory.h b/lib/cocoa/src/protocol/TProtocolFactory.h
index f200a6d..a022a7f 100644
--- a/lib/cocoa/src/protocol/TProtocolFactory.h
+++ b/lib/cocoa/src/protocol/TProtocolFactory.h
@@ -21,9 +21,16 @@
 #import "TProtocol.h"
 #import "TTransport.h"
 
+NS_ASSUME_NONNULL_BEGIN
+
 
 @protocol TProtocolFactory <NSObject>
 
-- (id <TProtocol>) newProtocolOnTransport: (id <TTransport>) transport;
+@property (readonly, nonatomic) NSString *protocolName;
+
+-(id<TProtocol>) newProtocolOnTransport:(id<TTransport>)transport;
 
 @end
+
+
+NS_ASSUME_NONNULL_END

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolUtil.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolUtil.h b/lib/cocoa/src/protocol/TProtocolUtil.h
index 757748a..82510cf 100644
--- a/lib/cocoa/src/protocol/TProtocolUtil.h
+++ b/lib/cocoa/src/protocol/TProtocolUtil.h
@@ -20,10 +20,14 @@
 #import "TProtocol.h"
 #import "TTransport.h"
 
-@interface TProtocolUtil : NSObject {
+NS_ASSUME_NONNULL_BEGIN
 
-}
 
-+ (void) skipType: (int) type onProtocol: (id <TProtocol>) protocol;
+@interface TProtocolUtil : NSObject
 
-@end
++(BOOL) skipType:(int)type onProtocol:(id <TProtocol>)protocol error:(NSError **)error;
+
+@end;
+
+
+NS_ASSUME_NONNULL_END
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/protocol/TProtocolUtil.m
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TProtocolUtil.m b/lib/cocoa/src/protocol/TProtocolUtil.m
index 13d7095..c0d65ac 100644
--- a/lib/cocoa/src/protocol/TProtocolUtil.m
+++ b/lib/cocoa/src/protocol/TProtocolUtil.m
@@ -21,84 +21,151 @@
 
 @implementation TProtocolUtil
 
-+ (void) skipType: (int) type onProtocol: (id <TProtocol>) protocol
++(BOOL) skipType:(int)type onProtocol:(id <TProtocol>)protocol error:(NSError **)error
 {
   switch (type) {
-  case TType_BOOL:
-    [protocol readBool];
-    break;
-  case TType_BYTE:
-    [protocol readByte];
-    break;
-  case TType_I16:
-    [protocol readI16];
-    break;
-  case TType_I32:
-    [protocol readI32];
-    break;
-  case TType_I64:
-    [protocol readI64];
-    break;
-  case TType_DOUBLE:
-    [protocol readDouble];
-    break;
-  case TType_STRING:
-    [protocol readString];
-    break;
-  case TType_STRUCT:
-    [protocol readStructBeginReturningName: NULL];
+  case TTypeBOOL: {
+    BOOL val;
+    if (![protocol readBool:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeBYTE: {
+    UInt8 val;
+    if (![protocol readByte:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeI16: {
+    SInt16 val;
+    if (![protocol readI16:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeI32: {
+    SInt32 val;
+    if (![protocol readI32:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeI64: {
+    SInt64 val;
+    if (![protocol readI64:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeDOUBLE: {
+    double val;
+    if (![protocol readDouble:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeSTRING: {
+    NSString *val;
+    if (![protocol readString:&val error:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeSTRUCT: {
+    if (![protocol readStructBeginReturningName:NULL error:error]) {
+      return NO;
+    }
     while (true) {
-      int fieldType;
-      [protocol readFieldBeginReturningName: nil type: &fieldType fieldID: nil];
-      if (fieldType == TType_STOP) {
+      SInt32 fieldType;
+      if (![protocol readFieldBeginReturningName:nil type:&fieldType fieldID:nil error:error]) {
+        return NO;
+      }
+      if (fieldType == TTypeSTOP) {
         break;
       }
-      [TProtocolUtil skipType: fieldType onProtocol: protocol];
-      [protocol readFieldEnd];
-    }
-    [protocol readStructEnd];
-    break;
-  case TType_MAP:
-  {
-    int keyType;
-    int valueType;
-    int size;
-    [protocol readMapBeginReturningKeyType: &keyType valueType: &valueType size: &size];
+      if (![self skipType:fieldType onProtocol:protocol error:error]) {
+        return NO;
+      }
+      if (![protocol readFieldEnd:error]) {
+        return NO;
+      }
+    }
+    if (![protocol readStructEnd:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeMAP: {
+    SInt32 keyType;
+    SInt32 valueType;
+    SInt32 size;
+    if (![protocol readMapBeginReturningKeyType:&keyType valueType:&valueType size:&size error:error]) {
+      return NO;
+    }
     int i;
     for (i = 0; i < size; i++) {
-      [TProtocolUtil skipType: keyType onProtocol: protocol];
-      [TProtocolUtil skipType: valueType onProtocol: protocol];
+      if (![TProtocolUtil skipType:keyType onProtocol:protocol error:error]) {
+        return NO;
+      }
+      if (![TProtocolUtil skipType:valueType onProtocol:protocol error:error]) {
+        return NO;
+      }
+    }
+    if (![protocol readMapEnd:error]) {
+      return NO;
     }
-    [protocol readMapEnd];
   }
-    break;
-    case TType_SET:
-    {
-      int elemType;
-      int size;
-      [protocol readSetBeginReturningElementType: &elemType size: &size];
-      int i;
-      for (i = 0; i < size; i++) {
-        [TProtocolUtil skipType: elemType onProtocol: protocol];
+  break;
+
+  case TTypeSET: {
+    SInt32 elemType;
+    SInt32 size;
+    if (![protocol readSetBeginReturningElementType:&elemType size:&size error:error]) {
+      return NO;
+    }
+    int i;
+    for (i = 0; i < size; i++) {
+      if (![TProtocolUtil skipType:elemType onProtocol:protocol error:error]) {
+        return NO;
       }
-      [protocol readSetEnd];
-    }
-      break;
-    case TType_LIST:
-    {
-      int elemType;
-      int size;
-      [protocol readListBeginReturningElementType: &elemType size: &size];
-      int i;
-      for (i = 0; i < size; i++) {
-        [TProtocolUtil skipType: elemType onProtocol: protocol];
+    }
+    if (![protocol readSetEnd:error]) {
+      return NO;
+    }
+  }
+  break;
+
+  case TTypeLIST: {
+    SInt32 elemType;
+    SInt32 size;
+    if (![protocol readListBeginReturningElementType:&elemType size:&size error:error]) {
+      return NO;
+    }
+    int i;
+    for (i = 0; i < size; i++) {
+      if (![TProtocolUtil skipType:elemType onProtocol:protocol error:error]) {
+        return NO;
       }
-      [protocol readListEnd];
     }
-      break;
-    default:
-      return;
+    if (![protocol readListEnd:error]) {
+      return NO;
+    }
   }
+  break;
+
+  }
+
+  return YES;
 }
 
 @end

http://git-wip-us.apache.org/repos/asf/thrift/blob/56e5b9b0/lib/cocoa/src/server/TSocketServer.h
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/server/TSocketServer.h b/lib/cocoa/src/server/TSocketServer.h
index c8ff9f0..fe657ea 100644
--- a/lib/cocoa/src/server/TSocketServer.h
+++ b/lib/cocoa/src/server/TSocketServer.h
@@ -27,23 +27,21 @@
 #import <CFNetwork/CFNetwork.h>
 #endif
 
-extern NSString * const kTSocketServer_ClientConnectionFinishedForProcessorNotification;
-extern NSString * const kTSocketServer_ProcessorKey;
-extern NSString * const kTSockerServer_TransportKey;
+NS_ASSUME_NONNULL_BEGIN
 
 
-@interface TSocketServer : NSObject {
-  NSFileHandle * mSocketFileHandle;
-  id <TProtocolFactory> mInputProtocolFactory;
-  id <TProtocolFactory> mOutputProtocolFactory;
-  id <TProcessorFactory> mProcessorFactory;
-}
+extern NSString *const TSocketServerClientConnectionFinished;
+extern NSString *const TSocketServerProcessorKey;
+extern NSString *const TSockerServerTransportKey;
 
-- (id) initWithPort: (int) port
-    protocolFactory: (id <TProtocolFactory>) protocolFactory
-   processorFactory: (id <TProcessorFactory>) processorFactory;
 
-@end
+@interface TSocketServer : NSObject
+
+-(instancetype) initWithPort:(int)port
+             protocolFactory:(id <TProtocolFactory>)protocolFactory
+            processorFactory:(id <TProcessorFactory>)processorFactory;
 
+@end
 
 
+NS_ASSUME_NONNULL_END