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