![]() |
VSM C++ SDK
Vehicle Specific Modules SDK
|
Socket processor. More...
#include <socket_processor.h>
Classes | |
class | Stream |
Socket specific stream. More... | |
Public Types | |
typedef std::shared_ptr < Socket_processor > | Ptr |
Pointer type. | |
typedef std::weak_ptr < Socket_processor > | Weak_ptr |
Pointer type. | |
typedef Stream | Socket_listener |
Stream type is used for listener socket type also. More... | |
typedef Callback_proxy< void, std::string, std::string, std::list< addrinfo > , Io_result > | Get_addr_info_handler |
Callback used in Get_addr_info() | |
typedef Callback_proxy< void, Stream::Ref, Io_result > | Listen_handler |
Callback used in Listen() | |
typedef Listen_handler | Connect_handler |
Callback used in Connect() is the same as for listen handler. | |
![]() | |
typedef std::shared_ptr < Request_context > | Ptr |
Pointer type. | |
typedef std::weak_ptr < Request_context > | Weak_ptr |
Pointer type. | |
![]() | |
enum | Type { Type::NONE = 0x0, Type::PROCESSOR = 0x1, Type::COMPLETION_CONTEXT = 0x2, Type::ANY = PROCESSOR | COMPLETION_CONTEXT, Type::TEMPORAL = 0x4, Type::TEMP_COMPLETION_CONTEXT = COMPLETION_CONTEXT | TEMPORAL } |
Container type. More... | |
typedef std::shared_ptr < Request_container > | Ptr |
Pointer type. | |
typedef std::weak_ptr < Request_container > | Weak_ptr |
Pointer type. | |
Public Member Functions | |
Socket_processor (Piped_request_waiter::Ptr piped_waiter=Piped_request_waiter::Create()) | |
Constructor. More... | |
Operation_waiter | Connect (std::string host, std::string service, Connect_handler completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create(), Io_stream::Type sock_type=Io_stream::Type::TCP, Socket_address::Ptr src_addr=nullptr) |
Operation_waiter | Connect (Socket_address::Ptr dest_addr, Connect_handler completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create(), Io_stream::Type sock_type=Io_stream::Type::TCP, Socket_address::Ptr src_addr=nullptr) |
template<class Callback_ptr > | |
Operation_waiter | Accept (Socket_listener::Ref listener, Callback_ptr completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create()) |
Accept incoming TCP/UDP connection. More... | |
Operation_waiter | Get_addr_info (const std::string &host, const std::string &service, addrinfo *hints, Get_addr_info_handler completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create()) |
Interface for nonblocking call to getaddrinfo(). More... | |
Operation_waiter | Listen (const std::string &host, const std::string &service, Listen_handler completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create(), Io_stream::Type sock_type=Io_stream::Type::TCP) |
Operation_waiter | Listen (Socket_address::Ptr addr, Listen_handler completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create(), Io_stream::Type sock_type=Io_stream::Type::TCP) |
Operation_waiter | Bind_udp (Socket_address::Ptr addr, Listen_handler completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create(), bool multicast=false) |
create local UDP endpoint socket and associate stream with it. More... | |
Operation_waiter | Bind_can (std::string interface, std::vector< int > filter_messges, Listen_handler completion_handler, Request_completion_context::Ptr completion_context=Request_temp_completion_context::Create()) |
Create CAN socket and associate stream with it. More... | |
![]() | |
virtual Type | Get_type () const override |
Get this container type. More... | |
![]() | |
Request_container (const std::string &name, Request_waiter::Ptr waiter=Request_waiter::Create()) | |
Create container with default associated waiter. More... | |
bool | Check_type (Type mask) |
Check if the container type conforms the specified characteristic mask. More... | |
void | Submit_request (Request::Ptr request) |
Submit request to this container for further processing or notification handlers invocation. More... | |
void | Submit_request_locked (Request::Ptr request, Request_waiter::Locker locker) |
The same as Submit_request, but with previously acquired lock of the associated waiter. More... | |
int | Process_requests (int requests_limit=0) |
Process all currently queued requests. More... | |
int | Process_requests (std::unique_lock< std::mutex > &lock, int requests_limit=0) |
Process all currently queued requests. More... | |
void | Set_waiter (Request_waiter::Ptr waiter) |
Set request waiter associated with this container. More... | |
Request_waiter::Ptr | Get_waiter () const |
Get request waiter associated with this container. More... | |
const std::string & | Get_name () |
Get the name of the container. More... | |
void | Enable () |
Enable the container. More... | |
void | Disable () |
Disable the container. More... | |
bool | Is_enabled () const |
Check if the container is currently enabled. More... | |
Static Public Member Functions | |
template<typename... Args> | |
static Ptr | Create (Args &&...args) |
Create an instance. More... | |
static Ptr | Create () |
Create processor instance. More... | |
template<typename... Args> | |
static Ptr | Get_instance (Args &&...args) |
Get global or create new processor instance. More... | |
static std::list< Local_interface > | Enumerate_local_interfaces () |
![]() | |
template<typename... Args> | |
static Ptr | Create (Args &&...args) |
Create an instance. More... | |
![]() | |
template<typename... Args> | |
static Ptr | Create (Args &&...args) |
Create an instance. More... | |
Protected Member Functions | |
void | On_enable () override |
Handle processor enabling. More... | |
void | On_disable () override |
Handle disable request. More... | |
void | Process_on_disable (Request::Ptr) |
Process disable in processor context. More... | |
void | On_wait_and_process () override |
Called when default processing loop want to wait for work and process it. More... | |
void | On_write (Write_request::Ptr request, Socket_address::Ptr addr=nullptr) |
void | On_read (Read_request::Ptr request, Socket_address::Ptr addr=nullptr) |
void | On_close (Io_request::Ptr request) |
void | On_set_peer_address (Io_request::Ptr request, Socket_address::Ptr addr) |
void | On_connect (Io_request::Ptr request, Stream::Ptr stream) |
void | On_listen (Io_request::Ptr request, Stream::Ptr stream, Socket_address::Ptr addr) |
void | On_bind_can (Io_request::Ptr request, std::vector< int > filter_messges, Stream::Ptr stream, std::string iface_id) |
void | On_accept (Io_request::Ptr request, Stream::Ptr stream, Stream::Ref listenstream) |
void | On_cancel (Io_request::Ptr request, Io_request::Ptr request_to_cancel) |
void | On_get_addr_info (Io_request::Ptr request, Get_addr_info_handler handler) |
Operation_waiter | Accept_impl (Socket_listener::Ref listener, Request::Handler completion_handler, Request_completion_context::Ptr completion_context, Stream::Ref &stream_arg, Io_result &result_arg) |
![]() | |
void | Processing_loop () |
Request processing loop implementation. More... | |
void | Set_disabled () |
Disable the container and notify its waiter. More... | |
Protected Attributes | |
std::thread | thread |
Worker thread of socket processor. More... | |
Piped_request_waiter::Ptr | piped_waiter |
Request_completion_context::Ptr | completion_ctx |
Default completion context handled by processor thread is used if user did not specify one. | |
![]() | |
Request_waiter::Ptr | waiter |
Waiter associated with this container. More... | |
std::list< Request::Ptr > | request_queue |
Queue of pending requests, i.e. More... | |
Socket processor.
Stream type is used for listener socket type also.
ugcs::vsm::Socket_processor::Socket_processor | ( | Piped_request_waiter::Ptr | piped_waiter = Piped_request_waiter::Create() | ) |
Constructor.
piped_waiter | Request waiter based on a pipe to multiplex socket and request operations using select. |
|
inline |
Accept incoming TCP/UDP connection.
TCP behavior is similar to accept() call. It returns a connected stream with its own socket.
For UDP this can be called on UDP (master) stream which is bound to specific port via Bind_udp() call. It will call completion_handler on each packet which has a new peer address:port tuple. All accepted streams share the same udp socket thus closing the the master stream implicitly closes all "accepted" streams. Read on accepted stream will return only packets from one peer. All Packets which do not belong to accepted streams can be read using from the master stream. All UDP streams use circular buffer for received packets. This is to avoid reads on accepted streams blocking on each other.
Operation_waiter ugcs::vsm::Socket_processor::Bind_can | ( | std::string | interface, |
std::vector< int > | filter_messges, | ||
Listen_handler | completion_handler, | ||
Request_completion_context::Ptr | completion_context = Request_temp_completion_context::Create() |
||
) |
Create CAN socket and associate stream with it.
For now this is linux-only stuff (via SocketCAN API).
How it works: Socket is bound to a given can interface by name. User can issue Read, Write on returned stream.
Write will accept only valid raw CAN frames with length 16. Caller is responsible of producing a valid frame.
Read will return only valid CAN frames. Caller is responsible of parsing payload data from frame.
Read_from and Write_to are not supported.
interface | CAN interface name. Typically "can0" or similar. |
filter_messges | set of can_ids to listen for. Empty means read all. |
See Listen_handler for completion handler parameters.
|
inline |
create local UDP endpoint socket and associate stream with it.
See Listen_handler for completion handler parameters.
There is no such thing as listen() on udp socket. But we make it possible to create a similar workflow as for tcp. How it works: udp socket is bound on given ip_address:service User can issue Read, Read_from or Write_to on returned stream For simple Write() user must first call Set_peer_address() on stream.
addr | local ip address/port for listener socket. Should be numeric. |
multicast | true : bind as multicast listener. On windows it uses SO_REUSEADDR. On Mac it uses SO_REUSEPORT. |
|
inlinestatic |
Create an instance.
|
static |
Create processor instance.
Operation_waiter ugcs::vsm::Socket_processor::Get_addr_info | ( | const std::string & | host, |
const std::string & | service, | ||
addrinfo * | hints, | ||
Get_addr_info_handler | completion_handler, | ||
Request_completion_context::Ptr | completion_context = Request_temp_completion_context::Create() |
||
) |
Interface for nonblocking call to getaddrinfo().
host | Hostname to resolve |
service | Service name to resolve. common port name or numerid port as string. |
hints | Passed as hints argument for getaddrinfo() call. |
|
inlinestatic |
Get global or create new processor instance.
|
overrideprotectedvirtual |
Handle disable request.
Reimplemented from ugcs::vsm::Request_container.
|
overrideprotectedvirtual |
Handle processor enabling.
Reimplemented from ugcs::vsm::Request_container.
|
overrideprotectedvirtual |
Called when default processing loop want to wait for work and process it.
Can be overridden by derived classes if they have non-trivial wait and process implementation.
Reimplemented from ugcs::vsm::Request_container.
|
protected |
Process disable in processor context.
|
protected |
Worker thread of socket processor.