当前位置:网站首页>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
 Insert picture description here

1. 1 Communication protocol design - Format

 Insert picture description here

1. 2 Communication protocol design -protobuf

 Insert picture description here
 Insert picture description here  Insert picture description here

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

 Insert picture description here

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

 Insert picture description here
 Insert picture description here

 Insert picture description here

2 login_server analysis

login_server

This service is used for load balancing
Yes

 Insert picture description here

3.1 reactor Model -CBaseSocket

 Insert picture description here

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 :
  1. void AddBaseSocket(CBaseSocket* pSocket) increase
  2. void RemoveBaseSocket(CBaseSocket* pSocket) Delete
  3. 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

 Insert picture description here

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

 Insert picture description here  Insert picture description here

Listen  Set callback 
netlib_option:NETLIB_OPT_SET_CALLBACK  Set the callback function 

login_server.cpp

4 Database design

 Insert picture description here

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

 Insert picture description here

Online status settings + Route status update

 Insert picture description here

 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

 Insert picture description here

  1. 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

 Insert picture description here

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

 Insert picture description here  Insert picture description here
 Insert picture description here

6.2 msg_server Send a message

 Insert picture description here

6.3 msg_server Login request response process - Update the information 1 Insert picture description here

6.3 msg_server Login request response process 2

 Insert picture description here

6.3 msg_server Login request response process 3

 Insert picture description here

Frame diagram

 Insert picture description here

原网站

版权声明
本文为[I want to be a fool, too]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210514293299.html