TCP/TCPWriter.m
author Jens Alfke <jens@mooseyard.com>
Fri May 30 13:54:38 2008 -0700 (2008-05-30)
changeset 9 980beba83fb7
parent 1 8267d5c429c4
child 10 a2aeb9b04ecc
permissions -rw-r--r--
Fixed a serious bug - a race condition where a data buffer in the writer's queue could be dealloced (not the NSData, but its bytes themselves) before the writer sent it, resulting in an EFAULT error.
     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         [_queue removeObjectAtIndex: 0];
    59     }
    60     
    61     const uint8_t* src = _currentData.bytes;
    62     src += _currentDataPos;
    63     NSInteger len = _currentData.length - _currentDataPos;
    64     NSInteger written = [(NSOutputStream*)_stream write: src maxLength: len];
    65     if( written < 0 )
    66         [self _gotError];
    67     else if( written < len ) {
    68         LogTo(TCPVerbose,@"%@ wrote %i bytes (of %u)", self,written,len);
    69         _currentDataPos += written;
    70     } else {
    71         LogTo(TCPVerbose,@"%@ wrote %i bytes", self,written);
    72         setObj(&_currentData,nil);
    73     }
    74 }
    75 
    76 
    77 - (void) queueIsEmpty
    78 {
    79 }
    80 
    81 
    82 @end
    83 
    84 
    85 /*
    86  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    87  
    88  Redistribution and use in source and binary forms, with or without modification, are permitted
    89  provided that the following conditions are met:
    90  
    91  * Redistributions of source code must retain the above copyright notice, this list of conditions
    92  and the following disclaimer.
    93  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    94  and the following disclaimer in the documentation and/or other materials provided with the
    95  distribution.
    96  
    97  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    98  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    99  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   100  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   101  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   102   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   103  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   104  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   105  */