TCP/TCPWriter.m
author Jens Alfke <jens@mooseyard.com>
Sat May 24 17:25:06 2008 -0700 (2008-05-24)
changeset 2 9fdd8dba529c
parent 0 9d67172bb323
child 7 5936db2c1987
permissions -rw-r--r--
* Added more documentation.
* Minor API changes.
     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 #import "Logging.h"
    13 #import "Test.h"
    14 
    15 
    16 @implementation TCPWriter
    17 
    18 
    19 - (void) dealloc
    20 {
    21     [_queue release];
    22     [_currentData release];
    23     [super dealloc];
    24 }
    25 
    26 
    27 - (TCPReader*) reader
    28 {
    29     return _conn.reader;
    30 }
    31 
    32 
    33 - (id) propertyForKey: (CFStringRef)cfStreamProperty
    34 {
    35     CFTypeRef value = CFWriteStreamCopyProperty((CFWriteStreamRef)_stream,cfStreamProperty);
    36     return [(id)CFMakeCollectable(value) autorelease];
    37 }
    38 
    39 - (void) setProperty: (id)value forKey: (CFStringRef)cfStreamProperty
    40 {
    41     if( ! CFWriteStreamSetProperty((CFWriteStreamRef)_stream,cfStreamProperty,(CFTypeRef)value) )
    42         Warn(@"%@ didn't accept property '%@'", self,cfStreamProperty);
    43 }
    44 
    45 
    46 - (BOOL) isBusy
    47 {
    48     return _currentData || _queue.count > 0;
    49 }
    50 
    51 
    52 - (void) writeData: (NSData*)data
    53 {
    54     if( !_queue )
    55         _queue = [[NSMutableArray alloc] init];
    56     [_queue addObject: data];
    57     if( _queue.count==1 && ((NSOutputStream*)_stream).hasSpaceAvailable )
    58         [self _canWrite];
    59 }
    60 
    61 
    62 - (void) _canWrite
    63 {
    64     if( ! _currentData ) {
    65         if( _queue.count==0 ) {
    66             [self queueIsEmpty]; // this may call -writeData, which will call _canWrite again
    67             return;
    68         }
    69         _currentData = [[_queue objectAtIndex: 0] retain];
    70         _currentDataPos = 0;
    71         [_queue removeObjectAtIndex: 0];
    72     }
    73     
    74     const uint8_t* src = _currentData.bytes;
    75     src += _currentDataPos;
    76     NSInteger len = _currentData.length - _currentDataPos;
    77     NSInteger written = [(NSOutputStream*)_stream write: src maxLength: len];
    78     if( written < 0 )
    79         [self _gotError];
    80     else if( written < len ) {
    81         LogTo(TCPVerbose,@"%@ wrote %i bytes (of %u)", self,written,len);
    82         _currentDataPos += written;
    83     } else {
    84         LogTo(TCPVerbose,@"%@ wrote %i bytes", self,written);
    85         setObj(&_currentData,nil);
    86     }
    87 }
    88 
    89 
    90 - (void) queueIsEmpty
    91 {
    92 }
    93 
    94 
    95 @end
    96 
    97 
    98 /*
    99  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   100  
   101  Redistribution and use in source and binary forms, with or without modification, are permitted
   102  provided that the following conditions are met:
   103  
   104  * Redistributions of source code must retain the above copyright notice, this list of conditions
   105  and the following disclaimer.
   106  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   107  and the following disclaimer in the documentation and/or other materials provided with the
   108  distribution.
   109  
   110  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   111  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   112  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   113  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   114  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   115   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   116  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   117  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   118  */