Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OODebugTCPConsoleClient(OOPrivate) Category Reference

Instance Methods

(void) - closeConnection
 
(BOOL) - sendBytes:count:
 
(void) - sendDictionary:
 
(void) - sendPacket:withParameters:
 
(void) - sendPacket:withValue:forParameter:
 
(void) - readData
 
(void) - dispatchPacket:ofType:
 
(void) - handleApproveConnectionPacket:
 
(void) - handleRejectConnectionPacket:
 
(void) - handleCloseConnectionPacket:
 
(void) - handleNoteConfigurationChangePacket:
 
(void) - handlePerformCommandPacket:
 
(void) - handleRequestConfigurationValuePacket:
 
(void) - handlePingPacket:
 
(void) - handlePongPacket:
 
(void) - disconnectFromServerWithMessage:
 
(void) - breakConnectionWithMessage:
 
(void) - breakConnectionWithBadStream:
 

Detailed Description

Definition at line 68 of file OODebugTCPConsoleClient.m.

Method Documentation

◆ breakConnectionWithBadStream:

- (void) breakConnectionWithBadStream: (NSStream *) stream

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

681 :(NSStream *)stream
682{
683 NSString *errorDesc = nil;
684 NSError *error = nil;
685
686 error = [stream streamError];
687 errorDesc = [error localizedDescription];
688 if (errorDesc == nil) errorDesc = [error description];
689 if (errorDesc == nil) errorDesc = @"bad stream.";
690 [self breakConnectionWithMessage:[NSString stringWithFormat:
691 @"Connection to debug console failed: '%@' (outStream status: %li, inStream status: %li).",
692 errorDesc, [_outStream streamStatus], [_inStream streamStatus]]];
693}
return nil
voidpf stream
Definition ioapi.h:134

◆ breakConnectionWithMessage:

- (void) breakConnectionWithMessage: (NSString *) message

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

658 :(NSString *)message
659{
660 [self closeConnection];
661
663
664 if ([message length] > 0)
665 {
666 OOLog(@"debugTCP.disconnect", @"No connection to debug console: \"%@\"", message);
667 }
668 else
669 {
670 OOLog(@"debugTCP.disconnect", @"%@", @"Debug console not connected.");
671 }
672
673#if 0
674 // Disconnecting causes crashiness for reasons I don't understand, and isn't very important anyway.
675 [_monitor disconnectDebugger:self message:message];
676 _monitor = nil;
677#endif
678}
@ kOOTCPClientConnectionRefused
@ kOOTCPClientDisconnected
#define OOLog(class, format,...)
Definition OOLogging.h:88

◆ closeConnection

- (void) closeConnection

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

323{
324 [_inStream close];
325 [_inStream setDelegate:nil];
326 [_inStream release];
327 _inStream = nil;
328
329 [_outStream close];
330 [_outStream setDelegate:nil];
331 [_outStream release];
332 _outStream = nil;
333
334 [_host release];
335 _host = nil;
336}

◆ disconnectFromServerWithMessage:

- (void) disconnectFromServerWithMessage: (NSString *) message

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

644 :(NSString *)message
645{
646 if (StatusIsSendable(_status))
647 {
648 [self sendPacket:kOOTCPPacket_CloseConnection
649 withValue:message
650 forParameter:kOOTCPMessage];
651 }
652 [self closeConnection];
653
654 _status = kOOTCPClientDisconnected;
655}
OOINLINE BOOL StatusIsSendable(OOTCPClientConnectionStatus status)

◆ dispatchPacket:ofType:

- (void) dispatchPacket: (NSDictionary *) packet
ofType: (NSString *) packetType 

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

485 :(NSDictionary *)packet ofType:(NSString *)packetType
486{
487 if (packet == nil || packetType == nil) return;
488
489#define PACKET_CASE(x) else if ([packetType isEqualToString:kOOTCPPacket_##x]) { [self handle##x##Packet:packet]; }
490
491 if (0) {}
492 PACKET_CASE(ApproveConnection)
493 PACKET_CASE(RejectConnection)
494 PACKET_CASE(CloseConnection)
495 PACKET_CASE(NoteConfigurationChange)
496 PACKET_CASE(PerformCommand)
497 PACKET_CASE(RequestConfigurationValue)
498 PACKET_CASE(Ping)
499 PACKET_CASE(Pong)
500 else
501 {
502 OOLog(@"debugTCP.protocolError.unknownPacketType", @"Unhandled packet type %@.", packetType);
503 }
504}
#define PACKET_CASE(x)

◆ handleApproveConnectionPacket:

- (void) handleApproveConnectionPacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

507 :(NSDictionary *)packet
508{
509 NSMutableString *connectedMessage = nil;
510 NSString *consoleIdentity = nil;
511 NSString *hostName = nil;
512
514 {
515 _status = kOOTCPClientConnected;
516
517 // Build "Connected..." message with two optional parts, console identity and host name.
518 connectedMessage = [NSMutableString stringWithString:@"Connected to debug console"];
519
520 consoleIdentity = [packet oo_stringForKey:kOOTCPConsoleIdentity];
521 if (consoleIdentity != nil) [connectedMessage appendFormat:@" \"%@\"", consoleIdentity];
522
523 hostName = [_host name];
524 if ([hostName length] != 0 &&
525 ![hostName isEqual:@"localhost"] &&
526 ![hostName isEqual:@"127.0.0.1"] &&
527 ![hostName isEqual:@"::1"])
528 {
529 [connectedMessage appendFormat:@" at %@", hostName];
530 }
531
532 OOLog(@"debugTCP.connected", @"%@.", connectedMessage);
533 }
534 else
535 {
536 OOLog(@"debugTCP.protocolError.outOfOrder", @"Got %@ packet from debug console in wrong context.", kOOTCPPacket_ApproveConnection);
537 }
538}
@ kOOTCPClientStartedConnectionStage2
@ kOOTCPClientConnected
#define kOOTCPPacket_ApproveConnection

◆ handleCloseConnectionPacket:

- (void) handleCloseConnectionPacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

560 :(NSDictionary *)packet
561{
562 NSString *message = nil;
563
564 if (!StatusIsSendable(_status))
565 {
566 OOLog(@"debugTCP.protocolError.outOfOrder", @"Got %@ packet from debug console in wrong context.", kOOTCPPacket_CloseConnection);
567 }
568 message = [packet oo_stringForKey:kOOTCPMessage];
569 if (message == nil) message = @"Console closed connection.";
570 [self breakConnectionWithMessage:message];
571}
#define kOOTCPPacket_CloseConnection

◆ handleNoteConfigurationChangePacket:

- (void) handleNoteConfigurationChangePacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

574 :(NSDictionary *)packet
575{
576 NSDictionary *configuration = nil;
577 NSArray *removed = nil;
578 NSEnumerator *keyEnum = nil;
579 NSString *key = nil;
580 id value = nil;
581
582 if (_monitor == nil) return;
583
584 configuration = [packet oo_dictionaryForKey:kOOTCPConfiguration];
585 if (configuration != nil)
586 {
587 for (keyEnum = [configuration keyEnumerator]; (key = [keyEnum nextObject]); )
588 {
589 value = [configuration objectForKey:key];
590 [_monitor setConfigurationValue:value forKey:key];
591 }
592 }
593
594 removed = [configuration oo_arrayForKey:kOOTCPRemovedConfigurationKeys];
595 for (keyEnum = [removed objectEnumerator]; (key = [keyEnum nextObject]); )
596 {
597 [_monitor setConfigurationValue:nil forKey:key];
598 }
599}

◆ handlePerformCommandPacket:

- (void) handlePerformCommandPacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

602 :(NSDictionary *)packet
603{
604 NSString *message = nil;
605
606 message = [packet oo_stringForKey:kOOTCPMessage];
607 if (message != nil) [_monitor performJSConsoleCommand:message];
608}

◆ handlePingPacket:

- (void) handlePingPacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

627 :(NSDictionary *)packet
628{
629 id message = nil;
630
631 message = [packet objectForKey:kOOTCPMessage];
632 [self sendPacket:kOOTCPPacket_Pong
633 withValue:message
634 forParameter:kOOTCPMessage];
635}

◆ handlePongPacket:

- (void) handlePongPacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

638 :(NSDictionary *)packet
639{
640 // Do nothing; we don't currently send pings.
641}

◆ handleRejectConnectionPacket:

- (void) handleRejectConnectionPacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

541 :(NSDictionary *)packet
542{
543 NSString *message = nil;
544
546 {
548 }
549 else
550 {
551 OOLog(@"debugTCP.protocolError.outOfOrder", @"Got %@ packet from debug console in wrong context.", kOOTCPPacket_RejectConnection);
552 }
553
554 message = [packet oo_stringForKey:kOOTCPMessage];
555 if (message == nil) message = @"Console refused connection.";
556 [self breakConnectionWithMessage:message];
557}
#define kOOTCPPacket_RejectConnection

◆ handleRequestConfigurationValuePacket:

- (void) handleRequestConfigurationValuePacket: (NSDictionary *) packet

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

611 :(NSDictionary *)packet
612{
613 NSString *key = nil;
614 id value = nil;
615
616 key = [packet oo_stringForKey:kOOTCPConfigurationKey];
617 if (key != nil)
618 {
619 value = [_monitor configurationValueForKey:key];
620 [self debugMonitor:_monitor
621 noteChangedConfigrationValue:value
622 forKey:key];
623 }
624}

◆ readData

- (void) readData

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

468{
469 enum { kBufferSize = 16 << 10 };
470
471 uint8_t buffer[kBufferSize];
472 NSInteger length;
473 NSData *data;
474
475 length = [_inStream read:buffer maxLength:kBufferSize];
476 while (length > 0)
477 {
478 data = [NSData dataWithBytesNoCopy:buffer length:length freeWhenDone:NO];
479 OOTCPStreamDecoderReceiveData(_decoder, data);
480 length = [_inStream read:buffer maxLength:kBufferSize];
481 }
482}
void OOTCPStreamDecoderReceiveData(OOTCPStreamDecoderRef decoder, OOALDataRef data)

◆ sendBytes:count:

- (BOOL) sendBytes: (const void *) bytes
count: (size_t) count 

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

339 :(const void *)bytes count:(size_t)count
340{
341 if (bytes == NULL || count == 0) return YES;
342 if (!StatusIsSendable(_status) || _outStream == nil) return NO;
343
344 do
345 {
346 NSInteger written = [_outStream write:bytes maxLength:count];
347 if (written < 1) return NO;
348
349 count -= written;
350 bytes += written;
351 }
352 while (count > 0);
353
354 return YES;
355}
unsigned count

◆ sendDictionary:

- (void) sendDictionary: (NSDictionary *) dictionary

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

358 :(NSDictionary *)dictionary
359{
360 NSData *data = nil;
361 NSString *errorDesc = NULL;
362 size_t count;
363 const uint8_t *bytes = NULL;
364 uint32_t header;
365 bool sentOK = YES;
366
367 if (dictionary == nil || !StatusIsSendable(_status)) return;
368
369 data = [NSPropertyListSerialization dataFromPropertyList:dictionary
370 format:NSPropertyListXMLFormat_v1_0
371 errorDescription:&errorDesc];
372
373 if (data == nil)
374 {
375 OOLog(@"debugTCP.conversionFailure", @"Could not convert dictionary to data for transmission to debug console: %@", errorDesc != NULL ? errorDesc : (NSString *)@"unknown error.");
376#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
377 [errorDesc autorelease];
378#endif
379 return;
380 }
381
382 LogSendPacket(dictionary);
383
384 count = [data length];
385 if (count == 0) return;
386 header = htonl(count);
387
388 bytes = [data bytes];
389
390 /* In testing, all bad stream errors were caused by the python console
391 rejecting headers. Made the protocol a bit more fault tolerant.
392 -- Kaks 2012.03.24
393 */
394 if (![self sendBytes:&header count:sizeof header])
395 {
396 OOLog(@"debugTCP.send.warning", @"%@", @"Error sending packet header, retrying.");
397 // wait 8 milliseconds, resend the header
398 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.008]];
399 if (![self sendBytes:&header count:sizeof header])
400 {
401 //OOLog(@"debugTCP.send.warning", @"Error sending packet header, retrying one more time.");
402 // wait 16 milliseconds, try to resend the header one last time!
403 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.016]];
404 if (![self sendBytes:&header count:sizeof header])
405 {
406 sentOK = NO;
407 }
408 }
409 }
410
411 if(sentOK && ![self sendBytes:bytes count:count])
412 {
413 OOLog(@"debugTCP.send.warning", @"%@", @"Error sending packet body, retrying.");
414 // wait 8 milliseconds, try again.
415 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.008]];
416 if(![self sendBytes:bytes count:count])
417 {
418 sentOK = NO;
419 }
420 }
421
422 if (!sentOK)
423 {
424 OOLog(@"debugTCP.send.error", @"The following packet could not be sent: %@", dictionary);
425 if(![[OODebugMonitor sharedDebugMonitor] TCPIgnoresDroppedPackets])
426 {
427 [self breakConnectionWithBadStream:_outStream];
428 }
429 }
430}
#define LogSendPacket(packet)

◆ sendPacket:withParameters:

- (void) sendPacket: (NSString *) packetType
withParameters: (NSDictionary *) parameters 

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

433 :(NSString *)packetType
434 withParameters:(NSDictionary *)parameters
435{
436 NSDictionary *dict = nil;
437
438 if (packetType == nil) return;
439
440 if (parameters != nil)
441 {
442 dict = [parameters dictionaryByAddingObject:packetType forKey:kOOTCPPacketType];
443 }
444 else
445 {
446 dict = [NSDictionary dictionaryWithObjectsAndKeys:packetType, kOOTCPPacketType, nil];
447 }
448
449 [self sendDictionary:dict];
450}

◆ sendPacket:withValue:forParameter:

- (void) sendPacket: (NSString *) packetType
withValue: (id) value
forParameter: (NSString *) paramKey 

Extends class OODebugTCPConsoleClient.

Definition at line 704 of file OODebugTCPConsoleClient.m.

453 :(NSString *)packetType
454 withValue:(id)value
455 forParameter:(NSString *)paramKey
456{
457 if (packetType == nil) return;
458 if (paramKey == nil) value = nil;
459
460 [self sendDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
461 packetType, kOOTCPPacketType,
462 value, paramKey,
463 nil]];
464}

The documentation for this category was generated from the following file: