//
|
// SocketDataControl.m
|
// HeBeiFM
|
//
|
// Created by Apple on 16/8/24.
|
// Copyright © 2016年 Apple. All rights reserved.
|
/**
|
socket数据就像一个水管,水管的水是一股一股流出来的
|
(假设数据是有一个一个的字典组成的,字典与字典之前用\n(换行符)区分)
|
|
例如:
|
|
其中一条数据为: {"key","value"}\n
|
|
收到的数据也可能为:
|
........
|
{"key","value"}\n
|
{"key","value"}\n
|
{"key","value"}\n
|
{"key","value"}\n
|
{"key",
|
|
会出现不完整的现象,因为socket发数据是一安次区分的,每次发送的数据大小可能是20KB超过的部分自然会被去掉,然后在下一次发送
|
下一次收到的数据一定是:
|
|
"value"}\n
|
{"key","value"}\n
|
{"key","value"}\n
|
{"key","value"}\n
|
........
|
|
只要在下一次接收数据时和上一次接收的数据拼接到一块就能获得完整的数据;
|
|
具思路是:
|
|
接收到一个数据后,先把这次的数据能提炼出来的字典提炼出来,显示给UI;然后把提炼出来的部分删掉,把尾巴留下来;下次再来一股数据时和上一次剩下的尾巴拼接在一块,继续处理,这样一直循环下去.....
|
|
*/
|
|
#import "DataControl.h"
|
#import "MJExtension.h"
|
//#import "DeviceModel.h"
|
|
@interface DataControl ()
|
{
|
//保存二级制文件数据
|
NSMutableArray *_currentData;
|
|
//DeviceModel * currentModel;
|
//保存有用的字符串数据
|
//NSMutableArray *_lastMessages;
|
}
|
@end
|
|
@implementation DataControl
|
|
|
+(DataControl*)shareControl
|
{
|
static DataControl *control = nil;
|
static dispatch_once_t onceToken;
|
dispatch_once(&onceToken, ^{
|
control = [[DataControl alloc] init];
|
});
|
return control;
|
}
|
|
-(instancetype)init
|
{
|
if (self = [super init]) {
|
//当前数据
|
_currentData = [[NSMutableArray alloc] init];
|
//currentModel = [[DeviceModel alloc] init];
|
//保存上一次的数据
|
//_lastMessages = [NSMutableArray new];
|
}
|
return self;
|
}
|
|
//-(void)appendingData:(id)data newData:(SocketDataBlock)block
|
//{
|
//1、拼接二进制数据
|
//[_currentData addObject: data];
|
|
//2、转化成字符串
|
// NSString *string = [[NSString alloc] initWithData:_currentData encoding:NSUTF8StringEncoding];
|
// NSLog(@"socket 收到的数据data = %@",string);
|
//NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
|
|
// NSUInteger len = [data length];
|
// Byte *byteData = (Byte*)malloc(len);
|
// memcpy(byteData, [data bytes], len);
|
//
|
// if(len > 64)
|
// NSLog(@"第%d个数字:%x",5,0x33);
|
//
|
// for( NSUInteger i = 0; i < len; i++ )
|
// {
|
// NSLog(@"第%ld个数字:%x",i,byteData[i]);
|
// }
|
|
// block([self modelArrFrom:data]);
|
|
// free(byteData);
|
// byteData = nil;
|
//}
|
|
/**
|
数模转换 数据解包
|
*/
|
//-(DeviceModel*)modelArrFrom:(NSData*)revData
|
//{
|
//[_currentData removeAllObjects];
|
|
//NSString *string = [[NSString alloc] initWithData:revData encoding:NSUnicodeStringEncoding];
|
|
|
//NSUInteger len = [revData length];
|
//Byte *byteData = (Byte*)malloc(len);
|
//memcpy(byteData, [revData bytes], len);
|
|
//currentModel.x6 = [NSString stringWithFormat:@"%d",byteData[22]];
|
// currentModel.x7 = [NSString stringWithFormat:@"%d",byteData[23]];
|
// currentModel.x8 = [NSString stringWithFormat:@"%d",byteData[34]];
|
// currentModel.x5 = [NSString stringWithFormat:@"%d",byteData[21]];
|
// currentModel.x1 = [NSString stringWithFormat:@"%d",byteData[14]*256+byteData[15]];
|
|
// currentModel.x9 = [NSString stringWithFormat:@"%d",byteData[25]];
|
/// currentModel.x10 = [NSString stringWithFormat:@"%d",byteData[26]];
|
// currentModel.x11 = [NSString stringWithFormat:@"%d",byteData[27]];
|
// currentModel.x12 = [NSString stringWithFormat:@"%d",byteData[28]];
|
//currentModel.x13 = [NSString stringWithFormat:@"%d",byteData[29]];
|
|
//free(byteData);
|
// byteData = nil;
|
|
// [_currentData addObject:currentModel];
|
// return currentModel;
|
//}
|
|
-(void)clearData
|
{
|
[_currentData removeAllObjects];
|
_currentData = nil;
|
_currentData = [[NSMutableArray alloc] init];
|
|
//currentModel = nil;
|
//currentModel = [[DeviceModel alloc] init];
|
}
|
|
@end
|