-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv002_TCP_Socket_ServerPlugIn.m
192 lines (147 loc) · 5.25 KB
/
v002_TCP_Socket_ServerPlugIn.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// v002_TCP_Socket_ReaderPlugIn.m
// v002 TCP Socket Reader
//
// Created by vade on 7/6/10.
// Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//
/* It's highly recommended to use CGL macros instead of changing the current context for plug-ins that perform OpenGL rendering */
#import <OpenGL/CGLMacro.h>
#import "v002_TCP_Socket_ServerPlugIn.h"
#define kQCPlugIn_Name @"v002 TCP Socket Server"
#define kQCPlugIn_Description @"Opens a raw TCP Socket on a specified port, and outputs data as a string."
#include <sys/socket.h>
#include <netinet/in.h>
@implementation v002_TCP_Socket_ServerPlugIn
@synthesize listenSocket;
@synthesize messageString;
@synthesize connectedSockets;
@dynamic inputPort;
@dynamic inputData;
@dynamic outputData;
+ (NSDictionary*) attributes
{
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey,
[kQCPlugIn_Description stringByAppendingString:kv002DescriptionAddOnText], QCPlugInAttributeDescriptionKey,
kQCPlugIn_Category, @"categories", nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
if([key isEqualToString:@"inputPort"])
return [NSDictionary dictionaryWithObjectsAndKeys:@"Port", QCPortAttributeNameKey, [NSNumber numberWithInteger:31337], QCPortAttributeDefaultValueKey, [NSNumber numberWithInteger:1024], QCPortAttributeMinimumValueKey, [NSNumber numberWithInteger:65535], QCPortAttributeMaximumValueKey, nil];
if([key isEqualToString:@"inputData"])
return [NSDictionary dictionaryWithObjectsAndKeys:@"Input (to all Clients)", QCPortAttributeNameKey, nil];
if([key isEqualToString:@"outputData"])
return [NSDictionary dictionaryWithObjectsAndKeys:@"Output (from any Client)", QCPortAttributeNameKey, nil];
return nil;
}
+ (QCPlugInExecutionMode) executionMode
{
return kQCPlugInExecutionModeProvider;
}
+ (QCPlugInTimeMode) timeMode
{
return kQCPlugInTimeModeIdle;
}
- (id) init
{
if(self = [super init])
{
// make a new socket.
self.listenSocket = [[[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()] autorelease];
//listenSocket = [[AsyncSocket alloc] initWithDelegate:self];
//[listenSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
// defaults
self.messageString = @"";
connectedSockets = [[NSMutableArray alloc] initWithCapacity:3];
}
return self;
}
- (void) dealloc
{
self.listenSocket = nil;
self.messageString = nil;
self.connectedSockets = nil;
[super dealloc];
}
@end
@implementation v002_TCP_Socket_ServerPlugIn (Execution)
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
if([self didValueForInputKeyChange:@"inputPort"])
{
// stop listening for incomming connections
[listenSocket disconnect];
// disconnect clients
// Stop any client connections
int i;
for(i = 0; i < [connectedSockets count]; i++)
{
// Call disconnect on the socket,
// which will invoke the onSocketDidDisconnect: method,
// which will remove the socket from the list.
[[connectedSockets objectAtIndex:i] disconnect];
}
if(![listenSocket acceptOnPort:self.inputPort error:nil])
{
[context logMessage:@"Unable to bind on port: %u", self.inputPort];
}
}
if([self didValueForInputKeyChange:@"inputData"])
{
NSData* data = [[self.inputData stringByAppendingString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding];
for(GCDAsyncSocket *sock in connectedSockets)
{
[sock writeData:data withTimeout:-1 tag:0];
}
}
self.outputData = self.messageString;
return YES;
}
#pragma mark - Async Socket handling
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
NSLog(@"Server didAcceptNewSocket");
[connectedSockets addObject:newSocket];
// [newSocket readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:0];
NSString *welcomeMsg = @"v002 TCP Socket Server Connection Accepted\r\n";
NSData *welcomeData = [welcomeMsg dataUsingEncoding:NSUTF8StringEncoding];
[newSocket writeData:welcomeData withTimeout:-1 tag:0];
[newSocket readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
NSLog(@"Server socket didConnectToHost %@ on port %u", host, port);
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
NSLog(@"Server socket did write data");
[sock readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSLog(@"Server socket did read data");
if([data length] > 1) // make sure its not just an empty line
{
NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];
NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease];
if(msg)
{
self.messageString = msg;
}
else
{
self.messageString = @"";
NSLog(@"Error converting received data into UTF-8 String");
}
}
else
self.messageString = @"";
[sock readDataToData:[GCDAsyncSocket LFData] withTimeout:-1 tag:0];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;
{
NSLog(@"Server socketDidDisconnect");
[connectedSockets removeObject:sock];
}
@end