3 * Copyright (c) 2007 - 2011, 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>
20 class NetworkCallbackController
;
22 @
class NetworkCallbackController
;
25 @protocol NetworkConnectionDelegate
;
26 @
class LoggingController
;
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
34 // The port to connect on.
37 // If the connection to the debugger engine is currently active.
40 // The thread on which network operations are performed. Weak.
43 // Reference to the message loop that the socket runs on. Weak.
46 // Internal class that manages CFNetwork callbacks. Strong.
47 NetworkCallbackController
* callbackController_
;
49 // The raw CFSocket on which the two streams are based. Strong.
52 // The read stream that is scheduled on the main run loop. Weak.
53 CFReadStreamRef readStream_
;
55 // The write stream. Weak.
56 CFWriteStreamRef writeStream_
;
58 // Run loop source used to quit the thread. Strong.
59 CFRunLoopSourceRef quitSource_
;
61 // An ever-increasing integer that gives each transaction a unique ID for the
63 NSUInteger transactionID
;
65 // The most recently received transaction ID.
66 NSUInteger lastReadTransaction_
;
68 // The last transactionID written to the stream.
69 NSUInteger lastWrittenTransaction_
;
71 // To prevent blocked writing, we enqueue all writes and then wait for the
72 // write stream to tell us it's ready. We store the pending commands in this
73 // array. We use this as a queue (FIFO), with index 0 being first.
74 NSMutableArray
* queuedWrites_
;
76 // We send queued writes in multiple places, sometimes off a run loop event.
77 // Because of this, we need to ensure that only one client is dequeing and
79 NSRecursiveLock
* writeQueueLock_
;
81 // Information about the current read loop. We append to |currentPacket_|
82 // until |currentPacketSize_| has reached |packetSize_|.
83 NSMutableString
* currentPacket_
;
85 int currentPacketIndex_
;
87 // The delegate. All methods are executed on the main thread.
88 NSObject
<NetworkConnectionDelegate
>* delegate_
;
91 @
property (readonly
) NSUInteger port
;
92 @
property (readonly
) BOOL connected
;
93 @
property (assign
) id
<NetworkConnectionDelegate
> delegate
;
95 - (id
)initWithPort
:(NSUInteger
)aPort
;
100 // This sends the given command format to the debugger. This method is thread
101 // safe and schedules the request on the |runLoop_|.
102 - (NSNumber
*)sendCommandWithFormat
:(NSString
*)format
, ...;
104 - (NSString
*)escapedURIPath
:(NSString
*)path
;
105 - (NSInteger
)transactionIDFromResponse
:(NSXMLDocument
*)response
;
106 - (NSInteger
)transactionIDFromCommand
:(NSString
*)command
;
110 // Delegate ////////////////////////////////////////////////////////////////////
112 @protocol NetworkConnectionDelegate
<NSObject
>
116 - (void)connectionDidAccept
:(NetworkConnection
*)cx
;
117 - (void)connectionDidClose
:(NetworkConnection
*)cx
;
119 - (void)handleInitialResponse
:(NSXMLDocument
*)response
;
121 - (void)handleResponse
:(NSXMLDocument
*)response
;
123 - (void)errorEncountered
:(NSString
*)error
;