-
Notifications
You must be signed in to change notification settings - Fork 26
Huge Update #2 #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LeonMatthes
wants to merge
38
commits into
futile:master
Choose a base branch
from
LeonMatthes:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 33 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
4484398
Replace deprecated std::mem::uninitialized with std::mem::MaybeUninit
htrefil 13e25f7
Remove dependency on byteorder, use functions from std for converting…
htrefil 7f20c12
Implement From<SocketAddrV4> for Address
htrefil 8751315
Make lazy_static a dev-dependency
htrefil 855596d
Merge branch 'master' of github.com:futile/enet-rs
htrefil 863054b
Make peers "unsized"
htrefil d9d9e36
Fix warnings
htrefil d87eaac
Add missing docs
htrefil cd8b9c8
Remove unnecessary lifetimes ('_)
htrefil a708198
Drop data on disconnected peers
htrefil bb88b65
Drop all peer data on Drop of Host structure
htrefil bcae5cf
Add missing docs
htrefil 5f6dcf8
Implement Index, IndexMut for Host
htrefil 09c2811
Add missing Debug implementations
htrefil e330de1
Update client example
htrefil f20b568
Make Host::service take std::time::Duration instead of u32
htrefil 8e24687
Update server example
htrefil 7d561be
Update both examples (again)
htrefil 4e4b19b
Improve docs about peer data lifetime
htrefil dc531b3
Drop peer data on Peer::disconnect_now
htrefil 82c8168
Rename user_data to data for consistency
htrefil 62e346a
Make Packet::new take a Vec<u8>
htrefil d49a232
Fix index calculation
htrefil e21bd2c
Replace user_data with data again
htrefil ef071c8
Update docs
htrefil 577d7bd
Make Address::from_hostname take a &CStr, rather than &CString
htrefil e9324cf
Gurantee that the data field of ENetPacket will always be a pointer t…
htrefil 3080364
Change Packet::new to use Box<[u8]> instead of Vec
LeonMatthes 4b25ddc
Create PeerID struct to increase type safety.
LeonMatthes 4efa37f
Use PeerID in Event
LeonMatthes d21b063
Readd Peer::receive()
LeonMatthes 01fe79d
Use PeerID for host.drop_disconnected
LeonMatthes da58586
Refactor drop_disconnected to use peer_mut()
LeonMatthes f77df67
Use Vec<u8> again for Packet construction
LeonMatthes 5aad870
Merge branch 'master' of https://github.com/futile/enet-rs
LeonMatthes e765e0f
Remove Index and IndexMut from Host
LeonMatthes b448b18
Make PeerID generational
LeonMatthes 62eb1d8
Incorporate feedback from @futile
LeonMatthes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,60 @@ | ||
| #![allow(non_upper_case_globals)] | ||
| use enet_sys::{ | ||
| ENetEvent, _ENetEventType_ENET_EVENT_TYPE_CONNECT, _ENetEventType_ENET_EVENT_TYPE_DISCONNECT, | ||
| _ENetEventType_ENET_EVENT_TYPE_NONE, _ENetEventType_ENET_EVENT_TYPE_RECEIVE, | ||
| }; | ||
|
|
||
| use crate::{Packet, Peer}; | ||
| use crate::{Host, Packet, PeerID}; | ||
|
|
||
| /// This enum represents an event that can occur when servicing an `EnetHost`. | ||
| /// | ||
| /// Also see the official ENet documentation for more information. | ||
| /// This struct represents an event that can occur when servicing an `Host`. | ||
LeonMatthes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #[derive(Debug)] | ||
| pub enum Event<'a, T> { | ||
| /// This variant represents the connection of a peer, contained in the only field. | ||
| Connect(Peer<'a, T>), | ||
| /// This variant represents the disconnection of a peer, either because it was requested or due to a timeout. | ||
| /// | ||
| /// The disconnected peer is contained in the first field, while the second field contains the user-specified | ||
| /// data for this disconnection. | ||
| Disconnect(Peer<'a, T>, u32), | ||
| /// This variants repersents a packet that was received. | ||
| pub struct Event { | ||
| /// The peer that this event happened on. | ||
| pub peer_id: PeerID, | ||
| /// The type of this event. | ||
| pub kind: EventKind, | ||
| } | ||
|
|
||
| /// The type of an event. | ||
| #[derive(Debug)] | ||
| pub enum EventKind { | ||
| /// Peer has connected. | ||
| Connect, | ||
| /// Peer has disconnected. | ||
| // | ||
| /// The data of the peer will be dropped on the next call to Host::service or when the structure is dropped. | ||
| Disconnect { | ||
| /// The data associated with this event. Usually a reason for disconnection. | ||
| data: u32, | ||
| }, | ||
| /// Peer has received a packet. | ||
| Receive { | ||
| /// The `Peer` that sent the packet. | ||
| sender: Peer<'a, T>, | ||
| /// The channel on which the packet was received. | ||
| /// ID of the channel that the packet was received on. | ||
| channel_id: u8, | ||
| /// The `Packet` that was received. | ||
| /// The received packet. | ||
| packet: Packet, | ||
| }, | ||
| } | ||
|
|
||
| impl<'a, T> Event<'a, T> { | ||
| pub(crate) fn from_sys_event<'b>(event_sys: &'b ENetEvent) -> Option<Event<'a, T>> { | ||
| #[allow(non_upper_case_globals)] | ||
| match event_sys.type_ { | ||
| _ENetEventType_ENET_EVENT_TYPE_NONE => None, | ||
| _ENetEventType_ENET_EVENT_TYPE_CONNECT => { | ||
| Some(Event::Connect(Peer::new(event_sys.peer))) | ||
| } | ||
| _ENetEventType_ENET_EVENT_TYPE_DISCONNECT => Some(Event::Disconnect( | ||
| Peer::new(event_sys.peer), | ||
| event_sys.data, | ||
| )), | ||
| _ENetEventType_ENET_EVENT_TYPE_RECEIVE => Some(Event::Receive { | ||
| sender: Peer::new(event_sys.peer), | ||
| impl Event { | ||
| pub(crate) fn from_sys_event<T>(event_sys: ENetEvent, host: &Host<T>) -> Option<Event> { | ||
| if event_sys.type_ == _ENetEventType_ENET_EVENT_TYPE_NONE { | ||
| return None; | ||
| } | ||
|
|
||
| let peer_id = unsafe { host.peer_id(event_sys.peer) }; | ||
| let kind = match event_sys.type_ { | ||
| _ENetEventType_ENET_EVENT_TYPE_CONNECT => EventKind::Connect, | ||
| _ENetEventType_ENET_EVENT_TYPE_DISCONNECT => EventKind::Disconnect { | ||
| data: event_sys.data, | ||
| }, | ||
| _ENetEventType_ENET_EVENT_TYPE_RECEIVE => EventKind::Receive { | ||
| channel_id: event_sys.channelID, | ||
| packet: Packet::from_sys_packet(event_sys.packet), | ||
| }), | ||
| _ => panic!("unrecognized event type: {}", event_sys.type_), | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| _ => panic!("unexpected event type: {}", event_sys.type_), | ||
| }; | ||
|
|
||
| impl<'a, T> Drop for Event<'a, T> { | ||
| fn drop(&mut self) { | ||
| match self { | ||
| // Seemingly, the lifetime of an ENetPeer ends with the end of the Disconnect event. | ||
| // However, this is *not really clear* in the ENet docs! | ||
| // It looks like the Peer *might* live longer, but not shorter, so it should be safe | ||
| // to destroy the associated data (if any) here. | ||
| Event::Disconnect(peer, _) => peer.set_data(None), | ||
| _ => (), | ||
| } | ||
| Some(Event { peer_id, kind }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.