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