SSocket< Module, Type, Protocol > Class Template Reference

#include <ssocket.h>

List of all members.


Detailed Description

template<typename Module, typename Type, typename Protocol = Socket::TCP>
class SSocket< Module, Type, Protocol >

SSocket template represents a socket that can be serve as communication medium between two remote parties.

The exact implementation is chosen based on the template parameters.

Parameters:
Module Required, the module governing this socket.
Type Type of the socket. Can be either Client or Server
Protocol Protocol to be used in the socket, e.g. TCP or UDP
Note:
This class acts as a Facade for the Scheduler engine. All public member function calls of this class are forwarded to the respective underlying scheduler classes.

Definition at line 73 of file ssocket.h.


Public Types

typedef Implement< Type, Protocol
>::Impl 
Impl
typedef Impl::PriorityType PriorityType
typedef Impl::EventType EventType
typedef ImplImplPtr
typedef boost::function< void(SSocket *, EventTypeHandlerFunc )
typedef Scheduler< Impl_Scheduler

Public Member Functions

 SSocket (HandlerFunc h=0)
 Construct and initialize, optionally setting event handler.
template<typename T>
 SSocket (T *obj, void(T::*func)(SSocket *, EventType))
 Convenience constructor - performs event handler functor binding internally.
 SSocket (typename Impl::AcceptType *s)
 Constructer used only internally during incoming connections accepting.
 ~SSocket ()
 Destructor.
void setTimeout (uint32_t t)
 Set socket timeout.
void setPriority (PriorityType prio)
 Set this sockets priority.
Input/Output
void write (const std::string &buf)
 Write data into socket.
void read (std::string *buf)
 Read data from socket.
std::string getData ()
 A more optimized version of read() method, this returns the incoming data buffer directly.
void connect (IPV4Address addr, uint32_t timeout=5000)
 Perform an outgoing connection.
void disconnect (bool lazy=true)
 Disconnect a connected socket.
void listen (IPV4Address addr)
 Start a listener, waiting for incoming connections.
void listen (uint32_t ip, uint16_t port)
 Convenience method - construct IPV4Address internally.
SSocket< Module, Socket::Client,
Protocol > * 
accept ()
 Accept an incoming connection.
void send (IPV4Address to, const std::string &buf)
 Send data to specific address.
void recv (IPV4Address *from, std::string *buf)
 Receive data from designated address.
Event handling
void setHandler (HandlerFunc handler)
 Set event handler, overwriting old handler.
template<typename T>
void setHandler (T *obj, void(T::*func)(SSocket *, EventType))
 Set the event handler, performing functor binding internally.
HandlerFunc getHandler () const
 Retrieve the handler function object.
void clearHandler ()
 Clear the existing event handler.
Queries
bool isConnected () const
bool isOk () const
bool isConnecting () const
bool isWritable () const
bool isListening () const
bool hasIncoming () const
IPV4Address getPeer () const
IPV4Address getAddr () const
PriorityType getPriority () const

Private Member Functions

 SSocket (const SSocket &)
 Forbidden.
SSocketoperator= (const SSocket &)
 Forbidden.
void onEvent (ImplPtr ptr, EventType evt)
 Internal event handler, called from scheduler.

Private Attributes

ImplPtr m_impl
 Implementation object.
HandlerFunc m_handler
 External event handler function.

Friends

SSocketoperator<< (SSocket &s, const std::string &data)
SSocketoperator>> (SSocket &s, std::string &data)

Member Typedef Documentation

template<typename Module, typename Type, typename Protocol = Socket::TCP>
typedef Scheduler<Impl> SSocket< Module, Type, Protocol >::_Scheduler
 

Definition at line 80 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
typedef Impl::EventType SSocket< Module, Type, Protocol >::EventType
 

Definition at line 77 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
typedef boost::function<void (SSocket*, EventType) SSocket< Module, Type, Protocol >::HandlerFunc)
 

Definition at line 79 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
typedef Implement<Type, Protocol>::Impl SSocket< Module, Type, Protocol >::Impl
 

Definition at line 75 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
typedef Impl* SSocket< Module, Type, Protocol >::ImplPtr
 

Definition at line 78 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
typedef Impl::PriorityType SSocket< Module, Type, Protocol >::PriorityType
 

Definition at line 76 of file ssocket.h.


Constructor & Destructor Documentation

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket< Module, Type, Protocol >::SSocket HandlerFunc  h = 0  )  [inline]
 

Construct and initialize, optionally setting event handler.

Parameters:
h Optional event handler

Definition at line 87 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_handler, SSocket< Module, Type, Protocol >::m_impl, and SSocket< Module, Type, Protocol >::onEvent().

Here is the call graph for this function:

template<typename Module, typename Type, typename Protocol = Socket::TCP>
template<typename T>
SSocket< Module, Type, Protocol >::SSocket T *  obj,
void(T::*)(SSocket< Module, Type, Protocol > *, EventType func
[inline]
 

Convenience constructor - performs event handler functor binding internally.

Parameters:
obj Object to receive event notifications
func Function to receive event notifications

Definition at line 101 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_handler, SSocket< Module, Type, Protocol >::m_impl, and SSocket< Module, Type, Protocol >::onEvent().

Here is the call graph for this function:

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket< Module, Type, Protocol >::SSocket typename Impl::AcceptType *  s  )  [inline]
 

Constructer used only internally during incoming connections accepting.

Parameters:
s New socket

Definition at line 114 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl, and SSocket< Module, Type, Protocol >::onEvent().

Here is the call graph for this function:

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket< Module, Type, Protocol >::~SSocket  )  [inline]
 

Destructor.

Definition at line 121 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket< Module, Type, Protocol >::SSocket const SSocket< Module, Type, Protocol > &   )  [private]
 

Forbidden.


Member Function Documentation

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket<Module, Socket::Client, Protocol>* SSocket< Module, Type, Protocol >::accept  )  [inline]
 

Accept an incoming connection.

Returns:
New socket, which is in connected state, ready to receive and transmit data. The return type depends on the underlying implementation. The returned socket is created in same module as the listening socket.
Exceptions:
if there was no incoming connection pending at this moment.

Definition at line 224 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::clearHandler  )  [inline]
 

Clear the existing event handler.

Definition at line 277 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_handler.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::connect IPV4Address  addr,
uint32_t  timeout = 5000
[inline]
 

Perform an outgoing connection.

Parameters:
addr Address to connect to
timeout Optional timeout for connection attempt. Defaults to 5 seconds.

Definition at line 175 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::disconnect bool  lazy = true  )  [inline]
 

Disconnect a connected socket.

If the socket is not connected, this function does nothing. Note that sockets are automatically disconnected when they are destroyed.

Parameters:
lazy If true, the actual disconnection is delayed until pending data has been sent out.

Definition at line 187 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
IPV4Address SSocket< Module, Type, Protocol >::getAddr  )  const [inline]
 

Definition at line 294 of file ssocket.h.

References IPV4Address::getAddr(), and SSocket< Module, Type, Protocol >::m_impl.

Here is the call graph for this function:

template<typename Module, typename Type, typename Protocol = Socket::TCP>
std::string SSocket< Module, Type, Protocol >::getData  )  [inline]
 

A more optimized version of read() method, this returns the incoming data buffer directly.

The different between getData() and read() methods is that in case of read(), the data is appended to the buffer, which means memcpy operation, however, with this method, only std::string copy-constructor is called, and majority of implementations have that optimized to avoid data copying. So this method should be prefered over read() method.

Returns:
All data that has been received thus far.
Note:
If you ignore the return value, the data is effectivly lost, since scheduler removes the data from it's internal buffer.

Definition at line 164 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
HandlerFunc SSocket< Module, Type, Protocol >::getHandler  )  const [inline]
 

Retrieve the handler function object.

Definition at line 275 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_handler.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
IPV4Address SSocket< Module, Type, Protocol >::getPeer  )  const [inline]
 

Definition at line 293 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
PriorityType SSocket< Module, Type, Protocol >::getPriority  )  const [inline]
 

Definition at line 295 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
bool SSocket< Module, Type, Protocol >::hasIncoming  )  const [inline]
 

Definition at line 292 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
bool SSocket< Module, Type, Protocol >::isConnected  )  const [inline]
 

Definition at line 287 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
bool SSocket< Module, Type, Protocol >::isConnecting  )  const [inline]
 

Definition at line 289 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
bool SSocket< Module, Type, Protocol >::isListening  )  const [inline]
 

Definition at line 291 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
bool SSocket< Module, Type, Protocol >::isOk  )  const [inline]
 

Definition at line 288 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
bool SSocket< Module, Type, Protocol >::isWritable  )  const [inline]
 

Definition at line 290 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::listen uint32_t  ip,
uint16_t  port
[inline]
 

Convenience method - construct IPV4Address internally.

Parameters:
ip Ip to listen on
port Port to listen on

Definition at line 210 of file ssocket.h.

References SSocket< Module, Type, Protocol >::listen().

Here is the call graph for this function:

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::listen IPV4Address  addr  )  [inline]
 

Start a listener, waiting for incoming connections.

Parameters:
addr Local address to listen on. If addr.ip is set to 0, connections are accepted from all networks, otherwise connections are only accepted from the designated net. For example, if ip is 127.0.0.1, only loopback connections are accepted.

Definition at line 200 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

Referenced by SSocket< Module, Type, Protocol >::listen().

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::onEvent ImplPtr  ptr,
EventType  evt
[inline, private]
 

Internal event handler, called from scheduler.

Forwards the event to user-defined event handler (if present).

Parameters:
ptr Implementation pointer generating this event. Must match m_impl member.
evt The event itself

Definition at line 312 of file ssocket.h.

References CHECK_THROW, SSocket< Module, Type, Protocol >::m_handler, and SSocket< Module, Type, Protocol >::m_impl.

Referenced by SSocket< Module, Type, Protocol >::SSocket().

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket& SSocket< Module, Type, Protocol >::operator= const SSocket< Module, Type, Protocol > &   )  [private]
 

Forbidden.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::read std::string *  buf  )  [inline]
 

Read data from socket.

Parameters:
buf Buffer to read data into. The data is appended to the specified string.

Definition at line 146 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::recv IPV4Address from,
std::string *  buf
[inline]
 

Receive data from designated address.

This applies only to UDP type sockets.

Parameters:
buf Buffer to write the retrieved data to. The data is appended to the designated string.
from Will receive the data source address

Definition at line 248 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::send IPV4Address  to,
const std::string &  buf
[inline]
 

Send data to specific address.

This applies only to UDP sockets.

Parameters:
to Address to send data to
buf Buffer containing the data to be sent

Definition at line 236 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
template<typename T>
void SSocket< Module, Type, Protocol >::setHandler T *  obj,
void(T::*)(SSocket< Module, Type, Protocol > *, EventType func
[inline]
 

Set the event handler, performing functor binding internally.

Definition at line 271 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_handler.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::setHandler HandlerFunc  handler  )  [inline]
 

Set event handler, overwriting old handler.

Definition at line 268 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_handler.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::setPriority PriorityType  prio  )  [inline]
 

Set this sockets priority.

Definition at line 281 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::setTimeout uint32_t  t  )  [inline]
 

Set socket timeout.

If no events happen before the timeout is over, the socket is closed and EVT_TIMEOUT posted.

Parameters:
t Timeout in milliseconds

Definition at line 259 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
void SSocket< Module, Type, Protocol >::write const std::string &  buf  )  [inline]
 

Write data into socket.

Parameters:
buf Data to be written

Definition at line 136 of file ssocket.h.

References SSocket< Module, Type, Protocol >::m_impl.


Friends And Related Function Documentation

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket& operator<< SSocket< Module, Type, Protocol > &  s,
const std::string &  data
[friend]
 

Definition at line 319 of file ssocket.h.

template<typename Module, typename Type, typename Protocol = Socket::TCP>
SSocket& operator>> SSocket< Module, Type, Protocol > &  s,
std::string &  data
[friend]
 

Definition at line 323 of file ssocket.h.


Member Data Documentation

template<typename Module, typename Type, typename Protocol = Socket::TCP>
HandlerFunc SSocket< Module, Type, Protocol >::m_handler [private]
 

External event handler function.

Definition at line 302 of file ssocket.h.

Referenced by SSocket< Module, Type, Protocol >::clearHandler(), SSocket< Module, Type, Protocol >::getHandler(), SSocket< Module, Type, Protocol >::onEvent(), SSocket< Module, Type, Protocol >::setHandler(), and SSocket< Module, Type, Protocol >::SSocket().

template<typename Module, typename Type, typename Protocol = Socket::TCP>
ImplPtr SSocket< Module, Type, Protocol >::m_impl [private]
 

Implementation object.

Definition at line 301 of file ssocket.h.

Referenced by SSocket< Module, Type, Protocol >::accept(), SSocket< Module, Type, Protocol >::connect(), SSocket< Module, Type, Protocol >::disconnect(), SSocket< Module, Type, Protocol >::getAddr(), SSocket< Module, Type, Protocol >::getData(), SSocket< Module, Type, Protocol >::getPeer(), SSocket< Module, Type, Protocol >::getPriority(), SSocket< Module, Type, Protocol >::hasIncoming(), SSocket< Module, Type, Protocol >::isConnected(), SSocket< Module, Type, Protocol >::isConnecting(), SSocket< Module, Type, Protocol >::isListening(), SSocket< Module, Type, Protocol >::isOk(), SSocket< Module, Type, Protocol >::isWritable(), SSocket< Module, Type, Protocol >::listen(), SSocket< Module, Type, Protocol >::onEvent(), SSocket< Module, Type, Protocol >::read(), SSocket< Module, Type, Protocol >::setPriority(), SSocket< Module, Type, Protocol >::setTimeout(), SSocket< Module, Type, Protocol >::SSocket(), SSocket< Module, Type, Protocol >::write(), and SSocket< Module, Type, Protocol >::~SSocket().


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