Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Rust Concurrent Cross-Platform TCP Server #1

Merged
merged 77 commits into from
Sep 23, 2022
Merged

Rust Concurrent Cross-Platform TCP Server #1

merged 77 commits into from
Sep 23, 2022

Conversation

AOx0
Copy link
Owner

@AOx0 AOx0 commented Sep 18, 2022

I have a working Rust-based TCP server that communicates all data to C++ Runtime for processing.

With this PR we get a pretty clean interface to communicate with any inbound TCP connection. Thus, emulating how a real DB works.

Is as simple as reading with string TcpServer::recv(), prepare a response and send it with void TcpServer::send(string)

{
  // Start the server
  TcpServer server = TcpServer();
  
  // Read a message from the stream
  Shared ctx = server.recv();
  string r = ctx.get_msg();
  
  // Start a stream to prepare an answer
  stringstream a;
  
  // Fill the stream with an answer
  a << "Hola desde C++" << endl;
  
  // Send the stream as string as the response
  server.send(ctx, a.str());

}  // Server stops automatically when dropped

The response time does not matter, the Rust TCP server "asynchronicity" makes all waiting messages to eventually arrive, although we could improve the Rust and C++ to make it concurrent, time will say.

This makes it easy for us to access from a terminal to the database or even add more layers like an HTTP server.

┌──────────────────┐                                            
│ HTTP             │                                            
└──────────────────┘       
┌──────────────────┐                                            
│ TCP              │                                            
└──────────────────┘ 
┌──────────────────┐                                            
│ DataBase         │                                            
└──────────────────┘ 

The following is an example of a query:

┌──────────────────┐       ┌──────────────────┐       ┌──────────────────┐                                            
│ User (terminal)  │       │ TCP Server (rs)  │       │ CPP Server (cpp) │                                            
└──────────────────┘       └──────────────────┘       └──────────────────┘                                            
          │  SELECT name FROM names  │                          │                                                     
          │─────────────────────────▶│                          │                                                     
          │                          │                          │                                                     
          │                          │  SELECT name FROM names  │                                                     
          │                          │─────────────────────────▶│                                                     
          │                          │                          │                                                     
          │                          │                          │─┐                                                   
          │                          │                          │ │ Query processing...                               
          │                          │                          │◀┘                                                   
          │                          │                          │                                                     
          │                          │                          │─┐                                                   
          │                          │                          │ │ Answer ready, example: [Daniel, Diego]     
          │                          │                          │◀┘                                                   
          │                          │                          │                                                     
          │                          │     [Daniel, Diego]      │                                                     
          │                          │◀─────────────────────────│                                                     
          │                          │                          │                                                     
          │     [Daniel, Diego]      │                          │                                                     
          │◀─────────────────────────│                          │                                                     
          │                          │                          │                                                     
┌──────────────────┐       ┌──────────────────┐       ┌──────────────────┐                                            
│ User (terminal)  │       │ TCP Server (rs)  │       │ CPP Server (cpp) │                                            
└──────────────────┘       └──────────────────┘       └──────────────────┘      

A more in-depth diagram of what happens would look like follows:

┌──────────────────┐ ┌──────────────────┐       ┌──────────────────┐       
│ CPP Server (cpp) │ │ TCP Server (rs)  │       │ User (terminal)  │       
└──────────────────┘ └──────────────────┘       └──────────────────┘       
          │      start()       │                          │                
          │───────────────────▶│                          │                
          │                    │                          │                
          │                    │─┐                        │                
          │                    │ │ Starts server...       │                
          │                    │◀┘                        │                
          │                    │                          │                
          │                    │─┐                                         
          │                    │ │ Listening for socket connections...     
          │                    │◀┘                                         
          │                    │                          │                
          │        Tcp         │                          │                
          │◀───────────────────│                          │                
          │                    │                          │                
          │─┐                  │                          │                
          │ │ Tcp::recv()      │                          │                
          │◀┘                  │                          │                
          │                    │                          │                
          │                    │  Connect 127.0.0.1:9999  │                
          │                    │◀─────────────────────────│                
          │                    │                          │                
          │                    │        Connected         │                
          │                    │─────────────────────────▶│                
          │                    │                          │                
          │                    │          Query           │                
          │                    │◀─────────────────────────│                
          │                    │                          │                
          │       Query        │                          │                
          │◀───────────────────│                          │                
          │                    │                          │                
          │─┐                                             │                
          │ │ Tcp::recv() - Received: `Query`             │                
          │◀┘                                             │                
          │                    │                          │                
          │─┐                                             │                
          │ │ Processing query ...                        │                
          │◀┘                                             │                
          │                    │                          │                
          │─┐                                             │                
          │ │ Tcp::send(`Response`)                       │                
          │◀┘                                             │                
          │                    │                          │                
          │      Response      │                          │                
          │───────────────────▶│                          │                
          │                    │                          │                
          │                    │         Response         │                
          │                    │─────────────────────────▶│                
          │                    │                          │                
┌──────────────────┐ ┌──────────────────┐       ┌──────────────────┐       
│ CPP Server (cpp) │ │ TCP Server (rs)  │       │ User (terminal)  │       
└──────────────────┘ └──────────────────┘       └──────────────────┘   

@AOx0 AOx0 self-assigned this Sep 18, 2022
@AOx0 AOx0 added the enhancement New feature or request label Sep 18, 2022
@AOx0 AOx0 changed the title Working experimental tcp Rust Concurrent Cross-Platfirm TCP Server Sep 23, 2022
@AOx0 AOx0 changed the title Rust Concurrent Cross-Platfirm TCP Server Rust Concurrent Cross-Platform TCP Server Sep 23, 2022
Restyle Rust Concurrent Cross-Platform TCP Server
@AOx0 AOx0 marked this pull request as ready for review September 23, 2022 03:37
@AOx0 AOx0 merged commit ad0fbc2 into main Sep 23, 2022
@AOx0 AOx0 deleted the experimental-tcp branch September 23, 2022 04:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants