当前位置:网站首页>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 !
边栏推荐
- Knowledge payment applet dream vending machine V2
- Zephyr 学习笔记2,Scheduling
- Electronic Association C language level 1 35, bank interest
- System architecture design of circle of friends
- 提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
- When JDBC connects to es query, is there a God who meets the following situation?
- Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx
- Used on windows Bat file startup project
- The idea of implementing charts chart view in all swiftui versions (1.0-4.0) was born
- Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
猜你喜欢
User login function: simple but difficult
MySQL中的文本处理函数整理,收藏速查
notepad++如何统计单词数量
Solution of running crash caused by node error
两年前美国芯片扭捏着不卖芯片,如今芯片堆积如山祈求中国帮忙
Chain ide -- the infrastructure of the metauniverse
SQL foundation 9 [grouping data]
Xcode 14之大变化详细介绍
【Kubernetes系列】Kubernetes 上安装 KubeSphere
NLP-文献阅读总结
随机推荐
[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)
Zephyr 学习笔记1,threads
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?
【森城市】GIS数据漫谈(一)
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
Flink memory model, network buffer, memory tuning, troubleshooting
kubernetes集群之Label管理
Experience installing VMware esxi 6.7 under VMware Workstation 16
Solution of running crash caused by node error
"Sword finger offer" 2nd Edition - force button brush question
MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
Rhcsa the next day
Finishing (III) - Exercise 2
How to share the source code anti disclosure scheme
Uniapp applet subcontracting
2022-021ARTS:下半年開始
SQL foundation 9 [grouping data]
Directory of tornado
Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx