From b1957b8a60e0f95cf22026e8deadb5e47a255ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E4=BD=95?= Date: Tue, 14 Feb 2017 10:21:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?debuger=20=E6=94=B9=E4=B8=BA=E6=B5=81?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes IOS/.DS_Store | Bin 0 -> 6148 bytes .../Classes/lvdebug/LVDebugConnection.m | 239 ++++++++---------- 3 files changed, 109 insertions(+), 130 deletions(-) create mode 100644 .DS_Store create mode 100644 IOS/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6bc17289aca8042c6957bce25177ef538d44320d GIT binary patch literal 6148 zcmeHK%Wl&^6ur{~YLiF}2&qM4VTlDSDil;ogxI8MvZyE&)ls|P5$xDXj9fc%oCXv{ z3JbQZ5wPX;9qwXc*a9StTynj5tWF@K;oD`gX{$3 z{X8w$l4>acnTQcnK;BLmx7~2TTQ_J0v;u#d0{q)urY(xdql7ZwZ|tInFAKMjuKW;& z5>bxY6jC6+@?Tz`7QE_+)}HlZs|#K`v`6-w*NcPy#V9EWfaw5H=81pcs9I=>x9sS`wgB4W!J4-SLKi-)y%5P5y!uYw!KrD|t_5fGV% z*($A#Mw^w*b?bWN=6Ky2RVrnaH*So_rg7o&#{Q!x&tJZN^Y-2Q4!u?2B9lYd{ViVA1wzNHux} zo{!fXV>0-N7t;~-&>Nzb%tjN_;#Fo^tjtBLgX1E82iGE-PnRtF+#K7cI^D)Ndg597 zKs?v%6Nt%_jbYM0#=ZsS9ihR|bSs)pQpP7%mVxU3Y-*wzd=1d170?Q31+oh8^}#~o zSXDTYD3%Un;s^lDqgfcr{HK9)EQM8t6N%`7F-ZkVDpN-cCh2ImlwVchM53ey+2nZQ;rWN?33j72aKiaea literal 0 HcmV?d00001 diff --git a/IOS/.DS_Store b/IOS/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d8017a3a64240c42c55bef22fa5b8b3939047cf0 GIT binary patch literal 6148 zcmeHK-D=c86h6~#HCtV}7lrmRH-ZHj%;bD0nUDF&5E1L=!46TIh%6L=ja5`bW)4_T#H<#Cy4au z0C_{2k^|=B8s}Gyvwjwbwo6<1m?rqjx79py0(>b=fa|q9LwrGs5vDbsm=I-Ni-z=o z9^qSEdP>jG+JnE1(FRqf)TPWsB4Za1t+Qn0u3`PN?z$H^a><>1Iz@ElJq1-x9 z$twV`fo^H=#ZQ9Z*cy8p3x#+BV~Pq?RHm;OOwrMA>$slALZPA))0YpX-^}z4g~_)g zzb(Uw^%R=gC}0$*DzL1YZE^nZoqzwYl1$4eU=+Ai3b4k&8T2tEeYP$PPMozi${~s{ p5mzXb6jb^+mX0`zH&LYFnJpY(Ph+7FJuvelAZ0L>QQ)5{@CO@1!R7z} literal 0 HcmV?d00001 diff --git a/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m b/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m index 705b4602..76167dae 100644 --- a/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m +++ b/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m @@ -2,8 +2,8 @@ // LVDebuger.m // CFSocketDemo // -// Created by dongxicheng on 9/16/15. -// Copyright (c) 2015 dongxicheng. All rights reserved. +// Created by 罗何 on 9/16/15. +// Copyright © 2015年 luohe. All rights reserved. // #import "LVDebugConnection.h" @@ -27,7 +27,7 @@ static NSString* SERVER_IP = @"127.0.0.1"; static int SERVER_PORT = 9876; -@interface LVDebugConnection () +@interface LVDebugConnection () @property(nonatomic,strong) NSThread* myThread; @property(nonatomic,assign) BOOL canWrite; @property(nonatomic,assign) NSInteger state; @@ -35,7 +35,11 @@ @interface LVDebugConnection () @end @implementation LVDebugConnection{ - CFSocketRef _socket; + NSOutputStream *_outputStream; + NSInputStream *_inputStream; + + BOOL _outputStreamCompleted; + BOOL _inputStreamCompleted; } -(id) init{ @@ -43,7 +47,7 @@ -(id) init{ if( self ) { static int index = 0; self.myThread = [[NSThread alloc] initWithTarget:self selector:@selector(run:) object:nil]; - self.myThread.name = [NSString stringWithFormat:@"LuaView.Debuger.%d",index]; + self.myThread.name = [NSString stringWithFormat:@"LV.Debuger.%d",index]; self.sendArray = [[NSMutableArray alloc] init]; self.receivedArray = [[NSMutableArray alloc] init]; [self startThread]; @@ -111,164 +115,139 @@ - (void) sendCmd:(NSString*) cmdName fileName:(NSString*)fileName info:(NSString [self sendString:buffer]; } --(void)Connect:(NSString*) ip port:(NSUInteger)port -{ +-(void)Connect:(NSString*) ip port:(NSUInteger)port{ #ifdef DEBUG - //////////////////////创建套接字////////////// - CFSocketContext socketConent = {0,NULL,NULL,NULL,NULL}; - socketConent.info = (__bridge void *)(self); - _socket = CFSocketCreate( - kCFAllocatorDefault, - PF_INET, - SOCK_STREAM, - IPPROTO_TCP, - kCFSocketConnectCallBack|kCFSocketReadCallBack|kCFSocketWriteCallBack, // 类型,表示连接时调用 - ServerConnectCallBack, // 调用的函数 - &socketConent ); + CFReadStreamRef readStream = NULL; + CFWriteStreamRef writeStream = NULL; + CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)ip, (UInt32)port, &readStream, &writeStream); + // 记录已经分配的输入流和输出流 + _inputStream = (__bridge NSInputStream *)readStream; + _outputStream = (__bridge NSOutputStream *)writeStream; + + //设置属性SSL + // [_inputStream setProperty:NSStreamSocketSecurityLevelSSLv3 forKey:NSStreamSocketSecurityLevelKey]; + // [_outputStream setProperty:NSStreamSocketSecurityLevelSSLv3 forKey:NSStreamSocketSecurityLevelKey]; + // CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, (__bridge CFTypeRef)([NSMutableDictionary dictionaryWithObjectsAndKeys:(id)kCFBooleanFalse,kCFStreamSSLValidatesCertificateChain,kCFBooleanFalse,kCFStreamSSLIsServer,nil])); - ////////////////////////////设置地址/////////////////// - struct sockaddr_in addr = {0}; - memset(&addr , 0,sizeof(addr)); - addr.sin_len = sizeof(addr); - addr.sin_family = AF_INET; - addr.sin_port = htons(port);// 端口 - addr.sin_addr.s_addr = inet_addr([ip UTF8String]); + // 设置代理,监听输入流和输出流中的变化 + _inputStream.delegate = self; + _outputStream.delegate = self; - CFDataRef address = CFDataCreate( - kCFAllocatorDefault, - (UInt8*)&addr, - sizeof(addr)); + // Scoket是建立的长连接,需要将输入输出流添加到主运行循环 + [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; + [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; - /////////////////////////////执行连接///////////////////// - CFSocketConnectToAddress(_socket,address,-1); - CFRunLoopRef cfrl = CFRunLoopGetCurrent(); // 获取当前运行循环 - CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault,_socket,0);//定义循环对象 - CFRunLoopAddSource(cfrl,source,kCFRunLoopCommonModes); //将循环对象加入当前循环中 - LVReleaseAndNull(source); - LVReleaseAndNull(address); + // 打开输入流和输出流,准备开始文件读写操作 + [_inputStream open]; + [_outputStream open]; #endif } +- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{ + switch (eventCode) { + case NSStreamEventOpenCompleted: + if(aStream == _inputStream) _inputStreamCompleted = YES; + if(aStream == _outputStream) _outputStreamCompleted = YES; + if(_inputStreamCompleted && _outputStreamCompleted) self.state = SOCKET_SUCCESS; + break; + case NSStreamEventHasBytesAvailable:{ + + uint8_t buf[16 * 1024]; + uint8_t *buffer = NULL; + NSUInteger len = 0; + if (![_inputStream getBuffer:&buffer length:&len]) { + NSInteger amount = [_inputStream read:buf maxLength:sizeof(buf)]; + buffer = buf; + len = amount; + } + if (0 < len) { + //head +// NSUInteger d0 = buffer[0]; +// NSUInteger d1 = buffer[1]; +// NSUInteger d2 = buffer[2]; +// NSUInteger d3 = buffer[3]; +// len = (d0<<24) + (d1<<16) + (d2<<8) + d3; +// buffer = buffer + 4; + + NSString *cmd = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding]; + if ( cmd && cmd.length>0) { + LVLog(@"received CMD: %@", cmd); + [self.receivedArray insertObject:cmd atIndex:0]; + } + // 关闭掉socket + if ( cmd.length<=0 ){ + [self closeAll]; + [self.receivedArray addObject:@"close"]; + [self.receivedArray addObject:@"close"]; + } else { + [self.lview callLuaToExecuteServerCmd]; + } + } + } + break; + case NSStreamEventHasSpaceAvailable: + self.canWrite = YES; + [self sendOneData]; + break; + case NSStreamEventErrorOccurred: + case NSStreamEventEndEncountered: + [self closeAll]; + [self.receivedArray addObject:@"close"]; + [self.receivedArray addObject:@"close"]; + [self.lview callLuaToExecuteServerCmd]; + default: + break; + } +} + -(void) closeAll{ + _outputStreamCompleted = NO; + _inputStreamCompleted = NO; + self.canWrite = FALSE; self.state = -1; - if (_socket != NULL) - { - CFSocketInvalidate(_socket); - LVReleaseAndNull(_socket); - } + [_inputStream close]; + _inputStream = nil; + + [_outputStream close]; + _outputStream = nil; if( !self.myThread.isCancelled ) { [self.myThread cancel]; } } -#ifdef DEBUG -static void ServerConnectCallBack( CFSocketRef socket, - CFSocketCallBackType type, - CFDataRef address, - const void *data, - void* info) -{ - LVDebugConnection* debuger = (__bridge LVDebugConnection *)(info); - switch ( type ){ - case kCFSocketReadCallBack: { - NSString* cmd = readString(socket); - if ( cmd && cmd.length>0) { - LVLog(@"received CMD: %@", cmd); - [debuger.receivedArray insertObject:cmd atIndex:0]; - } - // 关闭掉socket - if ( cmd.length<=0 ){ - [debuger closeAll]; - [debuger.receivedArray insertObject:@"close" atIndex:0]; - [debuger.receivedArray insertObject:@"close" atIndex:0]; - } else { - [debuger.lview callLuaToExecuteServerCmd]; - } - break; - } - case kCFSocketWriteCallBack: { - debuger.canWrite = YES; - [debuger sendOneData]; - break; - } - case kCFSocketConnectCallBack: - if( data ) { - // LVLog(@"Debuger Socket Connect failed" ); - debuger.state = SOCKET_ERROR; - } else { - LVLog(@"Debuger Socket connect Success"); - debuger.state = SOCKET_SUCCESS; - } - break; - default: { - LVLog(@"connect type %d", (int)type ); - break; - } - } -} -#endif - -(void) sendOneData{ NSData* data = self.sendArray.lastObject; if( self.canWrite && data) { [self.sendArray removeLastObject]; - if( data ) { - NSInteger sendLength = send(CFSocketGetNative(_socket), data.bytes, data.length, 0); - if( sendLength!=data.length ) { + if( data.length > 0 ) { + NSInteger sendLength = [_outputStream write:data.bytes maxLength:data.length]; + if( sendLength != data.length ) { LVError(@"Debuger socket Send length Error : %d != %d", (int)sendLength, (int)data.length); } } } } -///////////////////监听来自服务器的信息/////////////////// - -#ifdef DEBUG -static NSString* readString(CFSocketRef socket) -{ - unsigned char head[4] = {0}; - NSUInteger readLength0 = 0; - if( recv( CFSocketGetNative(socket), head, sizeof(head), 0 )==sizeof(head) ) { - NSUInteger d0 = head[0]; - NSUInteger d1 = head[1]; - NSUInteger d2 = head[2]; - NSUInteger d3 = head[3]; - readLength0 = (d0<<24) + (d1<<16) + (d2<<8) + d3; - } - - unsigned char buffer[512] = {0}; - NSUInteger readLen = readLength0; - NSMutableData* data = [[NSMutableData alloc] init]; - for(;readLen>0;){ - NSUInteger bufferLen = readLen>=sizeof(buffer)?sizeof(buffer):readLen; - NSUInteger recvLen = recv( CFSocketGetNative(socket), buffer, bufferLen, 0 ); - if ( recvLen>0 ) { - [data appendBytes:buffer length:recvLen]; - readLen -= recvLen; - } else { - break; - } - } - NSString* ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - return ret; -} -#endif /////////////////////////发送信息给服务器//////////////////////// - (void) sendString:(NSString *)string { if( self.canWrite ) { NSData* data = [string dataUsingEncoding:NSUTF8StringEncoding]; - NSUInteger len = data.length; + NSMutableData* buffer = [[NSMutableData alloc] init]; - unsigned char head[4] = {0}; - head[0] = (len>>24); - head[1] = (len>>16); - head[2] = (len>>8); - head[3] = (len); - [buffer appendBytes:head length:4]; + + //head +// NSUInteger len = data.length; +// unsigned char head[4] = {0}; +// head[0] = (len>>24); +// head[1] = (len>>16); +// head[2] = (len>>8); +// head[3] = (len); +// [buffer appendBytes:head length:4]; + [buffer appendData:data]; [self.sendArray insertObject:buffer atIndex:0]; From f7f842c1a959db38cc2a66883ccd6114f6b740d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E4=BD=95?= Date: Tue, 14 Feb 2017 13:43:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ios=E7=89=88=E6=9C=ACLVDebugConnection=20?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E7=BA=BF=E7=A8=8B=E4=B8=8D=E5=90=8C=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=93=8D=E4=BD=9CsendArray=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m | 1 - 1 file changed, 1 deletion(-) diff --git a/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m b/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m index 76167dae..af747e9f 100644 --- a/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m +++ b/IOS/LuaViewSDK/Classes/lvdebug/LVDebugConnection.m @@ -187,7 +187,6 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{ break; case NSStreamEventHasSpaceAvailable: self.canWrite = YES; - [self sendOneData]; break; case NSStreamEventErrorOccurred: case NSStreamEventEndEncountered: