21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
-
+
-
+
|
#import "OFPair.h"
#import "OFStdIOStream.h"
#import "OFStream.h"
#import "OFString.h"
#import "OFTCPSocket.h"
#import "OFURL.h"
#define BUFFER_LEN 4096
#define bufferLen 4096
@interface OFSock: OFObject <OFApplicationDelegate, OFStreamDelegate>
{
char _buffer[BUFFER_LEN];
char _buffer[bufferLen];
OFMutableArray OF_GENERIC(OFPair OF_GENERIC(OFStream *, OFStream *) *)
*_streams;
int _errors;
}
@end
OF_APPLICATION_DELEGATE(OFSock)
static OFPair OF_GENERIC(OFStream *, OFStream *) *
streamFromString(OFString *string)
{
OFURL *URL;
OFString *scheme;
if ([string isEqual: @"-"])
return [OFPair pairWithFirstObject: of_stdin
secondObject: of_stdout];
return [OFPair pairWithFirstObject: OFStdIn
secondObject: OFStdOut];
URL = [OFURL URLWithString: string];
scheme = URL.scheme;
if ([scheme isEqual: @"tcp"]) {
OFTCPSocket *sock = [OFTCPSocket socket];
if (URL.port == nil) {
[of_stderr writeLine: @"Need a port!"];
[OFStdErr writeLine: @"Need a port!"];
[OFApplication terminateWithStatus: 1];
}
[sock connectToHost: URL.host port: URL.port.shortValue];
return [OFPair pairWithFirstObject: sock secondObject: sock];
}
[of_stderr writeFormat: @"Invalid protocol: %@\n", scheme];
[OFStdErr writeFormat: @"Invalid protocol: %@\n", scheme];
[OFApplication terminateWithStatus: 1];
abort();
}
@implementation OFSock
- (void)applicationDidFinishLaunching
{
OFArray OF_GENERIC(OFString *) *arguments = [OFApplication arguments];
if (arguments.count < 1) {
[of_stderr writeLine: @"Need at least one argument!"];
[OFStdErr writeLine: @"Need at least one argument!"];
[OFApplication terminateWithStatus: 1];
}
_streams = [[OFMutableArray alloc] init];
for (OFString *argument in arguments) {
OFPair *pair = streamFromString(argument);
[pair.firstObject setDelegate: self];
[_streams addObject: pair];
}
if (arguments.count == 1) {
of_stdin.delegate = self;
OFStdIn.delegate = self;
[_streams addObject:
[_streams addObject: [OFPair pairWithFirstObject: OFStdIn
[OFPair pairWithFirstObject: of_stdin
secondObject: of_stdout]];
secondObject: OFStdOut]];
}
for (OFPair *pair in _streams)
[pair.firstObject asyncReadIntoBuffer: _buffer
length: BUFFER_LEN];
length: bufferLen];
}
- (void)removeDeadStream: (OFStream *)stream
{
size_t count = _streams.count;
for (size_t i = 0; i < count; i++) {
|
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
-
-
+
+
|
- (bool)stream: (OFStream *)stream
didReadIntoBuffer: (void *)buffer
length: (size_t)length
exception: (id)exception
{
if (exception != nil) {
[of_stderr writeFormat: @"Exception on stream %@: %@\n",
stream, exception];
[OFStdErr writeFormat: @"Exception on stream %@: %@\n",
stream, exception];
_errors++;
[self removeDeadStream: stream];
return false;
}
if (stream.atEndOfStream) {
[self removeDeadStream: stream];
|