当前位置:网站首页>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 !
边栏推荐
- NLP-文献阅读总结
- Set JTAG fuc invalid to normal IO port
- Vulhub vulnerability recurrence 76_ XXL-JOB
- Redis - detailed explanation of cache avalanche, cache penetration and cache breakdown
- It's healthy to drink medicinal wine like this. Are you drinking it right
- Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
- [Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)
- 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)
- Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
- Blog stop statement
猜你喜欢
In the era of low code development, is it still needed?
Zephyr 學習筆記2,Scheduling
[Mori city] random talk on GIS data (I)
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
window上用.bat文件启动项目
Computer connects raspberry pie remotely through putty
NLP-文献阅读总结
Uniapp applet subcontracting
Detailed introduction to the big changes of Xcode 14
Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)
随机推荐
大厂技术专家:架构设计中常用的思维模型
Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘
Unity 从Inspector界面打开资源管理器选择并记录文件路径
Label management of kubernetes cluster
[freertos] freertos Learning notes (7) - written freertos bidirectionnel Link LIST / source analysis
云Redis 有什么用? 云redis怎么用?
Jianmu continuous integration platform v2.2.2 release
[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)
【FreeRTOS】FreeRTOS学习笔记(7)— 手写FreeRTOS双向链表/源码分析
博客停更声明
How notepad++ counts words
[Flink] temporal semantics and watermark
Blog stop statement
[web security] nodejs prototype chain pollution analysis
[untitled] notice on holding "2022 traditional fermented food and modern brewing technology"
Node foundation ~ node operation
socket inet_ pton() inet_ Ntop() function (a new network address translation function, which converts the expression format and numerical format to each other. The old ones are inet_aton(), INET_ ntoa
CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
The IP bound to the socket is inaddr_ The meaning of any htonl (inaddr_any) (0.0.0.0 all addresses, uncertain addresses, arbitrary addresses)