Class DUdpModule



Provides UDP functionality to a DDispatcher. DUdpModule uses events to provide notifications, see the Event Handling section for more.

The steps to enable UDP in a DDispatcher are as follows:

  1. Create the DUdpModule and bind it to the desired port.
  2. Call DDispatcher::requestUdp, which accepts a pointer to the DUdpModule. At this point, the DDispatcher should already be connected to a remote host through TCP.
  3. The DDispatcher automatically sends a UDP request to the remote host, causing the remote host receive a udpRequestHandler event.
  4. The remote host creates and binds a DUdpModule, and points the udpRequestHandler's DUdpModule parameter at it.
  5. The remote host automatically sends a UDP result message back to us, which triggers our udpResultHandler.

The same DUdpModule can provide UDP to multiple DDispatchers. Received UDP messages are automatically sent to the proper DDispatcher based on the sender's port and IP address, as well as a unique code added to the UDP packet by DUdpModule.



DUdpModule(DEngine *netEngine)

DUdpModule constructor. netEngine is a pointer to the DEngine that will generate the module's events.


bool bind(IpVersion ipVersion, uint16_t portNumber)

Binds the socket to the specified portNumber. IpVersion should be set to DA_IPV4 or DA_IPV6. Returns false if an error occurred and sets errMsg.


virtual bool closeSocket(void)

Closes a socket that was opened with bind(). Returns false on failure and sets errMsg with a description of the problem.


string getErrMsg(void)

Returns a description of the most recent error for this socket. Returns an empty string if there haven't been any errors yet.


uint16_t getPortNumber(void)

Returns the port to which this socket is bound, or 0 if the socket is not bound to any port.


void resolveHost(const string &hostName, IpVersion ipVersion, boost::function<void (vector<string>)> resolveHandler)

Given a hostName of the form www.google.com, obtains the associated IP address using DNS. ipVersion can be set to DA_IPV4 or DA_IPV6. resolveHandler will be called when resolveHost finishes, and is given a vector of strings containing the matching IP addresses. If no addresses are returned (vector size is zero), errMsg will be set with a description of the problem.


void setErrorHandler(boost::function<void (string, bool)> handler)

Set the event handler to be called when an error occurs in an asynchronous operation. The string parameter contains a description of the error. The bool parameter, socketOK, indicates if this was an unrecoverable error and the socket was automatically closed. True means the socket is still open, false means it was closed.





















Back to class reference