Guest User

Untitled

a guest
Jun 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. pub struct RconConnection {
  2. pub addr: SocketAddr,
  3. pub secure: RconSecure,
  4. pub password: String,
  5. pub socket: Option<UdpSocket>,
  6. }
  7.  
  8. impl RconConnection {
  9. pub fn new(addr: SocketAddr, password: String, secure: RconSecure) -> Self {
  10. Self {
  11. addr: addr,
  12. secure: secure,
  13. password: password,
  14. socket: None,
  15. }
  16. }
  17.  
  18. pub fn connect(&mut self) -> Result<(), String> {
  19. let socket = UdpSocket::bind("0.0.0.0:0").map_err(|e| { format!("Could not bind socket: {}", e)})?;
  20. socket.connect(self.addr).map_err(|e| { format!("Could not connect: {}", e)})
  21. }
  22.  
  23. pub fn send(&mut self, command: &str) -> std::io::Result<(usize)> {
  24. let socket = self.socket.as_ref().expect("Can't send anything before connected");
  25. let packet = RconOutgoingPacket {
  26. secure: self.secure,
  27. command: command,
  28. password: &self.password,
  29. challenge: None,
  30. };
  31. let res = socket.send(packet.prepare().as_slice());
  32. res
  33. }
  34. }
Add Comment
Please, Sign In to add comment