Professional Documents
Culture Documents
1/19
Tài liệu Hướng dẫn sử dụng v1.0
TRANG KÝ
2/19
Tài liệu Hướng dẫn sử dụng v1.0
3/19
Tài liệu Hướng dẫn sử dụng v1.0
MỤC LỤC
4/19
Tài liệu Hướng dẫn sử dụng v1.0
5/19
Tài liệu Hướng dẫn sử dụng v1.0
2. MÔ TẢ CHUNG
Ví dụ trong tài liệu này thực hiện xây dựng hệ thống Client và Server sử dụng thư viện
Netty sử dụng Google Protobuf, xác thực bằng SSL hai chiều. Giao thức client và server bao
gồm hai loại bản tin là login và ping. Có thể bổ sung thêm các loại bản tin vào giao thức tùy theo
nhu cầu thực tế. Hệ thống client, server này có các đặc điểm sau:
- Giữ kết nối
- Sử dụng Google Protocol Buffer
- Xác thực và mã hóa kênh truyền bằng SSL 2 chiều
- Cả Client và Server đều sử dụng mô hình Event-Driven
6/19
Tài liệu Hướng dẫn sử dụng v1.0
Hệ thống (cả client-example và server-example) đều sử dụng các thư viện sau (khai báo
trong dependency maven ở file pom.xml)
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>4.0.17.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec</artifactId>
<version>4.0.17.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
<version>4.0.17.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.0.17.Final</version>
</dependency>
7/19
Tài liệu Hướng dẫn sử dụng v1.0
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>4.0.17.Final</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
8/19
Tài liệu Hướng dẫn sử dụng v1.0
message LoginRequest {
optional string client_id = 1;
}
message LoginResponse {
enum LoginStatus {
SUCCESS = 0;
FAILTURE = 1;
}
optional LoginStatus login_status = 1;
}
message PingRequest {
message PingResponse {
optional int64 time_recv = 1;
}
Có 2 loại message chính là Client và Server. Trong đó message Server là các message mà Client
gửi tới Server tức outbound của Client và inbound của Server. Ngược lại message Client là các
message mà Server gửi trả về Client tức inbound của Client và outbound của Server. Mỗi
message có trường bắt buộc là MessageType, tùy theo giá trị của MessageType mà message
chứa các message con tương ứng của Login (LoginRequest, LoginResponse), Ping (PingRequest,
PingResponse). Mô hình thiết kế này đảm bảo tính mở rộng các message tùy theo nhu cầu thực
tế trong tương lai.
9/19
Tài liệu Hướng dẫn sử dụng v1.0
10/19
Tài liệu Hướng dẫn sử dụng v1.0
ServerInitializer thực hiện khởi tạo ChannelPipeline và thêm lần lượt các pipeline vào để
xử lý lần lượt như sau:
p.addLast("ssl", new SslHandler(engine));
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new
ProtobufDecoder(ExampleProtocol.Server.getDefaultInstance()));
p.addLast("frameEncoder", new
ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
11/19
Tài liệu Hướng dẫn sử dụng v1.0
12/19
Tài liệu Hướng dẫn sử dụng v1.0
13/19
Tài liệu Hướng dẫn sử dụng v1.0
tin Ping tới Server. Nếu login không thành công, kết nối bị đóng và chờ Watchdog tiếp tục gọi
kết nối tới Server.
ClientInitializer thực hiện khởi tạo ChannelPipeline và thêm lần lượt các pipeline vào để
xử lý lần lượt như sau:
p.addLast("ssl", new SslHandler(engine));
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new
ProtobufDecoder(ExampleProtocol.Client.getDefaultInstance()));
p.addLast("frameEncoder", new
ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
14/19
Tài liệu Hướng dẫn sử dụng v1.0
15/19
Tài liệu Hướng dẫn sử dụng v1.0
7. PHỤ LỤC
N/A
16/19
Tài liệu Hướng dẫn sử dụng v1.0
17/19