I recommend a free open course for the zero acoustics academy. I think the teacher has done a good job. Share it with you: Linux, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, protocol, DPDK and other technical content, learn now
2 IM Logon 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 Distribution
0voice_im\server\src
◼ base Foundation Components
1.1 Communication protocol design-format
1.2 Communication Protocol Design-protobuf
1.3 Communication Protocol Design-Architecture
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 Sequence Number uint16_t reversed; // Retain } PduHeader_t;
Header + body
1.4 Communication protocol design-package integrity judgement 1
1. Socket TCP (Reliability)
2. Boundary handling header + body for protocol design, via header's
length field (4 bytes) resolution
3. Service ID and Command ID distinguish different commands
4. TCP sticking problem solves data via caching
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 sharing
Balanced
3.1 reactor model - CBaseSocket
socket Custom Status: SOCKET_STATE_IDLE : CBaseSocket() SOCKET_STATE_LISTENING: Listen() As server When using SOCKET_STATE_CONNECTING :Connect() As client When using SOCKET_STATE_CONNECTED: OnWrite() As client When using SOCKET_STATE_CLOSING: OnClose()
CBaseSocket: Administration socket io,As client perhaps server Need to instantiate one CBaseSocket
hash_map<net_handle_t, CBaseSocket*> SocketMap; Manage all connections to fd by key, CBaseSocket Object is value,Correspondence processing:
- void AddBaseSocket(CBaseSocket* pSocket) increase
- void RemoveBaseSocket(CBaseSocket* pSocket) deletion
- CBaseSocket* FindBaseSocket(net_handle_tfd) check
3.2 reactor model-CEventDispatch
CEventDispatch yes reactor Trigger, epoll Related functions are called here.
Timer Relevant AddTimer: Join Timing Event RemoveTimer: Delete Timing Events _CheckTimer: Detect Timing Events
Loop Relevant AddLoop: Join Loop Event _CheckLoop: Detect Cyclic Events
epoll Relevant AddEvent: Add to fd Event RemoveEvent: delete fd Event StartDispatch: Get into reactor Main cycle StopDispatch: Stop it reactor Main cycle
3.3 reactor model-api netlib
CEventDispatch yes reactor Trigger, but not external api,netlib Is external api.
fd Relevant ◼ netlib_listen: Monitor ◼ netlib_connect: Connect ◼ netlib_send: Send out ◼ netlib_recv: Receive ◼ netlib_close: Close ◼ netlib_option: Parameter Settings ◼ NETLIB_OPT_SET_CALLBACK Set Callback Function
Timer correlation
◼ netlib_register_timer: Register Timer ◼ netlib_delete_timer: Delete timer
Cyclic correlation ◼ netlib_add_loop: You can join transactions that require a loop to reactor ◼ netlib_eventloop: Get into reactor Main cycle
3.4 reactor model - Differences between http services and IM protocols
Listen Set Callback netlib_option: NETLIB_OPT_SET_CALLBACK Set Callback Function
login_server.cpp
4 Database Design
5.1 Login Business
Login account verification and status synchronization
◼ User name password verification
◼ Online Status Settings + Routing Status Updates
◼ Old client account offline
◼ Notify friends, mobile age, 24 hours in school
Login Data Preparation
◼ Grouping Information
◼ Friends List
◼ Group List
◼ Friend Information
◼ Unread message
◼ Other...
5.1 Login Business - Continue 1
Login account verification and status synchronization
◼ User name password verification
◼ Online Status Settings + Routing Status Updates
Client State
_Server-side status
Routing Status Update
Clear text password not available
Client md5(password)
5.1 Logon Business - Continue 2 Old Client Account Offline
Login account verification and status synchronization
◼ Force an account to go offline if it is already logged in
- Office pc login
(1) No problem with mobile phone re-login
(2) If the pc at home logs in, the office goes offline
2 Mobile login
(1) Change your mobile phone and log in again, before your mobile phone went offline
(2) If you use pc to login, no problem, prompt pc to login
Provide strategies based on actual needs.
5.1 Login Business - Continue 3 Notify Friends
Login account verification and status synchronization
◼ Notify Friends
5.1 Login Business - Continue 4 Synchronization Information
Synchronization information
◼ Friends List
◼ Group List
◼ Group List
◼ Unread message
Especially by logging in to WeChat
Especially with a new phone, the login process is very slow.
Job: Do you need to pull all of them for every login?
◼ Friends List
◼ Group List
◼ Unread message
How to do not pull all.
6.1 msg_server login process