当前位置:网站首页>Design of IM login server and message server
Design of IM login server and message server
2022-06-25 05:23:00 【I want to be a fool, too】
Recommend a free open course of zero sound College , Personally, I think the teacher spoke well , Share with you :Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK, Streaming media ,CDN,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Etc , Learn now
2 IM Login server and message server design
1. Communication protocol design
2. reactor Model
3. login_server analysis
4. Database design
5. Login business analysis
6. msg_server analysis
0 Source code distribution
0voice_im\server\src
base Basic components 
1. 1 Communication protocol design - Format

1. 2 Communication protocol design -protobuf



1.3 Communication protocol design - Structure
typedef struct {
uint32_t length; // the whole pdu length Represents this package
uint16_t version; // pdu version number
uint16_t flag; // not used
uint16_t service_id; //
uint16_t command_id; //
uint16_t seq_num; // Package number
uint16_t reversed; // Retain
} PduHeader_t;
Header + body
1.4 Communication protocol design - Package integrity judgment 1

1. Socket TCP( reliability )
2. Boundary treatment of protocol design header + body, adopt header Of
length Field ( 4 byte ) solve
3. Service ID and Command ID Distinguish between different commands
4. TCP The problem of sticking packets is solved by caching data
1.4 Communication protocol design - Package integrity judgment 2
CImPdu::ReadPdu
CImConn::OnRead


2 login_server analysis
login_server
This service is used for load balancing
Yes

3.1 reactor Model -CBaseSocket

socket Custom state :
SOCKET_STATE_IDLE :CBaseSocket()
SOCKET_STATE_LISTENING:Listen() As server When
SOCKET_STATE_CONNECTING :Connect() As client When
SOCKET_STATE_CONNECTED: OnWrite() As client When
SOCKET_STATE_CLOSING: OnClose()
CBaseSocket: management socket io, As client perhaps server You need to instantiate one CBaseSocket
hash_map<net_handle_t, CBaseSocket*> SocketMap; All connection management , With fd by key,
CBaseSocket The object is value, Deal with :
- void AddBaseSocket(CBaseSocket* pSocket) increase
- void RemoveBaseSocket(CBaseSocket* pSocket) Delete
- CBaseSocket* FindBaseSocket(net_handle_tfd) check
3.2 reactor Model -CEventDispatch
CEventDispatch yes reactor The trigger of ,epoll Relevant functions are called here .
Timer relevant
AddTimer: Add scheduled events
RemoveTimer: Delete scheduled events
_CheckTimer: Detect scheduled events
Loop relevant
AddLoop: Add loop Events
_CheckLoop: Detect loop events
epoll relevant
AddEvent: add to fd event
RemoveEvent: Delete fd event
StartDispatch: Get into reactor Main circulation
StopDispatch: stop it reactor Main circulation
3.3 reactor Model -api netlib

CEventDispatch yes reactor The trigger of , But not externally api,netlib It's external api.
fd relevant
netlib_listen: monitor
netlib_connect: Connect
netlib_send: send out
netlib_recv: receive
netlib_close: close
netlib_option: Parameter setting
NETLIB_OPT_SET_CALLBACK Set the callback function
Timer related
netlib_register_timer: Register timer
netlib_delete_timer: Delete timer
Cyclic correlation
netlib_add_loop: You can add transactions that need to be processed circularly to reactor
netlib_eventloop: Get into reactor Main circulation
3.4 reactor Model -http Service and IM The difference between agreements


Listen Set callback
netlib_option:NETLIB_OPT_SET_CALLBACK Set the callback function
login_server.cpp
4 Database design

5.1 Log in to business
Login account verification and status synchronization
User name and password verification
Online status settings + Route status update
The old client account goes offline
Notify friends , Mobile era , 24 Hours at school
Login data preparation
Group information
The friends list
Group list
Friend information
Unread message
other …
5.1 Log in to business - To continue 1
Login account verification and status synchronization
User name and password verification

Online status settings + Route status update

Client status
Server status
Route status update
Plaintext passwords cannot be used
client md5(password)
5.1 Log in to business - To continue 2 The old client account goes offline
Login account verification and status synchronization
If the account is already logged in , Then force it offline

- The office pc Sign in
( 1 ) There is no problem logging in from the mobile phone
( 2 ) If at home pc Sign in , Office offline
2 Mobile phone login
( 1 ) Change your mobile phone and log in , Previous mobile phones were offline
(2 ) If you use pc Sign in , No problem , Tips pc Login
Provide strategies based on actual needs .
5.1 Log in to business - To continue 3 Notify friends

Login account verification and status synchronization
Notify friends
5.1 Log in to business - To continue 4 Sync information
Sync information
The friends list
Group list
Group list
Unread message
Especially log in to wechat
Especially with a new mobile phone , The login process is particularly slow .
Homework : Is it necessary to pull all of them every time you log in :
The friends list
Group list
Unread message
How to do not pull all .
6.1 msg_server The login process



6.2 msg_server Send a message

6.3 msg_server Login request response process - Update the information 1
6.3 msg_server Login request response process 2

6.3 msg_server Login request response process 3

Frame diagram

边栏推荐
- C style string
- Specific operations for uploading pictures in PHP
- Charles and iPhone capture
- Go Context - Cancelation and Propagation
- The article is on the list. Welcome to learn
- Two dimensional array and function call cases of C language
- Conflict between v-mode and v-decorator in Ant Design
- Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
- Customize the console plot result style
- 3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
猜你喜欢

Matlab notes

Flex flexible layout for mobile terminal page production

Use serialize in egg to read and write split tables

DOM document object model (I)

Array: force deduction dichotomy

Example of dynamic programming 3 leetcode 55

Ctfhub eggs

Specific operations for uploading pictures in PHP

Five simple data types of JS

Laravel's little knowledge
随机推荐
File upload vulnerability (III)
API interface management setup -eolinker4.0
CTFHUB SSRF
XSS (cross site script attack) summary (II)
Array: force deduction dichotomy
H5 native player [learn video]
SRC platform summary
Eyeshot 2022 Released
Activereportsjs V3.0 comes on stage
C language -- Sanzi chess
SSRF-lab
Edge loss interpretation
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
Two hours to take you into the software testing industry (with a full set of software testing learning routes)
Eyeshot Ultimate 2022 Crack By Xacker
Read the general components of antd source code
Integrate CDN to create the ultimate service experience for customers!
Japanese fifty tone diagram
Enhanced paste quill editor
About the definition of pointer variables (personal notes)