Struct wstp::LinkServer
source · [−]pub struct LinkServer { /* private fields */ }Expand description
WSTP link server.
This is a wrapper around the
WSLinkServer
C type.
Usage
TODO: Document the two different methods for accepting new Link connections
from this type (waiting and an async callback).
Implementations
sourceimpl LinkServer
impl LinkServer
sourcepub fn bind<A: ToSocketAddrs>(addrs: A) -> Result<Self, Error>
pub fn bind<A: ToSocketAddrs>(addrs: A) -> Result<Self, Error>
Create a new LinkServer bound to the specified address.
Use Link::connect_to_link_server to connect to a LinkServer.
Examples
use wstp::LinkServer;
let server = LinkServer::bind("127.0.0.1:8080").unwrap();sourcepub fn new(port: u16) -> Result<Self, Error>
pub fn new(port: u16) -> Result<Self, Error>
Create a new link server.
It is not possible to register a callback function to accept new link connections
after the link server has been created. Use LinkServer::new_with_callback() if
that functionality is desired.
Use LinkServer::accept() to accept new connections to the link server.
sourcepub fn new_with_callback<F>(port: u16, callback: F) -> Result<Self, Error>where
F: FnMut(Link) + Send + Sync + 'static,
pub fn new_with_callback<F>(port: u16, callback: F) -> Result<Self, Error>where
F: FnMut(Link) + Send + Sync + 'static,
The callback is required to be Send so that it can be called from the link
server’s background thread, which accepts incoming connections.
'static bound
The 'static bound is required to prevent the callback closure from capturing a
reference to non-static data that it might outlive, for example a local variable:
use std::sync::Mutex;
use wstp::LinkServer;
let mut counter = Mutex::new(0);
let server = LinkServer::new_with_callback(
11235,
// Error: the closure may outlive borrowed value `counter`
|_| *counter.lock().unwrap() += 1
);
println!("counter: {}", counter.lock().unwrap());Note that the reasoning for the Send and 'static constraints is similiar to
that for std::thread::spawn(), whose documentation may be a useful
additional reference.
sourcepub fn port(&self) -> u16
pub fn port(&self) -> u16
Returns the TCPIP port number used by this link server.
WSTP C API Documentation: WSPortFromLinkServer
sourcepub fn interface(&self) -> IpAddr
pub fn interface(&self) -> IpAddr
Returns the IP address of the interface used by this link server.
WSTP C API Documentation: WSInterfaceFromLinkServer
sourcepub fn try_interface(&self) -> Result<IpAddr, Error>
pub fn try_interface(&self) -> Result<IpAddr, Error>
Fallible variant of LinkServer::interface().
sourcepub fn close(self)
pub fn close(self)
Close this link server.
This link server will stop accepting new connections, and unbind from the network port it is attached to.
WSTP C API Documentation: WSShutdownLinkServer
sourcepub fn accept(&self) -> Result<Link, Error>
pub fn accept(&self) -> Result<Link, Error>
Accept a new incoming connection to this link server.
This method blocks the current thread indefinitely until a connection is made to the port this link server is bound to.
Use LinkServer::new_with_callback() to create a link server which accepts
connections asyncronously via a callback function.
WSTP C API Documentation: WSWaitForNewLinkFromLinkServer
sourcepub fn incoming(&self) -> Incoming<'_>
pub fn incoming(&self) -> Incoming<'_>
Returns an iterator over the connections being received on this server.
The returned iterator will never return None. Iterating over it is equivalent to
calling LinkServer::accept in a loop.
sourcepub fn raw_link_server(&self) -> WSLinkServer
pub fn raw_link_server(&self) -> WSLinkServer
Returns the raw WSLinkServer
C type wrapped by this LinkServer.