-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPSocketProxy.m
65 lines (52 loc) · 2.34 KB
/
UDPSocketProxy.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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "UDPSocketProxy.h"
#import "TiUtils.h"
#include <sys/socket.h>
#include <netinet/in.h>
@implementation UDPSocketProxy
- (void) send: (NSArray*) args {
NSString *msg = [TiUtils stringValue:[args objectAtIndex: 0]];
NSString *host = [TiUtils stringValue:[args objectAtIndex: 1]];
NSInteger port = [TiUtils intValue: [args objectAtIndex: 2]];
NSLog(@"%@ send: %@ to %@:%i", self, msg, host, port);
struct sockaddr_in destinationAddress;
socklen_t sockaddr_destaddr_len = sizeof(destinationAddress);
memset(&destinationAddress, 0, sockaddr_destaddr_len);
destinationAddress.sin_len = (__uint8_t) sockaddr_destaddr_len;
destinationAddress.sin_family = AF_INET;
destinationAddress.sin_port = htons(port);
destinationAddress.sin_addr.s_addr = inet_addr([host cStringUsingEncoding: NSUTF8StringEncoding]);
NSData *destinationAddressData = [NSData dataWithBytes:&destinationAddress length:sizeof(destinationAddress)];
NSData *data = [msg dataUsingEncoding: NSUTF8StringEncoding];
CFSocketError socket_error = CFSocketSendData(_socket, (CFDataRef) destinationAddressData, (CFDataRef) data, 10);
if (socket_error) {
NSLog(@"socket error: %li", socket_error);
} else {
NSLog(@"sent: '%@' to %@:%i", msg, host, port);
}
}
- (id)init {
self = [super init];
if (self) {
int sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
_socket = CFSocketCreate(
kCFAllocatorDefault, // allocator
PF_INET, // protocolFamily
SOCK_DGRAM, // socketType
IPPROTO_UDP, // protocol
kCFSocketDataCallBack, // callbackTypes
NULL, // callout
NULL); // context
}
NSLog(@"[INFO] new UDPSocketProxy:%@", self);
return self;
}
@end