当前位置:网站首页>Cluster chat server: summary of project problems
Cluster chat server: summary of project problems
2022-07-25 07:14:00 【_ Soren】
List of articles
1. Briefly describe your project
This project is a network server project , It is divided into the following modules .
- The first is the network module , It's using muduo Network library to design , The advantage of the network library is that it decouples the network module code and the business module code , Enable developers to focus on business development .
- The business layer uses some C++11 Some of the technology , such as map、 Binder, etc , It's mainly a message ID And the binding of a callback operation after this message occurs , Callback mechanism . When the network I/O When you spit out a message request for me , According to the request Json, Get the news ID, Then process the message .
- The data storage layer uses MySQL, Some key data of the project have been landed , For example, the user's account 、 offline message , Group messages, etc , Are stored in the database . And realize the database connection pool , Save a lot of three handshakes and four waves , Improve the efficiency of accessing the database .
The above is the basic module of single server , But the concurrency ability of single server is limited , So deploy multiple servers , We need to configure Nginx TCP Load balancing ( Because it's a long connection , So it's based on TCP Of ).
Each server has different user registrations , If users of these different servers want to communicate , The project introduced redis Release - subscribe .
2. Security of data plaintext transmission
In this project , It uses HTTP Plaintext transmission , Then the problem of ensuring security is to encrypt .
Symmetric encryption
Asymmetric encryption
Symmetric encryption : The encryptor and the decryptor use the same key key.
Before sending, the sender encrypts the plaintext data to generate ciphertext , The decryptor uses the key to restore it to plaintext .
The advantage is high efficiency , for example AES Encryption and decryption algorithm

Asymmetric encryption : There are public key and private key , And they are matched one by one .
Encryption is complicated , Slow efficiency , But it's safe . for example RSA encryption algorithm .
Concrete realization :
Because asymmetric encryption is inefficient , So mixed encryption .
Generate public and private keys on the client and server when writing code , Then the client and server shake hands three times to establish a connection , Client side utilization RSA The public key encrypts symmetrically AES The key is encrypted into ciphertext , After sending to the server, the server uses RSA Private key decryption , Get this AES Private key , Then reply to the client with OK, Both client and server have AES Private key , Then in the subsequent data transmission , You can use it AES Symmetric encryption is used to encrypt plaintext .
Another problem is that so many customers need to connect to the server , Then each client must generate with the server AES secret key , This requires the server to store the key of each client in the database , Every time data is transmitted , In order to find the corresponding key easily , You can only put important parts of the data message body To encrypt , and userid No encryption , The server can userid Find the corresponding key .

3. How client messages are displayed in order
There will be this problem in the communication process , namely client1 Send to client2 The news of , Will not arrive in order , Because different routing nodes may be passed during transmission . Example : The news is out of order

Solution :
Add a serial number to each message seq, The receiver maintains the message sequence number of each friend seq.
Example :
The receiver maintains the messages of each friend seq, Initialize to 0, When a message is received , View the seq, If not 0, Cache the current message , Wait until the serial number is 0 After receiving the message of , For maintaining friends seq++, Then check the cached messages .

4. server How the client perceives the state of the client
If the network is seriously blocked ,ChatServer How does the client perceive whether the client is online or offline ?
Solution : heartbeat
Let the heartbeat business bind UDP8080 Port no. ,server Start a heartbeat timer at the end , The client sends a message to the server heartbeat Message type , Just reduce the timer of the user by one , If you time out, add one , In general , The value of the timer will be (-1, 0, 1) Change between .
If a client Your heartbeat timer has exceeded 5, It is judged that client The line has dropped , Then release this client All connections and other resources .
In general , For this kind of chat server, it is a long connection , Will design a heartbeat mechanism in the business layer .

Mention this heartbeat mechanism , There is another problem TCP Agreed keepalive Mechanism .
Example : stay client And server establish TCP After connection , But the two sides did not transmit data , that server How to know client The state of ,client Is it a drop in line , Or is the connection normal, but no data transmission , Or is the link disconnected ?
stay Linux in , We can enable a through code socket The heartbeat of .
// on by 1 Time means open keepalive Options , The default is 0
int on = 1;
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
but keepalive The default time interval for sending heartbeat detection is 7200s, Time is too long , Not practical .
You can set three related options to change this time interval , Namely :
- TCP_KEEPIDLE
- TCP_KEEPINTVL
- TCP_KEEPCNT
Example :
// send out keepalive Time interval of message
int val = 7200;
setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val));
// The time interval between two retry messages
int interval = 75;
setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
int cnt = 9;
setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
TCP_KEEPIDLE Option to send keepalive Time interval of message , When sending, if the opposite end replies ACK, Then this end TCP The protocol stack thinks that the connection is still alive , Continue to wait for 7200s Send later keepalive message ; If the peer replies RESET, It means that the peer process has been restarted , The local application should close the connection .
If there is no reply from the opposite end , Then retry at this end , If you try again 9 Time (TCP_KEEPCNT) Still not reachable , The interval between retries is 75s(TCP_KEEPINTVL), Return... To the application TIMEOUT( No response ) or EHOST error message .
边栏推荐
猜你喜欢

章鱼网络 Community Call #1|开启 Octopus DAO 构建

Can interface debugging still play like this?

【terminal】x86 Native Tools Command Prompt for VS 2017

Xinku online | cnopendata shareholder information data of A-share listed companies

2022 Tiangong cup ctf--- crypto1 WP

分层强化学习综述:Hierarchical reinforcement learning: A comprehensive survey

2022 Shenzhen cup

knapsack problem
![[yolov5 practice 3] traffic sign recognition system based on yolov5 - model training](/img/2f/1d2938dafa17c602c9aaf640be9bf1.png)
[yolov5 practice 3] traffic sign recognition system based on yolov5 - model training

微信小程序switchTab传参以及接收参数
随机推荐
150. Evaluation of inverse Polish expression
列表推导式
[Yugong series] July 2022 go teaching course 016 logical operators and other operators of operators
[semidrive source code analysis] [drive bringup] 38 - norflash & EMMC partition configuration
流量对于元宇宙来讲并不是最重要的,能否真正给传统的生活方式和生产方式带来改变,才是最重要的
[daily question] sword finger offer II 115. reconstruction sequence
Baidu Post Bar crawler gets web pages
Paddlepaddle 34 adjust the layer structure and forward process of the model (realize the addition, deletion, modification and forward modification of the layer)
9大最佳工程施工项目管理系统
微信小程序request请求携带cookie,验证是否已登录
Teach you to use cann to convert photos into cartoon style
Wechat applet wx.request interface
[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x
【每日一题】剑指 Offer II 115. 重建序列
Yolov7 model reasoning and training its own data set
Leetcode 206. reverse linked list I
Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform
How to learn C language?
Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22
%d,%s,%c,%x