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 #ifndef _XPCSocket 00034 #define _XPCSocket 00035 00036 #include "XPCGetProtocol.h" 00037 #include "XPCGetHostInfo.h" 00038 #include <stdio.h> 00039 //#include <string.h> 00040 00041 #ifdef UNIX 00042 #include <sys/socket.h> 00043 #include <unistd.h> 00044 #include <fcntl.h> 00045 #include <errno.h> 00046 #include <iostream> 00047 #include <sys/types.h> 00048 #include <sys/ioctl.h> 00049 #ifdef PLATFORM_LINUX 00050 #define FIONBIO 0x5421 00051 #endif 00052 #elif _WIN32 00053 #include <winsock2.h> 00054 #include "windows.h" 00055 #include "winbase.h" 00056 #include "winnt.h" 00057 typedef int socklen_t; 00058 #else 00059 #error "Looks like the build scripts didn't set the platform type" 00060 #endif 00061 00062 class XPCSocket 00063 { 00064 protected: 00065 int iPort; // Socket port number 00066 int iSocket; // Socket file descriptor 00067 int iBlocking; // Blocking flag 00068 char cBind; // Binding flag 00069 double m_dfLastRead; 00070 struct sockaddr_in clientAddress; // Address of the client that sent data 00071 public: 00072 void vSetRecieveTimeOut(int nTimeOut); 00073 void SetReadTime(double dfTime){m_dfLastRead = dfTime;}; 00074 double GetReadTime(){return m_dfLastRead;}; 00075 00076 //returns integer number of last socket error 00077 static int iGetLastError(); 00078 // Constructor. Creates a socket given a protocol (UDP / TCP) and a port number 00079 XPCSocket(const char *_sProtocol, int _iPort); 00080 00081 // Constructor. Stores a socket file descriptor 00082 XPCSocket(int _iSocket) : iSocket(_iSocket) { }; 00083 00084 // Destructor. Closes the socket 00085 virtual ~XPCSocket() 00086 { 00087 vCloseSocket(); 00088 } 00089 00090 // Closes the socket 00091 void vCloseSocket() 00092 { 00093 #ifdef WINDOWS_NT 00094 closesocket(iSocket); 00095 #else 00096 close(iSocket); 00097 #endif 00098 } 00099 00100 // The following member functions sets socket options on and off 00101 void vSetDebug(int _iToggle); 00102 void vSetBroadcast(int _iToggle); 00103 void vSetReuseAddr(int _iToggle); 00104 void vSetKeepAlive(int _iToggle); 00105 void vSetLinger(struct linger _lingerOption); 00106 void vSetSocketBlocking(int _iToggle); 00107 00108 // Sets the size of the send and receive buffer 00109 void vSetSendBuf(int _iSendBufSize); 00110 void vSetRecieveBuf(int _iRecieveBufSize); 00111 00112 // The following member functions retrieve socket option settings 00113 int iGetDebug(); 00114 int iGetBroadcast(); 00115 int iGetReuseAddr(); 00116 int iGetKeepAlive(); 00117 void vGetLinger(struct linger &_lingerOption); 00118 int iGetSendBuf(); 00119 int iGetRecieveBuf(); 00120 int iGetSocketBlocking() { return iBlocking; } 00121 00122 // Returns the socket file descriptor 00123 int iGetSocketFd() { return iSocket; } 00124 00125 // Gets the system error 00126 char *sGetError() 00127 { 00128 #ifdef UNIX 00129 return strerror(errno); 00130 #elif _WIN32 00131 static char buf[10]; 00132 sprintf(buf, "%d", WSAGetLastError()); 00133 return buf; 00134 #endif 00135 } 00136 }; 00137 00138 #endif 00139