A single unencrypted socket.This class provides basic support for socket based communication. Its aim is to encapsulate functionality for establishing a socket in active and passive mode. For using a socket, the owning class must implement the interface defined in csocket_plain_owner. More...
#include <csocket_plain.h>
Classes | |
struct | pout_entry_t |
Public Member Functions | |
csocket_plain (csocket_env *owner) | |
Constructor for new empty csocket_plain instances. More... | |
virtual | ~csocket_plain () |
Destructor. | |
virtual void | listen (cparams const ¶ms) |
Open socket in listening mode (server side). More... | |
virtual void | accept (cparams const &socket_params, int sd) |
Handle accepted socket descriptor obtained from external listening socket. | |
virtual void | connect (cparams const ¶ms) |
Open socket and connect to peer entity (client side). More... | |
virtual void | reconnect () |
Reconnect this socket. More... | |
virtual void | close () |
Closes this socket. More... | |
virtual ssize_t | recv (void *buf, size_t count) |
Reads bytes from socket. | |
virtual ssize_t | recv (void *buf, size_t count, int flags, rofl::csockaddr &from) |
virtual void | send (cmemory *mem, rofl::csockaddr const &dest=rofl::csockaddr()) |
Store a packet for transmission. More... | |
virtual bool | is_established () const |
virtual bool | write_would_block () const |
std::string | str () const |
![]() | |
csocket (csocket_env *env, enum socket_type_t socket_type) | |
csocket constructor More... | |
virtual | ~csocket () |
csocket destructor | |
int | get_sd () const |
enum rofl::csocket::socket_type_t | get_socket_type () const |
const rofl::cparams & | get_socket_params () const |
rofl::csockaddr & | set_laddr () |
rofl::csockaddr const & | get_laddr () const |
rofl::csockaddr & | set_raddr () |
rofl::csockaddr const & | get_raddr () const |
void | set_domain (int domain) |
int | get_domain () const |
void | set_type (int type) |
int | get_type () const |
void | set_protocol (int protocol) |
int | get_protocol () const |
![]() | |
ciosrv (pthread_t tid=0) | |
Initializes all structures for this ciosrv object. | |
virtual | ~ciosrv () |
Deallocates resources for this ciosrv object. | |
ciosrv (const ciosrv &iosrv) | |
Initializes all structures for this ciosrv object. | |
ciosrv & | operator= (const ciosrv &iosrv) |
void | notify (const cevent &event) |
Sends a notification to this ciosrv instance. More... | |
pthread_t | get_thread_id () const |
Returns thread-id of local thread. More... | |
Static Public Member Functions | |
static cparams | get_default_params () |
![]() | |
static csocket * | csocket_factory (enum socket_type_t socket_type, csocket_env *owner) |
static cparams | get_default_params (enum socket_type_t socket_type) |
static bool | supports_socket_type (enum socket_type_t socket_type) |
Protected Member Functions | |
virtual void | listen (const csockaddr &la, int domain=PF_INET, int type=SOCK_STREAM, int protocol=0, int backlog=10, std::string devname=std::string("")) |
Open socket in listening mode (server side). More... | |
virtual void | connect (csockaddr ra, csockaddr la=csockaddr(), int domain=PF_INET, int type=SOCK_STREAM, int protocol=0, bool do_reconnect=false) |
Open socket and connect to peer entity (client side). More... | |
virtual void | handle_accepted () |
virtual void | handle_accept_refused () |
virtual void | handle_connected () |
virtual void | handle_conn_refused () |
virtual void | handle_conn_failed () |
virtual void | handle_listen (int newsd) |
virtual void | handle_closed () |
virtual void | handle_read () |
virtual void | handle_write () |
virtual void | dequeue_packet () |
![]() | |
ctimer | get_next_timer () |
void | register_filedesc_r (int fd) |
Registers a file descriptor for read events. More... | |
void | deregister_filedesc_r (int fd) |
Deregisters a file descriptor from read events. More... | |
void | register_filedesc_w (int fd) |
Registers a file descriptor for write events. More... | |
void | deregister_filedesc_w (int fd) |
Deregisters a file descriptor from write events. More... | |
const rofl::ctimerid & | register_timer (int opaque, const rofl::ctimespec ×pec) |
Installs a new timer to fire in t seconds. More... | |
const rofl::ctimerid & | reset_timer (const rofl::ctimerid &timer_id, const rofl::ctimespec ×pec) |
Resets a running timer of type opaque. More... | |
ctimerid & | restart_timer (rofl::ctimerid &timer_id, int opaque, const rofl::ctimespec ×pec) |
Resets an existing or creates a new timer. More... | |
bool | pending_timer (const rofl::ctimerid &timer_id) |
Checks for a pending timer of type opaque. More... | |
void | cancel_timer (const rofl::ctimerid &timer_id) |
Cancels a pending timer. More... | |
void | cancel_all_timers () |
Cancels all pending timers of this instance. | |
void | cancel_all_events () |
Cancels all pending events of this instance. | |
Protected Attributes | |
bool | had_short_write |
pthread_rwlock_t | pout_squeue_lock |
std::list< pout_entry_t > | pout_squeue |
![]() | |
csocket_env * | socket_env |
enum socket_type_t | socket_type |
int | sd |
csockaddr | laddr |
csockaddr | raddr |
int | domain |
int | type |
int | protocol |
int | backlog |
cparams | socket_params |
Friends | |
std::ostream & | operator<< (std::ostream &os, csocket_plain const &sock) |
Additional Inherited Members | |
![]() | |
enum | socket_type_t { SOCKET_TYPE_UNKNOWN = 0, SOCKET_TYPE_PLAIN = 1, SOCKET_TYPE_OPENSSL = 2 } |
![]() | |
static std::string const | PARAM_KEY_DO_RECONNECT |
static std::string const | PARAM_KEY_REMOTE_HOSTNAME |
static std::string const | PARAM_KEY_REMOTE_PORT |
static std::string const | PARAM_KEY_LOCAL_HOSTNAME |
static std::string const | PARAM_KEY_LOCAL_PORT |
static std::string const | PARAM_KEY_DOMAIN |
static std::string const | PARAM_KEY_TYPE |
static std::string const | PARAM_KEY_PROTOCOL |
static std::string const | PARAM_DOMAIN_VALUE_INET_ANY |
static std::string const | PARAM_DOMAIN_VALUE_INET |
static std::string const | PARAM_DOMAIN_VALUE_INET6 |
static std::string const | PARAM_TYPE_VALUE_STREAM |
static std::string const | PARAM_TYPE_VALUE_DGRAM |
static std::string const | PARAM_PROTOCOL_VALUE_TCP |
static std::string const | PARAM_PROTOCOL_VALUE_UDP |
static std::string const | PARAM_SSL_KEY_CA_PATH |
static std::string const | PARAM_SSL_KEY_CA_FILE |
static std::string const | PARAM_SSL_KEY_CERT |
static std::string const | PARAM_SSL_KEY_PRIVATE_KEY |
static std::string const | PARAM_SSL_KEY_PRIVATE_KEY_PASSWORD |
static std::string const | PARAM_SSL_KEY_VERIFY_MODE |
static std::string const | PARAM_SSL_KEY_VERIFY_DEPTH |
static std::string const | PARAM_SSL_KEY_CIPHERS |
A single unencrypted socket.
This class provides basic support for socket based communication. Its aim is to encapsulate functionality for establishing a socket in active and passive mode. For using a socket, the owning class must implement the interface defined in csocket_plain_owner.
The socket is set to non-blocking mode, thus it does not block indefinitely during read or write operations, rather it returns control to the calling entity asap.
For listening sockets, method csocket_plain_owner::handle_accepted() will be called. The socket owner should create a new csocket_plain instance and assigning the new obtained socket descriptor to it.
csocket_plain::csocket_plain | ( | csocket_env * | owner | ) |
Constructor for new empty csocket_plain instances.
owner | socket owning entity implementing interface csocket_plain_owner |
|
virtual |
Closes this socket.
Calls the shutdown() system call and closes the socket. Also deletes all packets queued in pout_squeue for transmission. After calling cclose() it is safe to call caopen() or cpopen() again.
Implements rofl::csocket.
|
virtual |
Open socket and connect to peer entity (client side).
This opens a socket and connects to a peer entity.
params | parameters for this socket |
Implements rofl::csocket.
|
protectedvirtual |
Open socket and connect to peer entity (client side).
This opens a socket and connects to a peer entity.
ra | remote address of peer entity to connect to |
la | address used for binding socket locally |
domain | socket domain |
type | socket type |
protocol | socket protocol |
eSocketConnectFailed | thrown if the connect() operation failed finally |
eSocketError | thrown for all other socket related errors |
|
protectedvirtual |
Send packets in outgoing queue pout_squeue.
This method transmits all pending packets from the transmission queue pout_squeue.
|
inlineprotectedvirtual |
Accept on socket failed (server mode).
This notification method is called if the accept() operation fails on the socket. It should be overwritten by a derived class if the derived class wants to act upon this condition.
|
inlineprotectedvirtual |
Accept on socket succeeded (server mode).
This notification method is called if the connect() operation succeeds on the socket. It should be overwritten by a derived class if this signal is required for further operation.
|
inlineprotectedvirtual |
Socket was closed.
This notification method is called when the socket is closed.
sd | the socket descriptor |
|
inlineprotectedvirtual |
Connect on socket failed (client mode).
This notification method is called if the connect() operation fails on the socket. It should be overwritten by a derived class if the derived class wants to act upon this condition.
|
inlineprotectedvirtual |
Connect on socket failed (client mode).
This notification method is called if the connect() operation fails on the socket. It should be overwritten by a derived class if the derived class wants to act upon this condition.
|
inlineprotectedvirtual |
Connect on socket succeeded (client mode).
This notification method is called if the connect() operation succeeds on the socket. It should be overwritten by a derived class if this signal is required for further operation.
|
inlineprotectedvirtual |
A new incoming connection was accepted (listening mode).
This notification method is called upon receipt of a new incoming connection request. It should be overwritten by a derived class for creation of a new instance or direct handling.
newsd | the new socket descriptor |
ra | reference to the peer entity's address |
|
inlineprotectedvirtual |
Read data from socket.
This notification method is called from within csocket_plain::handle_revent(). A derived class should read data from the socket. This method must be overwritten by a derived class.
fd | the socket descriptor |
|
inlineprotectedvirtual |
Write data to socket.
This notification method is called from within csocket_plain::handle_wevent(). A derived class should write data to the socket. This method must be overwritten by a derived class.
fd | the socket descriptor |
|
virtual |
Open socket in listening mode (server side).
This opens a socket in listening mode bound to address 'la' with the specified socket parameters.
params | parameters for this socket |
Implements rofl::csocket.
|
protectedvirtual |
Open socket in listening mode (server side).
This opens a socket in listening mode bound to address 'la' with the specified socket parameters.
la | the local address for binding this socket |
domain | socket domain |
type | socket type |
protocol | socket protocol |
backlog | backlog value |
eSocketListenFailed | failure in listen() system call |
eSocketAddressInUse | bind error while calling bind() |
eSocketError | thrown for all other socket related errors |
|
virtual |
Reconnect this socket.
Reconnects this socket to the previously connected peer. The socket must be an active one, i.e. we have all data required for calling ::connect() towards the peer. A passive socket is throwing an exception of type eSocketError.
Implements rofl::csocket.
|
virtual |
Store a packet for transmission.
This method stores the packet in the outgoing queue for transmission. If the socket is not connected and not a raw socket, the packet will be deleted and thus dropped. After pushing the packet pointer onto the outgoing queue, the method registers the socket descriptor for a write operation and returns, giving the calling entity back control.
csocket_plain will call mem's destructor in order to remove the packet from heap once it has been sent out. Make sure, that mem is pointing to a heap allocated cmemory instance!
mem | cmemory instance to be sent out |
Implements rofl::csocket.
|
protected |
queue of outgoing packets
|
protected |
rwlock for access to pout_squeue