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

ArSocket.h

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
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 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #ifndef ARSOCKET_H
00028 #define ARSOCKET_H
00029 
00030 
00031 #ifndef WIN32
00032 #include <sys/time.h>
00033 #include <sys/types.h>
00034 #include <unistd.h>
00035 #include <sys/types.h>
00036 #include <sys/socket.h>
00037 #include <sys/stat.h>
00038 #include <sys/param.h>
00039 #include <fcntl.h>
00040 #include <netinet/in.h>
00041 #include <stdio.h>
00042 #include <errno.h>
00043 #include <stdarg.h>
00044 #endif
00045 
00046 
00047 #include <string>
00048 #include "ariaTypedefs.h"
00049 
00050 class ArFunctor;
00051 
00053 
00067 class ArSocket
00068 {
00069 public:
00070 
00071   enum Type {UDP, TCP, Unknown};
00072   enum Error {NoErr, NetFail, ConBadHost, ConNoRoute, ConRefused};
00073 
00075   ArSocket();
00076 
00078   ArSocket(const char *host, int port, Type type);
00079 
00081   ArSocket(int port, bool doClose, Type type);
00082 
00084   ~ArSocket();
00085 
00087   static bool init();
00088 
00090   static void shutdown();
00091 
00092   static bool ourInitialized;
00093 
00095   bool copy(int fd, bool doclose);
00096 
00098   void copy(ArSocket *s)
00099     {myFD=s->myFD; myDoClose=false; mySin=s->mySin;}
00100 
00102 
00105   void transfer(ArSocket *s)
00106     {myFD=s->myFD; myDoClose=true; s->myDoClose=false; mySin=s->mySin;
00107      myType=s->myType; strcpy(myIPString, s->myIPString); }
00108 
00110   bool connect(const char *host, int port, Type type = TCP);
00111 
00113   bool open(int port, Type type);
00114 
00116   bool create(Type type);
00117 
00119   bool findValidPort(int startPort);
00120 
00122   bool connectTo(const char *host, int port);
00123 
00125   bool connectTo(struct sockaddr_in *sin);
00126 
00128   bool accept(ArSocket *sock);
00129 
00131   bool close();
00132 
00134 
00139   int write(const void *buff, size_t len)
00140     {
00141       struct timeval tval;
00142       fd_set fdSet;
00143       tval.tv_sec = 0;
00144       tval.tv_usec = 0;
00145       FD_ZERO(&fdSet);
00146       FD_SET(myFD, &fdSet);
00147       if (select(myFD + 1, NULL, &fdSet, NULL, &tval) <= 0)
00148         return 0;
00149       
00150 #ifdef WIN32
00151       return(::send(myFD, (char*)buff, len, 0));
00152 #else
00153       return(::write(myFD, (char*)buff, len));
00154 #endif
00155     }
00156 
00158 
00164   int read(void *buff, size_t len, unsigned int msWait = 0)
00165     {
00166       if (msWait != 0)
00167       {
00168         struct timeval tval;
00169         fd_set fdSet;
00170         tval.tv_sec = msWait / 1000;
00171         tval.tv_usec = (msWait % 1000) * 1000;
00172         FD_ZERO(&fdSet);
00173         FD_SET(myFD, &fdSet);
00174         if (select(myFD + 1, &fdSet, NULL, NULL, &tval) <= 0)
00175           return 0;
00176       }
00177       return(::recv(myFD, (char*)buff, len, 0));
00178     }
00179 
00181   int sendTo(const void *msg, int len)
00182     {return(::sendto(myFD, (char*)msg, len, 0, (struct sockaddr*)&mySin,
00183                      sizeof(mySin)));}
00184 
00186   int sendTo(const void *msg, int len, struct sockaddr_in *sin)
00187     {return(::sendto(myFD, (char*)msg, len, 0, (struct sockaddr*)sin,
00188                      sizeof(struct sockaddr_in)));}
00189 
00191   int recvFrom(void *msg, int len, sockaddr_in *sin);
00192 
00194   static bool hostAddr(const char *host, struct in_addr &addr);
00195 
00197   static bool addrHost(struct in_addr &addr, char *host);
00198 
00200   static std::string getHostName();
00201 
00203   bool getSockName();
00204 
00206   struct sockaddr_in * sockAddrIn() {return(&mySin);}
00207 
00209   struct in_addr * inAddr() {return(&mySin.sin_addr);}
00210 
00212   unsigned short int inPort() {return(mySin.sin_port);}
00213 
00215   static void inToA(struct in_addr *addr, char *buff);
00216 
00218   static const size_t sockAddrLen() {return(sizeof(struct sockaddr_in));}
00219 
00220 #ifdef WIN32
00221 
00222   static const size_t maxHostNameLen() {return(MAXGETHOSTSTRUCT);}
00223 #else
00224 
00225   static const size_t maxHostNameLen() {return(MAXHOSTNAMELEN);}
00226 #endif
00227 
00229   static unsigned int hostToNetOrder(int i);
00230 
00232   static unsigned int netToHostOrder(int i);
00233 
00235   bool setLinger(int time);
00236 
00238   bool setBroadcast();
00239 
00241   bool setReuseAddress();
00242 
00244   bool setNonBlock();
00245 
00247   void setDoClose(bool yesno) {myDoClose=yesno;}
00248 
00250   int getFD() const {return(myFD);}
00251 
00253   Type getType() const {return(myType);}
00254 
00256   const std::string & getErrorStr() const {return(myErrorStr);}
00257 
00259   Error getError() const {return(myError);}
00260 
00261 #ifndef SWIG
00262 
00263   int writeString(const char *str, ...);
00264 #endif
00265 
00266   int writeStringPlain(const char *str) { return writeString(str); }
00268   char *readString(void);
00270   void setEcho(bool echo) 
00271   { myStringAutoEcho = false; myStringEcho = echo; }
00273   bool getEcho(void) { return myStringEcho; }
00275   void setLogWriteStrings(bool logWriteStrings) 
00276     { myLogWriteStrings = logWriteStrings; }
00278   bool getLogWriteStrings(void) { return myLogWriteStrings; }
00280   const char *getIPString(void) const { return myIPString; }
00282   void setCloseCallback(ArFunctor *functor) 
00283     { myCloseFunctor = functor; }
00285   ArFunctor *getCloseCallback(void) { return myCloseFunctor; }
00286 protected:
00288   void setIPString(void);
00290   void doStringEcho(void);
00291   // internal crossplatform init (mostly for string reading stuff)
00292   void internalInit(void);
00293 
00294   Type myType;
00295   Error myError;
00296   std::string myErrorStr;
00297   bool myDoClose;
00298   int myFD;
00299   bool myNonBlocking;
00300   struct sockaddr_in mySin;
00301 
00302   bool myLogWriteStrings;
00303   bool myStringAutoEcho;
00304   bool myStringEcho;
00305   char myStringBuf[1100];
00306   size_t myStringPos;
00307   char myStringBufEmpty[1];
00308   size_t myStringPosLast;
00309   char myIPString[128];
00310   bool myStringGotEscapeChars;
00311   bool myStringGotComplete;
00312   bool myStringHaveEchoed;
00313   
00314   // A functor to call when the socket closes
00315   ArFunctor *myCloseFunctor;
00316 };
00317 
00318 
00319 #endif // ARSOCKET_H
00320   

Generated on Wed Oct 19 12:56:37 2005 for Aria by  doxygen 1.4.0