当前位置:网站首页>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 !
边栏推荐
- Since DMS is upgraded to a new version, my previous SQL is in the old version of DMS. In this case, how can I retrieve my previous SQL?
- Unity 从Inspector界面打开资源管理器选择并记录文件路径
- [freertos] freertos Learning notes (7) - written freertos bidirectionnel Link LIST / source analysis
- Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown
- Four sets of APIs for queues
- Research on an endogenous data security interaction protocol oriented to dual platform and dual chain architecture
- 电子协会 C语言 1级 35 、银行利息
- Industrial computer anti-virus
- MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
- Selenium driver ie common problem solving message: currently focused window has been closed
猜你喜欢

Zephyr learning notes 1, threads

Research on an endogenous data security interaction protocol oriented to dual platform and dual chain architecture

Chain ide -- the infrastructure of the metauniverse

云Redis 有什么用? 云redis怎么用?

Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and

Crawler (III) crawling house prices in Tianjin

win10微软拼音输入法输入文字时候下方不出现中文提示

Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction

The idea of implementing charts chart view in all swiftui versions (1.0-4.0) was born

Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
随机推荐
The crackdown on Huawei prompted made in China to join forces to fight back, and another enterprise announced to invest 100 billion in R & D
Industrial computer anti-virus
Introduction to rce in attack and defense world
jdbc连接es查询的时候,有遇到下面这种情况的大神嘛?
University stage summary
The most effective futures trend strategy: futures reverse merchandising
The number of patent applications in China has again surpassed that of the United States and Japan, ranking first in the world for 11 consecutive years
Would you like to go? Go! Don't hesitate if you like it
[Mori city] random talk on GIS data (I)
The idea of implementing charts chart view in all swiftui versions (1.0-4.0) was born
win10微软拼音输入法输入文字时候下方不出现中文提示
The important role of host reinforcement concept in medical industry
A new understanding of how to encrypt industrial computers: host reinforcement application
输入年份、月份,确定天数
Life planning (flag)
Zabbix agent主动模式的实现
电脑通过Putty远程连接树莓派
Su Weijie, a member of Qingyuan Association and an assistant professor at the University of Pennsylvania, won the first Siam Youth Award for data science, focusing on privacy data protection, etc
Crawler (III) crawling house prices in Tianjin
Zephyr study notes 2, scheduling