宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
//  RHSocketService.m
//  RHSocketDemo
//
//  Created by zhuruhong on 15/6/18.
//  Copyright (c) 2015年 zhuruhong. All rights reserved.
//
 
#import "RHChannelService.h"
#import "RHSocketMacros.h"
#import "RHChannelBeats.h"
#import "RHChannelReconnect.h"
 
@interface RHChannelService ()
 
@property (nonatomic, strong) RHSocketChannel *channel;
@property (nonatomic, strong) RHChannelConfig *config;
@property (nonatomic, strong) RHChannelReconnect *channelReconnect;
 
@end
 
@implementation RHChannelService
 
#ifdef DEBUG
- (void)dealloc
{
    RHSocketLog(@"[Log]: %@ dealloc", self.class);
}
#endif
 
#pragma mark - getter & setter
 
- (RHSocketChannel *)channel
{
    if (nil == _channel) {
        _channel = [[RHSocketChannel alloc] init];
    }
    return _channel;
}
 
- (RHChannelReconnect *)channelReconnect
{
    if (nil == _channelReconnect) {
        _channelReconnect = [[RHChannelReconnect alloc] init];
    }
    return _channelReconnect;
}
 
#pragma mark -
 
- (void)startWithConfig:(void (^)(RHChannelConfig *config))configBlock
{
    //配置channel必要参数
    RHChannelConfig *config = [[RHChannelConfig alloc] init];
    configBlock(config);
    self.config = config;
    [self.config setup:self.channel];
    
    __weak typeof(self) weakSelf = self;
    //重连
    [self.channelReconnect stop];
    self.channelReconnect.connectBlock = ^(RHChannelReconnect *reconnect) {
        //触发重连
        RHSocketLog(@"[Log]: channel reconnect");
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.channel openConnection];
    };
    self.channelReconnect.overBlock = ^(RHChannelReconnect *reconnect) {
        RHSocketLog(@"[Log]: channel reconnect over");
    };
    
    [self.channel addDelegate:self];
    [self.channel openConnection];
}
 
- (void)stopService
{
    [self.config.channelBeats stop];
    [self.channelReconnect stop];
    [self.channel closeConnection];
    [self.channel removeDelegate:self];
    _isRunning = NO;
}
 
- (void)asyncSendPacket:(id<RHUpstreamPacket>)packet
{
    RHSocketLog(@"[Log]: upstream packet %@", [packet stringWithPacket]);
    [self.channel asyncSendPacket:packet];
}
 
#pragma mark - RHSocketChannelDelegate
 
- (void)channelOpened:(RHSocketChannel *)channel host:(NSString *)host port:(int)port
{
    _isRunning = YES;
    
    /** 停止重连 */
    [self.channelReconnect stop];
    /**
     * 开启心跳逻辑
     * 测试场景:连接后直接发起心跳
     * 正常场景:连接后,有用户校验等逻辑,需要在可以通信后开启心跳逻辑
     */
    if (self.config.connectParam.heartbeatEnabled) {
        [self.config.channelBeats start];
    }
}
 
- (void)channelClosed:(RHSocketChannel *)channel error:(NSError *)error
{
    _isRunning = NO;
    
    /**
     * 开启重连逻辑
     * 断开场景:开始重连
     * 连接失败:开始重连
     */
    if (self.config.connectParam.autoReconnect) {
        [self.channelReconnect start];
    }
}
 
- (void)channel:(RHSocketChannel *)channel received:(id<RHDownstreamPacket>)packet
{
    RHSocketLog(@"[Log]: downstream packet %@", [packet stringWithPacket]);
}
 
@end