Re-organize DebuggerConnection to give it some shape and a decent public interface.
[macgdbp.git] / Source / DebuggerConnection.h
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2010, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import <Cocoa/Cocoa.h>
18
19 @protocol DebuggerConnectionDelegate;
20 @class LoggingController;
21
22 // This class is the lowest level component to the network. It deals with all
23 // the intracies of network and stream programming. Almost all the work this
24 // class does is on a background thread, which is created when the connection is
25 // asked to connect and shutdown when asked to close.
26 @interface DebuggerConnection : NSObject
27 {
28 // The port to connect on.
29 NSUInteger port_;
30
31 // If the connection to the debugger engine is currently active.
32 BOOL connected_;
33
34 // Reference to the message loop that the socket runs on. Weak.
35 NSRunLoop* runLoop_;
36
37 // The raw CFSocket on which the two streams are based. Strong.
38 CFSocketRef socket_;
39
40 // The read stream that is scheduled on the main run loop. Weak.
41 CFReadStreamRef readStream_;
42
43 // The write stream. Weak.
44 CFWriteStreamRef writeStream_;
45
46 // An ever-increasing integer that gives each transaction a unique ID for the
47 // debugging engine.
48 NSUInteger transactionID;
49
50 // The most recently received transaction ID.
51 NSUInteger lastReadTransaction_;
52
53 // The last transactionID written to the stream.
54 NSUInteger lastWrittenTransaction_;
55
56 // To prevent blocked writing, we enqueue all writes and then wait for the
57 // write stream to tell us it's ready. We store the pending commands in this
58 // array. We use this as a queue (FIFO), with index 0 being first.
59 NSMutableArray* queuedWrites_;
60
61 // We send queued writes in multiple places, sometimes off a run loop event.
62 // Because of this, we need to ensure that only one client is dequeing and
63 // sending at a time.
64 NSRecursiveLock* writeQueueLock_;
65
66 // Information about the current read loop. We append to |currentPacket_|
67 // until |currentPacketSize_| has reached |packetSize_|.
68 NSMutableString* currentPacket_;
69 int packetSize_;
70 int currentPacketIndex_;
71
72 // The delegate.
73 id <DebuggerConnectionDelegate> delegate_;
74 }
75
76 @property (readonly) NSUInteger port;
77 @property (readonly) BOOL connected;
78 @property (assign) id <DebuggerConnectionDelegate> delegate;
79
80 - (id)initWithPort:(NSUInteger)aPort;
81
82 - (void)connect;
83 - (void)close;
84
85 - (void)send:(NSString*)command;
86
87 - (NSNumber*)sendCommandWithFormat:(NSString*)format, ...;
88
89 - (NSString*)escapedURIPath:(NSString*)path;
90 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response;
91 - (NSInteger)transactionIDFromCommand:(NSString*)command;
92
93 @end
94
95 // Delegate ////////////////////////////////////////////////////////////////////
96
97 @protocol DebuggerConnectionDelegate <NSObject>
98
99 @optional
100
101 - (void)connectionDidAccept:(DebuggerConnection*)cx;
102 - (void)connectionDidClose:(DebuggerConnection*)cx;
103
104 - (void)handleInitialResponse:(NSXMLDocument*)response;
105
106 - (void)handleResponse:(NSXMLDocument*)response;
107
108 - (void)errorEncountered:(NSString*)error;
109
110 @end
111