-
Notifications
You must be signed in to change notification settings - Fork 27
Mail Server and last_sync_time
The mail server contains a single database of all encrypted messages received, using a receipt-timestamp keyed map (_message_db). It also maintains a map of connections to Keyhotee clients, with each connection reading/writing to the central message database. The server only deals in encrypted messages, as it has no keys of it's own to decode messages. A client establishes a connection to the mail server and sends messages to the server. The server periodically pushes new messages it has received to all clients. It keeps a separate sync_time for each client, so it knows what messages it has already sent to that client.
In main.cpp, the Mail server creates an object mail::server and configures it. configure asyncs accept_loop that waits for tcp connections from KH clients. The server is limited to accepting one connection every 10ms. A connection object is created for each accepted connection to process messages from the connection.
The connection asyncs a read_loop when it is constructed. The read_loop continuously loops: blocking on the socket until it receives a message from a client, then doing a callback to the server via on_connection_message.
The mail server receives two possible message from a connection via callback to on_connection_message:
- an encrypted_message which is stored into the _message_db of the mail server (if the msg is less than 2MB in size)
- a client_info_message. The connection's _sync_time is set from this msg, then the connection enters exec_sync_loop (only enters loop if sync_time of 0 wasn't sent, why, what is the special meaning of 0?).
There is a _sync_time stored in each mail connection. This is used by exec_sync_loop to send all the encrypted messages after _sync_time (updating the connection's _sync_time to the last message sent as part of this syncing process). Every 15 seconds, the mail connection tries to send any new messages after _sync_time to it's associated KH client, while in exec_sync_loop. This loop is only canceled when the connection is destructed.
Each profile has a persistent last_sync_time. A KH Client has its own mail::connection which it re-uses to connect to each potential mail server node in mail_connect_loop.
- application::connect_to_network
- asyncs mail_connect_loop to connect to a mail server (first success in list of mailservers)
- for each default_mail_nodes (until successful send of client_info_message)
- connect to mail server node
- set connection's _sync_time to profile's last_sync_time
- send client_info_message containing profile's last_sync_time
- sets _mail_connected to true on successful connection
- exit mail_connect_loop
- if no successful mail connection, repeat attempt in 3 seconds
- for each default_mail_nodes (until successful send of client_info_message)
- asyncs connect_loop to connect to peers
- asyncs mail_connect_loop to connect to a mail server (first success in list of mailservers)
When a profile is first opened, it's last_sync_time is read from a file. If the value is 0 (e.g. no file existed), last_sync_time is set to now() minus 5 minutes.
Whenever a message is received, profile's last_sync_time is set to received message's sent timestamp. The message's timestamp is set in send_email of originating client to be now() + server_time_offset. server_time_offset is set as now() - server_time when receiving server_info_message from mail server. Currently server doesn't appear to send a server_info_message, but server_time_offset should initialize to 0 as its type is fc::microseconds.
KH clients receive two types of messages from its mail connection via on_connection_message callback:
- encrypted_message: KH tries to decode these using message keys it knows. If it succeeds, it forward the decrypted message on for further processing in bitchat_message_received. Profile's last_sync_time is updated with the message's timestamp (when message was sent, but for mail it is sender's local time plus server_time_offset).
- server_info_message: currently this is only used to update the server_time_offset from server reported time. This message also contains information such as server version, mirrors, and current_difficulty (all currently ignored). This message is never sent by mail server currently.