当前位置:网站首页>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】


TCP
TCP

- 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

TCP
TCP
Short 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
TCP
TCP
message{} & 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 !
边栏推荐
- The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
- 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
- The most effective futures trend strategy: futures reverse merchandising
- 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
- Pangu open source: multi support and promotion, the wave of chip industry
- Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?
- Boast about Devops
- 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
- Novel website program source code that can be automatically collected
- win10微软拼音输入法输入文字时候下方不出现中文提示
猜你喜欢
Set JTAG fuc invalid to normal IO port
Detailed introduction to the big changes of Xcode 14
Rhcsa the next day
用于压缩视频感知增强的多目标网络自适应时空融合
[C language] open the door of C
Chain ide -- the infrastructure of the metauniverse
Uniapp applet subcontracting
Zephyr 学习笔记1,threads
Implementation of ZABBIX agent active mode
Status of the thread
随机推荐
Splicing plain text into JSON strings - easy language method
Flink memory model, network buffer, memory tuning, troubleshooting
Rhcsa the next day
CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
2022-021ARTS:下半年開始
Summary of MySQL common judgment functions!! Have you used it
SQL foundation 9 [grouping data]
flask-sqlalchemy 循环引用
leetcode825. Age appropriate friends
How can the old version of commonly used SQL be migrated to the new version?
Boast about Devops
Adaptive spatiotemporal fusion of multi-target networks for compressed video perception enhancement
Docker install MySQL
Used on windows Bat file startup project
How to share the source code anti disclosure scheme
提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown
Cell reports: Wei Fuwen group of the Institute of zoology, Chinese Academy of Sciences analyzes the function of seasonal changes in the intestinal flora of giant pandas
Four sets of APIs for queues
果果带你写链表,小学生看了都说好