Browse files

WhiteSpace

  • Loading branch information...
1 parent 5aa3792 commit 9a862397524b9d560740ca38e1e8bd8086e1cd2d @dpalmans dpalmans committed Apr 27, 2011
Showing with 159 additions and 160 deletions.
  1. +3 −4 Wompt/SocketIOConnection.m
  2. +9 −9 Wompt/WebSocketConnection.h
  3. +125 −125 Wompt/WebSocketConnection.m
  4. +3 −3 Wompt/Wompt.h
  5. +19 −19 Wompt/Wompt.m
View
7 Wompt/SocketIOConnection.m
@@ -69,7 +69,6 @@ - (void)handleData:(NSData *)data
}
case 'h':
-// NSLog(@"Heartbeat: %u %u", atoi((char *)&_pos[3]), _heartbeat);
[self sendMessage:[NSString stringWithFormat:@"~h~%d", _heartbeat++]];
break;
}
@@ -81,13 +80,13 @@ - (void)handleData:(NSData *)data
- (void)sendMessage:(NSString *)string
{
- NSString * message = [NSString stringWithFormat:@"~m~%u~m~%@", [string length], string];
- [self sendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
+ NSString * message = [NSString stringWithFormat:@"~m~%u~m~%@", [string length], string];
+ [self sendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)sendDict:(NSDictionary *)dict
{
- [self sendMessage:[dict jsonStringValue]];
+ [self sendMessage:[dict jsonStringValue]];
}
@end
View
18 Wompt/WebSocketConnection.h
@@ -18,19 +18,19 @@
@protected
id <WebSocketConnectionDelegate> _delegate;
NSString *_hostName;
- short _port;
+ short _port;
+
+@private
+ int _sock;
+ dispatch_source_t _readSource;
-@private
- int _sock;
- dispatch_source_t _readSource;
-
uint8_t *_pos;
- uint8_t *_buffer;
- size_t _bufferlen;
+ uint8_t *_buffer;
+ size_t _bufferlen;
uint8_t _readbuf[4096];
-
- NSData * _challenge;
+
+ NSData * _challenge;
int _state;
int _counter;
View
250 Wompt/WebSocketConnection.m
@@ -29,72 +29,72 @@ - (id)initWithHost:(NSString *)hostName port:(short)port delegate:(id<WebSocketC
self = [super init];
if (self) {
_delegate = [delegate retain];
- _hostName = [hostName retain];
- _port = port;
+ _hostName = [hostName retain];
+ _port = port;
_bufferlen = 4096;
- _buffer = malloc(_bufferlen);
+ _buffer = malloc(_bufferlen);
_state = 0;
_counter = 0;
-
+
// Try to connect to host
- char portString[6] = {0};
- struct addrinfo hints = {0};
- hints.ai_family = PF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- sprintf(portString, "%hu", port);
-
- struct addrinfo * ai = NULL;
- struct addrinfo * next = NULL;
-
- int ret = getaddrinfo([_hostName UTF8String], portString, &hints, &ai);
- if (ret != 0) {
- NSLog(@"getaddrinfo() failed (%d): %s", ret, gai_strerror(ret));
- [self release];
- return nil;
- }
-
- _sock = -1;
- for (next = ai; next != NULL && _sock == -1; next = next->ai_next) {
- _sock = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
- if (_sock != -1) {
- ret = connect(_sock, ai->ai_addr, ai->ai_addrlen);
- if (ret != 0) {
- close(_sock);
- _sock = -1;
- }
- }
- }
-
- freeaddrinfo(ai);
+ char portString[6] = {0};
+ struct addrinfo hints = {0};
+ hints.ai_family = PF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ sprintf(portString, "%hu", port);
+
+ struct addrinfo * ai = NULL;
+ struct addrinfo * next = NULL;
+
+ int ret = getaddrinfo([_hostName UTF8String], portString, &hints, &ai);
+ if (ret != 0) {
+ NSLog(@"getaddrinfo() failed (%d): %s", ret, gai_strerror(ret));
+ [self release];
+ return nil;
+ }
+
+ _sock = -1;
+ for (next = ai; next != NULL && _sock == -1; next = next->ai_next) {
+ _sock = socket(next->ai_family, next->ai_socktype, next->ai_protocol);
+ if (_sock != -1) {
+ ret = connect(_sock, ai->ai_addr, ai->ai_addrlen);
+ if (ret != 0) {
+ close(_sock);
+ _sock = -1;
+ }
+ }
+ }
+
+ freeaddrinfo(ai);
// Were we able to connect?
- if (_sock == -1) {
- NSLog(@"Unable to connect.");
- [self release];
- return nil;
- }
+ if (_sock == -1) {
+ NSLog(@"Unable to connect.");
+ [self release];
+ return nil;
+ }
int opt = 1;
- setsockopt(_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
+ setsockopt(_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
-
- dispatch_queue_t readQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- _readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, _sock, 0, readQueue);
-
- dispatch_source_set_event_handler(_readSource, ^{
- [self read];
- });
- dispatch_source_set_cancel_handler(_readSource, ^{
- dispatch_release(_readSource);
- close(_sock);
- });
-
- dispatch_resume(_readSource);
+
+ dispatch_queue_t readQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
+ _readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, _sock, 0, readQueue);
+
+ dispatch_source_set_event_handler(_readSource, ^{
+ [self read];
+ });
+ dispatch_source_set_cancel_handler(_readSource, ^{
+ dispatch_release(_readSource);
+ close(_sock);
+ });
+
+ dispatch_resume(_readSource);
// Try sending the http headers
- [self sendHeaders];
+ [self sendHeaders];
}
return self;
@@ -103,7 +103,7 @@ - (id)initWithHost:(NSString *)hostName port:(short)port delegate:(id<WebSocketC
- (void)dealloc
{
if (_buffer) free(_buffer);
- [_hostName release];
+ [_hostName release];
[_delegate release];
[_challenge release];
[super dealloc];
@@ -146,11 +146,11 @@ - (void)parse:(uint8_t)byte
case 2:
if (byte == 0x00) {
- _state = 3;
- _pos = _buffer;
- } else {
- NSLog(@"Unexpected char: %c", byte);
- }
+ _state = 3;
+ _pos = _buffer;
+ } else {
+ NSLog(@"Unexpected char: %c", byte);
+ }
break;
case 3:
@@ -188,19 +188,19 @@ - (void)read
}
- (void)send:(const void *)data length:(size_t)len
-{
- ssize_t size;
- const uint8_t * pos = data;
-
- while (len > 0) {
- size = write(_sock, pos, len);
- if (size == -1) {
+{
+ ssize_t size;
+ const uint8_t * pos = data;
+
+ while (len > 0) {
+ size = write(_sock, pos, len);
+ if (size == -1) {
[NSException raise:@"Failed to send data" format:@"Failed to send data (%d): %s", errno, strerror(errno)];
- }
-
- len -= size;
- pos += size;
- }
+ }
+
+ len -= size;
+ pos += size;
+ }
}
- (void)sendData:(NSData *)data
@@ -210,88 +210,88 @@ - (void)sendData:(NSData *)data
[self send:&header length:sizeof(header)];
[self send:[data bytes] length:[data length]];
- [self send:&trailer length:sizeof(trailer)];
+ [self send:&trailer length:sizeof(trailer)];
}
- (NSString *)generateKeyString:(uint32_t *)keyVal
{
- int numSpaces = (arc4random() % 12) + 1;
- int numChars = (arc4random() % 12) + 1;
+ int numSpaces = (arc4random() % 12) + 1;
+ int numChars = (arc4random() % 12) + 1;
- uint32_t num = (arc4random() % (INT32_MAX / numSpaces)) * numSpaces;
- NSMutableString * s = [NSMutableString stringWithFormat:@"%u", num];
- *keyVal = num / numSpaces;
+ uint32_t num = (arc4random() % (INT32_MAX / numSpaces)) * numSpaces;
+ NSMutableString * s = [NSMutableString stringWithFormat:@"%u", num];
+ *keyVal = num / numSpaces;
- for (int i = 0; i < numChars; i++) {
- unsigned long pos = (arc4random() % [s length]) + 1;
-
- char c = arc4random() % (14+68);
- c = (c <= 14) ? (c + 0x21) : (c - 14 + 0x3a);
-
- NSString * s2 = [NSString stringWithFormat:@"%c", c];
-
- [s insertString:s2 atIndex:pos];
- }
-
- for (int i = 0; i < numSpaces; i++) {
- unsigned long pos = (arc4random() % ([s length]-1)) + 1;
- [s insertString:@" " atIndex:pos];
- }
-
- return s;
+ for (int i = 0; i < numChars; i++) {
+ unsigned long pos = (arc4random() % [s length]) + 1;
+
+ char c = arc4random() % (14+68);
+ c = (c <= 14) ? (c + 0x21) : (c - 14 + 0x3a);
+
+ NSString * s2 = [NSString stringWithFormat:@"%c", c];
+
+ [s insertString:s2 atIndex:pos];
+ }
+
+ for (int i = 0; i < numSpaces; i++) {
+ unsigned long pos = (arc4random() % ([s length]-1)) + 1;
+ [s insertString:@" " atIndex:pos];
+ }
+
+ return s;
}
- (NSData *)computeSignatureFromKey1:(uint32_t)key1 key2:(uint32_t)key2 key3:(uint8_t *)key3
{
- unsigned char md5[CC_MD5_DIGEST_LENGTH];
- CC_MD5_CTX ctx = {0};
+ unsigned char md5[CC_MD5_DIGEST_LENGTH];
+ CC_MD5_CTX ctx = {0};
key1 = OSSwapHostToBigInt32(key1);
key2 = OSSwapHostToBigInt32(key2);
- CC_MD5_Init(&ctx);
- CC_MD5_Update(&ctx, &key1, sizeof(key1));
- CC_MD5_Update(&ctx, &key2, sizeof(key2));
- CC_MD5_Update(&ctx, key3, 8);
- CC_MD5_Final(md5, &ctx);
+ CC_MD5_Init(&ctx);
+ CC_MD5_Update(&ctx, &key1, sizeof(key1));
+ CC_MD5_Update(&ctx, &key2, sizeof(key2));
+ CC_MD5_Update(&ctx, key3, 8);
+ CC_MD5_Final(md5, &ctx);
- return [[NSData alloc] initWithBytes:md5 length:CC_MD5_DIGEST_LENGTH];
+ return [[NSData alloc] initWithBytes:md5 length:CC_MD5_DIGEST_LENGTH];
}
- (void)sendHeaders
{
- // Generate keys and challenge
+ // Generate keys and challenge
uint32_t key1, key2;
uint8_t key3[8];
NSString *s1 = [self generateKeyString:&key1];
NSString *s2 = [self generateKeyString:&key2];
for (int i = 0; i < 8; i++)
- key3[i] = arc4random() % 255;
+ key3[i] = arc4random() % 255;
- _challenge = [self computeSignatureFromKey1:key1 key2:key2 key3:key3];
+ _challenge = [self computeSignatureFromKey1:key1 key2:key2 key3:key3];
NSMutableString * header = [NSMutableString string];
- [header appendString:@"GET /socket.io/websocket HTTP/1.1\r\n"];
- [header appendString:@"Upgrade: WebSocket\r\n"];
- [header appendString:@"Connection: Upgrade\r\n"];
- [header appendFormat:@"Host: %@:%u\r\n", _hostName, _port];
- [header appendFormat:@"Origin: http://%@:%u\r\n", _hostName, _port];
-
- if ([_delegate respondsToSelector:@selector(additionalHeaders)]) {
- NSDictionary * headers = [_delegate additionalHeaders];
- for (NSString * key in headers){
- [header appendFormat:@"%@: %@\r\n", key, [headers objectForKey:key]];
- }
- }
-
- [header appendFormat:@"Sec-WebSocket-Key1: %@\r\n", s1];
- [header appendFormat:@"Sec-WebSocket-Key2: %@\r\n", s2];
- [header appendString:@"\r\n"];
-
- NSData * headerData = [header dataUsingEncoding:NSUTF8StringEncoding];
-
+ [header appendString:@"GET /socket.io/websocket HTTP/1.1\r\n"];
+ [header appendString:@"Upgrade: WebSocket\r\n"];
+ [header appendString:@"Connection: Upgrade\r\n"];
+ [header appendFormat:@"Host: %@:%u\r\n", _hostName, _port];
+ [header appendFormat:@"Origin: http://%@:%u\r\n", _hostName, _port];
+
+ if ([_delegate respondsToSelector:@selector(additionalHeaders)]) {
+ NSDictionary * headers = [_delegate additionalHeaders];
+ for (NSString * key in headers){
+ [header appendFormat:@"%@: %@\r\n", key, [headers objectForKey:key]];
+ }
+ }
+
+ [header appendFormat:@"Sec-WebSocket-Key1: %@\r\n", s1];
+ [header appendFormat:@"Sec-WebSocket-Key2: %@\r\n", s2];
+ [header appendString:@"\r\n"];
+
+ NSData * headerData = [header dataUsingEncoding:NSUTF8StringEncoding];
+
[self send:[headerData bytes] length:[headerData length]];
[self send:key3 length:sizeof(key3)];
}
View
6 Wompt/Wompt.h
@@ -15,10 +15,10 @@
@interface Wompt : NSObject <WebSocketConnectionDelegate> {
@private
SocketIOConnection * _womptConnection;
-
+
NSString * _channel;
- NSURL * _channelURL;
-
+ NSURL * _channelURL;
+
NSString * _connectorID;
NSString * _connectionID;
View
38 Wompt/Wompt.m
@@ -23,8 +23,8 @@ - (id)initWithChannel:(NSString *)channel
{
self = [super init];
if (self) {
- _channel = [channel retain];
- _channelURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://wompt.com/chat/%@", _channel]];
+ _channel = [channel retain];
+ _channelURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://wompt.com/chat/%@", _channel]];
_formatter = [[NSDateFormatter alloc] init];
[_formatter setDateFormat:@"hh:mm:ss"];
@@ -49,12 +49,12 @@ - (void)dealloc
- (void)connect
{
#if 0
- // Request this chat page to get a connector_id.
- NSURLRequest * request = [NSURLRequest requestWithURL:_channelURL];
- NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- NSString * html = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
+ // Request this chat page to get a connector_id.
+ NSURLRequest * request = [NSURLRequest requestWithURL:_channelURL];
+ NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ NSString * html = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
- // Find the connector_id in the resulting html
+ // Find the connector_id in the resulting html
regex_t re;
regmatch_t * matches = malloc(2 * sizeof(regmatch_t));
@@ -68,38 +68,38 @@ - (void)connect
// Use the reauthenticate method to get a connector_id.
NSURL * url = [NSURL URLWithString:@"http://wompt.com/re-authenticate"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
- NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- NSString * html = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
+ NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ NSString * html = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSDictionary * dict = [NSDictionary dictionaryWithJSONString:html];
_connectorID = [[dict objectForKey:@"connector_id"] retain];
#endif
- // Connect!
+ // Connect!
_womptConnection = [[SocketIOConnection alloc] initWithHost:@"wompt.com" port:80 delegate:self];
}
- (NSDictionary *)additionalHeaders
{
return nil;
/*
- // Get cookies for our channel url
- NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:_channelURL];
- return [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
+ // Get cookies for our channel url
+ NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:_channelURL];
+ return [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
*/
}
- (void)gotConnectionID:(NSString *)connectionID
{
- // We're connected, try to join the channel
- NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
+ // We're connected, try to join the channel
+ NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"join", @"action",
@"chat", @"namespace",
_channel, @"channel",
_connectorID, @"connector_id", nil];
[_womptConnection sendDict:dict];
- // Save our connectionID to indicate we're connected
+ // Save our connectionID to indicate we're connected
_connectionID = [connectionID retain];
// NSLog(@"connectionID: %@", _connectionID);
@@ -270,8 +270,8 @@ - (void)run
static int tabCB(EObjectType cdktype GCC_UNUSED, void *object, void *clientData, chtype key GCC_UNUSED)
{
- CDKSWINDOW *swindow = (CDKSWINDOW *)clientData;
- CDKENTRY *entry = (CDKENTRY *)object;
+ CDKSWINDOW *swindow = (CDKSWINDOW *)clientData;
+ CDKENTRY *entry = (CDKENTRY *)object;
activateCDKSwindow(swindow, 0);
drawCDKEntry (entry, ObjOf(entry)->box);
@@ -299,4 +299,4 @@ void help (CDKENTRY *entry)
mesg[12] = "<B=Tab/Escape> Returns to the command line.";
mesg[13] = "";
popupLabel(ScreenOf(entry), mesg, 14);
-}
+}

0 comments on commit 9a86239

Please sign in to comment.