3 * Copyright (c) 2007 - 2010, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import <Cocoa/Cocoa.h>
19 @protocol DebuggerConnectionDelegate
;
20 @
class LoggingController
;
22 @interface DebuggerConnection
: NSObject
24 // The port to connect on.
27 // If the connection to the debugger engine is currently active.
30 // Reference to the message loop that the socket runs on. Weak.
33 // The raw CFSocket on which the two streams are based. Strong.
36 // The read stream that is scheduled on the main run loop. Weak.
37 CFReadStreamRef readStream_
;
39 // The write stream. Weak.
40 CFWriteStreamRef writeStream_
;
42 // An ever-increasing integer that gives each transaction a unique ID for the
44 NSUInteger transactionID
;
46 // The most recently received transaction ID.
47 NSUInteger lastReadTransaction_
;
49 // The last transactionID written to the stream.
50 NSUInteger lastWrittenTransaction_
;
52 // To prevent blocked writing, we enqueue all writes and then wait for the
53 // write stream to tell us it's ready. We store the pending commands in this
54 // array. We use this as a stack (FIFO), with index 0 being first.
55 NSMutableArray
* queuedWrites_
;
57 // We send queued writes in multiple places, sometimes off a run loop event.
58 // Because of this, we need to ensure that only one client is dequeing and
60 NSRecursiveLock
* writeQueueLock_
;
62 // Information about the current read loop. We append to |currentPacket_|
63 // until |currentPacketSize_| has reached |packetSize_|.
64 NSMutableString
* currentPacket_
;
66 int currentPacketIndex_
;
69 id
<DebuggerConnectionDelegate
> delegate_
;
72 @
property (readonly
) NSUInteger port
;
73 @
property (readonly
) BOOL connected
;
74 @
property (assign
) id
<DebuggerConnectionDelegate
> delegate
;
76 - (id
)initWithPort
:(NSUInteger
)aPort
;
80 - (void)socketDidAccept
;
81 - (void)socketDisconnected
;
82 - (void)readStreamHasData
;
83 - (void)send
:(NSString
*)command
;
84 - (void)performSend
:(NSString
*)command
;
85 - (void)errorEncountered
:(NSString
*)error
;
87 - (void)handleResponse
:(NSXMLDocument
*)response
;
88 - (void)handlePacket
:(NSString
*)packet
;
90 - (NSNumber
*)sendCommandWithFormat
:(NSString
*)format
, ...;
92 - (void)sendQueuedWrites
;
94 - (NSString
*)escapedURIPath
:(NSString
*)path
;
95 - (NSInteger
)transactionIDFromResponse
:(NSXMLDocument
*)response
;
96 - (NSInteger
)transactionIDFromCommand
:(NSString
*)command
;
100 // Delegate ////////////////////////////////////////////////////////////////////
102 @protocol DebuggerConnectionDelegate
<NSObject
>
106 - (void)connectionDidAccept
:(DebuggerConnection
*)cx
;
107 - (void)connectionDidCose
:(DebuggerConnection
*)cx
;
109 - (void)handleInitialResponse
:(NSXMLDocument
*)response
;
111 - (void)handleResponse
:(NSXMLDocument
*)response
;
113 - (void)errorEncountered
:(NSString
*)error
;