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