simple-socket library 1.9.0
Loading...
Searching...
No Matches
NET::CANSocket Class Reference

CAN socket class. More...

#include <simple_socket/CANSocket.h>

Inheritance diagram for NET::CANSocket:
Collaboration diagram for NET::CANSocket:

Public Member Functions

void connect (std::string_view interface="")
void bind (std::string_view interface="")
std::string getLocalInterface () const
std::string getForeignInterface () const
int nativeHandle () noexcept
 Return the native descriptor. Ownership remains with the object.
int duplicateNativeHandle () const
 Duplicates the underlaying native socket descriptor.
bool valid () const noexcept
 Returns true if socket is in a usable (non-moved) state and open.
IOResult send (const void *buffer, size_t len)
 send data through a connected socket
IOResult receive (void *buffer, size_t len)
 receive data from a bound socket
IOResult timedReceive (void *buffer, size_t len, int timeout)
 receive data from a bound socket, return after the given timespan
void disconnect ()
 Disconnect and unset any foreign addresses.
bool isConnected () const
 check if the kernel currently has an associated peer address
void shutdown (ShutdownDirection type)
 shutdown the connection in the specified direction
bool peerDisconnected () const
 returns whether a currently associated peer disconnected
bool isBlocking () const
 returns true if socket is in blocking mode
void setBlocking (bool blocking)
 set socket to blocking or non-blocking I/O operations
void setSendBufferSize (int bytes)
 override default send buffer size for this socket
void setReceiveBufferSize (int bytes)
 override default receive buffer size for this socket

Static Public Member Functions

static std::string formatCANID (::canid_t can_id)
 format canid_t into string for logs

Protected Member Functions

 CANSocket (int type, int protocol)
 allows a subclass to create new socket
 CANSocket (CANSocket &&other) noexcept
CANSocket & operator= (CANSocket &&other) noexcept
std::string getInterfaceName (const sockaddr_can &addr) const
 get CAN interface name from socket address structure
int getInterfaceIndex (std::string_view interface) const
 get CAN interface index from interface name
void listen (int backlog=128)
 listen for incoming connections
ConnectResult finishConnect (int timeout=0)
 check if a previously started connected attempt is finished
IOResult sendAll (const void *buffer, size_t len)
 send data through a connected socket
IOResult receiveAll (void *buffer, size_t len)
 receive exactly the requested amount unless the peer disconnects

Protected Attributes

int m_socket
bool m_connectionOriented

Detailed Description

CAN socket class.

Definition at line 10 of file CANSocket.h.

Constructor & Destructor Documentation

◆ CANSocket() [1/2]

NET::CANSocket::CANSocket ( int type,
int protocol )
protected

allows a subclass to create new socket

◆ CANSocket() [2/2]

NET::CANSocket::CANSocket ( CANSocket && other)
protectednoexcept

Member Function Documentation

◆ bind()

void NET::CANSocket::bind ( std::string_view interface = "")

Set the local interface to the specified interface

Parameters
interfacespecifies the CAN interface to bind to
Exceptions
SocketExceptionthrown if setting local path fails

◆ connect()

void NET::CANSocket::connect ( std::string_view interface = "")

Establish a socket connection with the given socket

Parameters
interfacespecifies the CAN interface to connect to
Exceptions
SocketExceptionthrown if unable to establish connection

◆ disconnect()

void NET::SimpleSocket::disconnect ( )
inherited

Disconnect and unset any foreign addresses.

This will ungracefully abort the connection by sending an RST frame. disconnect() should not be used under normal circumstances, as it will terminate the connection OOB, preventing the other end of the communication from retrieving data that would still be available in the OS buffer.

For normal termination of a connection use shutdown().

Exceptions
SocketExceptionthrown if unable to disconnect

◆ duplicateNativeHandle()

int NET::SimpleSocket::duplicateNativeHandle ( ) const
nodiscardinherited

Duplicates the underlaying native socket descriptor.

◆ finishConnect()

ConnectResult NET::SimpleSocket::finishConnect ( int timeout = 0)
protectedinherited

check if a previously started connected attempt is finished

Use this to check if a non-blocking connect() call has completed.

Parameters
timeoutthe timeout in ms after which we stop waiting, connection is still active
Exceptions
SocketException

◆ formatCANID()

std::string NET::CANSocket::formatCANID ( ::canid_t can_id)
static

format canid_t into string for logs

◆ getForeignInterface()

std::string NET::CANSocket::getForeignInterface ( ) const

Get the foreign interface. Call connect() before using this function.

Returns
foreign interface
Exceptions
SocketExceptionthrown if fetch fails

◆ getInterfaceIndex()

int NET::CANSocket::getInterfaceIndex ( std::string_view interface) const
protected

get CAN interface index from interface name

◆ getInterfaceName()

std::string NET::CANSocket::getInterfaceName ( const sockaddr_can & addr) const
protected

get CAN interface name from socket address structure

◆ getLocalInterface()

std::string NET::CANSocket::getLocalInterface ( ) const

Get the local interface (after binding the socket)

Returns
local interface of socket
Exceptions
SocketExceptionthrown if fetch fails

◆ isBlocking()

bool NET::SimpleSocket::isBlocking ( ) const
nodiscardinherited

returns true if socket is in blocking mode

◆ isConnected()

bool NET::SimpleSocket::isConnected ( ) const
nodiscardinherited

check if the kernel currently has an associated peer address

For stream sockets, true means a peer association exists; it does not guarantee that the remote endpoint is alive or that I/O can succeed. A peer that has sent FIN may still be associated with the socket.

For datagram sockets, true only indicates that connect() configured a default peer address; it does not establish a network connection.

Returns
false if getpeername() reports ENOTCONN
Exceptions
SocketExceptionfor other getpeername() errors

◆ listen()

void NET::SimpleSocket::listen ( int backlog = 128)
protectedinherited

listen for incoming connections

listen() can be called on a bound socket. The socket will then go into a passive state and accept incoming connections. To answer to an incoming connection call accept() or timedAccept() on the socket.

The backlog specifies the maximum requested number of pending connections waiting to be accepted.

Parameters
backlogrequested pending connection queue size
Exceptions
SocketException

◆ nativeHandle()

int NET::SimpleSocket::nativeHandle ( )
inlinenoexceptinherited

Return the native descriptor. Ownership remains with the object.

Definition at line 128 of file SimpleSocket.h.

◆ operator=()

CANSocket & NET::CANSocket::operator= ( CANSocket && other)
protectednoexcept

◆ peerDisconnected()

bool NET::SimpleSocket::peerDisconnected ( ) const
nodiscardinherited

returns whether a currently associated peer disconnected

Returns whether the OS currently reports hangup or error for the stream peer. A false result does not guarantee that the remote peer is reachable.

Returns
true if a peer disconnected or is no longer connected

◆ receive()

IOResult NET::SimpleSocket::receive ( void * buffer,
size_t len )
inherited

receive data from a bound socket

receive() can only be used on a socket that called bind() or connect() before. If you try to use receive() on a not bound socket, SocketException will be thrown.

If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.

If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.

If the remote host has closed the connection (on a connection based socket like TCP) receive() will return PeerDisconnected in the result status. If you are using a connectionless protocol (like UDP) you may get DestinationUnavailable in the result status when the peer refuses via ICMP.

Parameters
bufferthe buffer the received data will be written to
lenlength of the provided buffer, receive will not read more than that
Returns
IOResult
Exceptions
SocketExceptionin case an error occured

◆ receiveAll()

IOResult NET::SimpleSocket::receiveAll ( void * buffer,
size_t len )
protectedinherited

receive exactly the requested amount unless the peer disconnects

receiveAll() repeatedly calls receive() until len bytes have been received. This is useful for fixed-size data on TCP, where one receive() call is not guaranteed to return all bytes sent by the peer.

If the peer closes or resets the connection before len bytes arrive, the function returns an error status with the number of bytes received so far.

Parameters
bufferbuffer the received data will be written to
lennumber of bytes to receive
Returns
IOResult
Exceptions
SocketExceptionon receive errors other than peer loss

◆ send()

IOResult NET::SimpleSocket::send ( const void * buffer,
size_t len )
inherited

send data through a connected socket

send() can only be used on a socket that called connect() before. If you try to use send() on a not connected socket, SocketException will be thrown.

If you are using a stream oriented socket (like TCPSocket), the operating system is allowed to send only a part of the packet you told it to send, so send() will return the number of bytes actually sent. It is your responsibility to resend the data not sent by send()

If you are using a datagram oriented socket (like UDPSocket or SCTPSocket) the operating system will only send and receive complete datagrams, but send() will fail if you are trying to send too much data. In that case SocketException will be thrown.

Parameters
bufferdata to be send
lenlength of the data to be sent
Returns
IOResult
Exceptions
SocketExceptionif sending went wrong

◆ sendAll()

IOResult NET::SimpleSocket::sendAll ( const void * buffer,
size_t len )
protectedinherited

send data through a connected socket

sendAll() can only be used on a socket that called connect() before. If you try to use sendAll() on a unconnected socket, a SocketException will be thrown.

When using a stream oriented socket, the operating system is allowed to send only a part of the data you requested it to send. Because of this, you would have to check the number of sent bytes when using send(), and resend unsent data manually. sendAll() takes that responsibility from you and resends as long as it needs, to completely send the data given to it.

Parameters
bufferdata to be send
lenlength of the data to be sent
Returns
IOResult
Exceptions
SocketException

◆ setBlocking()

void NET::SimpleSocket::setBlocking ( bool blocking)
inherited

set socket to blocking or non-blocking I/O operations

A non-blocking socket will return from all I/O operations right away. The IOStatus WouldBlock is used to signify no work can be done at the moment. An event handler can use poll(), epoll(), etc. to monitor the socket for readyness for the next operation.

All timed operations provided by socket classes are not affected. Timed calls wait up to timeout ms for even with non-blocking sockets.

◆ setReceiveBufferSize()

void NET::SimpleSocket::setReceiveBufferSize ( int bytes)
inherited

override default receive buffer size for this socket

◆ setSendBufferSize()

void NET::SimpleSocket::setSendBufferSize ( int bytes)
inherited

override default send buffer size for this socket

◆ shutdown()

void NET::SimpleSocket::shutdown ( ShutdownDirection type)
inherited

shutdown the connection in the specified direction

This will gracefully end a connection by sending a FIN frame. Use this to end a normal communication cycle. To abort a connection see disconnect().

Depending on the specified ShutdownDirection, calls for that direction will stop working. Use this function if you want to have more control than just destroing the socket. It allows you to cut the connection in one direction, or both.

If you use shutdown() on an unconnected socket, the corresponding calls will simply stop working.

Parameters
typethe ShutdownDirection that be used
Exceptions
SocketExceptionin case an error occured
Examples
TCP_Server.cpp.

◆ timedReceive()

IOResult NET::SimpleSocket::timedReceive ( void * buffer,
size_t len,
int timeout )
inherited

receive data from a bound socket, return after the given timespan

timedReceive() can only be used on a socket that called bind() or connect before. If you try to use receive() on a not bound socket, SocketException will be thrown.

If using a stream oriented Socket, receive can return a part of a received messge, e.g. if you send 100 bytes, it's possible you will receive 50 bytes two times in a row. However, the order of the sent data will be preserved.

If you are using a datagram oriented sockets, you will only receive whole datagrams. But beware of using a too small buffer. If the receive buffer is too small for the received datagram, the data you didn't read in the receive call will be discared.

If the remote host has closed the connection (on a connection based socket like TCP) receive() will return PeerDisconnected in the result status. If you are using a connectionless protocol (like UDP) you may get DestinationUnavailable in the result status when the peer refuses via ICMP.

Parameters
bufferthe buffer the received data will be written to
lenlength of the provided buffer, receive will not read more than that
timeoutthe timeout in ms after which receive will give up and return
Returns
IOResult
Exceptions
SocketExceptionin case an error occured
Examples
TCP_Client.cpp, and TCP_Client_Reconnect.cpp.

◆ valid()

bool NET::SimpleSocket::valid ( ) const
noexceptinherited

Returns true if socket is in a usable (non-moved) state and open.

Member Data Documentation

◆ m_connectionOriented

bool NET::SimpleSocket::m_connectionOriented
protectedinherited

Definition at line 395 of file SimpleSocket.h.

◆ m_socket

int NET::SimpleSocket::m_socket
protectedinherited

Definition at line 394 of file SimpleSocket.h.


The documentation for this class was generated from the following file: