TCP/TCPWriter.m
author Jens Alfke <jens@mooseyard.com>
Sun May 10 19:05:52 2009 -0700 (2009-05-10)
changeset 46 50dc5502ef46
parent 7 5936db2c1987
permissions -rw-r--r--
Unnecessary self-merge (I'm just being confused with hg. Sorry.)
     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 - (BOOL) isBusy
    34 {
    35     return _currentData || _queue.count > 0;
    36 }
    37 
    38 
    39 - (void) writeData: (NSData*)data
    40 {
    41     if( !_queue )
    42         _queue = [[NSMutableArray alloc] init];
    43     [_queue addObject: data];
    44     if( _queue.count==1 && ((NSOutputStream*)_stream).hasSpaceAvailable )
    45         [self _canWrite];
    46 }
    47 
    48 
    49 - (void) _canWrite
    50 {
    51     if( ! _currentData ) {
    52         if( _queue.count==0 ) {
    53             [self queueIsEmpty]; // this may call -writeData, which will call _canWrite again
    54             return;
    55         }
    56         _currentData = [[_queue objectAtIndex: 0] retain];
    57         _currentDataPos = 0;
    58         LogTo(TCPVerbose,@"%@ using _currentData %p (%u bytes)", self,_currentData,_currentData.length);
    59         [_queue removeObjectAtIndex: 0];
    60     }
    61     
    62     const uint8_t* src = _currentData.bytes;
    63     src += _currentDataPos;
    64     NSInteger len = _currentData.length - _currentDataPos;
    65     NSInteger written = [(NSOutputStream*)_stream write: src maxLength: len];
    66     if( written < 0 )
    67         [self _gotError];
    68     else if( written < len ) {
    69         LogTo(TCPVerbose,@"%@ wrote %i bytes (of %u) from %p", self,written,len,_currentData);
    70         _currentDataPos += written;
    71     } else {
    72         LogTo(TCPVerbose,@"%@ wrote %i bytes, released %p", self,written,_currentData);
    73         setObj(&_currentData,nil);
    74     }
    75 }
    76 
    77 
    78 - (void) queueIsEmpty
    79 {
    80 }
    81 
    82 
    83 @end
    84 
    85 
    86 /*
    87  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    88  
    89  Redistribution and use in source and binary forms, with or without modification, are permitted
    90  provided that the following conditions are met:
    91  
    92  * Redistributions of source code must retain the above copyright notice, this list of conditions
    93  and the following disclaimer.
    94  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    95  and the following disclaimer in the documentation and/or other materials provided with the
    96  distribution.
    97  
    98  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    99  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   100  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   101  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   102  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   103   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   104  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   105  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   106  */