TCP/TCPWriter.m
author Jens Alfke <jens@mooseyard.com>
Fri May 23 17:37:36 2008 -0700 (2008-05-23)
changeset 0 9d67172bb323
child 1 8267d5c429c4
permissions -rw-r--r--
First checkin after breaking out of Cloudy
     1 //
     2 //  TCPWriter.m
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 5/10/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "TCPWriter.h"
    10 #import "TCP_Internal.h"
    11 
    12 
    13 @implementation TCPWriter
    14 
    15 
    16 - (void) dealloc
    17 {
    18     [_queue release];
    19     [_currentData release];
    20     [super dealloc];
    21 }
    22 
    23 
    24 - (TCPReader*) reader
    25 {
    26     return _conn.reader;
    27 }
    28 
    29 
    30 - (id) propertyForKey: (CFStringRef)cfStreamProperty
    31 {
    32     CFTypeRef value = CFWriteStreamCopyProperty((CFWriteStreamRef)_stream,cfStreamProperty);
    33     return [(id)CFMakeCollectable(value) autorelease];
    34 }
    35 
    36 - (void) setProperty: (id)value forKey: (CFStringRef)cfStreamProperty
    37 {
    38     if( ! CFWriteStreamSetProperty((CFWriteStreamRef)_stream,cfStreamProperty,(CFTypeRef)value) )
    39         Warn(@"%@ didn't accept property '%@'", self,cfStreamProperty);
    40 }
    41 
    42 
    43 - (BOOL) isBusy
    44 {
    45     return _currentData || _queue.count > 0;
    46 }
    47 
    48 
    49 - (void) writeData: (NSData*)data
    50 {
    51     if( !_queue )
    52         _queue = [[NSMutableArray alloc] init];
    53     [_queue addObject: data];
    54     if( _queue.count==1 && ((NSOutputStream*)_stream).hasSpaceAvailable )
    55         [self _canWrite];
    56 }
    57 
    58 
    59 - (void) _canWrite
    60 {
    61     if( ! _currentData ) {
    62         if( _queue.count==0 ) {
    63             [self queueIsEmpty]; // this may call -writeData, which will call _canWrite again
    64             return;
    65         }
    66         _currentData = [[_queue objectAtIndex: 0] retain];
    67         _currentDataPos = 0;
    68         [_queue removeObjectAtIndex: 0];
    69     }
    70     
    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];
    75     if( written < 0 )
    76         [self _gotError];
    77     else if( written < len ) {
    78         LogTo(TCPVerbose,@"%@ wrote %i bytes (of %u)", self,written,len);
    79         _currentDataPos += written;
    80     } else {
    81         LogTo(TCPVerbose,@"%@ wrote %i bytes", self,written);
    82         setObj(&_currentData,nil);
    83     }
    84 }
    85 
    86 
    87 - (void) queueIsEmpty
    88 {
    89 }
    90 
    91 
    92 @end
    93 
    94 
    95 /*
    96  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    97  
    98  Redistribution and use in source and binary forms, with or without modification, are permitted
    99  provided that the following conditions are met:
   100  
   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
   105  distribution.
   106  
   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.
   115  */