Do not ever close the socket until we are definitely closing the connection. We can...
[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 intricacies 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 // Whether or not in reconnect mode.
35 BOOL reconnect_;
36
37 // The thread on which network operations are performed. Weak.
38 NSThread* thread_;
39
40 // Reference to the message loop that the socket runs on. Weak.
41 NSRunLoop* runLoop_;
42
43 // The raw CFSocket on which the two streams are based. Strong.
44 CFSocketRef socket_;
45
46 // The read stream that is scheduled on the main run loop. Weak.
47 CFReadStreamRef readStream_;
48
49 // The write stream. Weak.
50 CFWriteStreamRef writeStream_;
51
52 // Run loop source used to quit the thread.
53 CFRunLoopSourceRef quitSource_;
54
55 // An ever-increasing integer that gives each transaction a unique ID for the
56 // debugging engine.
57 NSUInteger transactionID;
58
59 // The most recently received transaction ID.
60 NSUInteger lastReadTransaction_;
61
62 // The last transactionID written to the stream.
63 NSUInteger lastWrittenTransaction_;
64
65 // To prevent blocked writing, we enqueue all writes and then wait for the
66 // write stream to tell us it's ready. We store the pending commands in this
67 // array. We use this as a queue (FIFO), with index 0 being first.
68 NSMutableArray* queuedWrites_;
69
70 // We send queued writes in multiple places, sometimes off a run loop event.
71 // Because of this, we need to ensure that only one client is dequeing and
72 // sending at a time.
73 NSRecursiveLock* writeQueueLock_;
74
75 // Information about the current read loop. We append to |currentPacket_|
76 // until |currentPacketSize_| has reached |packetSize_|.
77 NSMutableString* currentPacket_;
78 int packetSize_;
79 int currentPacketIndex_;
80
81 // The delegate. All methods are executed on the main thread.
82 NSObject<DebuggerConnectionDelegate>* delegate_;
83 }
84
85 @property (readonly) NSUInteger port;
86 @property (readonly) BOOL connected;
87 @property (readonly, getter=inReconnectMode) BOOL reconnect;
88 @property (assign) id <DebuggerConnectionDelegate> delegate;
89
90 - (id)initWithPort:(NSUInteger)aPort;
91
92 - (void)connect;
93 - (void)close;
94
95 // Marks the connection as being in "reconnect mode," which sets the expectation
96 // of receiving another <init> message.
97 - (void)reconnect;
98
99 - (void)send:(NSString*)command;
100
101 // This sends the given command format to the debugger. This method is thread
102 // safe and schedules the request on the |runLoop_|.
103 - (NSNumber*)sendCommandWithFormat:(NSString*)format, ...;
104
105 - (NSString*)escapedURIPath:(NSString*)path;
106 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response;
107 - (NSInteger)transactionIDFromCommand:(NSString*)command;
108
109 @end
110
111 // Delegate ////////////////////////////////////////////////////////////////////
112
113 @protocol DebuggerConnectionDelegate <NSObject>
114
115 @optional
116
117 - (void)connectionDidAccept:(DebuggerConnection*)cx;
118 - (void)connectionDidClose:(DebuggerConnection*)cx;
119
120 - (void)handleInitialResponse:(NSXMLDocument*)response;
121
122 - (void)handleResponse:(NSXMLDocument*)response;
123
124 - (void)errorEncountered:(NSString*)error;
125
126 @end