Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added ping-pong and removed unused PTExampleTextFrame struct

  • Loading branch information...
commit bb39e6d2479141ff6199d4f881e16e474d948499 1 parent 6ac6d9e
@rsms authored
View
46 Peertalk Example/PTAppDelegate.m
@@ -14,6 +14,7 @@ @interface PTAppDelegate () {
PTChannel *connectedChannel_;
NSDictionary *consoleTextAttributes_;
NSDictionary *consoleStatusTextAttributes_;
+ NSMutableDictionary *pings_;
}
@property (readonly) NSNumber *connectedDeviceID;
@@ -26,6 +27,7 @@ - (void)disconnectFromCurrentChannel;
- (void)enqueueConnectToLocalIPv4Port;
- (void)connectToLocalIPv4Port;
- (void)connectToUSBDevice;
+- (void)ping;
@end
@@ -68,6 +70,9 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Put a little message in the UI
[self presentMessage:@"Ready for action — connecting at will." isStatus:YES];
+
+ // Start pinging
+ [self ping];
}
@@ -134,12 +139,51 @@ - (void)setConnectedChannel:(PTChannel*)connectedChannel {
}
+#pragma mark - Ping
+
+
+- (void)pongWithTag:(uint32_t)tagno error:(NSError*)error {
+ NSNumber *tag = [NSNumber numberWithUnsignedInt:tagno];
+ NSMutableDictionary *pingInfo = [pings_ objectForKey:tag];
+ if (pingInfo) {
+ NSDate *now = [NSDate date];
+ [pingInfo setObject:now forKey:@"date ended"];
+ [pings_ removeObjectForKey:tag];
+ NSLog(@"Ping total roundtrip time: %.3f ms", [now timeIntervalSinceDate:[pingInfo objectForKey:@"date created"]]*1000.0);
+ }
+}
+
+
+- (void)ping {
+ if (connectedChannel_) {
+ if (!pings_) {
+ pings_ = [NSMutableDictionary dictionary];
+ }
+ uint32_t tagno = [connectedChannel_.protocol newTag];
+ NSNumber *tag = [NSNumber numberWithUnsignedInt:tagno];
+ NSMutableDictionary *pingInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSDate date], @"date created", nil];
+ [pings_ setObject:pingInfo forKey:tag];
+ [connectedChannel_ sendFrameOfType:PTExampleFrameTypePing tag:tagno withPayload:nil callback:^(NSError *error) {
+ [self performSelector:@selector(ping) withObject:nil afterDelay:1.0];
+ [pingInfo setObject:[NSDate date] forKey:@"date sent"];
+ if (error) {
+ [pings_ removeObjectForKey:tag];
+ }
+ }];
+ } else {
+ [self performSelector:@selector(ping) withObject:nil afterDelay:1.0];
+ }
+}
+
+
#pragma mark - PTChannelDelegate
- (BOOL)ioFrameChannel:(PTChannel*)channel shouldAcceptFrameOfType:(uint32_t)type tag:(uint32_t)tag payloadSize:(uint32_t)payloadSize {
if ( type != PTExampleFrameTypeDeviceInfo
&& type != PTExampleFrameTypeTextMessage
+ && type != PTExampleFrameTypePing
+ && type != PTExampleFrameTypePong
&& type != PTFrameTypeEndOfStream) {
NSLog(@"Unexpected frame of type %u", type);
[channel close];
@@ -157,6 +201,8 @@ - (void)ioFrameChannel:(PTChannel*)channel didReceiveFrameOfType:(uint32_t)type
} else if (type == PTExampleFrameTypeTextMessage) {
NSString *message = [[NSString alloc] initWithBytes:payload.data length:payload.length encoding:NSUTF8StringEncoding];
[self presentMessage:[NSString stringWithFormat:@"[%@]: %@", channel.userInfo, message] isStatus:NO];
+ } else if (type == PTExampleFrameTypePong) {
+ [self pongWithTag:tag error:nil];
}
}
View
7 Peertalk Example/PTExampleProtocol.h
@@ -9,11 +9,8 @@ static const int PTExampleProtocolIPv4PortNumber = 2345;
enum {
PTExampleFrameTypeDeviceInfo = 100,
PTExampleFrameTypeTextMessage = 101,
+ PTExampleFrameTypePing = 102,
+ PTExampleFrameTypePong = 103,
};
-typedef struct _PTExampleTextFrame {
- const uint8_t *utf8text;
-} PTExampleTextFrame;
-
-
#endif
View
4 Peertalk iOS Example/PTViewController.m
@@ -122,7 +122,7 @@ - (BOOL)ioFrameChannel:(PTChannel*)channel shouldAcceptFrameOfType:(uint32_t)typ
if (channel != peerChannel_) {
// A previous channel that has been canceled but not yet ended. Ignore.
return NO;
- } else if (type != PTExampleFrameTypeTextMessage) {
+ } else if (type != PTExampleFrameTypeTextMessage && type != PTExampleFrameTypePing) {
NSLog(@"Unexpected frame of type %u", type);
[channel close];
return NO;
@@ -137,6 +137,8 @@ - (void)ioFrameChannel:(PTChannel*)channel didReceiveFrameOfType:(uint32_t)type
if (type == PTExampleFrameTypeTextMessage) {
NSString *message = [[NSString alloc] initWithBytes:payload.data length:payload.length encoding:NSUTF8StringEncoding];
[self appendOutputMessage:[NSString stringWithFormat:@"[%@]: %@", channel.userInfo, message]];
+ } else if (type == PTExampleFrameTypePing && peerChannel_) {
+ [peerChannel_ sendFrameOfType:PTExampleFrameTypePong tag:tag withPayload:nil callback:nil];
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.