Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,21 @@
</Actions>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "63FFDE83-75A9-4970-AE1E-8A023B1DCD1F"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "iOS-Network-Stack-Dive/CoreNetworkStack/TransportLayer/V3_FinalProduct/Connect/TJPConnectionManager.m"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "180"
endingLineNumber = "180"
landmarkName = "-handleError:withReason:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ - (void)incrementCounter:(NSString *)key {
}

- (void)incrementCounter:(NSString *)key by:(NSUInteger)value {
if (!key) return;

[self performLocked:^{
// 特殊处理字节计数器
if ([key isEqualToString:TJPMetricsKeyBytesSend]) {
Expand Down Expand Up @@ -268,10 +270,10 @@ - (void)recordError:(NSError *)error forKey:(NSString *)key {
if (self.errors.count > 30) {
[self.errors removeObjectsInRange:NSMakeRange(0, self.errors.count - 30)];
}

// 增加错误计数
[self incrementCounter:TJPMetricsKeyErrorCount];
}];

// 增加错误计数
[self incrementCounter:TJPMetricsKeyErrorCount];
}

- (NSArray<NSDictionary *> *)recentErrors {
Expand Down Expand Up @@ -323,9 +325,14 @@ - (void)recordEvent:(NSString *)eventName withParameters:(NSDictionary *)params

#pragma mark - 线程安全操作
- (void)performLocked:(void (^)(void))block {
NSLog(@"准备获取锁 - 线程: %@", [NSThread currentThread]);
os_unfair_lock_lock(&_lock);
NSLog(@"已获取锁 - 线程: %@", [NSThread currentThread]);
block();
NSLog(@"准备释放锁 - 线程: %@", [NSThread currentThread]);
os_unfair_lock_unlock(&_lock);
NSLog(@"已释放锁 - 线程: %@", [NSThread currentThread]);

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,14 @@ - (void)setupNetwork {
NSString *host = @"127.0.0.1";
uint16_t port = 12345;

// TJPNetworkConfig *config = [TJPNetworkConfig configWithHost:host port:port maxRetry:5 heartbeat:15.0];
//
// // 2. 创建会话(中心协调器自动管理)
// self.session = [[TJPNetworkCoordinator shared] createSessionWithConfiguration:config];
//
// // 3. 连接服务器
// [self.session connectToHost:host port:port];

// 2. 创建TJPIMClient
self.client = [TJPIMClient shared];
[self.client connectToHost:host port:port];
//可以进行相关client设置

// 3. 建立不同类型的连接
[self.client connectToHost:host port:port forType:TJPSessionTypeChat];
// [client connectToHost:@"media.example.com" port:8081 forType:TJPSessionTypeMedia];

}

- (void)setupLogTextView {
Expand All @@ -99,14 +97,16 @@ - (void)setupSendMessageButton {

// 发送消息按钮点击事件
- (void)sendMessageButtonTapped {
// 4. 发送消息
// NSData *messageData = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
// [self.session sendData:messageData];
// NSLog(@"发送消息: %@", [[NSString alloc] initWithData:messageData encoding:NSUTF8StringEncoding]);

// 4. 发送不同类型消息
TJPTextMessage *textMsg = [[TJPTextMessage alloc] initWithText:@"Hello World!!!!!111112223333"];
[self.client sendMessage:textMsg];
NSLog(@"发送消息: %@", textMsg.text);
// 发送消息 - 手动指定会话
[self.client sendMessage:textMsg throughType:TJPSessionTypeChat];

// 发送消息 - 自动路由
// TJPMediaMessage *mediaMsg = [[TJPMediaMessage alloc] initWithMediaId:@"12345"];
// [self.client sendMessageWithAutoRoute:mediaMsg]; // 自动路由到媒体会话
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ - (void)setVersionInfo:(uint8_t)majorVersion minorVersion:(uint8_t)minorVersion
}

#pragma mark - Private Methods

- (void)handleError:(NSError *)error withReason:(TJPDisconnectReason)reason {
self.disconnectReason = reason;
[self setInternalState:TJPConnectionStateDisconnected];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
/// 独立的sessionId
@property (nonatomic, copy) NSString *sessionId;

@property (nonatomic, assign) TJPSessionType sessionType;

/// 配置
@property (nonatomic, strong) TJPNetworkConfig *config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,32 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, strong, readonly) NSMapTable<NSString *, id<TJPSessionProtocol>> *sessionMap;


/// 网络状态
@property (nonatomic, strong) Reachability *reachability;


/// session专用队列 串行:增删改查操作
@property (nonatomic, strong) dispatch_queue_t sessionQueue;
/// 解析专用队列 串行:数据解析专用
@property (nonatomic, strong) dispatch_queue_t parseQueue;
/// 监控专用队列 串行:网络监控相关
@property (nonatomic, strong) dispatch_queue_t monitorQueue;

/// 新增Session类型 为多路复用做支撑
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *sessionTypeMap;



/// 单例
+ (instancetype)shared;

/// 创建会话方法
- (id<TJPSessionProtocol>)createSessionWithConfiguration:(TJPNetworkConfig *)config;
/// 通过类型创建会话方法 多路复用必须使用此方法
- (id<TJPSessionProtocol>)createSessionWithConfiguration:(TJPNetworkConfig *)config type:(TJPSessionType)type;

/// 新增默认session配置方法
- (TJPNetworkConfig *)defaultConfigForSessionType:(TJPSessionType)type;

/// 统一更新所有会话状态
- (void)updateAllSessionsState:(TJPConnectState)state;
/// 统一管理重连
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,24 @@ - (instancetype)init {
if (self = [super init]) {
_networkChangeDebounceInterval = 2;
_sessionMap = [NSMapTable strongToStrongObjectsMapTable];
_sessionTypeMap = [NSMutableDictionary dictionary];

// 初始化队列
[self setupQueues];
// 初始化网络监控
[self setupNetworkMonitoring];
}
return self;
}

#pragma mark - Private Method
- (void)setupQueues {
//串行队列,只处理会话
// 串行队列,只处理会话
_sessionQueue = dispatch_queue_create("com.networkCoordinator.tjp.sessionQueue", DISPATCH_QUEUE_SERIAL);
//专用数据解析队列 并发高优先级
// 专用数据解析队列 并发高优先级
_parseQueue = dispatch_queue_create("com.networkCoordinator.tjp.parseQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_set_target_queue(_parseQueue, dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0));
//串行监控队列
// 串行监控队列
_monitorQueue = dispatch_queue_create("com.networkCoordinator.tjp.monitorQueue", DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(_monitorQueue, dispatch_get_global_queue(QOS_CLASS_UTILITY, 0));
}
Expand Down Expand Up @@ -313,17 +317,38 @@ - (void)notifySessionsOfNetworkStatus:(BOOL)available {

#pragma mark - Public Method
- (id<TJPSessionProtocol>)createSessionWithConfiguration:(TJPNetworkConfig *)config {
return [self createSessionWithConfiguration:config type:TJPSessionTypeDefault];
}

- (id<TJPSessionProtocol>)createSessionWithConfiguration:(TJPNetworkConfig *)config type:(TJPSessionType)type {
_currConfig = config;
TJPConcreteSession *session = [[TJPConcreteSession alloc] initWithConfiguration:config];
session.sessionType = type;
session.delegate = self;

dispatch_barrier_async(self->_sessionQueue, ^{
[self.sessionMap setObject:session forKey:session.sessionId];

// 记录会话类型映射
self.sessionTypeMap[@(type)] = session.sessionId;

TJPLOG_INFO(@"Session created: %@, total: %lu", session.sessionId, (unsigned long)self.sessionMap.count);
});
return session;
}

// 根据类型获取会话
- (id<TJPSessionProtocol>)sessionForType:(TJPSessionType)type {
__block id<TJPSessionProtocol> session = nil;
dispatch_sync(self->_sessionQueue, ^{
NSString *sessionId = self.sessionTypeMap[@(type)];
if (sessionId) {
session = [self.sessionMap objectForKey:sessionId];
}
});
return session;
}


- (void)updateAllSessionsState:(TJPConnectState)state {
dispatch_barrier_async(self->_sessionQueue, ^{
Expand All @@ -339,6 +364,8 @@ - (void)updateAllSessionsState:(TJPConnectState)state {
});
}



- (void)removeSession:(id<TJPSessionProtocol>)session {
dispatch_barrier_async(self->_sessionQueue, ^{
[self.sessionMap removeObjectForKey:session.sessionId];
Expand All @@ -363,6 +390,45 @@ - (void)scheduleReconnectForSession:(id<TJPSessionProtocol>)session {
});
}

- (TJPNetworkConfig *)defaultConfigForSessionType:(TJPSessionType)type {
TJPNetworkConfig *config = [TJPNetworkConfig new];

switch (type) {
case TJPSessionTypeChat:
// 聊天会话配置 - 重视低延迟
config.maxRetry = 5;
config.heartbeat = 15.0;
config.connectTimeout = 10.0;
break;

case TJPSessionTypeMedia:
// 媒体会话配置 - 重视吞吐量
config.maxRetry = 3;
config.heartbeat = 30.0;
config.connectTimeout = 20.0;
// 媒体会话可能需要更大的缓冲区
// config.readBufferSize = 65536;
break;

case TJPSessionTypeSignaling:
// 信令会话配置 - 极致低延迟
config.maxRetry = 8;
config.heartbeat = 5.0;
config.connectTimeout = 5.0;
break;

default:
// 默认配置
config.maxRetry = 5;
config.heartbeat = 15.0;
config.connectTimeout = 15.0;
break;
}

return config;
}




#pragma mark - TJPSessionDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ NS_ASSUME_NONNULL_BEGIN

/// 连接方法
- (void)connectToHost:(NSString *)host port:(uint16_t)port;
- (void)connectToHost:(NSString *)host port:(uint16_t)port forType:(TJPSessionType)type;

/// 发送消息 消息类型详见 TJPCoreTypes 头文件定义的 TJPContentType
- (void)sendMessage:(id<TJPMessageProtocol>)message;
- (void)sendMessage:(id<TJPMessageProtocol>)message throughType:(TJPSessionType)type;


/// 断开连接
- (void)disconnect;
Expand Down
Loading