Move out of 1.5 beta to stable, and update the copyright years.
[macgdbp.git] / Source / NetworkConnection.h
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, 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 #ifdef __cplusplus
20 class NetworkCallbackController;
21 #else
22 @class NetworkCallbackController;
23 #endif
24
25 @protocol NetworkConnectionDelegate;
26 @class LoggingController;
27
28 // This class is the lowest level component to the network. It deals with all
29 // the intricacies of network and stream programming. Almost all the work this
30 // class does is on a background thread, which is created when the connection is
31 // asked to connect and shutdown when asked to close.
32 @interface NetworkConnection : NSObject
33 {
34 // The port to connect on.
35 NSUInteger port_;
36
37 // If the connection to the debugger engine is currently active.
38 BOOL connected_;
39
40 // The thread on which network operations are performed. Weak.
41 NSThread* thread_;
42
43 // Reference to the message loop that the socket runs on. Weak.
44 NSRunLoop* runLoop_;
45
46 // Internal class that manages CFNetwork callbacks. Strong.
47 NetworkCallbackController* callbackController_;
48
49 // Run loop source used to quit the thread. Strong.
50 CFRunLoopSourceRef quitSource_;
51
52 // An ever-increasing integer that gives each transaction a unique ID for the
53 // debugging engine.
54 NSInteger transactionID;
55
56 // The most recently received transaction ID.
57 NSInteger lastReadTransaction_;
58
59 // The last transactionID written to the stream.
60 NSInteger lastWrittenTransaction_;
61
62 // To prevent blocked writing, we enqueue all writes and then wait for the
63 // write stream to tell us it's ready. We store the pending commands in this
64 // array. We use this as a queue (FIFO), with index 0 being first.
65 NSMutableArray* queuedWrites_;
66
67 // We send queued writes in multiple places, sometimes off a run loop event.
68 // Because of this, we need to ensure that only one client is dequeing and
69 // sending at a time.
70 NSRecursiveLock* writeQueueLock_;
71
72 // Information about the current read loop. We append to |currentPacket_|
73 // until |currentPacketSize_| has reached |packetSize_|.
74 NSMutableString* currentPacket_;
75 NSInteger packetSize_;
76 NSInteger currentPacketIndex_;
77
78 // The delegate. All methods are executed on the main thread.
79 NSObject<NetworkConnectionDelegate>* delegate_;
80 }
81
82 @property (readonly) NSUInteger port;
83 @property (readonly) BOOL connected;
84 @property (assign) id <NetworkConnectionDelegate> delegate;
85
86 - (id)initWithPort:(NSUInteger)aPort;
87
88 - (void)connect;
89 - (void)close;
90
91 // This sends the given command format to the debugger. This method is thread
92 // safe and schedules the request on the |runLoop_|.
93 - (NSNumber*)sendCommandWithFormat:(NSString*)format, ...;
94
95 // Sends a command to the debugger. The command must have a substring |{txn}|
96 // within it, which will be replaced with the transaction ID. Use this if
97 // |-sendCommandWithFormat:|'s insertion of the transaction ID is incorrect.
98 - (NSNumber*)sendCustomCommandWithFormat:(NSString*)format, ...;
99
100 - (NSString*)escapedURIPath:(NSString*)path;
101 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response;
102 - (NSInteger)transactionIDFromCommand:(NSString*)command;
103
104 @end
105
106 // Delegate ////////////////////////////////////////////////////////////////////
107
108 @protocol NetworkConnectionDelegate <NSObject>
109
110 @optional
111
112 - (void)connectionDidAccept:(NetworkConnection*)cx;
113 - (void)connectionDidClose:(NetworkConnection*)cx;
114
115 - (void)handleInitialResponse:(NSXMLDocument*)response;
116
117 - (void)handleResponse:(NSXMLDocument*)response;
118
119 - (void)errorEncountered:(NSString*)error;
120
121 @end