MOOS 0.2375
|
00001 00002 // 00003 // MOOS - Mission Oriented Operating Suite 00004 // 00005 // A suit of Applications and Libraries for Mobile Robotics Research 00006 // Copyright (C) 2001-2005 Massachusetts Institute of Technology and 00007 // Oxford University. 00008 // 00009 // This software was written by Paul Newman at MIT 2001-2002 and Oxford 00010 // University 2003-2005. email: pnewman@robots.ox.ac.uk. 00011 // 00012 // This file is part of a MOOS Core Component. 00013 // 00014 // This program is free software; you can redistribute it and/or 00015 // modify it under the terms of the GNU General Public License as 00016 // published by the Free Software Foundation; either version 2 of the 00017 // License, or (at your option) any later version. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00022 // General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00029 // The XPC classes in MOOS are modified versions of the source provided 00030 // in "Making UNIX and Windows NT Talk" by Mark Nadelson and Thomas Haga 00031 // 00033 #include "XPCSocket.h" 00034 00035 XPCSocket::XPCSocket(const char *_sProtocol, int _iPort) 00036 { 00037 iPort = _iPort; 00038 iBlocking = 0; 00039 00040 try 00041 { 00042 // Retrieve the socket protocol 00043 XPCGetProtocol socketProtocol(_sProtocol); 00044 00045 // If the protocol is UDP a UDP socket is created 00046 if (strcmp(socketProtocol.sGetProtocolName(), "udp") == 0) 00047 { 00048 if ((iSocket = socket(AF_INET, SOCK_DGRAM, socketProtocol.iGetProtocolNumber())) == -1) 00049 { 00050 char sMsg[512]; 00051 00052 sprintf(sMsg, "Error opening socket: %s", sGetError()); 00053 XPCException socketExcept(sMsg); 00054 throw socketExcept; 00055 return; 00056 } 00057 } 00058 00059 // If the protocol is TCP a TCP socket is created 00060 else if (strcmp(socketProtocol.sGetProtocolName(), "tcp") == 0) 00061 { 00062 if ((iSocket = socket(AF_INET, SOCK_STREAM, socketProtocol.iGetProtocolNumber())) == -1) 00063 { 00064 char sMsg[512]; 00065 00066 sprintf(sMsg, "Error opening socket: %s", sGetError()); 00067 XPCException socketExcept(sMsg); 00068 throw socketExcept; 00069 return; 00070 } 00071 } 00072 } 00073 catch(XPCException &exceptObject) 00074 { 00075 char sMsg[512]; 00076 00077 sprintf(sMsg, "Protocol Error Definition: %s", exceptObject.sGetException()); 00078 XPCException socketExcept(sMsg); 00079 throw socketExcept; 00080 return; 00081 } 00082 00083 // The client address is initialized to all addresses at the specified port 00084 clientAddress.sin_family = AF_INET; 00085 clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); 00086 clientAddress.sin_port = htons(iPort); 00087 } 00088 00089 void XPCSocket::vSetDebug(int _iToggle) 00090 { 00091 if (setsockopt(iSocket, SOL_SOCKET, SO_DEBUG, (char *)&_iToggle, sizeof(_iToggle)) == -1) 00092 { 00093 char sMsg[512]; 00094 00095 sprintf(sMsg, "Error Setting Debug Option: %s", sGetError()); 00096 XPCException sockOptExcept(sMsg); 00097 throw sockOptExcept; 00098 return; 00099 } 00100 } 00101 00102 void XPCSocket::vSetBroadcast(int _iToggle) 00103 { 00104 if (setsockopt(iSocket, SOL_SOCKET, SO_BROADCAST, (char *)&_iToggle, sizeof(_iToggle)) == -1) 00105 { 00106 char sMsg[512]; 00107 00108 sprintf(sMsg, "Error Setting Broadcast Option: %s", sGetError()); 00109 XPCException sockOptExcept(sMsg); 00110 throw sockOptExcept; 00111 return; 00112 } 00113 } 00114 00115 void XPCSocket::vSetReuseAddr(int _iToggle) 00116 { 00117 if (setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&_iToggle, sizeof(_iToggle)) == -1) 00118 { 00119 char sMsg[512]; 00120 00121 sprintf(sMsg, "Error Setting Reuseaddr Option: %s", sGetError()); 00122 XPCException sockOptExcept(sMsg); 00123 throw sockOptExcept; 00124 return; 00125 } 00126 } 00127 00128 void XPCSocket::vSetKeepAlive(int _iToggle) 00129 { 00130 if (setsockopt(iSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&_iToggle, sizeof(_iToggle)) == -1) 00131 { 00132 char sMsg[512]; 00133 00134 sprintf(sMsg, "Error Setting Keepalive Option: %s", sGetError()); 00135 XPCException sockOptExcept(sMsg); 00136 throw sockOptExcept; 00137 return; 00138 } 00139 } 00140 00141 void XPCSocket::vSetLinger(struct linger _lingerOption) 00142 { 00143 if (setsockopt(iSocket, SOL_SOCKET, SO_LINGER, (char *)&_lingerOption, sizeof(struct linger)) == -1) 00144 { 00145 char sMsg[512]; 00146 00147 sprintf(sMsg, "Error Setting Linger Option: %s", sGetError()); 00148 XPCException sockOptExcept(sMsg); 00149 throw sockOptExcept; 00150 return; 00151 } 00152 } 00153 00154 void XPCSocket::vSetSendBuf(int _iSendBufSize) 00155 { 00156 if (setsockopt(iSocket, SOL_SOCKET, SO_SNDBUF, (char *)&_iSendBufSize, sizeof(_iSendBufSize)) == -1) 00157 { 00158 char sMsg[512]; 00159 00160 sprintf(sMsg, "Error Setting SendBufSize Option: %s", sGetError()); 00161 XPCException sockOptExcept(sMsg); 00162 throw sockOptExcept; 00163 return; 00164 } 00165 } 00166 00167 void XPCSocket::vSetRecieveBuf(int _iRecieveBufSize) 00168 { 00169 if (setsockopt(iSocket, SOL_SOCKET, SO_SNDBUF, (char *)&_iRecieveBufSize, sizeof(_iRecieveBufSize)) == -1) 00170 { 00171 char sMsg[512]; 00172 00173 sprintf(sMsg, "Error Setting RecieveBufSize Option: %s", sGetError()); 00174 XPCException sockOptExcept(sMsg); 00175 throw sockOptExcept; 00176 return; 00177 } 00178 } 00179 00180 void XPCSocket::vSetSocketBlocking(int _iToggle) 00181 { 00182 char sMsg[512]; 00183 00184 if (_iToggle) 00185 { 00186 if (iGetSocketBlocking()) 00187 return; 00188 else 00189 { 00190 iBlocking = 1; 00191 // Socket blocking is turned ON 00192 #ifdef WINDOWS_NT 00193 if (ioctlsocket(iSocket, FIONBIO, (unsigned long *)&iBlocking) == -1) 00194 #else 00195 if (ioctl(iSocket, FIONBIO, (char *)&iBlocking) == -1) 00196 #endif 00197 { 00198 sprintf(sMsg, "Error Turning ON Socket Blocking Status: %s", sGetError()); 00199 XPCException sockOptExcept(sMsg); 00200 throw sockOptExcept; 00201 return; 00202 } 00203 00204 } 00205 } 00206 else 00207 { 00208 if (!iGetSocketBlocking()) 00209 return; 00210 else 00211 { 00212 iBlocking = 0; 00213 // Socket blocking is turned off 00214 #ifdef WINDOWS_NT 00215 if (ioctlsocket(iSocket, FIONBIO, (unsigned long *)&iBlocking) == -1) 00216 #else 00217 if (ioctl(iSocket, FIONBIO, (char *)&iBlocking) == -1) 00218 #endif 00219 { 00220 sprintf(sMsg, "Error Turning OFF Socket Blocking Status: %s", sGetError()); 00221 XPCException sockOptExcept(sMsg); 00222 throw sockOptExcept; 00223 return; 00224 } 00225 00226 } 00227 } 00228 } 00229 00230 int XPCSocket::iGetDebug() 00231 { 00232 int iGetOption; 00233 00234 int iOptionLen = sizeof(iGetOption); 00235 00236 if (getsockopt(iSocket, SOL_SOCKET, SO_DEBUG, (char *)&iGetOption,(socklen_t*) &iOptionLen) == -1) 00237 { 00238 char sMsg[512]; 00239 00240 sprintf(sMsg, "Error Recieving Debug Option: %s", sGetError()); 00241 XPCException sockOptExcept(sMsg); 00242 throw sockOptExcept; 00243 return -1; 00244 } 00245 return iGetOption; 00246 } 00247 00248 int XPCSocket::iGetBroadcast() 00249 { 00250 int iGetOption; 00251 00252 int iOptionLen = sizeof(iGetOption); 00253 00254 if (getsockopt(iSocket, SOL_SOCKET, SO_BROADCAST, (char *)&iGetOption,(socklen_t*) &iOptionLen) == -1) 00255 { 00256 char sMsg[512]; 00257 00258 sprintf(sMsg, "Error Extracting Broadcast Option: %s", sGetError()); 00259 XPCException sockOptExcept(sMsg); 00260 throw sockOptExcept; 00261 return -1; 00262 } 00263 return iGetOption; 00264 } 00265 00266 int XPCSocket::iGetReuseAddr() 00267 { 00268 int iGetOption; 00269 00270 int iOptionLen = sizeof(iGetOption); 00271 00272 if (getsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&iGetOption,(socklen_t*) &iOptionLen) == -1) 00273 { 00274 char sMsg[512]; 00275 00276 sprintf(sMsg, "Error Extracting Resuseaddr Option: %s", sGetError()); 00277 XPCException sockOptExcept(sMsg); 00278 throw sockOptExcept; 00279 return -1; 00280 } 00281 return iGetOption; 00282 } 00283 00284 int XPCSocket::iGetKeepAlive() 00285 { 00286 int iGetOption; 00287 00288 int iOptionLen = sizeof(iGetOption); 00289 00290 if (getsockopt(iSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&iGetOption,(socklen_t*) &iOptionLen) == -1) 00291 { 00292 char sMsg[512]; 00293 00294 sprintf(sMsg, "Error Extracting Keepalive Option: %s", sGetError()); 00295 XPCException sockOptExcept(sMsg); 00296 throw sockOptExcept; 00297 return -1; 00298 } 00299 return iGetOption; 00300 } 00301 00302 void XPCSocket::vGetLinger(struct linger &_lingerOption) 00303 { 00304 int iOptionLen = sizeof(struct linger); 00305 00306 if (getsockopt(iSocket, SOL_SOCKET, SO_LINGER, (char *)&_lingerOption,(socklen_t*) &iOptionLen) == -1) 00307 { 00308 char sMsg[512]; 00309 00310 sprintf(sMsg, "Error Extracting Linger Option: %s", sGetError()); 00311 XPCException sockOptExcept(sMsg); 00312 throw sockOptExcept; 00313 return; 00314 } 00315 return; 00316 } 00317 00318 int XPCSocket::iGetSendBuf() 00319 { 00320 int iSendBuf; 00321 00322 int iOptionLen = sizeof(iSendBuf); 00323 00324 if (getsockopt(iSocket, SOL_SOCKET, SO_SNDBUF, (char *)&iSendBuf,(socklen_t*) &iOptionLen) == -1) 00325 { 00326 char sMsg[512]; 00327 00328 sprintf(sMsg, "Error Extracting SendBuf Option: %s", sGetError()); 00329 XPCException sockOptExcept(sMsg); 00330 throw sockOptExcept; 00331 return -1; 00332 } 00333 return iSendBuf; 00334 } 00335 00336 int XPCSocket::iGetRecieveBuf() 00337 { 00338 int iRcvBuf; 00339 00340 int iOptionLen = sizeof(iRcvBuf); 00341 00342 if (getsockopt(iSocket, SOL_SOCKET, SO_RCVBUF, (char *)&iRcvBuf,(socklen_t*) &iOptionLen) == -1) 00343 { 00344 char sMsg[512]; 00345 00346 sprintf(sMsg, "Error Extracting RcvBuf Option: %s", sGetError()); 00347 XPCException sockOptExcept(sMsg); 00348 throw sockOptExcept; 00349 return -1; 00350 } 00351 return iRcvBuf; 00352 } 00353 00354 int XPCSocket::iGetLastError() 00355 { 00356 #ifdef UNIX 00357 return errno; 00358 #else 00359 return WSAGetLastError(); 00360 #endif 00361 } 00362 00363 void XPCSocket::vSetRecieveTimeOut(int nTimeOut) 00364 { 00365 if (setsockopt(iSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&nTimeOut, sizeof(nTimeOut)) == -1) 00366 { 00367 char sMsg[512]; 00368 00369 sprintf(sMsg, "Error Setting ReciveTimeOut Option: %s", sGetError()); 00370 XPCException sockOptExcept(sMsg); 00371 throw sockOptExcept; 00372 return; 00373 } 00374 }