Write MessageQueue to replace most NetworkCallbackController.
[macgdbp.git] / Source / MessageQueue.h
1 /*
2 * MacGDBp
3 * Copyright (c) 2013, 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 <Foundation/Foundation.h>
18
19 @protocol MessageQueueDelegate;
20
21 // MessageQueue operates a listening socket, that is connected to another
22 // program with which it exchanges UTF8 string messages. A message contains two
23 // parts, both terminated by '\0'. The first is an ASCII integer number that is
24 // the length of the second part. The second part is the actual string message.
25 @interface MessageQueue : NSObject {
26 @private
27 // The port number on which to open a listening socket.
28 NSUInteger _port;
29
30 // The thread and its run loop on which this class primarily operates.
31 NSThread* _thread;
32 NSRunLoop* _runLoop;
33
34 // Whether or not the message queue is connected to a client.
35 BOOL _connected;
36
37 // A queue of messages that are waiting to be sent.
38 NSMutableArray* _queue;
39
40 // The delegate for this class.
41 id<MessageQueueDelegate> _delegate;
42
43 // The socket that listens for new incoming connections.
44 CFSocketRef _socket;
45
46 // The child socket that has been accepted from |_socket|.
47 CFSocketNativeHandle _child;
48
49 // The read and write streams that are created on the |_child| socket.
50 CFReadStreamRef _readStream;
51 CFWriteStreamRef _writeStream;
52
53 // When a message is being read, this temporary buffer is used to build up
54 // the complete message from successive reads.
55 NSMutableString* _message;
56 NSUInteger _totalMessageSize;
57 NSUInteger _messageSize;
58 }
59
60 // Creates a new MessasgeQueue that will listen on |port| and report information
61 // to its |delegate|.
62 - (id)initWithPort:(NSUInteger)port delegate:(id<MessageQueueDelegate>)delegate;
63
64 // Whether or not the message queue has attached itself to a child.
65 - (BOOL)isConnected;
66
67 // Opens a socket that will listen for connections.
68 - (void)connect;
69
70 // Closes either the listening or child socket and completely disconnects.
71 - (void)disconnect;
72
73 // Enqueues a |message| to be sent to the client. This may be called from any
74 // thread.
75 - (void)sendMessage:(NSString*)message;
76
77 @end
78
79 // Delegate ////////////////////////////////////////////////////////////////////
80
81 // The delegate for the message queue. These methods may be called on any thread.
82 @protocol MessageQueueDelegate <NSObject>
83 // Callback for any errors that the MessageQueue encounters.
84 - (void)messageQueueError:(NSError*)error;
85
86 // Called when the listening socket has accepted a child socket.
87 - (void)clientDidConnect:(MessageQueue*)queue;
88
89 // Called when the child socket has been disconnected.
90 - (void)clientDidDisconnect:(MessageQueue*)queue;
91
92 // If the write stream is ready, the delegate controls whether or not the next
93 // pending message should be sent via the result of this method.
94 - (BOOL)shouldSendMessage;
95
96 // Callback for when a message has been sent.
97 - (void)didSendMessage:(NSString*)message;
98
99 // Callback with the message content when one has been receieved.
100 - (void)didReceiveMessage:(NSString*)message;
101 @end