pub struct Client { /* private fields */ }
Implementations§
source§impl Client
impl Client
sourcepub fn connect(ip: &str, port: u16) -> Result<Client>
pub fn connect(ip: &str, port: u16) -> Result<Client>
Examples found in repository?
examples/simple_client.rs (line 61)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
fn main() -> Result<()> {
// Create a client and connect to localhost
let mut client = Client::connect("127.0.0.1", 7667)?;
let mut counter = 0;
loop {
// Sleep for a lil bit so we don't hog the CPU
std::thread::sleep(std::time::Duration::from_millis(100));
// Send a ping packet every 20 ticks
counter += 1;
if (counter % 20) == 0 {
let ping = PingPacket {
msg: format!("Ping! Tick {}", counter),
};
client.send(ping);
}
// Run the network tick and process any events it generates
for event in client.tick().iter() {
match event {
ClientEvent::Disconnected => {
println!("Disconnected from server!");
break;
}
ClientEvent::ReceivedPacket(byte_count) => {
println!("Received packet from server ({} bytes)", byte_count);
}
ClientEvent::SentPacket(byte_count) => {
println!("Sent packet to server ({} bytes)", byte_count);
}
_ => eprintln!("Unhandled ClientEvent!"),
}
}
if client.is_disconnected() {
break;
}
// Process incoming packets
for packet in client.drain_incoming_packets().iter() {
match packet.header.id {
0x00 => {
let packet = PongPacket::deserialize(&packet.body);
println!("Got pong: {}", packet.unwrap().msg);
}
_ => eprintln!("Unhandled packet! id: {}", packet.header.id),
}
}
}
Ok(())
}
sourcepub fn is_disconnected(&self) -> bool
pub fn is_disconnected(&self) -> bool
Examples found in repository?
examples/simple_client.rs (line 94)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
fn main() -> Result<()> {
// Create a client and connect to localhost
let mut client = Client::connect("127.0.0.1", 7667)?;
let mut counter = 0;
loop {
// Sleep for a lil bit so we don't hog the CPU
std::thread::sleep(std::time::Duration::from_millis(100));
// Send a ping packet every 20 ticks
counter += 1;
if (counter % 20) == 0 {
let ping = PingPacket {
msg: format!("Ping! Tick {}", counter),
};
client.send(ping);
}
// Run the network tick and process any events it generates
for event in client.tick().iter() {
match event {
ClientEvent::Disconnected => {
println!("Disconnected from server!");
break;
}
ClientEvent::ReceivedPacket(byte_count) => {
println!("Received packet from server ({} bytes)", byte_count);
}
ClientEvent::SentPacket(byte_count) => {
println!("Sent packet to server ({} bytes)", byte_count);
}
_ => eprintln!("Unhandled ClientEvent!"),
}
}
if client.is_disconnected() {
break;
}
// Process incoming packets
for packet in client.drain_incoming_packets().iter() {
match packet.header.id {
0x00 => {
let packet = PongPacket::deserialize(&packet.body);
println!("Got pong: {}", packet.unwrap().msg);
}
_ => eprintln!("Unhandled packet! id: {}", packet.header.id),
}
}
}
Ok(())
}
sourcepub fn drain_incoming_packets(&mut self) -> Vec<Packet>
pub fn drain_incoming_packets(&mut self) -> Vec<Packet>
Examples found in repository?
examples/simple_client.rs (line 99)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
fn main() -> Result<()> {
// Create a client and connect to localhost
let mut client = Client::connect("127.0.0.1", 7667)?;
let mut counter = 0;
loop {
// Sleep for a lil bit so we don't hog the CPU
std::thread::sleep(std::time::Duration::from_millis(100));
// Send a ping packet every 20 ticks
counter += 1;
if (counter % 20) == 0 {
let ping = PingPacket {
msg: format!("Ping! Tick {}", counter),
};
client.send(ping);
}
// Run the network tick and process any events it generates
for event in client.tick().iter() {
match event {
ClientEvent::Disconnected => {
println!("Disconnected from server!");
break;
}
ClientEvent::ReceivedPacket(byte_count) => {
println!("Received packet from server ({} bytes)", byte_count);
}
ClientEvent::SentPacket(byte_count) => {
println!("Sent packet to server ({} bytes)", byte_count);
}
_ => eprintln!("Unhandled ClientEvent!"),
}
}
if client.is_disconnected() {
break;
}
// Process incoming packets
for packet in client.drain_incoming_packets().iter() {
match packet.header.id {
0x00 => {
let packet = PongPacket::deserialize(&packet.body);
println!("Got pong: {}", packet.unwrap().msg);
}
_ => eprintln!("Unhandled packet! id: {}", packet.header.id),
}
}
}
Ok(())
}
sourcepub fn send(&mut self, packet: impl PacketBody)
pub fn send(&mut self, packet: impl PacketBody)
Examples found in repository?
examples/simple_client.rs (line 74)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
fn main() -> Result<()> {
// Create a client and connect to localhost
let mut client = Client::connect("127.0.0.1", 7667)?;
let mut counter = 0;
loop {
// Sleep for a lil bit so we don't hog the CPU
std::thread::sleep(std::time::Duration::from_millis(100));
// Send a ping packet every 20 ticks
counter += 1;
if (counter % 20) == 0 {
let ping = PingPacket {
msg: format!("Ping! Tick {}", counter),
};
client.send(ping);
}
// Run the network tick and process any events it generates
for event in client.tick().iter() {
match event {
ClientEvent::Disconnected => {
println!("Disconnected from server!");
break;
}
ClientEvent::ReceivedPacket(byte_count) => {
println!("Received packet from server ({} bytes)", byte_count);
}
ClientEvent::SentPacket(byte_count) => {
println!("Sent packet to server ({} bytes)", byte_count);
}
_ => eprintln!("Unhandled ClientEvent!"),
}
}
if client.is_disconnected() {
break;
}
// Process incoming packets
for packet in client.drain_incoming_packets().iter() {
match packet.header.id {
0x00 => {
let packet = PongPacket::deserialize(&packet.body);
println!("Got pong: {}", packet.unwrap().msg);
}
_ => eprintln!("Unhandled packet! id: {}", packet.header.id),
}
}
}
Ok(())
}
sourcepub fn tick(&mut self) -> Vec<ClientEvent>
pub fn tick(&mut self) -> Vec<ClientEvent>
Runs a network tick, which sends/receives packets based on socket readiness
Examples found in repository?
examples/simple_client.rs (line 78)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
fn main() -> Result<()> {
// Create a client and connect to localhost
let mut client = Client::connect("127.0.0.1", 7667)?;
let mut counter = 0;
loop {
// Sleep for a lil bit so we don't hog the CPU
std::thread::sleep(std::time::Duration::from_millis(100));
// Send a ping packet every 20 ticks
counter += 1;
if (counter % 20) == 0 {
let ping = PingPacket {
msg: format!("Ping! Tick {}", counter),
};
client.send(ping);
}
// Run the network tick and process any events it generates
for event in client.tick().iter() {
match event {
ClientEvent::Disconnected => {
println!("Disconnected from server!");
break;
}
ClientEvent::ReceivedPacket(byte_count) => {
println!("Received packet from server ({} bytes)", byte_count);
}
ClientEvent::SentPacket(byte_count) => {
println!("Sent packet to server ({} bytes)", byte_count);
}
_ => eprintln!("Unhandled ClientEvent!"),
}
}
if client.is_disconnected() {
break;
}
// Process incoming packets
for packet in client.drain_incoming_packets().iter() {
match packet.header.id {
0x00 => {
let packet = PongPacket::deserialize(&packet.body);
println!("Got pong: {}", packet.unwrap().msg);
}
_ => eprintln!("Unhandled packet! id: {}", packet.header.id),
}
}
}
Ok(())
}