1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* Modification History
*
* 2004-November-3 Jason Rohrer
* Created.
*
* 2004-November-4 Jason Rohrer
* Added default receive timeout (infinite).
*
* 2004-November-9 Jason Rohrer
* Added functions for comparing and copying UDPAddresses.
*/
#ifndef SOCKET_UDP_INCLUDED
#define SOCKET_UDP_INCLUDED
/**
* Structure representing a UDP endpoint.
*/
struct UDPAddress {
// binary internet address in network byte order
unsigned long mIPAddress;
// port number in network byte order
unsigned short mPort;
};
/**
* Network socket that can be used as an endpoint for sending and receiving
* UDP packets (unreliable datagrams).
*
* Note: Implementation for the functions defined here is provided
* separately for each platform (in the unix/ and win32/ subdirectories).
*
* Socket::initSocketFramework() must be called once before this class
* is used.
*
* @author Jason Rohrer
*/
class SocketUDP {
public:
/**
* Constructs a UDP socket and starts listening for incoming datagrams.
*
* @param inReceivePort the port to listen on, in platform-dependent
* byte order.
*/
SocketUDP( unsigned short inReceivePort );
~SocketUDP();
/**
* Makes a UDPAddress structure.
*
* @param inAddress the IP address in ascii numbers-and-dots notation.
* Must be destroyed by caller if non-const.
* @param inPort the port number in platform-specific byte order.
*
* @return an address structure, or NULL if converting the address
* fails.
* Must be destroyed by caller if non-NULL.
*/
static struct UDPAddress *makeAddress( const char *inAddress,
unsigned short inPort );
/**
* Extracts address elements from a UDPAddress structure.
*
* @param inAddress the address structure. Must be destroyed by
* caller.
* @param outPort pointer to where the port number, in
* platform-specific byte order, should be returned.
*
* @return the IP address in ascci numbers-and-dots notation.
* Must be destroyed by caller.
*/
static char *extractAddress( struct UDPAddress *inAddress,
unsigned short *outPort );
/**
* Compares two UDP addresses.
*
* @param inFirst the first address.
* @param inSecond the second address.
*
* @return true if the addresses are equal, or false if they are
* different.
*/
static char compare( struct UDPAddress *inFirst,
struct UDPAddress *inSecond );
/**
* Makes a copy of a UDP address.
*
* @param inAddress the address to copy.
*
* @return a copy of the address. Must be destroyed by caller.
*/
static struct UDPAddress *copy( struct UDPAddress *inAddress );
/**
* Sends a datagram through this socket.
*
* Note: the recommended maximum data length is 512 bytes
* to ensure that the datagram can be routed without
* fragmentation through all spec-compliant routers.
* Most routers support larger datagrams, however.
*
* @param inAddress the address to send data through. Must be
* destroyed by caller.
* @param inData the data bytes to send.
* @param inNumBytes the number of bytes to send.
*
* @return the number of bytes sent successfully,
* or -1 for a socket error.
*/
int send( struct UDPAddress *inAddress,
unsigned char *inData, unsigned long inNumBytes );
/**
* Receives a datagram from this socket.
*
* @param outAddress pointer to where the address of the remote
* host (the datagram sender) should be returned.
* Will be set to NULL on socket error or timeout.
* Must be destroyed by caller if non-NULL.
* @param outData pointer to where the received data should be
* returned. Will be set to NULL on socket error or timeout.
* Must be destroyed by caller if non-NULL.
* @param inTimeout the timeout for this receive operation in
* milliseconds. Set to -1 for an infinite timeout.
* -2 is returned from this call in the event of a timeout.
* Defaults to -1.
*
* @return the number of bytes received successfully,
* -1 for a socket error, or -2 for a timeout.
*/
int receive( struct UDPAddress **outAddress,
unsigned char **outData,
long inTimeout = -1 );
/**
* Used by platform-specific implementations.
*/
void *mNativeObjectPointer;
};
inline char SocketUDP::compare( struct UDPAddress *inFirst,
struct UDPAddress *inSecond ) {
if( inFirst->mIPAddress == inSecond->mIPAddress &&
inFirst->mPort == inSecond->mPort ) {
return true;
}
else {
return false;
}
}
inline struct UDPAddress *SocketUDP::copy( struct UDPAddress *inAddress ) {
struct UDPAddress *returnAddress = new struct UDPAddress;
returnAddress->mIPAddress = inAddress->mIPAddress;
returnAddress->mPort = inAddress->mPort;
return returnAddress;
}
#endif
|