forked from Figure53/F53OSC
-
Notifications
You must be signed in to change notification settings - Fork 7
/
F53OSCSocket.m
executable file
·222 lines (182 loc) · 5.37 KB
/
F53OSCSocket.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// F53OSCSocket.m
//
// Created by Christopher Ashworth on 1/28/13.
//
// Copyright (c) 2013 Figure 53 LLC, http://figure53.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "F53OSCSocket.h"
#import "F53OSCPacket.h"
#define TIMEOUT 3
#define END 0300 /* indicates end of packet */
#define ESC 0333 /* indicates byte stuffing */
#define ESC_END 0334 /* ESC ESC_END means END data byte */
#define ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
@implementation F53OSCSocket
+ (F53OSCSocket *) socketWithTcpSocket:(GCDAsyncSocket *)socket
{
return [[F53OSCSocket alloc] initWithTcpSocket:socket];
}
+ (F53OSCSocket *) socketWithUdpSocket:(GCDAsyncUdpSocket *)socket
{
return [[F53OSCSocket alloc] initWithUdpSocket:socket];
}
- (id) initWithTcpSocket:(GCDAsyncSocket *)socket;
{
self = [super init];
if ( self )
{
_tcpSocket = socket;
_udpSocket = nil;
_host = @"localhost";
_port = 0;
}
return self;
}
- (id) initWithUdpSocket:(GCDAsyncUdpSocket *)socket
{
self = [super init];
if ( self )
{
_tcpSocket = nil;
_udpSocket = socket;
_host = @"localhost";
_port = 0;
}
return self;
}
- (void) dealloc
{
[_tcpSocket setDelegate:nil];
[_tcpSocket disconnect];
//[_tcpSocket release];
_tcpSocket = nil;
[_udpSocket setDelegate:nil];
//[_udpSocket release];
_udpSocket = nil;
//[_host release];
_host = nil;
//[super dealloc];
}
- (NSString *) description
{
if ( self.isTcpSocket )
return [NSString stringWithFormat:@"[TCP socket %@:%u isConnected = %i]", _host, _port, self.isConnected ];
else
return [NSString stringWithFormat:@"[UDP socket %@:%u]", _host, _port ];
}
- (GCDAsyncSocket *) tcpSocket
{
return _tcpSocket;
}
- (GCDAsyncUdpSocket *) udpSocket
{
return _udpSocket;
}
- (BOOL) isTcpSocket
{
return ( _tcpSocket != nil );
}
- (BOOL) isUdpSocket
{
return ( _udpSocket != nil );
}
@synthesize host = _host;
@synthesize port = _port;
- (BOOL) startListening
{
if ( _tcpSocket )
return [_tcpSocket acceptOnPort:_port error:nil];
if ( _udpSocket )
{
if ( [_udpSocket bindToPort:_port error:nil] )
return [_udpSocket beginReceiving:nil];
else
return NO;
}
return NO;
}
- (void) stopListening
{
if ( _tcpSocket )
[_tcpSocket disconnectAfterWriting];
if ( _udpSocket )
[_udpSocket close];
}
- (BOOL) connect
{
if ( _tcpSocket )
{
if ( _host && _port )
return [_tcpSocket connectToHost:_host onPort:_port error:nil];
else
return NO;
}
if ( _udpSocket )
return YES;
return NO;
}
- (void) disconnect
{
[_tcpSocket disconnect];
}
- (BOOL) isConnected
{
if ( _tcpSocket )
return [_tcpSocket isConnected];
if ( _udpSocket )
return YES;
return NO;
}
- (void) sendPacket:(F53OSCPacket *)packet
{
//NSLog( @"%@ sending packet: %@", self, packet );
NSData *data = [packet packetData];
//NSLog( @"%@ sending message with native length: %li", self, [data length] );
if ( _tcpSocket )
{
// Outgoing OSC messages are framed using the double END SLIP protocol: http://www.rfc-editor.org/rfc/rfc1055.txt
NSMutableData *slipData = [NSMutableData data];
Byte esc_end[2] = {ESC, ESC_END};
Byte esc_esc[2] = {ESC, ESC_ESC};
Byte end[1] = {END};
[slipData appendBytes:end length:1];
NSUInteger length = [data length];
const Byte *buffer = [data bytes];
for ( NSUInteger index = 0; index < length; index++ )
{
if ( buffer[index] == END )
[slipData appendBytes:esc_end length:2];
else if ( buffer[index] == ESC )
[slipData appendBytes:esc_esc length:2];
else
[slipData appendBytes:&(buffer[index]) length:1];
}
[slipData appendBytes:end length:1];
[_tcpSocket writeData:slipData withTimeout:TIMEOUT tag:[slipData length]];
}
else if ( _udpSocket )
{
[_udpSocket sendData:data toHost:_host port:_port withTimeout:TIMEOUT tag:0];
[_udpSocket closeAfterSending];
}
}
@end