### BoomNet Cargo.toml Dependency Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Declare the `boomnet` dependency in your `Cargo.toml` file, selecting desired features for your project. This example includes features for TLS (rustls-webpki), WebSocket (ws), and extensions (ext). ```toml [dependencies] boomnet = { version = "0.0.55", features = ["rustls-webpki", "ws", "ext"]} ``` -------------------------------- ### Define Binance Trade Endpoint Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Illustrates defining a custom Rust struct `TradeEndpoint` that implements the `TlsWebsocketEndpoint` trait to connect to Binance's trade stream. It handles connection setup, subscription message sending, and message polling. ```rust struct TradeEndpoint { id: u32, connection_info: ConnectionInfo, ws_endpoint: String, instrument: &'static str, } impl TradeEndpoint { pub fn new(id: u32, url: &'static str, instrument: &'static str) -> TradeEndpoint { let (connection_info, ws_endpoint, _) = boomnet::ws::util::parse_url(url).unwrap(); Self { id, connection_info, ws_endpoint, instrument, } } } impl ConnectionInfoProvider for TradeEndpoint { fn connection_info(&self) -> &ConnectionInfo { &self.connection_info } } impl TlsWebsocketEndpoint for TradeEndpoint { type Stream = MioStream; // called by the IO service whenever a connection has to be established for this endpoint fn create_websocket(&mut self, addr: SocketAddr) -> io::Result>> { let mut ws = TcpStream::try_from((&self.connection_info, addr))? .into_mio_stream() .into_tls_websocket(&self.ws_endpoint); // send subscription message ws.send_text( true, Some(format!(r#"{{\"method\":\"SUBSCRIBE\",\"params\":[\"{}@trade\"],\"id\":1}}"#, self.instrument).as_bytes()), )?; Ok(Some(ws)) } #[inline] fn poll(&mut self, ws: &mut TlsWebsocket) -> io::Result<()> { // iterate over available frames in the current batch for frame in ws.read_batch()? { if let WebsocketFrame::Text(fin, data) = frame? { println!("[{}] ({fin}) {}", self.id, String::from_utf8_lossy(data)); } } Ok(()) } } ``` -------------------------------- ### Initializing IOService with MioSelector Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Illustrates the initialization of an `IOService` using `MioSelector`, which provides an abstraction over OS-specific mechanisms like `epoll` for efficient socket readiness event monitoring. ```rust let mut io_service = MioSelector::new()?.into_io_service(); ``` -------------------------------- ### Context-Aware IOService Initialization Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Demonstrates initializing a context-aware `IOService` by creating a `FeedContext` instance and passing it to `into_io_service_with_context`. The `poll` method then requires the context to be passed along. ```rust let mut context = FeedContext::new(); // Assuming FeedContext has a new() method let mut io_service = MioSelector::new()?.into_io_service_with_context(&mut context); loop { io_service.poll(&mut context)?; // Pass context to poll } ``` -------------------------------- ### Applying WebSocket Protocol to a Stream Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Shows how to apply the WebSocket protocol on top of an existing stream abstraction, such as a `RecordedStream` with TLS. This highlights the composability of streams and protocols within the framework. ```rust let ws: Websocket>> = stream.into_websocket("/ws"); ``` -------------------------------- ### Stream Abstraction with TLS and Recording Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Demonstrates creating a TLS-secured TCP stream and wrapping it with a `RecordedStream` for network byte stream recording and replay. This showcases the generic nature of streams in BoomNet. ```rust let stream: RecordedStream> = TcpStream::try_from((host, port))? .into_tls_stream() .into_default_recorded_stream(); ``` -------------------------------- ### Main Event Loop with Multiple Endpoints Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Demonstrates the main function that initializes the `IOService` and registers multiple `TradeEndpoint` instances for different cryptocurrencies (BTC, ETH, XRP). It then enters an infinite loop to poll the service for incoming messages. ```rust fn main() -> anyhow::Result<()> { let mut io_service = MioSelector::new()?.into_io_service(); let endpoint_btc = TradeEndpoint::new(0, "wss://stream1.binance.com:443/ws", "btcusdt"); let endpoint_eth = TradeEndpoint::new(1, "wss://stream2.binance.com:443/ws", "ethusdt"); let endpoint_xrp = TradeEndpoint::new(2, "wss://stream3.binance.com:443/ws", "xrpusdt"); io_service.register(endpoint_btc); io_service.register(endpoint_eth); io_service.register(endpoint_xrp); loop { // will never block io_service.poll()?; } } ``` -------------------------------- ### Context-Aware Endpoint Implementation Source: https://github.com/havefuntrading/boomnet/blob/main/README.md Shows how to implement context-aware WebSocket endpoints using `TlsWebsocketEndpointWithContext`. This allows sharing state between the `IOService` and the endpoint logic, enabling access to shared data within `create_websocket` and `poll` methods. ```rust struct FeedContext; // use the marker trait impl Context for FeedContext {} impl TlsWebsocketEndpointWithContext for TradeEndpoint { type Stream = MioStream; fn create_websocket(&mut self, addr: SocketAddr, ctx: &mut FeedContext) -> io::Result>> { // we now have access to context // ... Ok(None) // Placeholder } #[inline] fn poll(&mut self, ws: &mut TlsWebsocket, ctx: &mut FeedContext) -> io::Result<()> { // we now have access to context // ... Ok(()) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.