宽窄优行-由【嘉易行】项目成品而来
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
//
//  RHSocketChannel.h
//  RHSocketKitDemo
//
//  Created by zhuruhong on 15/12/15.
//  Copyright © 2015年 zhuruhong. All rights reserved.
//
 
#import "RHSocketConnection.h"
#import "RHChannelBufferProtocol.h"
#import "RHSocketCodecProtocol.h"
 
@class RHSocketChannel;
 
@protocol RHSocketChannelDelegate <NSObject>
 
@required
 
- (void)channelOpened:(RHSocketChannel *)channel host:(NSString *)host port:(int)port;
- (void)channelClosed:(RHSocketChannel *)channel error:(NSError *)error;
- (void)channel:(RHSocketChannel *)channel received:(id<RHDownstreamPacket>)packet;
 
@end
 
/**
 *  在RHSocketConnection基础上做封装,负责对socket中的二进制通讯数据做缓存、编码、解码处理。
 *
 *  1-需要有一个编码解码器,对数据块做封包和解包。很多人不理解这个,其实很简单。
 *  比如一句话里面没有标点符号你怎么知道什么时候是结束什么时候开始呢
 *  2-内部带有一个数据缓存,用于对数据的拼包。
 *  发送网络数据时,一条数据会被切成多个网络包[不是我们上层协议中的概念],需要对收到的数据做合并,完整后才能正常解码。
 *  3-发送数据,接收数据,解码数据 都是在socket线程中处理。回调上层处理时,切换回主线程。
 */
@interface RHSocketChannel : RHSocketConnection
<
RHUpstreamBufferDelegate,
RHSocketEncoderOutputProtocol,
RHDownstreamBufferDelegate,
RHSocketDecoderOutputProtocol
>
 
/** 上行数据包缓存 */
@property (nonatomic, strong) id<RHUpstreamBuffer> upstreamBuffer;
/** 下行数据包缓存 */
@property (nonatomic, strong) id<RHDownstreamBuffer> downstreamBuffer;
 
@property (nonatomic, strong) id<RHSocketEncoderProtocol> encoder;
@property (nonatomic, strong) id<RHSocketDecoderProtocol> decoder;
 
/**
 *  socket connection的回调代理,查看RHSocketConnectionDelegate
 *  已经被addDelegate/removeDelegate替代
 */
@property (nonatomic, weak) id<RHSocketChannelDelegate> delegate DEPRECATED_ATTRIBUTE;
@property (nonatomic, strong, readonly) NSMapTable *delegateMap;
 
/**
 *  替换setDelegate方法,可以同时注册多个监听
 */
- (void)addDelegate:(id<RHSocketChannelDelegate>)delegate;
- (void)removeDelegate:(id<RHSocketChannelDelegate>)delegate;
 
- (void)openConnection;
- (void)closeConnection;
 
- (void)asyncSendPacket:(id<RHUpstreamPacket>)packet;
/** 重连成功后,刷新发送缓存区 */
- (void)flushSendPackets;
 
@end
 
@interface RHSocketChannel (WriteInt)
 
- (void)writeInt8:(int8_t)param;
- (void)writeInt16:(int16_t)param;
- (void)writeInt32:(int32_t)param;
- (void)writeInt64:(int64_t)param;
 
@end