mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-05-18 09:35:50 +00:00
[Protocol] Replace IpAddr with SocketAddr in protocols (#44)
* [Crate] Replace IpAddr with SocketAddr in protocols * [Crate] Remove usage of address.to_string in socket * [Crate] Update CHANGELOG.md
This commit is contained in:
parent
596d15df78
commit
e0830bdae5
70 changed files with 215 additions and 286 deletions
|
|
@ -1,6 +1,5 @@
|
|||
use crate::{
|
||||
protocols::types::TimeoutSettings,
|
||||
utils::address_and_port_as_string,
|
||||
GDError::{PacketReceive, PacketSend, SocketBind, SocketConnect},
|
||||
GDResult,
|
||||
};
|
||||
|
|
@ -9,12 +8,12 @@ use std::{
|
|||
io::{Read, Write},
|
||||
net,
|
||||
};
|
||||
use std::net::IpAddr;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
const DEFAULT_PACKET_SIZE: usize = 1024;
|
||||
|
||||
pub trait Socket {
|
||||
fn new(address: &IpAddr, port: u16) -> GDResult<Self>
|
||||
fn new(address: &SocketAddr) -> GDResult<Self>
|
||||
where Self: Sized;
|
||||
|
||||
fn apply_timeout(&self, timeout_settings: Option<TimeoutSettings>) -> GDResult<()>;
|
||||
|
|
@ -28,11 +27,9 @@ pub struct TcpSocket {
|
|||
}
|
||||
|
||||
impl Socket for TcpSocket {
|
||||
fn new(address: &IpAddr, port: u16) -> GDResult<Self> {
|
||||
let complete_address = address_and_port_as_string(address, port);
|
||||
|
||||
fn new(address: &SocketAddr) -> GDResult<Self> {
|
||||
Ok(Self {
|
||||
socket: net::TcpStream::connect(complete_address).map_err(|_| SocketConnect)?,
|
||||
socket: net::TcpStream::connect(address).map_err(|_| SocketConnect)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -61,17 +58,16 @@ impl Socket for TcpSocket {
|
|||
|
||||
pub struct UdpSocket {
|
||||
socket: net::UdpSocket,
|
||||
complete_address: String,
|
||||
address: SocketAddr,
|
||||
}
|
||||
|
||||
impl Socket for UdpSocket {
|
||||
fn new(address: &IpAddr, port: u16) -> GDResult<Self> {
|
||||
let complete_address = address_and_port_as_string(address, port);
|
||||
fn new(address: &SocketAddr) -> GDResult<Self> {
|
||||
let socket = net::UdpSocket::bind("0.0.0.0:0").map_err(|_| SocketBind)?;
|
||||
|
||||
Ok(Self {
|
||||
socket,
|
||||
complete_address,
|
||||
address: address.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -85,7 +81,7 @@ impl Socket for UdpSocket {
|
|||
|
||||
fn send(&mut self, data: &[u8]) -> GDResult<()> {
|
||||
self.socket
|
||||
.send_to(data, &self.complete_address)
|
||||
.send_to(data, &self.address)
|
||||
.map_err(|_| PacketSend)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -116,10 +112,8 @@ mod tests {
|
|||
stream.write(&buf).unwrap();
|
||||
});
|
||||
|
||||
let address = bound_address.ip();
|
||||
|
||||
// Create a TCP socket and send a message to the server
|
||||
let mut socket = TcpSocket::new(&address, bound_address.port()).unwrap();
|
||||
let mut socket = TcpSocket::new(&bound_address).unwrap();
|
||||
let message = b"hello, world!";
|
||||
socket.send(message).unwrap();
|
||||
|
||||
|
|
@ -149,10 +143,8 @@ mod tests {
|
|||
socket.send_to(&buf, src_addr).unwrap();
|
||||
});
|
||||
|
||||
let address = bound_address.ip();
|
||||
|
||||
// Create a UDP socket and send a message to the server
|
||||
let mut socket = UdpSocket::new(&address, bound_address.port()).unwrap();
|
||||
let mut socket = UdpSocket::new(&bound_address).unwrap();
|
||||
let message = b"hello, world!";
|
||||
socket.send(message).unwrap();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue