Struct carrier_pigeon::Server
source · [−]pub struct Server { /* private fields */ }
Expand description
A server.
Listens on a address and port, allowing for clients to connect. Newly connected clients will
be given a client ID (CId) starting at 1
that is unique for the session.
This will manage multiple connections to clients. Each connection will have a TCP and UDP connection on the same address and port.
Implementations
sourceimpl Server
impl Server
sourcepub fn new(
listen_addr: SocketAddr,
parts: MsgTableParts,
config: Config
) -> Result<Self>
pub fn new(
listen_addr: SocketAddr,
parts: MsgTableParts,
config: Config
) -> Result<Self>
sourcepub fn disconnect<T: Any + Send + Sync>(
&mut self,
discon_msg: &T,
cid: CId
) -> Result<()>
pub fn disconnect<T: Any + Send + Sync>(
&mut self,
discon_msg: &T,
cid: CId
) -> Result<()>
Disconnects from the given cid
. You should always disconnect all clients before dropping
the server to let the clients know that you intentionally disconnected. The discon_msg
allows you to give a reason for the disconnect.
sourcepub fn handle_new_con<C: Any + Send + Sync, R: Any + Send + Sync>(
&mut self,
hook: impl FnMut(CId, C) -> (bool, R)
) -> bool
pub fn handle_new_con<C: Any + Send + Sync, R: Any + Send + Sync>(
&mut self,
hook: impl FnMut(CId, C) -> (bool, R)
) -> bool
Handles all available new connection attempts in a loop, calling the given hook for each.
The hook function should return (should_accept, response_msg)
.
Types C
and R
need to match the C
and R
types that you passed into
MsgTable::build()
.
Returns whether a connection was handled.
sourcepub fn handle_new_cons<C: Any + Send + Sync, R: Any + Send + Sync>(
&mut self,
hook: impl FnMut(CId, C) -> (bool, R)
) -> u32
pub fn handle_new_cons<C: Any + Send + Sync, R: Any + Send + Sync>(
&mut self,
hook: impl FnMut(CId, C) -> (bool, R)
) -> u32
Handles all available new connection attempts in a loop, calling the given hook for each.
The hook function should return (should_accept, response_msg)
.
Types C
and R
need to match the C
and R
types that you passed into
MsgTable::build()
.
Returns the number of handled connections.
sourcepub fn handle_disconnect(&mut self, hook: impl FnMut(CId, Status)) -> bool
pub fn handle_disconnect(&mut self, hook: impl FnMut(CId, Status)) -> bool
Handles a single disconnect, if there is one available to handle.
If there is no disconnects to handle, hook
will not be called.
Returns weather it handled a disconnect.
sourcepub fn handle_disconnects(&mut self, hook: impl FnMut(CId, Status)) -> u32
pub fn handle_disconnects(&mut self, hook: impl FnMut(CId, Status)) -> u32
Handles all remaining disconnects.
Returns the number of disconnects handled.
sourcepub fn send_to<T: Any + Send + Sync>(&self, cid: CId, msg: &T) -> Result<()>
pub fn send_to<T: Any + Send + Sync>(&self, cid: CId, msg: &T) -> Result<()>
Sends a message to the CId
cid
.
sourcepub fn broadcast<T: Any + Send + Sync>(&self, msg: &T) -> Result<()>
pub fn broadcast<T: Any + Send + Sync>(&self, msg: &T) -> Result<()>
Broadcasts a message to all connected clients.
sourcepub fn send_spec<T: Any + Send + Sync>(
&self,
spec: CIdSpec,
msg: &T
) -> Result<()>
pub fn send_spec<T: Any + Send + Sync>(
&self,
spec: CIdSpec,
msg: &T
) -> Result<()>
Sends a message to all CId
s that match spec
.
sourcepub fn recv<'s, T: Any + Send + Sync>(
&'s self
) -> impl Iterator<Item = NetMsg<'_, T>> + 's
pub fn recv<'s, T: Any + Send + Sync>(
&'s self
) -> impl Iterator<Item = NetMsg<'_, T>> + 's
Gets an iterator for the messages of type T
.
Make sure to call recv_msgs()
before calling this.
Panics
Panics if the type T
was not registered.
For a non-panicking version, see try_recv().
sourcepub fn try_recv<'s, T: Any + Send + Sync>(
&'s self
) -> Option<impl Iterator<Item = NetMsg<'_, T>> + 's>
pub fn try_recv<'s, T: Any + Send + Sync>(
&'s self
) -> Option<impl Iterator<Item = NetMsg<'_, T>> + 's>
Gets an iterator for the messages of type T
.
Make sure to call recv_msgs()
before calling this.
Returns None
if the type T
was not registered.
sourcepub fn recv_spec<T: Any + Send + Sync>(
&self,
spec: CIdSpec
) -> impl Iterator<Item = NetMsg<'_, T>> + '_
pub fn recv_spec<T: Any + Send + Sync>(
&self,
spec: CIdSpec
) -> impl Iterator<Item = NetMsg<'_, T>> + '_
Gets an iterator for the messages of type T
that have been received from CId
s that
match spec
.
Make sure to call recv_msgs()
Panics
Panics if the type T
was not registered.
For a non-panicking version, see try_recv_spec().
sourcepub fn try_recv_spec<T: Any + Send + Sync>(
&self,
spec: CIdSpec
) -> Option<impl Iterator<Item = NetMsg<'_, T>> + '_>
pub fn try_recv_spec<T: Any + Send + Sync>(
&self,
spec: CIdSpec
) -> Option<impl Iterator<Item = NetMsg<'_, T>> + '_>
Gets an iterator for the messages of type T
that have been received from CId
s that
match spec
.
Make sure to call recv_msgs()
Returns None
if the type T
was not registered.
sourcepub fn recv_msgs(&mut self) -> u32
pub fn recv_msgs(&mut self) -> u32
Receives the messages from the connections. This should be done before calling recv<T>()
.
When done in a game loop, you should call clear_msgs()
, then recv_msgs()
before default
time. This will clear the messages between frames.
sourcepub fn clear_msgs(&mut self)
pub fn clear_msgs(&mut self)
Clears messages from the buffer.
sourcepub fn listen_addr(&self) -> SocketAddr
pub fn listen_addr(&self) -> SocketAddr
Gets the address that the server is listening on.
sourcepub fn alive(&self, cid: CId) -> bool
pub fn alive(&self, cid: CId) -> bool
Returns whether the connection of the given CId
is alive.
sourcepub fn valid_tid(&self, tid: TypeId) -> bool
pub fn valid_tid(&self, tid: TypeId) -> bool
Returns whether a message of type tid
can be sent.
sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
The number of active connections. To ensure an accurate count, it is best to call this
after calling handle_disconnects()
.
Auto Trait Implementations
impl !RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl !UnwindSafe for Server
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more