#include <vsisocket.h>
Public Member Functions | |
CvsiUDPsocket () | |
~CvsiUDPsocket () | |
int | Initialize (vsiIPaddress *LocalAddress) |
int | Close () |
int | IsOpen () |
int | Send (vsiIPaddress *Target, char *Buffer, int Size) |
int | Receive (vsiIPaddress *Source, char *Buffer, int *Size) |
Private Attributes | |
int | m_Socket |
Definition at line 39 of file vsisocket.h.
CvsiUDPsocket::CvsiUDPsocket | ( | ) |
Definition at line 71 of file vsisocket.cpp.
00072 { 00073 m_Socket = VSI_INVALID_SOCKET; 00074 }
CvsiUDPsocket::~CvsiUDPsocket | ( | ) |
Definition at line 76 of file vsisocket.cpp.
00077 { 00078 if(m_Socket != VSI_INVALID_SOCKET) 00079 { 00080 Close(); 00081 } 00082 }
int CvsiUDPsocket::Close | ( | void | ) |
Definition at line 135 of file vsisocket.cpp.
00136 { 00137 if(m_Socket != VSI_INVALID_SOCKET) 00138 { 00139 close(m_Socket); 00140 m_Socket = VSI_INVALID_SOCKET; 00141 return (0); 00142 } 00143 return VSI_ERROR_SOCKET_NOT_OPEN; 00144 }
int CvsiUDPsocket::Initialize | ( | vsiIPaddress * | LocalAddress | ) |
Definition at line 96 of file vsisocket.cpp.
00097 { 00098 int sock; 00099 struct sockaddr_in myaddress; 00100 00101 if(m_Socket != VSI_INVALID_SOCKET) 00102 { 00103 Close(); 00104 } 00105 00106 // create UDP socket 00107 sock = socket (AF_INET, SOCK_DGRAM, 0); 00108 if(sock < 0 ) 00109 { 00110 return VSI_ERROR_SOCKET_CREATION; 00111 } 00112 00113 // Initialize local address 00114 myaddress.sin_family = AF_INET; 00115 myaddress.sin_port = LocalAddress->Port; 00116 00117 if(LocalAddress->Host == VSI_SOCKET_UNDEFINED_IP_ADDRESS) 00118 myaddress.sin_addr.s_addr = INADDR_ANY; 00119 else 00120 myaddress.sin_addr.s_addr = LocalAddress->Host; 00121 00122 // bind socket to local IP address 00123 if( bind (sock,(sockaddr *) &myaddress,sizeof(myaddress)) < 0) 00124 { 00125 close (sock); 00126 return VSI_ERROR_SOCKET_BIND; 00127 } 00128 00129 m_Socket = sock; 00130 return (0); 00131 }
int CvsiUDPsocket::IsOpen | ( | ) |
Definition at line 86 of file vsisocket.cpp.
00087 { 00088 if(m_Socket == VSI_INVALID_SOCKET) 00089 return false; 00090 else 00091 return true; 00092 }
int CvsiUDPsocket::Receive | ( | vsiIPaddress * | Source, | |
char * | Buffer, | |||
int * | Size | |||
) |
Definition at line 197 of file vsisocket.cpp.
00198 { 00199 struct sockaddr_in orig; 00200 unsigned int origsize = sizeof(orig); 00201 00202 if(m_Socket == VSI_INVALID_SOCKET) 00203 { 00204 return VSI_ERROR_SOCKET_NOT_OPEN; 00205 } 00206 00207 int nb = recvfrom ( m_Socket, Buffer, *nBytes, MSG_WAITALL, 00208 (sockaddr*) &orig, &origsize); 00209 00210 if(nb < 0) 00211 { 00212 Rioperror("CvsiUDPsocket::Receive()"); 00213 return VSI_ERROR_SOCKET_RECV; 00214 } 00215 // Update source address structure 00216 Source->Host = orig.sin_addr.s_addr; 00217 Source->Port = orig.sin_port; 00218 00219 *nBytes = nb; 00220 00221 return (0); 00222 }
int CvsiUDPsocket::Send | ( | vsiIPaddress * | Target, | |
char * | Buffer, | |||
int | Size | |||
) |
Definition at line 148 of file vsisocket.cpp.
00149 { 00150 struct sockaddr_in dest; 00151 00152 if(m_Socket == VSI_INVALID_SOCKET) 00153 { 00154 return VSI_ERROR_SOCKET_NOT_OPEN; 00155 } 00156 00157 // Initialize destination address structure 00158 dest.sin_family = AF_INET; 00159 dest.sin_addr.s_addr = Target->Host; 00160 dest.sin_port = Target->Port; 00161 00162 int nb; 00163 00164 // loop necessary for error ECONREFUSED which is ignored 00165 // (see comment bellow) 00166 while(1) 00167 { 00168 nb = sendto ( m_Socket, Buffer, nBytes, MSG_WAITALL, 00169 (sockaddr*) &dest, sizeof (dest)); 00170 // Check if there was an error 00171 if(nb < 0) 00172 { 00173 /* can get ECONREFUSED on any socket operation from 00174 the same socket which sent a packet which caused 00175 an ICMP error from some host - ignore these 00176 (which were for a previous sent packet) 00177 and resend this packet */ 00178 if(errno != ECONNREFUSED) 00179 { 00180 return VSI_ERROR_SOCKET_SEND; 00181 } 00182 } 00183 else 00184 { 00185 if(nb != nBytes) 00186 { 00187 return VSI_ERROR_SOCKET_SEND_INCOMPLETE; 00188 } 00189 return (0); 00190 } 00191 } 00192 }
int CvsiUDPsocket::m_Socket [private] |
Definition at line 50 of file vsisocket.h.