send.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::{net::transport::Dialer, Result};
  19. use darkfi_serial::{
  20. async_trait, serialize_async, AsyncDecodable, AsyncEncodable, SerialDecodable, SerialEncodable,
  21. };
  22. use std::time::UNIX_EPOCH;
  23. use url::Url;
  24. use evgrd::{VersionMessage, MSG_SENDEVENT};
  25. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  26. pub struct Privmsg {
  27. pub channel: String,
  28. pub nick: String,
  29. pub msg: String,
  30. }
  31. async fn amain() -> Result<()> {
  32. let endpoint = "tcp://127.0.0.1:5588";
  33. let endpoint = Url::parse(endpoint)?;
  34. let dialer = Dialer::new(endpoint, None).await?;
  35. let timeout = std::time::Duration::from_secs(60);
  36. println!("Connecting...");
  37. let mut stream = dialer.dial(Some(timeout)).await?;
  38. println!("Connected!");
  39. let version = VersionMessage::new();
  40. version.encode_async(&mut stream).await?;
  41. let server_version = VersionMessage::decode_async(&mut stream).await?;
  42. println!("Server version: {}", server_version.protocol_version);
  43. let msg = Privmsg {
  44. channel: "#random".to_string(),
  45. nick: "anon".to_string(),
  46. msg: "i'm so random!".to_string(),
  47. };
  48. let timestamp = UNIX_EPOCH.elapsed().unwrap().as_millis() as u64;
  49. MSG_SENDEVENT.encode_async(&mut stream).await?;
  50. timestamp.encode_async(&mut stream).await?;
  51. let content: Vec<u8> = serialize_async(&msg).await;
  52. content.encode_async(&mut stream).await?;
  53. Ok(())
  54. }
  55. fn main() {
  56. let is_success = smol::block_on(amain());
  57. println!("Finished: {is_success:?}");
  58. }