Class DUdpSocket



A wrapper around a Boost UDP socket. DUdpSocket uses events to provide notifications, see the Event Handling section for more.


DUdpSocket(DEngine *netEngine)

DUdpSocket constructor. netEngine is a pointer to the DEngine that will generate the socket'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.


void clearAllHandlers(void)

Convenience method that removes all user-defined event handlers from this socket. For example, handlers set with setReceiveHandler, setSendHandler, etc.


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 send(const DBuffer &theData, const string &ipAddress, uint16_t destPort)

Sends theData to the specified IP address and port number. The socket must be bound before send can be used. Up to 65507 bytes can be sent at a time.


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.


void setReceiveHandler(boost::function<void (DBuffer&, string&, uint16_t)> handler)

Set the event handler to be called when we've received data on this socket. The DBuffer contains the data, the string is the remote host's IP address, and the uint16_t is the remote host's port number.


void setSendHandler(boost::function<void (void)> handler)

Set the event handler to be called when all pending data has been sent.





















Back to class reference