Generic SocketHandle with client interface. More...
#include <senf/Socket/ClientSocketHandle.hh>
Public Types | |
typedef SPolicy::AddressingPolicy::Address | Address |
Address type from the addressing policy. More... | |
typedef boost::call_traits< Address >::param_type | AddressParam |
'Best' type for passing address as parameter More... | |
typedef ServerSocketHandle< SPolicy > | ServerHandle |
Corresponding server socket handle with the same policy. More... | |
Public Types inherited from senf::SocketHandle< SPolicy > | |
typedef SPolicy | Policy |
Public Member Functions | |
void | state (SocketStateMap &map, unsigned lod=0) |
std::string | dumpState (unsigned lod=0) |
unsigned | available () |
Public Member Functions inherited from senf::SocketHandle< SPolicy > | |
template<class OtherPolicy > | |
IsCompatible< OtherPolicy >::type const & | operator= (SocketHandle< OtherPolicy > other) |
Assign from other socket handle checking policy compatibility. More... | |
void | state (SocketStateMap &map, unsigned lod=0) |
Inquire state information of socket handle. More... | |
std::string | dumpState (unsigned lod=0) |
Format complete state information as string. More... | |
template<class Facet > | |
Facet & | facet () |
Access a protocol facet. More... | |
SocketHandle () | |
template<class OtherPolicy > | |
SocketHandle (SocketHandle< OtherPolicy > other, typename IsCompatible< OtherPolicy >::type *=0) | |
Convert from other socket handle checking policy compatibility. More... | |
Public Member Functions inherited from senf::FileHandle | |
void | close () |
Close filehandle. More... | |
void | terminate () |
Close filehandle ignoring error conditions. More... | |
bool | readable () const |
Check, whether a read on the handle would not block (ignoring blocking state) More... | |
bool | waitReadable (senf::ClockService::clock_type timeout=senf::ClockService::clock_type(-1)) const |
Wait, until read on the handle would not block (ignoring blocking state) More... | |
bool | writeable () const |
Check, whether a write on the handle would not block (ignoring blocking state) More... | |
bool | waitWriteable (senf::ClockService::clock_type timeout=senf::ClockService::clock_type(-1)) const |
Wait, until a write on the handle would not block (ignoring blocking state) More... | |
bool | oobReadable () const |
Check, whether a read of prioritized data on the handle would not block (ignoring blocking state) More... | |
bool | waitOOBReadable (senf::ClockService::clock_type timeout=senf::ClockService::clock_type(-1)) const |
Wait, until read of prioritized data on the handle does not block (ignoring blocking state) More... | |
bool | blocking () const |
Return current blocking state. More... | |
void | blocking (bool status) |
Set blocking state. More... | |
bool | eof () const |
Check EOF condition. More... | |
bool | valid () const |
Check filehandle validity. More... | |
bool | boolean_test () const |
Short for valid() && ! eof() More... | |
int | fd () const |
Return the raw FileHandle. More... | |
int | refcount () const |
bool | is_shared () const |
FileHandle () | |
~FileHandle () | |
Public Member Functions inherited from senf::comparable_safe_bool< class > | |
operator bool_type () const | |
bool | operator! () const |
Static Public Member Functions | |
static ClientSocketHandle | cast_static (FileHandle handle) |
static ClientSocketHandle | cast_dynamic (FileHandle handle) |
Static Public Member Functions inherited from senf::SocketHandle< SPolicy > | |
static SocketHandle | cast_static (FileHandle handle) |
static SocketHandle | cast_dynamic (FileHandle handle) |
Static Public Member Functions inherited from senf::FileHandle | |
static FileHandle | cast_static (FileHandle handle) |
static FileHandle | cast_dynamic (FileHandle handle) |
Protected Member Functions | |
ClientSocketHandle (FileHandle other, bool isChecked) | |
ClientSocketHandle (std::unique_ptr< SocketBody > body) | |
Protected Member Functions inherited from senf::SocketHandle< SPolicy > | |
SocketHandle (std::unique_ptr< SocketBody > body) | |
Initialize SocketHandle providing the protocol. More... | |
SocketHandle (FileHandle other, bool isChecked) | |
Initialize SocketHandle from arbitrary checked FileHandle. More... | |
SocketBody & | body () |
Access socket body. More... | |
SocketBody const & | body () const |
Access socket body in const context. More... | |
SocketProtocol & | protocol () const |
Access protocol class. More... | |
void | assign (FileHandle other) |
Protected Member Functions inherited from senf::FileHandle | |
FileHandle (std::unique_ptr< FileBody > body) | |
create new FileHandle instance More... | |
FileHandle (FileBody::ptr body) | |
FileBody & | body () |
Access body. More... | |
FileBody const & | body () const |
Access body in const context. More... | |
void | fd (int fd) |
Set raw filehandle. More... | |
void * | extraPtr () const |
get extra data pointer More... | |
void | extraPtr (void *ptr) |
set extra data pointer More... | |
Protected Member Functions inherited from senf::comparable_safe_bool< class > | |
~comparable_safe_bool () | |
Protected Member Functions inherited from senf::safe_bool_base | |
void | this_type_does_not_support_comparisons () const |
safe_bool_base () | |
safe_bool_base (const safe_bool_base &) | |
safe_bool_base & | operator= (const safe_bool_base &) |
~safe_bool_base () | |
Structors and default members | |
ClientSocketHandle () | |
ClientSocketHandle (ClientSocketHandle< OtherPolicy > other) | |
template<class OtherPolicy > | |
ClientSocketHandle< OtherPolicy > const & | operator= (ClientSocketHandle< OtherPolicy > other) |
Reading and Writing | |
std::string | read (unsigned limit=0) |
Read data from socket. More... | |
template<class ForwardWritableRange > | |
boost::range_iterator< ForwardWritableRange >::type | read (ForwardWritableRange const &range) |
Read data into range. More... | |
template<class ForwardWritableRange > | |
boost::range_iterator< ForwardWritableRange >::type | read (ForwardWritableRange &range) |
Read data into range. More... | |
template<class Sequence > | |
void | read (Sequence &container, unsigned limit) |
Read data into container. More... | |
char * | read (char *start, char *end) |
Read data into memory area. More... | |
std::pair< std::string, Address > | readfrom (unsigned limit=0) |
Read data from unconnected socket returning address. More... | |
template<class ForwardWritableRange > | |
boost::range_iterator< ForwardWritableRange const >::type | readfrom (ForwardWritableRange const &range, Address &from) |
Read data into range. More... | |
template<class ForwardWritableRange > | |
boost::range_iterator< ForwardWritableRange >::type | readfrom (ForwardWritableRange &range, Address &from) |
Read data into range. More... | |
template<class Sequence > | |
void | readfrom (Sequence &container, Address &from, unsigned limit) |
Read data into container. More... | |
char * | readfrom (char *start, char *end, Address &from) |
Read data into memory buffer. More... | |
template<class ForwardReadableRange > | |
boost::range_const_iterator< ForwardReadableRange const >::type | write (ForwardReadableRange const &range) |
Write data to socket. More... | |
char const * | write (char const *start, char const *end) |
Write data to socket from memory buffer. More... | |
template<class ForwardReadableRange > | |
boost::range_const_iterator< ForwardReadableRange const >::type | writeto (AddressParam addr, ForwardReadableRange const &range) |
Write data to unconnected socket. More... | |
char const * | writeto (AddressParam addr, char const *start, char const *end) |
Write data from memory buffer to unconnected socket. More... | |
Queue based Reading and Writing | |
boost::optional< typename SPolicy::ReadPolicy::Buffer > | dequeue () |
Read data from packet queue. More... | |
void | release () |
Release all queue frames to the kernel. More... | |
boost::optional< typename SPolicy::WritePolicy::Buffer > | enqueue () |
Write data to packet queue. More... | |
bool | send () |
Send all data in the write queue. More... | |
Addressing | |
void | connect (AddressParam addr) const |
Connect to remote peer. More... | |
void | bind (AddressParam addr) const |
Set local address. More... | |
Address | peer () const |
Query remote address. More... | |
void | peer (Address &addr) const |
Query remote address. More... | |
Address | local () const |
Query local address. More... | |
void | local (Address &addr) const |
Query local address. More... | |
Additional Inherited Members | |
Protected Types inherited from senf::safe_bool_base | |
typedef void(safe_bool_base::* | bool_type) () const |
Static Protected Member Functions inherited from senf::FileHandle | |
static FileBody & | body (FileHandle &handle) |
Access body of another FileHandle instance. More... | |
static FileBody const & | body (FileHandle const &handle) |
Access body of another FileHandle instance in const context. More... | |
Related Functions inherited from senf::SocketHandle< SPolicy > | |
template<class SPolicy > | |
std::ostream & | operator<< (std::ostream &os, SocketHandle< SPolicy > handle) |
Write stream status dump to output stream. More... | |
template<class Target , class Source > | |
Target | static_socket_cast (Source handle) |
static socket (down-)cast More... | |
template<class Target , class Source > | |
Target | dynamic_socket_cast (Source handle) |
dynamic socket (down-)cast More... | |
template<class Target , class Source > | |
bool | check_socket_cast (Source handle) |
dynamically check cast validity More... | |
Related Functions inherited from senf::FileHandle | |
int | retrieve_filehandle (FileHandle handle) |
Adapt FileHandle to senf::scheduler. More... | |
Generic SocketHandle with client interface.
This class provides the client side policy interface of the socket abstraction. ClientSocketHandle defines the complete policy interface. It does not implement any functionality itself however. The following table shows, to which policy members each group of ClientSocketHandle members is forwarded. The last column shows, on which other policies this member-group depends in the default policy classes. If you define your own policy classes, the dependencies are up to you.
ClientSocketHandle member | Policy member | Other policies |
---|---|---|
read() | ReadPolicy::read (senf::ReadPolicyBase) | |
readfrom() | ReadPolicy::readfrom (senf::ReadPolicyBase) | UnconnectedCommunicationPolicy |
write() | WritePolicy::write (senf::WritePolicyBase) | ConnectedCommunicationPolicy |
writeto() | WritePolicy::writeto (senf::WritePolicyBase) | UnconnectedCommunicationPolicy |
connect() | AddressingPolicy::connect (senf::AddressingPolicyBase) | |
bind() | AddressingPolicy::bind (senf::AddressingPolicyBase) | |
peer() | AddressingPolicy::peer (senf::AddressingPolicyBase) | |
local() | AddressingPolicy::local (senf::AddressingPolicyBase) | |
dequeue() | ReadPolicy::dequeue (senf::ReadPolicyBase) | |
release() | ReadPolicy::release (senf::ReadPolicyBase) | |
enqueue() | WritePolicy::release (senf::WritePolicyBase) | |
send() | WritePolicy::send (senf::WritePolicyBase) |
It is important to note, that not all members are always accessible. Which are depends on the Policy
template argument. If any of the policy axis is left unspecified the corresponding members will not be callable (you will get a compile time error). Even if every policy axis is defined, some members might (and will) not exist if they are meaningless for the protocol of the socket. This depends on the exact policy.
To find out, which members are available, you have to check the documentation of the policy classes. You can also find a summary of all members available in the leaf protocol class documentation.
The last set of members is special: it provides an alternative read/write implementation available only on special handles supporting the QueueReadPolicy and/or QueueWritePolicy.
protocol()
template member and an additional template arg Policies
. This arg should be a typelist of policy classes which can be accessed. You use protocol<ProtocolClass>() to access a protocol class. Policies
can of course be underspecified or even empty.Definition at line 88 of file ClientSocketHandle.hh.
typedef SPolicy::AddressingPolicy::Address senf::ClientSocketHandle< SPolicy >::Address |
Address type from the addressing policy.
Definition at line 96 of file ClientSocketHandle.hh.
typedef boost::call_traits<Address>::param_type senf::ClientSocketHandle< SPolicy >::AddressParam |
'Best' type for passing address as parameter
Depending on the type of Address
, this will be either Address
or Address const &
. See call_traits documentation in the Boost.Utility library.
Definition at line 103 of file ClientSocketHandle.hh.
typedef ServerSocketHandle<SPolicy> senf::ClientSocketHandle< SPolicy >::ServerHandle |
Corresponding server socket handle with the same policy.
This class will probably only be usable, if the CommunicationPolicy
is ConnectedCommunicationPolicy
and the AddressingPolicy
is not NoAddressingPolicy
.
Definition at line 108 of file ClientSocketHandle.hh.
senf::ClientSocketHandle< SPolicy >::ClientSocketHandle | ( | ) |
senf::ClientSocketHandle< SPolicy >::ClientSocketHandle | ( | ClientSocketHandle< OtherPolicy > | other | ) |
|
protected |
|
explicitprotected |
unsigned senf::ClientSocketHandle< SPolicy >::available | ( | ) |
void senf::ClientSocketHandle< SPolicy >::bind | ( | AddressParam | addr | ) | const |
Set local address.
For addressable protocols (AddressingPolicy is not NoAddressingPolicy), bind() will set the local address of the socket.
[in] | addr | Local socket address to assign |
senf::SystemException |
|
static |
|
static |
void senf::ClientSocketHandle< SPolicy >::connect | ( | AddressParam | addr | ) | const |
Connect to remote peer.
This member will establish a connection for addressable connection-oriented protocols (that is, the CommunicationPolicy is ConnectedCommunicationPolicy and the AddressingPolicy is not NoAddressingPolicy).
[in] | addr | Address to connect to |
senf::SystemException |
boost::optional<typename SPolicy::ReadPolicy::Buffer> senf::ClientSocketHandle< SPolicy >::dequeue | ( | ) |
Read data from packet queue.
Some protocols support a more efficient queue based read/write protocol. If the handle supports this protocol, dequeue() and release() are used to read packets from that queue.
The API works as follows:
The API has some special features:
boost::none
.boost::none
, if no data is availablestd::string senf::ClientSocketHandle< SPolicy >::dumpState | ( | unsigned | lod = 0 | ) |
boost::optional<typename SPolicy::WritePolicy::Buffer> senf::ClientSocketHandle< SPolicy >::enqueue | ( | ) |
Write data to packet queue.
Some protocols support a more efficient queue based read/write protocol. If the handle supports this protocol, enqueue() and release() are used to write packets to that queue.
The API works as follows:
resize()
on the buffer object to set the size of the packet to be sent send()
The API has some special features:
boost::none
, if there is no room in the write queue.Address senf::ClientSocketHandle< SPolicy >::local | ( | ) | const |
Query local address.
This member will return the address of the local socket in addressable protocols (AddressingPolicy is not NoAddressingPolicy).
There are two Variants of this member, one will return the address by value, the other takes a reference argument to elide the copy operation.
senf::SystemException |
void senf::ClientSocketHandle< SPolicy >::local | ( | Address & | addr | ) | const |
Query local address.
ClientSocketHandle<OtherPolicy> const& senf::ClientSocketHandle< SPolicy >::operator= | ( | ClientSocketHandle< OtherPolicy > | other | ) |
Address senf::ClientSocketHandle< SPolicy >::peer | ( | ) | const |
Query remote address.
This member will return the address of the communication partner in addressable connection-oriented protocols (that is, the CommunicationPolicy is ConnectedCommunicationPolicy and the AddressingPolicy is not NoAddressingPolicy).
There are two Variants of this member, one will return the address by value, the other takes a reference argument to elide the copy operation.
senf::SystemException |
void senf::ClientSocketHandle< SPolicy >::peer | ( | Address & | addr | ) | const |
Query remote address.
std::string senf::ClientSocketHandle< SPolicy >::read | ( | unsigned | limit = 0 | ) |
Read data from socket.
If the sockets FramingPolicy
is DatagramFramingPolicy
, every read() command will return a single datagram. If the sockets FramingPolicy is StreamFraming, the operation will return as much data as possible from the socket buffer. However it cannot be guaranteed, that the socket buffer will be empty after read() returns.
There are several variants of read which differ in how they return the read string.
If the further document doesn't tell something differently, on a blocking socket the members will always return some data (as long as the socket has not been closed at the other end) and will block, if no data is available now. If you do not want to block, you must make the socket non-blocking (using FileHandle::blocking()).
senf::SystemException |
This variant will read up to limit
bytes from the socket and return them as a std::string
object.
[in] | limit | Maximum number of bytes to read or 0 if unlimited. |
read
calls, not recv
. boost::range_iterator<ForwardWritableRange>::type senf::ClientSocketHandle< SPolicy >::read | ( | ForwardWritableRange const & | range | ) |
Read data into range.
Read data into the given range. At most boost::size(range)
characters are read. The data read will start at the beginning of the range. read returns a past-the-end iterator after the last character read. This iterator will point to somewhere within the input range.
[in,out] | range | Range to store data in |
boost::range_iterator<ForwardWritableRange>::type senf::ClientSocketHandle< SPolicy >::read | ( | ForwardWritableRange & | range | ) |
Read data into range.
void senf::ClientSocketHandle< SPolicy >::read | ( | Sequence & | container, |
unsigned | limit | ||
) |
Read data into container.
The data read is written into the given container. Old data in the container will be removed. For this to work, the container must be a model of 'Sequence' as defined in the STL documentation
[out] | container | Container to write data to |
[in] | limit | Maximum number of characters to read |
char* senf::ClientSocketHandle< SPolicy >::read | ( | char * | start, |
char * | end | ||
) |
Read data into memory area.
This variant will read data into the memory area from start to before end. This is guaranteed to be the most efficient version of read().
[in] | start | address of buffer to store data at |
[in] | end | address one past the end of the buffer |
std::pair<std::string, Address> senf::ClientSocketHandle< SPolicy >::readfrom | ( | unsigned | limit = 0 | ) |
Read data from unconnected socket returning address.
The readfrom() group of member behaves like read() but should only be available, if the sockets CommunicationPolicy
is UnconnectedCommunicationPolicy
and the AddressingPolicy
is not NoAddressingPolicy
. readfrom() will in addition to the data return the address of the sender.
senf::SystemException |
This variant will return the data read and the address as a std::pair.
std::pair
of data read (a string) and the peers addressrecvfrom
from the BSD socket API. boost::range_iterator<ForwardWritableRange const>::type senf::ClientSocketHandle< SPolicy >::readfrom | ( | ForwardWritableRange const & | range, |
Address & | from | ||
) |
Read data into range.
Read data into the given range. At most boost::size(range)
characters are read. The data read will start at the beginning of the range. read returns a past-the-end iterator after the last character read. This iterator will point to somewhere within the input range.
[in,out] | range | Range to store data in |
[out] | from | peers address from which the data was received |
boost::range_iterator<ForwardWritableRange>::type senf::ClientSocketHandle< SPolicy >::readfrom | ( | ForwardWritableRange & | range, |
Address & | from | ||
) |
Read data into range.
void senf::ClientSocketHandle< SPolicy >::readfrom | ( | Sequence & | container, |
Address & | from, | ||
unsigned | limit | ||
) |
Read data into container.
The data read is written into the given container. Old data in the container will be removed. For this to work, the container must be a model of 'Sequence' as defined in the STL documentation
[out] | container | Container to write data to |
[in] | limit | Maximum number of characters to read |
[out] | from | peers address from which the data was received |
char* senf::ClientSocketHandle< SPolicy >::readfrom | ( | char * | start, |
char * | end, | ||
Address & | from | ||
) |
Read data into memory buffer.
This variant will read data into the memory area from start to before end. This is guaranteed to be the most efficient version of readfrom().
[in] | start | address of buffer to store data at |
[in] | end | address one past the end of the buffer |
[out] | from | peers address from which the data was received |
void senf::ClientSocketHandle< SPolicy >::release | ( | ) |
Release all queue frames to the kernel.
All the frames dequeued since the last call to release() will be returned to kernel space.
bool senf::ClientSocketHandle< SPolicy >::send | ( | ) |
Send all data in the write queue.
This call will send out all frames allocated since the last call to send().
void senf::ClientSocketHandle< SPolicy >::state | ( | SocketStateMap & | map, |
unsigned | lod = 0 |
||
) |
boost::range_const_iterator<ForwardReadableRange const>::type senf::ClientSocketHandle< SPolicy >::write | ( | ForwardReadableRange const & | range | ) |
Write data to socket.
The write() family of members will write out the data to the socket. If the sockets FramingPolicy
is DatagramFramingPolicy
, every write() call will result in one datagram.
A single write call might depending on the circumstances write only part of the data.
There are two variants of this member
senf::SystemException |
This variant will write out the range data
.
[in] | range | Data to write |
write
calls, not send
. char const* senf::ClientSocketHandle< SPolicy >::write | ( | char const * | start, |
char const * | end | ||
) |
Write data to socket from memory buffer.
[in] | start | beginning of area to write |
[in] | end | past-the-end pointer to area to write |
boost::range_const_iterator<ForwardReadableRange const>::type senf::ClientSocketHandle< SPolicy >::writeto | ( | AddressParam | addr, |
ForwardReadableRange const & | range | ||
) |
Write data to unconnected socket.
This member behaves like write() but should only be available, if the sockets CommunicationPolicy
is UnconnectedCommunicationPolicy
and the AddressingPolicy
is not NoAddressingPolicy
. The writeto() family of members takes the target address as an additional argument.
There are two variants of this member.
senf::SystemException |
This variant will send the range range
to peer addr
.
[in] | addr | Address of peer to send data to |
[in] | range | data to send |
char const* senf::ClientSocketHandle< SPolicy >::writeto | ( | AddressParam | addr, |
char const * | start, | ||
char const * | end | ||
) |
Write data from memory buffer to unconnected socket.
[in] | addr | Address of peer to send data to |
[in] | start | address of buffer to write |
[in] | end | past-the-end pointer after data to write |