Thread portable class


Gate portable class


Timer class


Pool class


Terimber 2.0


About C++


Downloads Products & Services Support Clients Open Source About



Home / Open source / Terimber 2.0

aiosock Class Reference

expands windows IO Completion Port idea to Linux More...

#include <aiosock.h>

Inheritance diagram for aiosock:

terimber_thread_employer terimber_aiosock terimber_log_helper terimber_log

List of all members.

Public Member Functions

 aiosock (size_t capacity, size_t deactivate_time_msec)
 constructor
 ~aiosock ()
 destructor
bool on ()
 activates
void off ()
 deactivates
virtual size_t create (terimber_aiosock_callback *callback, bool tcp_udp)
 creates socket
virtual void close (size_t ident)
 closes socket
virtual int send (size_t ident, const void *buf, size_t len, size_t timeout, const sockaddr_in *toaddr, void *userdata)
 sends buffer to specified socket asynchronously
virtual int receive (size_t ident, void *buf, size_t len, size_t timeout, const sockaddr_in *fromaddr, void *userdata)
 receives buffer of bytes from specified socket asynchronously
virtual int connect (size_t ident, const char *address, unsigned short port, size_t timeout, void *userdata)
 connects to the specified socket synchronously
virtual int listen (size_t ident, unsigned short port, size_t max_connection, const char *address, unsigned short accept_pool, void *userdata)
 turns the specified socket to ***othe*** listening state and will accept automatically the incoming connections asynchronously
virtual int bind (size_t ident, const char *address, unsigned short port)
 binds UDP socket to address
virtual int getpeeraddr (size_t ident, sockaddr_in &addr)
 gets the peer address
virtual int getsockaddr (size_t ident, sockaddr_in &addr)
 gets the sock address
virtual bool get_error_description (int err, char *buf, size_t len) const
 gets error string by code
virtual void doxray ()
 makes the snapshot of internal state

Static Public Member Functions

static bool resolve_sock_error_code (int err, char *buf, size_t len)

Public Attributes

mutex _mtx
 multithreaded access to socket map
aiosock_socket_map_t _socket_map
 socket map
aiosock_reverse_map_t _reverse_map
 reverse socket map
aiosock_listener_map_t _listeners_map
 listeners map
aiosock_delay_key_t _delay_key_map
 map of the keys which can not be returned back to generator right now
unique_key_generator _socket_generator
 generates socket idents
aiosock_pblock_allocator_t _incoming_list_allocator
 external allocator for list of pointers to blocks
aiosock_block_allocator_t _block_allocator
 block allocator
aiosock_pblock_list_t _initial_list
 keeps initial processing reuqests
aiosock_pblock_list_t _outgoing_list
 keeps processed asynchronous requests
aiosock_pblock_list_t _abounded_list
 keeps abounded asynchronous requests for Complition Port only

Protected Member Functions

virtual bool v_has_job (size_t ident, void *data)
 detects if there is any job to do
virtual void v_do_job (size_t ident, void *data)
 executes actual job in separate thread

Private Types

typedef node_allocator
< base_list< aiosock_block * >
::_node > 
aiosock_pblock_allocator_t
 node allocator for list of blocks
typedef _list< aiosock_block
*, aiosock_pblock_allocator_t
aiosock_pblock_alloc_list_t
 list of block pointers with external allocator
typedef list< aiosock_block * > aiosock_pblock_list_t
 list of block pointers with internal allocator
typedef node_allocator
< aiosock_block
aiosock_block_allocator_t
 node allocator - block factory
typedef map< size_t,
listener_info
aiosock_listener_map_t
 maps listener ident to listener information
typedef map< size_t,
aiosock_socket
aiosock_socket_map_t
 maps socket ident yo socket info
typedef map< size_t,
aiosock_socket >::iterator 
aiosock_socket_map_iterator_t
 aiosock_socket_map iterator
typedef map< aio_sock_handle,
aiosock_socket_map_iterator_t
aiosock_reverse_map_t
 reverses map socket handle to socket map interator
typedef map< aio_sock_handle,
aiosock_socket_map_iterator_t >
::iterator 
aiosock_reverse_map_iterator_t
 aiosock_reverse_map iterator
typedef map< size_t, size_t > aiosock_delay_key_t
 maps socket ident to delay counter

Private Member Functions

void _clear_block_lists ()
 clears all block lists outgoing, abounded, initial
aiosock_block_get_block ()
 gets new blocks
void _put_block (aiosock_block *block)
 returns used block back
bool resolve_socket_address (const char *address, unsigned short port, sockaddr_in &addr)
 resolves address into IP
aio_sock_handle find_socket_handle (size_t ident)
 thread-safe function to find socket handle by socket ident
void process_timeouted_blocks ()
 processes timeouted blocks
bool process_accept_blocks ()
 initiates new accepts
void complete_block (size_t sock_key, aiosock_block *ov, int err, size_t processed)
 completes asynchronous action
int _activate_block (size_t ident, aiosock_block *block, const sockaddr_in *pddr)
 inserts block into incoming queue for processing in a separate thread
void _cancel_socket (aio_sock_handle handle)
 cancels asynchronous operations
size_t _assign_socket (aio_sock_handle handle, terimber_aiosock_callback *callback, bool tcp_udp)
 associates socket handle with internal structures
void _close_socket (aio_sock_handle handle, bool tcp_udp)
 closes socket
int _process_block (aiosock_block *block)
 starts actual IO processing
int _process_connect (aio_sock_handle handle, aiosock_block *block)
 processing connect
int _process_accept (aio_sock_handle handle, terimber_aiosock_callback *client, aiosock_block *block)
 processing accept
int _process_send (aio_sock_handle handle, aiosock_block *block, bool tcp_udp)
 processing send
int _process_recv (aio_sock_handle handle, aiosock_block *block, bool tcp_udp)
 processing receive
void wait_for_io_completion ()
 waits for completion actions in separate thread Terimber Completion Port

Private Attributes

HANDLE _aiosock_io_handle
 this Terimber port handle
threadpool _thread_pool
 thread pool
size_t _capacity
 max thread pool capacity
thread _in_thread
 housekeeping thread - process initatial and timeouted blocks
bool _on
 activation flag
bool _flag_io_port
 signals that the Terimber Completion Port is running
event _start_io_port
 signals that the thread is waiting for completion actions - Terimber Completion Port
event _stop_io_port
 signals that the thread stopped for completion actions - Terimber Completion Port

Static Private Attributes

static bool _port_init
 initialize once

Classes

class  aiosock_socket
 keeps socket information including initiated asynchronous actions blocks More...
class  listener_info
 listener information More...


Detailed Description

expands windows IO Completion Port idea to Linux

Definition at line 91 of file aiosock.h.


Member Typedef Documentation

node allocator for list of blocks

Definition at line 96 of file aiosock.h.

list of block pointers with external allocator

Definition at line 99 of file aiosock.h.

list of block pointers with internal allocator

Definition at line 102 of file aiosock.h.

node allocator - block factory

Definition at line 105 of file aiosock.h.

maps listener ident to listener information

Definition at line 129 of file aiosock.h.

maps socket ident yo socket info

Definition at line 163 of file aiosock.h.

aiosock_socket_map iterator

Definition at line 166 of file aiosock.h.

reverses map socket handle to socket map interator

Definition at line 169 of file aiosock.h.

aiosock_reverse_map iterator

Definition at line 172 of file aiosock.h.

maps socket ident to delay counter

Definition at line 175 of file aiosock.h.


Constructor & Destructor Documentation

aiosock::aiosock ( size_t  capacity,
size_t  deactivate_time_msec 
)

constructor

Parameters:
capacity  additional threads for processing asynchronous completion callbacks
deactivate_time_msec  timeout in milliseconds to despose unused threads

Definition at line 301 of file aiosock.cpp.

aiosock::~aiosock (  ) 

destructor

Definition at line 314 of file aiosock.cpp.

References off().


Member Function Documentation

void aiosock::_clear_block_lists (  )  [inline, private]

clears all block lists outgoing, abounded, initial

Definition at line 179 of file aiosock.h.

References _abounded_list, _initial_list, _outgoing_list, _put_block(), base_list< T >::begin(), list< T >::clear(), base_list< T >::empty(), and base_list< T >::end().

Referenced by off().

aiosock_block* aiosock::_get_block (  )  [inline, private]

gets new blocks

Definition at line 221 of file aiosock.h.

References _block_allocator, and node_allocator< T >::allocate().

Referenced by connect(), process_accept_blocks(), receive(), and send().

void aiosock::_put_block ( aiosock_block block  )  [inline, private]

returns used block back

Definition at line 235 of file aiosock.h.

References _block_allocator, and rep_allocator::deallocate().

Referenced by _activate_block(), _clear_block_lists(), close(), complete_block(), process_timeouted_blocks(), and v_do_job().

size_t aiosock::create ( terimber_aiosock_callback callback,
bool  tcp_udp 
) [virtual]

creates socket

Parameters:
callback  callback function
tcp_udp  socket type TCP/UDP

Implements terimber_aiosock.

Definition at line 912 of file aiosock.cpp.

References _aiosock_io_handle, _assign_socket(), _mtx, en_log_error, and terimber_log::format_logging().

Referenced by aiogate::bind(), aiogate::connect(), aiogate::listen(), msg_sock_connection::msg_sock_connection(), and msg_sock_listener::v_on().

int aiosock::send ( size_t  ident,
const void *  buf,
size_t  len,
size_t  timeout,
const sockaddr_in *  toaddr,
void *  userdata 
) [virtual]

sends buffer to specified socket asynchronously

Parameters:
ident  socket ident
buf  buffer to send
len  buffer length
timeout  timeout in milliseconds
toaddr  peer address, optional for TCP sockets
userdata  user defined data

Implements terimber_aiosock.

Definition at line 1074 of file aiosock.cpp.

References _activate_block(), aiosock_block::_buf, _get_block(), aiosock_block::_len, _mtx, aiosock_block::_type, aiosock_block::_userdata, AIOSOCK_SEND, and aiosock_block::settimeout().

Referenced by aiogate::send_bulk(), msg_sock_connection::v_do_job(), msg_sock_connection::v_on_send(), and aiogate::v_on_send().

int aiosock::receive ( size_t  ident,
void *  buf,
size_t  len,
size_t  timeout,
const sockaddr_in *  fromaddr,
void *  userdata 
) [virtual]

receives buffer of bytes from specified socket asynchronously

Parameters:
ident  socket ident
buf  buffer to receive
len  buffer length
timeout  timeout in milliseconds
fromaddr  peer address, optional for TCP sockets
userdata  user defined data

Implements terimber_aiosock.

Definition at line 1098 of file aiosock.cpp.

References _activate_block(), aiosock_block::_buf, _get_block(), aiosock_block::_len, _mtx, aiosock_block::_type, aiosock_block::_userdata, AIOSOCK_RECV, and aiosock_block::settimeout().

Referenced by msg_sock_connection::accept(), aiogate::recv(), msg_sock_connection::v_on_connect(), and msg_sock_connection::v_on_receive().

int aiosock::connect ( size_t  ident,
const char *  address,
unsigned short  port,
size_t  timeout,
void *  userdata 
) [virtual]

connects to the specified socket synchronously

Parameters:
ident  socket ident
address  peer address: IP, DNS name, localhost
port  peer port
timeout  timeout in milliseconds
userdata  user defined data

Implements terimber_aiosock.

Definition at line 1121 of file aiosock.cpp.

References _activate_block(), aiosock_block::_address, _get_block(), _mtx, aiosock_block::_type, aiosock_block::_userdata, AIOSOCK_CONNECT, en_log_error, terimber_log::format_logging(), resolve_socket_address(), and aiosock_block::settimeout().

Referenced by msg_sock_connection::connect(), and aiogate::connect().

int aiosock::listen ( size_t  ident,
unsigned short  port,
size_t  max_connection,
const char *  address,
unsigned short  accept_pool,
void *  userdata 
) [virtual]

turns the specified socket to ***othe*** listening state and will accept automatically the incoming connections asynchronously

Parameters:
ident  socket ident
port  listener port
max_connection  max waiting connections to accept
address  listener address: IP, DNS name, localhost
accept_pool  max initiated acceptors
userdata  user defined data

Implements terimber_aiosock.

Definition at line 1149 of file aiosock.cpp.

References _in_thread, _listeners_map, _mtx, bind(), en_log_error, en_log_info, base_map< K, T, Pr, M >::end(), base_map< K, T, Pr, M >::find(), find_socket_handle(), terimber_log::format_logging(), map< K, T, Pr, M >::insert(), resolve_socket_address(), mutex_keeper::unlock(), and thread::wakeup().

Referenced by aiogate::listen(), and msg_sock_listener::v_on().

int aiosock::bind ( size_t  ident,
const char *  address,
unsigned short  port 
) [virtual]

binds UDP socket to address

Parameters:
ident  socket ident
address  address: IP, DNS name, localhost
port  port

Implements terimber_aiosock.

Definition at line 1044 of file aiosock.cpp.

References en_log_error, find_socket_handle(), terimber_log::format_logging(), and resolve_socket_address().

Referenced by _process_connect(), aiogate::bind(), aiogate::connect(), and listen().

int aiosock::getpeeraddr ( size_t  ident,
sockaddr_in &  addr 
) [virtual]

gets the peer address

Parameters:
ident  socket ident
addr  peer address

Implements terimber_aiosock.

Definition at line 1206 of file aiosock.cpp.

References en_log_error, find_socket_handle(), and terimber_log::format_logging().

Referenced by aiogate::v_on_accept(), and aiogate::v_on_connect().

int aiosock::getsockaddr ( size_t  ident,
sockaddr_in &  addr 
) [virtual]

gets the sock address

Parameters:
ident  socket ident
addr  this socket address

Implements terimber_aiosock.

Definition at line 1229 of file aiosock.cpp.

References en_log_error, find_socket_handle(), and terimber_log::format_logging().

Referenced by aiogate::bind(), aiogate::v_on_accept(), and aiogate::v_on_connect().

virtual bool aiosock::get_error_description ( int  err,
char *  buf,
size_t  len 
) const [inline, virtual]

gets error string by code

Parameters:
err  error code
buf  [in, out] buffer
len  buffer length

Implements terimber_aiosock.

Definition at line 332 of file aiosock.h.

References resolve_sock_error_code().

Referenced by sockStatus().

bool aiosock::resolve_sock_error_code ( int  err,
char *  buf,
size_t  len 
) [static]

Parameters:
err  error code
buf  [in, out] buffer
len  buffer length

Definition at line 281 of file aiosock.cpp.

References aiosockTable, exception_table::get_error(), os_get_error(), and str_template::strcpy().

Referenced by get_error_description().

bool aiosock::v_has_job ( size_t  ident,
void *  data 
) [protected, virtual]

void aiosock::v_do_job ( size_t  ident,
void *  data 
) [protected, virtual]

executes actual job in separate thread

Parameters:
ident  thread ident
data  user defined data

Implements terimber_thread_employer.

Definition at line 647 of file aiosock.cpp.

References aiosock_block::_accept_ident, aiosock_block::_address, _assign_socket(), aiosock_block::_buf, _capacity, _delay_key_map, aiosock_block::_err, _in_thread, _initial_list, aiosock_block::_len, _listeners_map, _mtx, _outgoing_list, _process_block(), aiosock_block::_processed, _put_block(), _socket_generator, aiosock_block::_socket_ident, _socket_map, _thread_pool, aiosock_block::_type, aiosock_block::_userdata, AIOSOCK_ACCEPT, aiosock_completion_io_port_ident, AIOSOCK_CONNECT, aiosock_io_initiation_ident, AIOSOCK_RECV, AIOSOCK_SEND, aiosock_working_ident, aiosock_working_thread_alert, threadpool::borrow_from_range(), threadpool::borrow_thread(), close(), base_list< T >::empty(), en_log_error, base_map< K, T, Pr, M >::end(), map< K, T, Pr, M >::erase(), base_map< K, T, Pr, M >::find(), terimber_log::format_logging(), base_list< T >::front(), base_map< size_t, size_t, less< size_t >, false >::iterator, mutex_keeper::lock(), list< T >::pop_front(), process_accept_blocks(), process_timeouted_blocks(), list< T >::push_back(), unique_key_generator::save(), SO_UPDATE_ACCEPT_CONTEXT, SO_UPDATE_CONNECT_CONTEXT, mutex_keeper::unlock(), terimber_aiosock_callback::v_on_accept(), terimber_aiosock_callback::v_on_connect(), terimber_aiosock_callback::v_on_error(), terimber_aiosock_callback::v_on_receive(), terimber_aiosock_callback::v_on_send(), wait_for_io_completion(), and thread::wakeup().

bool aiosock::resolve_socket_address ( const char *  address,
unsigned short  port,
sockaddr_in &  addr 
) [private]

resolves address into IP

Parameters:
address  socket address as string
port  socket port
addr  socket address as struct

Definition at line 1446 of file aiosock.cpp.

Referenced by bind(), connect(), and listen().

aio_sock_handle aiosock::find_socket_handle ( size_t  ident  )  [private]

thread-safe function to find socket handle by socket ident

Parameters:
ident  socket ident

Definition at line 1436 of file aiosock.cpp.

References _mtx, _socket_map, base_map< K, T, Pr, M >::end(), and base_map< K, T, Pr, M >::find().

Referenced by bind(), getpeeraddr(), getsockaddr(), and listen().

bool aiosock::process_accept_blocks (  )  [private]

void aiosock::complete_block ( size_t  sock_key,
aiosock_block ov,
int  err,
size_t  processed 
) [private]

int aiosock::_activate_block ( size_t  ident,
aiosock_block block,
const sockaddr_in *  pddr 
) [private]

inserts block into incoming queue for processing in a separate thread

Parameters:
ident  socket ident
block  asynchronous block pointer
pddr  peer address, optional for TCP sockets

Definition at line 1586 of file aiosock.cpp.

References aiosock_block::_address, _in_thread, _initial_list, _put_block(), aiosock_block::_socket_ident, _socket_map, en_log_error, base_map< K, T, Pr, M >::end(), base_map< K, T, Pr, M >::find(), terimber_log::format_logging(), list< T >::push_back(), and thread::wakeup().

Referenced by connect(), process_accept_blocks(), receive(), and send().

void aiosock::_cancel_socket ( aio_sock_handle  handle  )  [private]

cancels asynchronous operations

Parameters:
handle  socket handle

Definition at line 1412 of file aiosock.cpp.

Referenced by close(), and off().

size_t aiosock::_assign_socket ( aio_sock_handle  handle,
terimber_aiosock_callback callback,
bool  tcp_udp 
) [private]

associates socket handle with internal structures

Parameters:
handle  socket handle
callback  callback for processing completion actions
tcp_udp  socket type TCP/UDP

Definition at line 1280 of file aiosock.cpp.

References _aiosock_io_handle, _close_socket(), aiosock::aiosock_socket::_handle, _reverse_map, _socket_generator, _socket_map, en_log_error, en_log_info, base_map< K, T, Pr, M >::end(), map< K, T, Pr, M >::erase(), terimber_log::format_logging(), unique_key_generator::generate(), map< K, T, Pr, M >::insert(), and unique_key_generator::save().

Referenced by _process_accept(), create(), and v_do_job().

void aiosock::_close_socket ( aio_sock_handle  handle,
bool  tcp_udp 
) [private]

closes socket

Parameters:
handle  socket handle
tcp_udp  socket type TCP/UDP

Definition at line 1358 of file aiosock.cpp.

References close(), en_log_info, and terimber_log::format_logging().

Referenced by _assign_socket(), close(), and off().

int aiosock::_process_connect ( aio_sock_handle  handle,
aiosock_block block 
) [private]

processing connect

Parameters:
handle  socket handle
block  pointer to block

Definition at line 1622 of file aiosock.cpp.

References aiosock_block::_address, aiosock_block::_err, aiosock_block::_timeout, bind(), and GUID_MSWSOCK_CONNECTEX.

Referenced by _process_block().

int aiosock::_process_accept ( aio_sock_handle  handle,
terimber_aiosock_callback client,
aiosock_block block 
) [private]

processing accept

Parameters:
handle  socket handle
client  callback for processing completion actions
block  pointer to block

Definition at line 1667 of file aiosock.cpp.

References aiosock_block::_accept_buf, aiosock_block::_accept_ident, _assign_socket(), aiosock_block::_err, aiosock_block::_processed, close(), and GUID_MSWSOCK_ACCEPTEX.

Referenced by _process_block().

int aiosock::_process_send ( aio_sock_handle  handle,
aiosock_block block,
bool  tcp_udp 
) [private]

processing send

Parameters:
handle  socket handle
block  pointer to block
tcp_udp  socket type TCP/UDP

Definition at line 1721 of file aiosock.cpp.

References aiosock_block::_address, aiosock_block::_buf, aiosock_block::_err, aiosock_block::_len, aiosock_block::_processed, and aiosock_block::_timeout.

Referenced by _process_block().

int aiosock::_process_recv ( aio_sock_handle  handle,
aiosock_block block,
bool  tcp_udp 
) [private]

processing receive

Parameters:
handle  socket handle
block  pointer to block
tcp_udp  socket type TCP/UDP

Definition at line 1763 of file aiosock.cpp.

References aiosock_block::_accept_ident, aiosock_block::_address, aiosock_block::_buf, aiosock_block::_err, aiosock_block::_flags, aiosock_block::_len, aiosock_block::_processed, and aiosock_block::_timeout.

Referenced by _process_block().

void aiosock::wait_for_io_completion (  )  [private]

waits for completion actions in separate thread Terimber Completion Port

Definition at line 534 of file aiosock.cpp.

References _aiosock_io_handle, _flag_io_port, _start_io_port, _stop_io_port, complete_block(), os_minus_one, and event::signal().

Referenced by v_do_job().


Member Data Documentation

reverse socket map

Definition at line 470 of file aiosock.h.

Referenced by _assign_socket(), close(), and off().

map of the keys which can not be returned back to generator right now

Definition at line 472 of file aiosock.h.

Referenced by close(), doxray(), off(), process_timeouted_blocks(), and v_do_job().

generates socket idents

Definition at line 473 of file aiosock.h.

Referenced by _assign_socket(), close(), off(), process_timeouted_blocks(), and v_do_job().

external allocator for list of pointers to blocks

Definition at line 474 of file aiosock.h.

Referenced by _process_block(), close(), complete_block(), off(), and process_timeouted_blocks().

block allocator

Definition at line 475 of file aiosock.h.

Referenced by _get_block(), _put_block(), and off().

keeps initial processing reuqests

Definition at line 476 of file aiosock.h.

Referenced by _activate_block(), _clear_block_lists(), close(), doxray(), v_do_job(), and v_has_job().

keeps processed asynchronous requests

Definition at line 477 of file aiosock.h.

Referenced by _clear_block_lists(), _process_block(), close(), complete_block(), doxray(), v_do_job(), and v_has_job().

keeps abounded asynchronous requests for Complition Port only

Definition at line 480 of file aiosock.h.

Referenced by _clear_block_lists(), close(), complete_block(), doxray(), and process_timeouted_blocks().

HANDLE aiosock::_aiosock_io_handle [private]

this Terimber port handle

Definition at line 484 of file aiosock.h.

Referenced by _assign_socket(), _process_block(), close(), create(), off(), on(), v_has_job(), and wait_for_io_completion().

thread pool

Definition at line 485 of file aiosock.h.

Referenced by _process_block(), complete_block(), doxray(), off(), on(), and v_do_job().

size_t aiosock::_capacity [private]

max thread pool capacity

Definition at line 486 of file aiosock.h.

Referenced by _process_block(), complete_block(), and v_do_job().

housekeeping thread - process initatial and timeouted blocks

Definition at line 487 of file aiosock.h.

Referenced by _activate_block(), listen(), off(), on(), and v_do_job().

bool aiosock::_port_init [static, private]

initialize once

Definition at line 488 of file aiosock.h.

bool aiosock::_on [private]

activation flag

Definition at line 489 of file aiosock.h.

Referenced by off(), and on().

bool aiosock::_flag_io_port [private]

signals that the Terimber Completion Port is running

Definition at line 490 of file aiosock.h.

Referenced by on(), v_has_job(), and wait_for_io_completion().

signals that the thread is waiting for completion actions - Terimber Completion Port

Definition at line 491 of file aiosock.h.

Referenced by on(), and wait_for_io_completion().

signals that the thread stopped for completion actions - Terimber Completion Port

Definition at line 492 of file aiosock.h.

Referenced by off(), and wait_for_io_completion().


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


© Copyright Terimber 2003-.