Finished enough of the basic refactoring to get things working.
[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 @interface DebuggerConnection : NSObject
23 {
24 // The port to connect on.
25 NSUInteger port_;
26
27 // If the connection to the debugger engine is currently active.
28 BOOL connected_;
29
30 // The raw CFSocket on which the two streams are based. Strong.
31 CFSocketRef socket_;
32
33 // The read stream that is scheduled on the main run loop. Weak.
34 CFReadStreamRef readStream_;
35
36 // The write stream. Weak.
37 CFWriteStreamRef writeStream_;
38
39 // An ever-increasing integer that gives each transaction a unique ID for the
40 // debugging engine.
41 NSUInteger transactionID;
42
43 // The most recently received transaction ID.
44 NSUInteger lastReadTransaction_;
45
46 // The last transactionID written to the stream.
47 NSUInteger lastWrittenTransaction_;
48
49 // Callback table. This maps transaction IDs to selectors. When the engine
50 // returns a response to the debugger, we will dispatch the response XML to
51 // the selector, based on transaction_id.
52 NSMutableDictionary* callTable_;
53
54 // To prevent blocked writing, we enqueue all writes and then wait for the
55 // write stream to tell us it's ready. We store the pending commands in this
56 // array. We use this as a stack (FIFO), with index 0 being first.
57 NSMutableArray* queuedWrites_;
58
59 // We send queued writes in multiple places, sometimes off a run loop event.
60 // Because of this, we need to ensure that only one client is dequeing and
61 // sending at a time.
62 NSRecursiveLock* writeQueueLock_;
63
64 // Information about the current read loop. We append to |currentPacket_|
65 // until |currentPacketSize_| has reached |packetSize_|.
66 NSMutableString* currentPacket_;
67 int packetSize_;
68 int currentPacketIndex_;
69
70 // The delegate.
71 id <DebuggerConnectionDelegate> delegate_;
72 }
73
74 @property (readonly) NSUInteger port;
75 @property (readonly) BOOL connected;
76 @property (assign) id <DebuggerConnectionDelegate> delegate;
77
78 - (id)initWithPort:(NSUInteger)aPort;
79
80 - (void)connect;
81 - (void)close;
82 - (void)socketDidAccept;
83 - (void)socketDisconnected;
84 - (void)readStreamHasData;
85 - (void)send:(NSString*)command;
86 - (void)performSend:(NSString*)command;
87 - (void)errorEncountered:(NSString*)error;
88
89 - (void)handleResponse:(NSXMLDocument*)response;
90 - (void)handlePacket:(NSString*)packet;
91
92 - (NSNumber*)sendCommandWithCallback:(SEL)callback format:(NSString*)format, ...;
93
94 - (void)sendQueuedWrites;
95
96 - (NSString*)escapedURIPath:(NSString*)path;
97 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response;
98 - (NSInteger)transactionIDFromCommand:(NSString*)command;
99
100 @end
101
102 // Delegate ////////////////////////////////////////////////////////////////////
103
104 @protocol DebuggerConnectionDelegate <NSObject>
105
106 @optional
107
108 - (void)connectionDidAccept:(DebuggerConnection*)cx;
109 - (void)connectionDidCose:(DebuggerConnection*)cx;
110
111 - (void)handleInitialResponse:(NSXMLDocument*)response;
112
113 - (void)handleResponse:(NSXMLDocument*)response;
114
115 - (void)errorEncountered:(NSString*)error;
116
117 @end
118