Permalink
Browse files

using a custom struct to pass the message, for illustrating the use o…

…f sending custom structured data
  • Loading branch information...
1 parent bb39e6d commit 072016a576daf944261e00a13c7436d5d49034e3 @rsms committed Apr 21, 2012
@@ -79,7 +79,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
- (IBAction)sendMessage:(id)sender {
if (connectedChannel_) {
NSString *message = self.inputTextField.stringValue;
- dispatch_data_t payload = [[message dataUsingEncoding:NSUTF8StringEncoding] createReferencingDispatchData];
+ dispatch_data_t payload = PTExampleTextDispatchDataWithString(message);
[connectedChannel_ sendFrameOfType:PTExampleFrameTypeTextMessage tag:PTFrameNoTag withPayload:payload callback:^(NSError *error) {
if (error) {
NSLog(@"Failed to send message: %@", error);
@@ -199,7 +199,9 @@ - (void)ioFrameChannel:(PTChannel*)channel didReceiveFrameOfType:(uint32_t)type
NSDictionary *deviceInfo = [NSDictionary dictionaryWithContentsOfDispatchData:payload.dispatchData];
[self presentMessage:[NSString stringWithFormat:@"Connected to %@", deviceInfo.description] isStatus:YES];
} else if (type == PTExampleFrameTypeTextMessage) {
- NSString *message = [[NSString alloc] initWithBytes:payload.data length:payload.length encoding:NSUTF8StringEncoding];
+ PTExampleTextFrame *textFrame = (PTExampleTextFrame*)payload.data;
+ textFrame->length = ntohl(textFrame->length);
+ NSString *message = [[NSString alloc] initWithBytes:textFrame->utf8text length:textFrame->length encoding:NSUTF8StringEncoding];
[self presentMessage:[NSString stringWithFormat:@"[%@]: %@", channel.userInfo, message] isStatus:NO];
} else if (type == PTExampleFrameTypePong) {
[self pongWithTag:tag error:nil];
@@ -13,4 +13,24 @@ enum {
PTExampleFrameTypePong = 103,
};
+typedef struct _PTExampleTextFrame {
+ uint32_t length;
+ uint8_t utf8text[0];
+} PTExampleTextFrame;
+
+
+static dispatch_data_t PTExampleTextDispatchDataWithString(NSString *message) {
+ // Use a custom struct
+ const char *utf8text = [message cStringUsingEncoding:NSUTF8StringEncoding];
+ size_t length = strlen(utf8text);
+ PTExampleTextFrame *textFrame = CFAllocatorAllocate(nil, sizeof(PTExampleTextFrame) + length, 0);
+ memcpy(textFrame->utf8text, utf8text, length); // Copy bytes to utf8text array
+ textFrame->length = htonl(length); // Convert integer to network byte order
+
+ // Wrap the textFrame in a dispatch data object
+ return dispatch_data_create((const void*)textFrame, sizeof(PTExampleTextFrame)+length, nil, ^{
+ CFAllocatorDeallocate(nil, textFrame);
+ });
+}
+
#endif
@@ -58,7 +58,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- (void)sendMessage:(NSString*)message {
if (peerChannel_) {
- dispatch_data_t payload = [[message dataUsingEncoding:NSUTF8StringEncoding] createReferencingDispatchData];
+ dispatch_data_t payload = PTExampleTextDispatchDataWithString(message);
[peerChannel_ sendFrameOfType:PTExampleFrameTypeTextMessage tag:PTFrameNoTag withPayload:payload callback:^(NSError *error) {
if (error) {
NSLog(@"Failed to send message: %@", error);
@@ -135,7 +135,9 @@ - (BOOL)ioFrameChannel:(PTChannel*)channel shouldAcceptFrameOfType:(uint32_t)typ
- (void)ioFrameChannel:(PTChannel*)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(PTData*)payload {
//NSLog(@"didReceiveFrameOfType: %u, %u, %@", type, tag, payload);
if (type == PTExampleFrameTypeTextMessage) {
- NSString *message = [[NSString alloc] initWithBytes:payload.data length:payload.length encoding:NSUTF8StringEncoding];
+ PTExampleTextFrame *textFrame = (PTExampleTextFrame*)payload.data;
+ textFrame->length = ntohl(textFrame->length);
+ NSString *message = [[NSString alloc] initWithBytes:textFrame->utf8text length:textFrame->length encoding:NSUTF8StringEncoding];
[self appendOutputMessage:[NSString stringWithFormat:@"[%@]: %@", channel.userInfo, message]];
} else if (type == PTExampleFrameTypePing && peerChannel_) {
[peerChannel_ sendFrameOfType:PTExampleFrameTypePong tag:tag withPayload:nil callback:nil];

0 comments on commit 072016a

Please sign in to comment.