Made C99 project default.
5 // Created by Jens Alfke on 5/18/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
10 #import "BLIPWriter.h"
11 #import "BLIP_Internal.h"
17 #define kDefaultFrameSize 4096
20 @implementation BLIPWriter
31 [_outBox makeObjectsPerformSelector: @selector(_connectionClosed) withObject: nil];
36 @synthesize numRequestsSent=_numRequestsSent;
41 return _outBox.count>0 || [super isBusy];
45 - (void) _queueMessage: (BLIPMessage*)msg isNew: (BOOL)isNew
47 int n = _outBox.count, index;
48 if( msg.urgent && n > 1 ) {
49 // High-priority gets queued after the last existing high-priority message,
50 // leaving one regular-priority message in between if possible.
51 for( index=n-1; index>0; index-- ) {
52 BLIPMessage *otherMsg = [_outBox objectAtIndex: index];
53 if( [otherMsg urgent] ) {
54 index = MIN(index+2, n);
56 } else if( isNew && otherMsg._bytesWritten==0 ) {
57 // But have to keep message starts in order
65 // Regular priority goes at the end of the queue:
69 _outBox = [[NSMutableArray alloc] init];
70 [_outBox insertObject: msg atIndex: index];
73 LogTo(BLIP,@"%@ queuing outgoing %@ at index %i",self,msg,index);
80 - (BOOL) sendMessage: (BLIPMessage*)message
82 Assert(!message.sent,@"message has already been sent");
83 [self _queueMessage: message isNew: YES];
88 - (BOOL) sendRequest: (BLIPRequest*)q response: (BLIPResponse*)response
91 Warn(@"%@: Attempt to send a request after the connection has started closing: %@",self,q);
94 [q _assignedNumber: ++_numRequestsSent];
96 [response _assignedNumber: _numRequestsSent];
97 [(BLIPReader*)self.reader _addPendingResponse: response];
99 return [self sendMessage: q];
103 - (void) queueIsEmpty
105 if( _outBox.count > 0 ) {
106 // Pop first message in queue:
107 BLIPMessage *msg = [[_outBox objectAtIndex: 0] retain];
108 [_outBox removeObjectAtIndex: 0];
110 // As an optimization, allow message to send a big frame unless there's a higher-priority
111 // message right behind it:
112 size_t frameSize = kDefaultFrameSize;
113 if( msg.urgent || _outBox.count==0 || ! [[_outBox objectAtIndex: 0] urgent] )
116 if( [msg _writeFrameTo: self maxSize: frameSize] ) {
117 // add it back so it can send its next frame later:
118 [self _queueMessage: msg isNew: NO];
122 LogTo(BLIPVerbose,@"%@: no more work for writer",self);
132 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
134 Redistribution and use in source and binary forms, with or without modification, are permitted
135 provided that the following conditions are met:
137 * Redistributions of source code must retain the above copyright notice, this list of conditions
138 and the following disclaimer.
139 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
140 and the following disclaimer in the documentation and/or other materials provided with the
143 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
144 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
145 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
146 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
147 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
148 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
149 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
150 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.