IOS socket GCDAsyncSocket

來源:互聯網
上載者:User

 開源地址:https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Intro_GCDAsyncSocket

怎麼樣使用:看網站下面的具體使用說明。

封裝後的代碼;

//
//  ClientSocket.h
//  GameClientEngine
//
//  Created by chao zhang on 12-2-22.
//  Copyright (c) 2012騫?hangzhou. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "GCDAsyncSocket.h"
#import "AmfMessage.h"

static const int HEADTAG = 0;
static const int BODYTAG = 1;

@class AmfHandler;
@interface ClientSocket : GCDAsyncSocket <UIApplicationDelegate>

@property(nonatomic,assign)int header;

@property(nonatomic, retain)AmfHandler* amfHandler;

-(BOOL)bind:(NSString*) host onPort:(int)port;

-(void) writer:(AmfMessage*)msg;

-(void)shutdown;

@end

 

//
//  ClientSocket.m
//  GameClientEngine
//
//  Created by chao zhang on 12-2-22.
//  Copyright (c) 2012騫?hangzhou. All rights reserved.
//

#import "ClientSocket.h"
#import "AmfMessageEncoder.h"
#import "AmfMessageDecoder.h"
#import "AmfHandler.h"

@implementation ClientSocket

@synthesize header;

@synthesize amfHandler;

-(id)init{
    self = [super initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    header = 4;
    amfHandler = [[AmfHandler alloc]init];
    
    return self;
}

-(void)writer:(AmfMessage *)msg{
    AmfMessageEncoder* encoder = [[AmfMessageEncoder alloc]init];
    NSData * encodeData = [encoder encode:msg];
    [self writeData:encodeData withTimeout:-1 tag:2];
}

-(void) socket:(ClientSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    if(tag == 0){
        int8_t b[header];
        [data getBytes:b length:header];
        
        NSMutableData* tempData = [[NSMutableData alloc] init];
        for(int bb=header-1 ;bb > -1;bb--){
            int8_t temp = b[bb];
            [tempData appendBytes:&temp length:sizeof(temp)];
        }
        
        int headlength;
        [tempData getBytes:&headlength range:NSMakeRange(0, sizeof(headlength))];
        [sock readDataToLength:headlength withTimeout:-1 tag:BODYTAG];
        
    }else if(tag == 1){
        AmfMessageDecoder* decoder = [[AmfMessageDecoder alloc]init];
        AmfMessage* msg = [decoder decode:data];
        [amfHandler messageReceived:msg socket:self];
        [sock readDataToLength:header withTimeout:-1 tag:HEADTAG];
        
    }

}

-(BOOL)bind:(NSString *)host onPort:(int)port{
    NSError *error = nil;
    if(![self connectToHost:host onPort:port error:&error]){
        NSLog(@"Error connecting: %@", error);
        return NO;
    }else{
        [self readDataToLength: header withTimeout:-1 tag:HEADTAG];
        return YES;
    }
}

-(void)shutdown{
    [self disconnect];
}

 

@end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.