Move out of 1.5 beta to stable, and update the copyright years.
[macgdbp.git] / Source / NetworkCallbackController.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 <CoreFoundation/CoreFoundation.h>
18 #import <Foundation/Foundation.h>
19
20 @class NetworkConnection;
21
22 // This class is used for the CFNetwork callbacks. It is a private class and
23 // the instance is owned by the NewtorkConnection instance. This class can be
24 // considered an extension of NetworkConnection.
25 class NetworkCallbackController
26 {
27 public:
28 // This object should be constructed on the thread which the streams are
29 // to be scheduled on. It will hold a weak reference to the run loop on that
30 // thread.
31 explicit NetworkCallbackController(NetworkConnection* connection);
32
33 // Creates a socket and schedules it on the current run loop.
34 void OpenConnection(NSUInteger port);
35
36 // Closes down the read/write streams.
37 void CloseConnection();
38
39 // Checks whether the write stream is ready for writing.
40 BOOL WriteStreamCanAcceptBytes();
41
42 // Writes the string to the write stream. This will block, so be sure to check
43 // if it can write before calling this. Returns YES if the string was
44 // successfully written.
45 BOOL WriteString(NSString* string);
46
47 private:
48 // These static methods forward an invocation to the instance methods. The
49 // last void pointer, named |self|, is the instance of this class.
50 static void SocketAcceptCallback(CFSocketRef socket,
51 CFSocketCallBackType callbackType,
52 CFDataRef address,
53 const void* data,
54 void* self);
55 static void ReadStreamCallback(CFReadStreamRef stream,
56 CFStreamEventType eventType,
57 void* self);
58 static void WriteStreamCallback(CFWriteStreamRef stream,
59 CFStreamEventType eventType,
60 void* self);
61
62 void OnSocketAccept(CFSocketRef socket,
63 CFDataRef address,
64 const void* data);
65 void OnReadStreamEvent(CFReadStreamRef stream, CFStreamEventType eventType);
66 void OnWriteStreamEvent(CFWriteStreamRef stream, CFStreamEventType eventType);
67
68 // Closes down the listening socket but keeps the streams alive. This can be
69 // called multiple times, even if the socket is NULL.
70 void CloseSocket();
71
72 // Removes the read or write stream from the run loop, closes the stream,
73 // releases the reference.
74 void UnscheduleReadStream();
75 void UnscheduleWriteStream();
76
77 // Messages the NetworkConnection's delegate and takes ownership of |error|.
78 void ReportError(CFErrorRef error);
79
80 // The socket that listens for incoming connections from the engine.
81 CFSocketRef listeningSocket_; // Strong.
82
83 // The child socket from the |listeningSocket_| that is connected to the
84 // engine. The streams are attached to this socket.
85 CFSocketNativeHandle socketHandle_;
86
87 // The read and write streams that are scheduled on the |runLoop_|. Both are
88 // weak and are owned by the run loop source.
89 CFReadStreamRef readStream_;
90 CFWriteStreamRef writeStream_;
91
92 NetworkConnection* connection_; // Weak, owns this.
93 CFRunLoopRef runLoop_; // Weak.
94 };
95