BLIP/BLIP_Internal.h
author Jens Alfke <jens@mooseyard.com>
Fri May 30 13:54:38 2008 -0700 (2008-05-30)
changeset 9 980beba83fb7
child 16 6f608b552b77
permissions -rw-r--r--
Fixed a serious bug - a race condition where a data buffer in the writer's queue could be dealloced (not the NSData, but its bytes themselves) before the writer sent it, resulting in an EFAULT error.
jens@0
     1
//
jens@0
     2
//  BLIP_Internal.h
jens@0
     3
//  MYNetwork
jens@0
     4
//
jens@0
     5
//  Created by Jens Alfke on 5/10/08.
jens@0
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@0
     7
//
jens@0
     8
jens@0
     9
#import "BLIPConnection.h"
jens@0
    10
#import "BLIPRequest.h"
jens@0
    11
#import "BLIPProperties.h"
jens@0
    12
@class BLIPWriter;
jens@0
    13
jens@0
    14
jens@0
    15
/* Private declarations and APIs for BLIP implementation. Not for use by clients! */
jens@0
    16
jens@0
    17
jens@0
    18
/* BLIP message types; encoded in each frame's header. */
jens@0
    19
typedef enum {
jens@0
    20
    kBLIP_MSG,                      // initiating message
jens@0
    21
    kBLIP_RPY,                      // response to a MSG
jens@0
    22
    kBLIP_ERR                       // error response to a MSG
jens@0
    23
} BLIPMessageType;
jens@0
    24
jens@0
    25
/* Flag bits in a BLIP frame header */
jens@0
    26
enum {
jens@0
    27
    kBLIP_TypeMask  = 0x000F,       // bits reserved for storing message type
jens@0
    28
    kBLIP_Compressed= 0x0010,       // data is gzipped
jens@0
    29
    kBLIP_Urgent    = 0x0020,       // please send sooner/faster
jens@0
    30
    kBLIP_NoReply   = 0x0040,       // no RPY needed
jens@0
    31
    kBLIP_MoreComing= 0x0080,       // More frames coming (Applies only to individual frame)
jens@0
    32
};
jens@0
    33
typedef UInt16 BLIPMessageFlags;
jens@0
    34
jens@0
    35
jens@0
    36
/** Header of a BLIP frame as sent across the wire. All fields are big-endian. */
jens@0
    37
typedef struct {
jens@0
    38
    UInt32           magic;         // magic number (kBLIPFrameHeaderMagicNumber)
jens@0
    39
    UInt32           number;        // serial number of MSG
jens@0
    40
    BLIPMessageFlags flags;         // encodes frame type, "more" flag, and other delivery options
jens@0
    41
    UInt16           size;          // total size of frame, _including_ this header
jens@0
    42
} BLIPFrameHeader;
jens@0
    43
jens@0
    44
#define kBLIPFrameHeaderMagicNumber 0x9B34F205
jens@0
    45
jens@0
    46
jens@0
    47
NSError *BLIPMakeError( int errorCode, NSString *message, ... ) __attribute__ ((format (__NSString__, 2, 3)));
jens@0
    48
jens@0
    49
jens@0
    50
@interface BLIPConnection ()
jens@0
    51
- (void) _dispatchRequest: (BLIPRequest*)request;
jens@0
    52
- (void) _dispatchResponse: (BLIPResponse*)response;
jens@0
    53
@end
jens@0
    54
jens@0
    55
jens@0
    56
@interface BLIPMessage ()
jens@0
    57
@property BOOL sent, propertiesAvailable, complete;
jens@0
    58
- (void) _setFlag: (BLIPMessageFlags)flag value: (BOOL)value;
jens@0
    59
- (void) _encode;
jens@0
    60
@end
jens@0
    61
jens@0
    62
jens@0
    63
@interface BLIPMessage ()
jens@0
    64
- (id) _initWithConnection: (BLIPConnection*)connection
jens@0
    65
                    isMine: (BOOL)isMine
jens@0
    66
                     flags: (BLIPMessageFlags)flags
jens@0
    67
                    number: (UInt32)msgNo
jens@0
    68
                      body: (NSData*)body;
jens@0
    69
- (BOOL) _writeFrameTo: (BLIPWriter*)writer maxSize: (UInt16)maxSize;
jens@0
    70
@property (readonly) SInt32 _bytesWritten;
jens@0
    71
- (void) _assignedNumber: (UInt32)number;
jens@0
    72
- (BOOL) _receivedFrameWithHeader: (const BLIPFrameHeader*)header body: (NSData*)body;
jens@0
    73
- (void) _connectionClosed;
jens@0
    74
@end
jens@0
    75
jens@0
    76
jens@0
    77
@interface BLIPRequest ()
jens@0
    78
- (id) _initWithConnection: (BLIPConnection*)connection
jens@0
    79
                      body: (NSData*)body 
jens@0
    80
                properties: (NSDictionary*)properties;
jens@0
    81
@end
jens@0
    82
jens@0
    83
jens@0
    84
@interface BLIPResponse ()
jens@0
    85
- (id) _initWithRequest: (BLIPRequest*)request;
jens@0
    86
@end