Raw CAN socket class.
More...
#include <simple_socket/CANRawSocket.h>
|
| | CANRawSocket () |
| void | send (const ::can_frame &frame) |
| | (overload) send can_frame through a connected socket
|
| int | receive (::can_frame &frame) |
| | (overload) receive can frame from a bound socket
|
| int | timedReceive (::can_frame &frame, int timeout) |
| | (overload) receive can frame from a bound socket with timeout
|
| void | sendTo (const ::can_frame &frame, std::string_view interface) |
| int | receiveFrom (::can_frame &frame, std::string &interface) |
| int | timedReceiveFrom (::can_frame &frame, std::string &interface, int timeout) |
| void | setReceiveFilter (const ::can_filter *filters, size_t len) |
| void | setErrorFilter (::can_err_mask_t filter) |
| void | setLocalLoopback (bool enable) |
| void | setReceiveOwnMessages (bool enable) |
| 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 handle of the open socket
|
| int | send (const void *buffer, size_t len) |
| | send data through a connected socket
|
| int | receive (void *buffer, size_t len) |
| | receive data from a bound socket
|
| int | 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.
|
| void | shutdown (ShutdownDirection type) |
| | shutdown the connection in the specified direction
|
| bool | peerDisconnected () const noexcept |
| | returns whether a currently associated peer disconnected
|
|
| static std::string | formatCANID (::canid_t can_id) |
| | format canid_t into string for logs
|
|
| 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=5) |
| | listen for incoming connections
|
| int | sendAll (const void *buffer, size_t len) |
| | send data through a connected socket
|
| int | receiveAll (void *buffer, size_t len) |
| | receive exactly the requested amount unless the peer disconnects
|
| int | accept () const |
| | allows stream sockets to accept connections into Handles
|
| int | timedAccept (int timeout) const |
| | allows stream sockets timed accept
|
Raw CAN socket class.
Definition at line 9 of file CANRawSocket.h.
◆ ShutdownDirection
| Enumerator |
|---|
| STOP_SEND | disable all variants of send() on the socket, receive calls still work
|
| STOP_RECEIVE | disable all variants of receive() on the socket, send calls still work
|
| STOP_BOTH | disable all send() and receive() calls on the socket
|
Definition at line 57 of file SimpleSocket.h.
◆ CANRawSocket()
| CANRawSocket::CANRawSocket |
( |
| ) |
|
◆ accept()
| int SimpleSocket::accept |
( |
| ) |
const |
|
protectedinherited |
allows stream sockets to accept connections into Handles
Definition at line 144 of file SimpleSocket.cpp.
◆ bind()
| void CANSocket::bind |
( |
std::string_view | interface = "" | ) |
|
|
inherited |
Set the local interface to the specified interface
- Parameters
-
| interface | specifies the CAN interface to bind to |
- Exceptions
-
Definition at line 28 of file CANSocket.cpp.
◆ connect()
| void CANSocket::connect |
( |
std::string_view | interface = "" | ) |
|
|
inherited |
Establish a socket connection with the given socket
- Parameters
-
| interface | specifies the CAN interface to connect to |
- Exceptions
-
Definition at line 18 of file CANSocket.cpp.
◆ disconnect()
| void 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
-
Definition at line 112 of file SimpleSocket.cpp.
◆ formatCANID()
| std::string CANSocket::formatCANID |
( |
::canid_t | can_id | ) |
|
|
staticinherited |
format canid_t into string for logs
Definition at line 91 of file CANSocket.cpp.
◆ getForeignInterface()
| std::string CANSocket::getForeignInterface |
( |
| ) |
const |
|
inherited |
Get the foreign interface. Call connect() before using this function.
- Returns
- foreign interface
- Exceptions
-
Definition at line 49 of file CANSocket.cpp.
◆ getInterfaceIndex()
| int CANSocket::getInterfaceIndex |
( |
std::string_view | interface | ) |
const |
|
protectedinherited |
get CAN interface index from interface name
Definition at line 71 of file CANSocket.cpp.
◆ getInterfaceName()
| std::string CANSocket::getInterfaceName |
( |
const sockaddr_can & | addr | ) |
const |
|
protectedinherited |
get CAN interface name from socket address structure
Definition at line 60 of file CANSocket.cpp.
◆ getLocalInterface()
| std::string CANSocket::getLocalInterface |
( |
| ) |
const |
|
inherited |
Get the local interface (after binding the socket)
- Returns
- local interface of socket
- Exceptions
-
Definition at line 38 of file CANSocket.cpp.
◆ listen()
| void SimpleSocket::listen |
( |
int | backlog = 5 | ) |
|
|
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 is a buffer for incoming connections. If backlog is set to 0, the number of incoming connections is only limited by the operating system. If backlog is set to a value > 0, only the upper limit of connections is set, the OS can limit the number further.
After starting to listen, use accept to accept incoming connections.
- Parameters
-
| backlog | upper limit of pending incoming connections |
- Exceptions
-
Definition at line 137 of file SimpleSocket.cpp.
◆ nativeHandle()
| int NET::SimpleSocket::nativeHandle |
( |
| ) |
|
|
inlinenoexceptinherited |
return the native handle of the open socket
Definition at line 68 of file SimpleSocket.h.
◆ peerDisconnected()
| bool SimpleSocket::peerDisconnected |
( |
| ) |
const |
|
noexceptinherited |
returns whether a currently associated peer disconnected
For stream sockets this is set when the peer closes or resets the connection. Use this function after a call to receive, returned 0 received bytes.
For connected datagram sockets it may also be set when the network reports that the configured peer is unreachable.
The flag is cleared by a successful connect().
- Returns
- true if a peer disconnected
- Examples
- TCP_Client.cpp.
Definition at line 132 of file SimpleSocket.cpp.
◆ receive() [1/2]
| int CANRawSocket::receive |
( |
::can_frame & | frame | ) |
|
(overload) receive can frame from a bound socket
Definition at line 20 of file CANRawSocket.cpp.
◆ receive() [2/2]
| int 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 or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
- Parameters
-
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
- Returns
- number of bytes received
- Exceptions
-
Definition at line 70 of file SimpleSocket.cpp.
◆ receiveAll()
| int 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 the number of bytes received so far. In that case peerDisconnected() is set by the underlying receive() call.
- Parameters
-
| buffer | buffer the received data will be written to |
| len | number of bytes to receive |
- Returns
- number of bytes received
- Exceptions
-
Definition at line 188 of file SimpleSocket.cpp.
◆ receiveFrom()
| int CANRawSocket::receiveFrom |
( |
::can_frame & | frame, |
|
|
std::string & | interface ) |
Read one CAN frame from this socket.
In order to receive data, the socket has to be bound at least to a specific interface. This is done by explicitly binding the socket, or sending data through the socket.
- Parameters
-
| frame | CAN frame structure to be send |
| interface | specifies the CAN interface used for reception |
- Returns
- number of bytes received
- Exceptions
-
Definition at line 43 of file CANRawSocket.cpp.
◆ send() [1/2]
| void CANRawSocket::send |
( |
const ::can_frame & | frame | ) |
|
(overload) send can_frame through a connected socket
Definition at line 15 of file CANRawSocket.cpp.
◆ send() [2/2]
| int 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
-
| buffer | data to be send |
| len | length of the data to be sent |
- Returns
- number of bytes sent
- Exceptions
-
Definition at line 50 of file SimpleSocket.cpp.
◆ sendAll()
| int 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 bit 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
-
| buffer | data to be send |
| len | length of the data to be sent |
- Returns
- number of bytes sent
- Exceptions
-
Definition at line 175 of file SimpleSocket.cpp.
◆ sendTo()
| void CANRawSocket::sendTo |
( |
const ::can_frame & | frame, |
|
|
std::string_view | interface ) |
Send the given CAN frame using the specified interface.
Can be substituted with send() if the socket is first connected to any interface. If sendTo() is used on a connected socket, the destination parameters will be ignored, and the datagram will be sent to the connected socket.
Like send(), sendTo() blocks until the message was sent.
- Parameters
-
| frame | CAN frame structure to be send |
| interface | specifies the CAN interface to send used for sending |
- Exceptions
-
Definition at line 30 of file CANRawSocket.cpp.
◆ setErrorFilter()
| void CANRawSocket::setErrorFilter |
( |
::can_err_mask_t | filter | ) |
|
- Parameters
-
| filter | Linux SocketCAN error-class mask |
- Exceptions
-
Definition at line 85 of file CANRawSocket.cpp.
◆ setLocalLoopback()
| void CANRawSocket::setLocalLoopback |
( |
bool | enable | ) |
|
◆ setReceiveFilter()
| void CANRawSocket::setReceiveFilter |
( |
const ::can_filter * | filters, |
|
|
size_t | len ) |
- Parameters
-
| filters | Linux SocketCAN receive filters array |
| len | number of filters to set |
- Exceptions
-
Definition at line 79 of file CANRawSocket.cpp.
◆ setReceiveOwnMessages()
| void CANRawSocket::setReceiveOwnMessages |
( |
bool | enable | ) |
|
◆ shutdown()
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
-
- Exceptions
-
- Examples
- TCP_Server.cpp.
Definition at line 126 of file SimpleSocket.cpp.
◆ timedAccept()
| int SimpleSocket::timedAccept |
( |
int | timeout | ) |
const |
|
protectedinherited |
◆ timedReceive() [1/2]
| int CANRawSocket::timedReceive |
( |
::can_frame & | frame, |
|
|
int | timeout ) |
(overload) receive can frame from a bound socket with timeout
Definition at line 25 of file CANRawSocket.cpp.
◆ timedReceive() [2/2]
| int 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 or SCTP) receive() will return 0. If you are using a connectionless protocol (like UDP) there is no way to determine wheter the connection has been closed by the remote host or not.
- Parameters
-
| buffer | the buffer the received data will be written to |
| len | length of the provided buffer, receive will not read more than that |
| timeout | the timeout in ms after which receive will give up and return |
- Returns
- number of bytes received, 0 on timeout
- Exceptions
-
- Examples
- TCP_Client.cpp.
Definition at line 89 of file SimpleSocket.cpp.
◆ timedReceiveFrom()
| int CANRawSocket::timedReceiveFrom |
( |
::can_frame & | frame, |
|
|
std::string & | interface, |
|
|
int | timeout ) |
Read one CAN frame from this socket. If no interface has received a frame before the timeout runs out, the function will return without changing the buffer.
In order to receive data, the socket has to be bound at least to a specific interface. This is done by explicitly binding the socket, or sending data through the socket.
- Parameters
-
| frame | CAN frame structure to be send |
| interface | specifies the CAN interface used for reception |
| timeout | timeout in milliseconds |
- Returns
- number of bytes received, 0 on timeout
- Exceptions
-
Definition at line 56 of file CANRawSocket.cpp.
◆ m_connectionOriented
| bool NET::SimpleSocket::m_connectionOriented |
|
protectedinherited |
◆ m_peerDisconnected
| bool NET::SimpleSocket::m_peerDisconnected |
|
protectedinherited |
◆ m_socket
| int NET::SimpleSocket::m_socket |
|
protectedinherited |
The documentation for this class was generated from the following files: