VSM C++ SDK
Vehicle Specific Modules SDK
ugcs::vsm::Socket_processor Class Reference

Socket processor. More...

#include <socket_processor.h>

Inheritance diagram for ugcs::vsm::Socket_processor:
ugcs::vsm::Request_context< is_processor > ugcs::vsm::Request_container

Classes

class  Stream
 Socket specific stream. More...
 

Public Types

typedef std::shared_ptr< Socket_processorPtr
 Pointer type.
 
typedef std::weak_ptr< Socket_processorWeak_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_resultGet_addr_info_handler
 Callback used in Get_addr_info()
 
typedef Callback_proxy< void, Stream::Ref, Io_resultListen_handler
 Callback used in Listen()
 
typedef Listen_handler Connect_handler
 Callback used in Connect() is the same as for listen handler.
 
- Public Types inherited from ugcs::vsm::Request_context< is_processor >
typedef std::shared_ptr< Request_contextPtr
 Pointer type.
 
typedef std::weak_ptr< Request_contextWeak_ptr
 Pointer type.
 
- Public Types inherited from ugcs::vsm::Request_container
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_containerPtr
 Pointer type.
 
typedef std::weak_ptr< Request_containerWeak_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...
 
- Public Member Functions inherited from ugcs::vsm::Request_context< is_processor >
virtual Type Get_type () const override
 Get this container type. More...
 
- Public Member Functions inherited from ugcs::vsm::Request_container
 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_interfaceEnumerate_local_interfaces ()
 
- Static Public Member Functions inherited from ugcs::vsm::Request_context< is_processor >
template<typename... Args>
static Ptr Create (Args &&...args)
 Create an instance. More...
 
- Static Public Member Functions inherited from ugcs::vsm::Request_container
template<typename... Args>
static Ptr Create (Args &&...args)
 Create an instance. More...
 

Protected Member Functions

virtual void On_enable () override
 Handle processor enabling. More...
 
virtual void On_disable () override
 Handle disable request. More...
 
void Process_on_disable (Request::Ptr)
 Process disable in processor context. More...
 
virtual 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)
 
- Protected Member Functions inherited from ugcs::vsm::Request_container
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.
 
- Protected Attributes inherited from ugcs::vsm::Request_container
Request_waiter::Ptr waiter
 Waiter associated with this container. More...
 
std::list< Request::Ptrrequest_queue
 Queue of pending requests, i.e. More...
 

Detailed Description

Socket processor.

Member Typedef Documentation

Stream type is used for listener socket type also.

Constructor & Destructor Documentation

ugcs::vsm::Socket_processor::Socket_processor ( Piped_request_waiter::Ptr  piped_waiter = Piped_request_waiter::Create())

Constructor.

Parameters
piped_waiterRequest waiter based on a pipe to multiplex socket and request operations using select.

Member Function Documentation

template<class Callback_ptr >
Operation_waiter ugcs::vsm::Socket_processor::Accept ( Socket_listener::Ref  listener,
Callback_ptr  completion_handler,
Request_completion_context::Ptr  completion_context = Request_temp_completion_context::Create() 
)
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.

Parameters
interfaceCAN interface name. Typically "can0" or similar.
filter_messgesset of can_ids to listen for. Empty means read all.

See Listen_handler for completion handler parameters.

Operation_waiter ugcs::vsm::Socket_processor::Bind_udp ( Socket_address::Ptr  addr,
Listen_handler  completion_handler,
Request_completion_context::Ptr  completion_context = Request_temp_completion_context::Create(),
bool  multicast = false 
)
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.

Parameters
addrlocal ip address/port for listener socket. Should be numeric.
multicasttrue : bind as multicast listener. On windows it uses SO_REUSEADDR. On Mac it uses SO_REUSEPORT.
template<typename... Args>
static Ptr ugcs::vsm::Socket_processor::Create ( Args &&...  args)
inlinestatic

Create an instance.

static Ptr ugcs::vsm::Socket_processor::Create ( )
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().

Parameters
hostHostname to resolve
serviceService name to resolve. common port name or numerid port as string.
hintsPassed as hints argument for getaddrinfo() call.
template<typename... Args>
static Ptr ugcs::vsm::Socket_processor::Get_instance ( Args &&...  args)
inlinestatic

Get global or create new processor instance.

virtual void ugcs::vsm::Socket_processor::On_disable ( )
overrideprotectedvirtual

Handle disable request.

Reimplemented from ugcs::vsm::Request_container.

virtual void ugcs::vsm::Socket_processor::On_enable ( )
overrideprotectedvirtual

Handle processor enabling.

Reimplemented from ugcs::vsm::Request_container.

virtual void ugcs::vsm::Socket_processor::On_wait_and_process ( )
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.

void ugcs::vsm::Socket_processor::Process_on_disable ( Request::Ptr  )
protected

Process disable in processor context.

Member Data Documentation

std::thread ugcs::vsm::Socket_processor::thread
protected

Worker thread of socket processor.


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