5 // Created by Jens Alfke on 5/10/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
10 #import "TCP_Internal.h"
13 @implementation TCPWriter
19 [_currentData release];
30 - (id) propertyForKey: (CFStringRef)cfStreamProperty
32 CFTypeRef value = CFWriteStreamCopyProperty((CFWriteStreamRef)_stream,cfStreamProperty);
33 return [(id)CFMakeCollectable(value) autorelease];
36 - (void) setProperty: (id)value forKey: (CFStringRef)cfStreamProperty
38 if( ! CFWriteStreamSetProperty((CFWriteStreamRef)_stream,cfStreamProperty,(CFTypeRef)value) )
39 Warn(@"%@ didn't accept property '%@'", self,cfStreamProperty);
45 return _currentData || _queue.count > 0;
49 - (void) writeData: (NSData*)data
52 _queue = [[NSMutableArray alloc] init];
53 [_queue addObject: data];
54 if( _queue.count==1 && ((NSOutputStream*)_stream).hasSpaceAvailable )
61 if( ! _currentData ) {
62 if( _queue.count==0 ) {
63 [self queueIsEmpty]; // this may call -writeData, which will call _canWrite again
66 _currentData = [[_queue objectAtIndex: 0] retain];
68 [_queue removeObjectAtIndex: 0];
71 const uint8_t* src = _currentData.bytes;
72 src += _currentDataPos;
73 NSInteger len = _currentData.length - _currentDataPos;
74 NSInteger written = [(NSOutputStream*)_stream write: src maxLength: len];
77 else if( written < len ) {
78 LogTo(TCPVerbose,@"%@ wrote %i bytes (of %u)", self,written,len);
79 _currentDataPos += written;
81 LogTo(TCPVerbose,@"%@ wrote %i bytes", self,written);
82 setObj(&_currentData,nil);
96 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
98 Redistribution and use in source and binary forms, with or without modification, are permitted
99 provided that the following conditions are met:
101 * Redistributions of source code must retain the above copyright notice, this list of conditions
102 and the following disclaimer.
103 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
104 and the following disclaimer in the documentation and/or other materials provided with the
107 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
108 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
109 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
110 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
111 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
112 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
113 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
114 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.