nidas v1.2.3
|
A McSocket provides a way to establish a TCP stream socket connection, or a pair of UDP datagram sockets using a common port number. More...
#include <McSocket.h>
Public Member Functions | |
McSocket () | |
Create a McSocket for requesting or accepting multicast requests for a socket connection. | |
McSocket (const McSocket< SocketT > &) | |
Copy constructor. | |
McSocket< SocketT > & | operator= (const McSocket< SocketT > &rhs) |
Assignment operator. | |
virtual | ~McSocket () |
void | setInterface (Inet4NetworkInterface iaddr) |
Set a specific interface for the multicasts. | |
Inet4NetworkInterface | getInterface () const |
std::list< Inet4NetworkInterface > | getInterfaces () const |
Return all network interfaces on this system. | |
const Inet4SocketAddress & | getInet4McastSocketAddress () const |
Get the multicast address for listening to requests. | |
void | setInet4McastSocketAddress (const Inet4SocketAddress &val) |
Set the multicast address for listening to requests. | |
void | setRequestType (int val) |
Set the request type value. | |
int | getRequestType () const |
Get the request type number. | |
void | listen () |
Register with a McSocketListener to listen on the multicast address. | |
SocketT * | accept (Inet4PacketInfoX &) |
Like ServerSocket::accept(), this method will return a connected socket. | |
virtual void | connected (SocketT *, const Inet4PacketInfoX &) |
Virtual method that is called when a socket connection is established. | |
void | request () |
Start issuing requests for a connection by multicasting McSocketDatagrams. | |
SocketT * | connect (Inet4PacketInfoX &) |
Do a request(), and then wait until a TCP connection is established, or a UDP datagram is received back. | |
virtual void | close () |
Unregister this McSocket from the multicasting and listening threads. | |
Private Member Functions | |
void | offer (SocketT *sock, const Inet4PacketInfoX &pktinfo) |
How a McSocketListener passes back a connected TCP socket or DatagramSocket. | |
void | offer (int err) |
void | joinMulticaster () |
Private Attributes | |
Inet4SocketAddress | _mcastAddr |
Inet4NetworkInterface | _iface |
int | _requestType |
Cond | _connectCond |
SocketT * | _newsocket |
Inet4PacketInfoX | _newpktinfo |
bool | _socketOffered |
int | _offerErrno |
Thread * | _multicaster |
The thread we start which multicasts for connections. | |
Mutex | _multicaster_mutex |
Friends | |
class | McSocketListener |
McSocketListener and McSocketMulticaster are friends that setup the socket connection and call the private offer() method. | |
template<class SocketTT > | |
class | McSocketMulticaster |
A McSocket provides a way to establish a TCP stream socket connection, or a pair of UDP datagram sockets using a common port number.
The communication is esablished by multicasting a request for the service. Contained in the multicast datagram is an integer value for the type of service requested, allowing this protocol to be used for any type of service that can be provided over TCP or UDP sockets.
Use a McSocket<Socket> to establish a TCP SOCK_STREAM connection. Use a McSocket<DatagramSocket> to establish a pair of UDP SOCK_DGRAM sockets.
McSocket is used to establish the connection at either end. A McSocket can either listen for a connection (like a ServerSocket) or connect to a remote McSocket.
When McSocket does a connect(), it starts a McSocketMulticaster thread. This thread creates either a ServerSocket (for McSocket<Socket>) or a DatagramSocket (for McSocket<DatagramSocket>), using any available port number. Then it multicasts McSocketDatagrams containing the request type of the McSocket, the socket type (SOCK_STREAM or SOCK_DGRAM), and the port number of the ServerSocket or DatagramSocket. When a select indicates that data is readable on the created socket, then the McSocketMulticaster passes the accepted socket back to the McSocket as the return value of the connect() method. The McSocketMulticaster thread then quits. This connection can also be established asynchronously in a two-step sequence using request() and connected() instead of connect().
When a McSocket does an accept(), it registers itself with a McSocketListener, a thread which is listening for McSocketDatagrams on a multicast address. When the McSocketListener receives a McSocketDatagram, it checks if a McSocket has requested a connection for the given request type and socket type. If so, then it creates a Socket or DatagramSocket and connects it to the socket port on the requesting host. This Socket is returned as the value of the accept() method. This connection can also be established asynchronously in a two-step sequence using listen() and connected() instead of accept().
nidas::util::McSocket< SocketT >::McSocket | ( | ) |
Create a McSocket for requesting or accepting multicast requests for a socket connection.
Typical usage:
nidas::util::McSocket< SocketT >::McSocket | ( | const McSocket< SocketT > & | x | ) |
Copy constructor.
|
virtual |
SocketT * nidas::util::McSocket< SocketT >::accept | ( | Inet4PacketInfoX & | pktinfo | ) |
Like ServerSocket::accept(), this method will return a connected socket.
As with listen(), it registers with a McSocketListener to listen on the multicast address, then waits until a request is received with a matching request type and socket type. accept() will return with a pointer to a connected TCP Socket for a McSocket<Socket>, or to a DatagramSocket for a McSocket<DatagramSocket>, with a default destination address of the requesting host and port. The caller owns the pointer to the socket and is responsible for closing and deleting it when done.
IOException |
References VLOG.
Referenced by nidas::core::McSocket::connect(), nidas::core::McSocketUDP::connect(), and nidas::core::MultipleUDPSockets::connect().
|
virtual |
Unregister this McSocket from the multicasting and listening threads.
IOException |
References nidas::util::McSocketListener::close(), and VLOG.
Referenced by nidas::core::McSocket::close(), and nidas::core::McSocketUDP::close().
SocketT * nidas::util::McSocket< SocketT >::connect | ( | Inet4PacketInfoX & | pktinfo | ) |
Do a request(), and then wait until a TCP connection is established, or a UDP datagram is received back.
Returns the pointer to the connected TCP Socket or the DatagramSocket. McSocket then owns the pointer to the socket and is responsible for closing and deleting it when done.
IOException |
References VLOG.
Referenced by nidas::core::McSocket::connect(), and nidas::core::McSocketUDP::connect().
|
inlinevirtual |
Virtual method that is called when a socket connection is established.
This must be implemented in derived classes if the user wants to implement an asynchronous, two-step connection using listen() instead of accept() for incoming requests or request() instead of connect() for outgoing requests.
sock | The socket which is ready for I/O. McSocket then owns the pointer to the socket and is responsible for closing and deleting it when done. |
Reimplemented in nidas::core::McSocketUDP::MyMcSocket, and nidas::core::McSocket::MyMcSocket.
|
inline |
Get the multicast address for listening to requests.
References nidas::util::McSocket< SocketT >::_mcastAddr.
Referenced by nidas::core::McSocket::getInet4McastSocketAddress(), and nidas::core::McSocketUDP::getInet4McastSocketAddress().
|
inline |
References nidas::util::McSocket< SocketT >::_iface.
std::list< Inet4NetworkInterface > nidas::util::McSocket< SocketT >::getInterfaces | ( | ) | const |
Return all network interfaces on this system.
IOException |
|
inline |
Get the request type number.
References nidas::util::McSocket< SocketT >::_requestType.
Referenced by nidas::core::McSocket::getRequestType(), and nidas::core::McSocketUDP::getRequestType().
|
private |
References nidas::util::Logger::getInstance(), LOG_ERR, and VLOG.
void nidas::util::McSocket< SocketT >::listen | ( | ) |
Register with a McSocketListener to listen on the multicast address.
When a request is received on the socket port, with a matching request type and socket type, McSocketListener will either do a Socket::connect() to establish a TCP connection back to the requesting host and port if the socket type is SOCK_STREAM, or if the socket type is SOCK_DGRAM, will create a DatagramSocket with a default destination address of the requesting host and port. Once this has been done, McSocketListener will call the offer() method of this McSocket. offer() then calls the virtual connected() method, passing a pointer to the connected socket. Use either listen() or accept() to wait for a connection.
IOException |
References nidas::util::McSocketListener::accept().
Referenced by nidas::core::McSocket::requestConnection(), and nidas::core::McSocketUDP::requestConnection().
|
private |
References err.
|
private |
How a McSocketListener passes back a connected TCP socket or DatagramSocket.
offer() calls the connected() virtual method. McSocket will own the pointer to the socket and is responsible for closing and deleting it when done.
sock | A pointer to a Socket. May be null, in which case err will be a non-zero errno that occured. |
err | If sock is null, an errno. |
McSocket< SocketT > & nidas::util::McSocket< SocketT >::operator= | ( | const McSocket< SocketT > & | rhs | ) |
Assignment operator.
void nidas::util::McSocket< SocketT >::request | ( | ) |
Start issuing requests for a connection by multicasting McSocketDatagrams.
When a response is received, either in the form of a TCP socket connection in the case of a socket type of SOCK_STREAM, or a datagram is received in the case of a socket type of SOCK_DGRAM, then the connected() method is called.
IOException |
Referenced by nidas::core::McSocket::requestConnection(), and nidas::core::McSocketUDP::requestConnection().
|
inline |
Set the multicast address for listening to requests.
val | Multicast address to listen for McSocketDatagrams. |
References nidas::util::McSocket< SocketT >::_mcastAddr.
Referenced by nidas::core::McSocket::setInet4McastSocketAddress(), and nidas::core::McSocketUDP::setInet4McastSocketAddress().
|
inline |
Set a specific interface for the multicasts.
If a request() or connect() is done, then requests will be sent on this interface. If an listen() or accept() is done, then an MulticastSocket::joinInterface() will be done on this interface to listen for incoming datagrams. If the interface is not set, or is left at the default of INADDR_ANY, then McSocket will send on or join all available interfaces capable of multicast, including the loopback interface.
References nidas::util::McSocket< SocketT >::_iface.
|
inline |
Set the request type value.
This can be any number, agreed upon by the McSocket sending requests and the McSocket listening for requests.
val | Request number |
References nidas::util::McSocket< SocketT >::_requestType.
Referenced by nidas::core::McSocket::setRequestType(), and nidas::core::McSocketUDP::setRequestType().
|
friend |
McSocketListener and McSocketMulticaster are friends that setup the socket connection and call the private offer() method.
|
private |
|
private |
|
private |
|
private |
The thread we start which multicasts for connections.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |