Skip to main content

TcpSession

Struct TcpSession 

Source
pub struct TcpSession { /* private fields */ }
Expand description

A connected FCI command socket.

TcpSession is Sync: one thread may block in TcpSession::blocking_receive_response while another calls TcpSession::send_request, which is how Robot::stop() interrupts a running motion in libfranka.

Implementations§

Source§

impl TcpSession

Source

pub fn connect( addr: SocketAddr, timeout: Duration, layout: HeaderLayout, ) -> FrankaResult<TcpSession>

Connects to addr and configures the socket the way franka::Network’s constructor does: blocking, 1 s connect timeout, TCP keepalive with a 1 s idle time, 3 probes and a 1 s interval (best effort — libfranka swallows failures of the three setOption calls).

The receive timeout is set to POLL_TIMEOUT so that a blocking receive wakes every 10 ms exactly like libfranka’s poll(kTimeout, SELECT_READ).

Error texts are libfranka’s, verbatim.

Source

pub fn layout(&self) -> HeaderLayout

The header layout this session frames with.

Source

pub fn local_addr(&self) -> FrankaResult<SocketAddr>

Local address of the command socket (used by tests).

Source

pub fn send_request(&self, command: u32, payload: &[u8]) -> FrankaResult<u32>

Sends a request and returns its command_id.

The header’s size is header_len + payload.len(), i.e. it counts the header, exactly like CommandHeader::size in libfranka. Command ids start at 0 and increase by one per request (Network::command_id_).

Source

pub fn blocking_receive_response( &self, command_id: u32, ) -> FrankaResult<Vec<u8>>

Blocks until the response with command_id has been received and returns the whole message, header included.

Port of Network::tcpBlockingReceiveResponse: lock, read for at most 10 ms, look the id up, unlock, std::this_thread::yield(), repeat.

Source

pub fn try_receive_response( &self, command_id: u32, ) -> FrankaResult<Option<Vec<u8>>>

Tries to claim the response with command_id without blocking.

Port of Network::tcpReceiveResponse: a try_lock that gives up immediately if another thread holds the socket, then a zero-timeout read. Returns Ok(None) when the response has not arrived yet.

Source

pub fn is_alive(&self) -> bool

Whether the socket is free of pending errors (Network::isTcpSocketAlive).

Source

pub fn throw_if_connection_closed(&self) -> FrankaResult<()>

Returns Err(Network("libfranka: server closed connection")) when the peer performed an orderly shutdown (Network::tcpThrowIfConnectionClosed).

Like libfranka this is a no-op when another thread holds the socket lock, and it peeks rather than consuming, so a queued response is not lost.

Source

pub fn shutdown(&self)

Shuts the command socket down in both directions, best effort.

libfranka calls tcp_socket_.shutdown() whenever a UDP receive fails (network.h:146) so that the next TCP operation fails fast instead of waiting on a connection the robot has already given up on; crate::network::Network does the same.

Trait Implementations§

Source§

impl Debug for TcpSession

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for TcpSession

Source§

fn drop(&mut self)

franka::Network::~Network shuts the socket down; dropping the TcpStream closes it.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.