当前位置:网站首页>I was pressed for the draft, so let's talk about how long links can be as efficient as short links in the development of mobile terminals
I was pressed for the draft, so let's talk about how long links can be as efficient as short links in the development of mobile terminals
2022-07-04 07:27:00 【InfoQ】


TCPTCP
- Short link : One request Corresponding to one respones, It must appear in pairs , Yes, there is. , Their relationship is :R:Q = N:N = 1
- Long link : Sending and receiving just write data to or read data from the cache , There is no clear request & respones The concept of , Their relationship is :R:Q = M:N

TCPTCPShort link example
NSString *url = @"http://api.xxx.com/method";
[[self shareAFNManager] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject-->%@",responseObject);
UpdateUI()
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error-->%@",error);
}];
Long link
programme
TCPTCPmessage{} & message_reply{}message ping{
string request_id = 1;
}
message ping_reply{
string request_id = 1;
}
request_id

typedef void (^SocketDidReadBlock)(NSError *__nullable error, id __nullable data);
@interface SendCore : NSObject
+ (nullable SendCore *)sharedManager;
- (void) sendEnterChatRoom:(nullable EnterChatRoom *) data completion:(__nullable SocketDidReadBlock)callback;
- (void) CloseSocket;
@end
- (void) sendEnterChatRoom:(nullable EnterChatRoom *) data completion:(__nullable SocketDidReadBlock)callback
{
if (data == nil ) {
return;
}
NSString * blockRequestID =[self createRequestID];
data.requestId = blockRequestID;
if (callback) {
[self addRequestIDToMap:blockRequestID block:callback];
}
[self sendProtocolWithCmd:CmdType_Enter1V1MovieRoom withCmdData:[data data] completion:callback];
}
-(void) handerProtocol:(CmdType) protocolID packet:(NSData *) packet
{
NSError *error = nil;
id reslutData = nil;
NSString *requestId = nil;
switch (protocolID) {
case CmdType_Enter1V1MovieRoomReply:
error = nil;
syncObjInfo =[SyncObjInfo parseFromData:packet error:&error];
reslutData = syncObjInfo;
requestId = syncObjInfo.requestId;
didReadBlock = [self getBlockWith:requestId];
break;
}
if (didReadBlock) {
didReadBlock(error, reslutData);
}
if (requestId != nil &&
requestId.length > 0) {
[self removeRequestIDFormMap:requestId];
}
}
[[SendCore sharedManager] sendEnterChatRoom:room completion:^(NSError * _Nullable error, id _Nullable data) {
NSLog(@"%@", error);
if (error == nil) {
NSLog(@" Successfully entered the chat room ");
}
}];
Overtime
TCP- (void) addRequestIDToMap:(NSString *) requestID block:(nullable SocketDidReadBlock)callback withTime:(BOOL) timeout{
if (requestID == nil ||
requestID.length == 0) {
return;
}
if (callback == nil) {
return;
}
[self.requestsMapLock lock];
[self.requestsMap setObject:callback forKey:requestID];
if (timeout) {
[self.requestsTimeMap setObject:[NSDate date] forKey:requestID];
}
[self.requestsMapLock unlock];
}
-(void) checkRequestProcessTimeout
{
NSError * Socket_WAIT_PROCESS_TIMEOUT_SECOND = [NSError errorWithDomain:@"_Socket_WAIT_PROCESS_TIMEOUT_SECOND_" code:408 userInfo:nil];
NSMutableArray * timeoutRequestIDs = [NSMutableArray array];
NSDate * now = [NSDate date];
for (NSString * requestID in [self.requestsTimeMap allKeys]) {
if (requestID == nil ||
[requestID length] <= 0) {
continue;
}
NSDate * fireDate = [self.requestsTimeMap objectForKey:requestID];
NSDate * timeOutTime = [NSDate dateWithTimeInterval:_Socket_WAIT_PROCESS_TIMEOUT_SECOND_ sinceDate:fireDate];
if ([timeOutTime compare:now] == NSOrderedAscending) {
[timeoutRequestIDs addObject:requestID];
}
}
for (NSString * requestID in timeoutRequestIDs) {
if (requestID == nil ||
[requestID length] <= 0) {
continue;
}
SocketDidReadBlock didReadBlock = [self getBlockWith:requestID];
didReadBlock(Socket_WAIT_PROCESS_TIMEOUT_SECOND, nil);
[self removeRequestIDFormMap:requestID];
}
}
cache
request_id Small wonderful use of
- (NSString *)createRequestID {
NSInteger timeInterval = [NSDate date].timeIntervalSince1970 * 1000000;
NSString *randomRequestID = [NSString stringWithFormat:@"%ld%d", timeInterval, arc4random() % 100000];
return randomRequestID;
}
const USER_INFO_GET_REQUEST_ID = @“04e1c446-b918-40f7-9061-d06b569a9cf0”
@“/ Big function / Small feature /xxx”
end
More interesting, please pay attention to our official account. 「 Hundred bottle technology 」, There are occasional benefits !
边栏推荐
- Master-slave replication principle of MySQL database
- tornado项目之路由装饰器
- Zephyr 學習筆記2,Scheduling
- Zephyr 学习笔记1,threads
- Implementation of ZABBIX agent active mode
- A new understanding of how to encrypt industrial computers: host reinforcement application
- 输入年份、月份,确定天数
- 节点基础~节点操作
- Technical experts from large factories: common thinking models in architecture design
- Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
猜你喜欢

window上用.bat文件启动项目

University stage summary

Selenium driver ie common problem solving message: currently focused window has been closed

BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment

Pangu open source: multi support and promotion, the wave of chip industry

Chain ide -- the infrastructure of the metauniverse

Transition technology from IPv4 to IPv6

用于压缩视频感知增强的多目标网络自适应时空融合

Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction

Go learning notes - constants
随机推荐
【森城市】GIS数据漫谈(一)
NLP literature reading summary
Docker install MySQL
Knowledge payment applet dream vending machine V2
Data double write consistency between redis and MySQL
How to share the source code anti disclosure scheme
The final week, I split
Selenium ide plug-in download, installation and use tutorial
Centos8 install mysql 7 unable to start up
Distributed transaction management DTM: the little helper behind "buy buy buy"
Blog stop statement
Zephyr 学习笔记1,threads
Unity opens the explorer from the inspector interface, selects and records the file path
Uniapp applet subcontracting
rapidjson读写json文件
Campus network problems
Zephyr 学习笔记2,Scheduling
电子协会 C语言 1级 34 、分段函数
System architecture design of circle of friends
Industrial computer anti-virus