Passing TCP to an actor
When it comes to routing TCP data to actors, we need to import our actors and channels into the main.rs
file in our server project with the following code:
. . . use tokio::sync::mpsc; mod actors; use actors::{OrderBookActor, BuyOrder, Message}; . . .
Now, we have to construct our order book actor and run it. However, as you may recall, we merely ran the order book actor at the end of the Tokio runtime. However, if we apply this strategy here, we will block the loop from executing, so we can listen to incoming traffic. If we run the order book actor after the loop, the order book actor will never run as the loop runs indefinitely in a while
loop and thus blocks the execution of any code following it. In our case, there is a further complication. This complication is that the actor run function enters a while
loop, which further explains the need to put this entire code in a separate spawned Tokio task. Because of this, we must spawn a thread before the...