simple-socket library 1.2.0
NET::CANRawSocket Class Reference

Raw CAN socket class. More...

#include <CANRawSocket.h>

Public Types

enum  ShutdownDirection { STOP_SEND = SHUT_WR , STOP_RECEIVE = SHUT_RD , STOP_BOTH = SHUT_RDWR }
 

Public Member Functions

void bind (std::string_view interface="")
 
 CANRawSocket ()
 
void connect (std::string_view interface="")
 
void disconnect ()
 Disconnect and unset any foreign addresses.
 
std::string getForeignInterface () const
 
std::string getLocalInterface () const
 
int nativeHandle ()
 return the native handle of the open socket
 
bool peerDisconnected () const
 returns whether a peer disconnected
 
int receive (void *buffer, size_t len)
 receive data from a bound socket
 
int receiveFrom (void *buffer, size_t len, std::string &interface)
 
int send (const void *buffer, size_t len)
 send data through a connected socket
 
void sendTo (const void *buffer, size_t len, std::string_view interface)
 
void setErrorFilter (const can_filter *filters, size_t len)
 
void setLocalLoopback (bool enable)
 
void setReceiveFilter (const can_filter *filters, size_t len)
 
void setReceiveOwnMessages (bool enable)
 
void shutdown (ShutdownDirection type)
 shutdown the connection in the specified direction
 
int timedReceive (void *buffer, size_t len, int timeout)
 receive data from a bound socket, return after the given timespan
 
int timedReceiveFrom (void *buffer, size_t len, std::string &interface, int timeout)
 

Protected Types

enum  SocketDomain { INTERNET = PF_INET , UNIX = PF_LOCAL , CAN = PF_CAN }
 
enum  SocketType { RAW = SOCK_RAW , STREAM = SOCK_STREAM , DATAGRAM = SOCK_DGRAM }
 

Protected Member Functions

int getInterfaceIndex (std::string_view interface) const
 get CAN interface index from interface name
 
std::string getInterfaceName (const sockaddr_can &addr) const
 get CAN interface name from socket address structure
 

Protected Attributes

bool m_peerDisconnected
 
int m_socket
 

Detailed Description

Raw CAN socket class.

Definition at line 10 of file CANRawSocket.h.

Member Enumeration Documentation

◆ 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 51 of file SimpleSocket.h.

◆ SocketDomain

enum NET::SimpleSocket::SocketDomain
protectedinherited
Enumerator
INTERNET 
UNIX 
CAN 

Definition at line 183 of file SimpleSocket.h.

◆ SocketType

enum NET::SimpleSocket::SocketType
protectedinherited
Enumerator
RAW 
STREAM 
DATAGRAM 

Definition at line 190 of file SimpleSocket.h.

Constructor & Destructor Documentation

◆ CANRawSocket()

CANRawSocket::CANRawSocket ( )

Construct a raw CAN socket

Exceptions
SocketExceptionthrown if unable to create the socket

Definition at line 11 of file CANRawSocket.cpp.

Member Function Documentation

◆ sendTo()

void CANRawSocket::sendTo ( const void * buffer,
size_t len,
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
bufferpointer to CAN frame structure to be send
lensize of the can frame structure
interfacespecifies the CAN interface to send used for sending
Exceptions
SocketExceptionthrown if unable to send frame

Definition at line 15 of file CANRawSocket.cpp.

◆ receiveFrom()

int CANRawSocket::receiveFrom ( void * buffer,
size_t len,
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
bufferpointer to CAN frame structure
lensize of the can frame structure
interfacespecifies the CAN interface used for reception
Returns
number of bytes received
Exceptions
SocketExceptionthrown if unable to receive datagram

Definition at line 28 of file CANRawSocket.cpp.

◆ timedReceiveFrom()

int CANRawSocket::timedReceiveFrom ( void * buffer,
size_t len,
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
bufferpointer to CAN frame structure
lensize of the can frame structure
interfacespecifies the CAN interface used for reception
timeouttimeout in milliseconds
Returns
number of bytes received, 0 on timeout
Exceptions
SocketExceptionthrown if unable to receive datagram

Definition at line 41 of file CANRawSocket.cpp.

◆ setReceiveFilter()

void CANRawSocket::setReceiveFilter ( const can_filter * filters,
size_t len )
Parameters
filters
lennumber of filters to set
Exceptions
SocketException

Definition at line 61 of file CANRawSocket.cpp.

◆ setErrorFilter()

void CANRawSocket::setErrorFilter ( const can_filter * filters,
size_t len )
Parameters
filters
lennumber of filters to set
Exceptions
SocketException

Definition at line 71 of file CANRawSocket.cpp.

◆ setLocalLoopback()

void CANRawSocket::setLocalLoopback ( bool enable)
Parameters
enable
Exceptions
SocketException

Definition at line 81 of file CANRawSocket.cpp.

◆ setReceiveOwnMessages()

void CANRawSocket::setReceiveOwnMessages ( bool enable)
Parameters
enable
Exceptions
SocketException

Definition at line 92 of file CANRawSocket.cpp.

◆ connect()

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

Establish a socket connection with the given socket

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

Definition at line 16 of file CANSocket.cpp.

◆ bind()

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

Set the local interface to the specified interface

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

Definition at line 26 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
SocketExceptionthrown if fetch fails

Definition at line 36 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
SocketExceptionthrown if fetch fails

Definition at line 47 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 58 of file CANSocket.cpp.

◆ getInterfaceIndex()

int CANSocket::getInterfaceIndex ( std::string_view interface) const
protectedinherited

get CAN interface index from interface name

Definition at line 69 of file CANSocket.cpp.

◆ nativeHandle()

int NET::SimpleSocket::nativeHandle ( )
inlineinherited

return the native handle of the open socket

Definition at line 61 of file SimpleSocket.h.

◆ send()

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
bufferdata to be send
lenlength of the data to be sent
Returns
number of bytes sent
Exceptions
SocketExceptionif sending went wrong

Definition at line 44 of file SimpleSocket.cpp.

◆ receive()

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
bufferthe buffer the received data will be written to
lenlength of the provided buffer, receive will not read more than that
Returns
number of bytes received
Exceptions
SocketExceptionin case an error occured

Definition at line 62 of file SimpleSocket.cpp.

◆ timedReceive()

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
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
number of bytes received, 0 on timeout
Exceptions
SocketExceptionin case an error occured

Definition at line 69 of file SimpleSocket.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
SocketExceptionthrown if unable to disconnect

Definition at line 93 of file SimpleSocket.cpp.

◆ shutdown()

void 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

Definition at line 108 of file SimpleSocket.cpp.

◆ peerDisconnected()

bool SimpleSocket::peerDisconnected ( ) const
inherited

returns whether a peer disconnected

Will only work if you use a connection oriented, connected socket. Returns true if the peer disconnected. Use this function after a call to receive, returned 0 received bytes.

Returns
true if a peer disconnected

Definition at line 114 of file SimpleSocket.cpp.

Member Data Documentation

◆ m_socket

int NET::SimpleSocket::m_socket
protectedinherited

Definition at line 204 of file SimpleSocket.h.

◆ m_peerDisconnected

bool NET::SimpleSocket::m_peerDisconnected
protectedinherited

Definition at line 205 of file SimpleSocket.h.


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