Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArSocket.h

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #ifndef ARSOCKET_H
00027 #define ARSOCKET_H
00028 
00029 
00030 #ifndef WIN32
00031 #include <sys/time.h>
00032 #include <sys/types.h>
00033 #include <unistd.h>
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <sys/stat.h>
00037 #include <sys/param.h>
00038 #include <fcntl.h>
00039 #include <netinet/in.h>
00040 #include <stdio.h>
00041 #include <errno.h>
00042 #include <stdarg.h>
00043 #endif
00044 
00045 
00046 #include <string>
00047 #include "ariaTypedefs.h"
00048 #include "ArMutex.h"
00049 
00050 class ArFunctor;
00051 
00052 
00054 
00072 class ArSocket
00073 {
00074 public:
00075 
00076   enum Type {UDP, TCP, Unknown};
00077   enum Error {NoErr, NetFail, ConBadHost, ConNoRoute, ConRefused};
00078 
00080   AREXPORT ArSocket();
00081 
00083   AREXPORT ArSocket(const char *host, int port, Type type);
00084 
00086   AREXPORT ArSocket(int port, bool doClose, Type type);
00087 
00089   AREXPORT ~ArSocket();
00090 
00092   AREXPORT static bool init();
00093 
00095   AREXPORT static void shutdown();
00096 
00097   AREXPORT static bool ourInitialized;
00098 
00100   AREXPORT bool copy(int fd, bool doclose);
00101 
00103   AREXPORT void copy(ArSocket *s)
00104     {myFD=s->myFD; myDoClose=false; mySin=s->mySin;}
00105 
00107 
00110   AREXPORT void transfer(ArSocket *s)
00111     {myFD=s->myFD; myDoClose=true; s->myDoClose=false; mySin=s->mySin;
00112      myType=s->myType; strcpy(myIPString, s->myIPString); }
00113 
00115   AREXPORT bool connect(const char *host, int port, Type type = TCP);
00116 
00120   AREXPORT bool open(int port, Type type, const char *openOnIP = NULL);
00121 
00123   AREXPORT bool create(Type type);
00124 
00129   AREXPORT bool findValidPort(int startPort, const char *openOnIP = NULL);
00130 
00132   AREXPORT bool connectTo(const char *host, int port);
00133 
00135   AREXPORT bool connectTo(struct sockaddr_in *sin);
00136 
00138   AREXPORT bool accept(ArSocket *sock);
00139 
00141   AREXPORT bool close();
00142 
00144   AREXPORT int write(const void *buff, size_t len);
00145 
00147   AREXPORT int read(void *buff, size_t len, unsigned int msWait = 0);
00148 
00150   AREXPORT int sendTo(const void *msg, int len);
00151 
00153   AREXPORT int sendTo(const void *msg, int len, struct sockaddr_in *sin);
00154 
00156   AREXPORT int recvFrom(void *msg, int len, sockaddr_in *sin);
00157 
00159   AREXPORT static bool hostAddr(const char *host, struct in_addr &addr);
00160 
00162   AREXPORT static bool addrHost(struct in_addr &addr, char *host);
00163 
00165   AREXPORT static std::string getHostName();
00166 
00168   AREXPORT bool getSockName();
00169 
00171   struct sockaddr_in * sockAddrIn() {return(&mySin);}
00172 
00174   struct in_addr * inAddr() {return(&mySin.sin_addr);}
00175 
00177   unsigned short int inPort() {return(mySin.sin_port);}
00178 
00180   AREXPORT static void inToA(struct in_addr *addr, char *buff);
00181 
00183   static const size_t sockAddrLen() {return(sizeof(struct sockaddr_in));}
00184 
00185 #ifdef WIN32
00186 
00187   static const size_t maxHostNameLen() {return(MAXGETHOSTSTRUCT);}
00188 #else
00189 
00190   static const size_t maxHostNameLen() {return(MAXHOSTNAMELEN);}
00191 #endif
00192 
00194   AREXPORT static unsigned int hostToNetOrder(int i);
00195 
00197   AREXPORT static unsigned int netToHostOrder(int i);
00198 
00200   AREXPORT bool setLinger(int time);
00201 
00203   AREXPORT bool setBroadcast();
00204 
00206   AREXPORT bool setReuseAddress();
00207 
00209   AREXPORT bool setNonBlock();
00210 
00212   AREXPORT void setDoClose(bool yesno) {myDoClose=yesno;}
00213 
00215   AREXPORT int getFD() const {return(myFD);}
00216 
00218   AREXPORT Type getType() const {return(myType);}
00219 
00221   AREXPORT const std::string & getErrorStr() const {return(myErrorStr);}
00222 
00224   AREXPORT Error getError() const {return(myError);}
00225 
00226 #ifndef SWIG
00227 
00230   AREXPORT int writeString(const char *str, ...);
00231 #endif
00232 
00233   AREXPORT int writeStringPlain(const char *str) { return writeString(str); }
00235   AREXPORT char *readString(void);
00237   AREXPORT void setEcho(bool echo) 
00238   { myStringAutoEcho = false; myStringEcho = echo; }
00240   AREXPORT bool getEcho(void) { return myStringEcho; }
00242   AREXPORT void setLogWriteStrings(bool logWriteStrings) 
00243     { myLogWriteStrings = logWriteStrings; }
00245   AREXPORT bool getLogWriteStrings(void) { return myLogWriteStrings; }
00247   AREXPORT const char *getIPString(void) const { return myIPString; }
00249   AREXPORT void setCloseCallback(ArFunctor *functor) 
00250     { myCloseFunctor = functor; }
00252   AREXPORT ArFunctor *getCloseCallback(void) { return myCloseFunctor; }
00254   long getSends(void) { return mySends; }
00256   long getBytesSent(void) { return myBytesSent; }
00258   long getRecvs(void) { return myRecvs; }
00260   long getBytesRecvd(void) { return myBytesRecvd; }
00262   void resetTracking(void) 
00263     { mySends = 0; myBytesSent = 0; myRecvs = 0; myBytesRecvd = 0; }
00264   
00266   AREXPORT bool setNoDelay(bool flag);
00267 
00268 protected:
00270   void setIPString(void);
00272   void doStringEcho(void);
00273   // internal crossplatform init (mostly for string reading stuff)
00274   void internalInit(void);
00275 
00276   Type myType;
00277   Error myError;
00278   std::string myErrorStr;
00279   bool myDoClose;
00280   int myFD;
00281   bool myNonBlocking;
00282   struct sockaddr_in mySin;
00283 
00284   bool myLogWriteStrings;
00285   ArMutex myReadStringMutex;
00286   ArMutex myWriteStringMutex;
00287   bool myStringAutoEcho;
00288   bool myStringEcho;
00289   char myStringBuf[1100];
00290   size_t myStringPos;
00291   char myStringBufEmpty[1];
00292   size_t myStringPosLast;
00293   char myIPString[128];
00294   bool myStringGotEscapeChars;
00295   bool myStringGotComplete;
00296   bool myStringHaveEchoed;
00297 
00298   long mySends;
00299   long myBytesSent;
00300   long myRecvs;
00301   long myBytesRecvd;
00302   
00303   // A functor to call when the socket closes
00304   ArFunctor *myCloseFunctor;
00305 };
00306 
00307 
00308 #endif // ARSOCKET_H
00309   

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0