InetOSC.h

00001 /* Open SoundControl kit in C++                                              */
00002 /* Copyright (C) 2002-2004 libOSC++ contributors. See AUTHORS                */
00003 /*                                                                           */
00004 /* This library is free software; you can redistribute it and/or             */
00005 /* modify it under the terms of the GNU Lesser General Public                */
00006 /* License as published by the Free Software Foundation; either              */
00007 /* version 2.1 of the License, or (at your option) any later version.        */
00008 /*                                                                           */
00009 /* This library is distributed in the hope that it will be useful,           */
00010 /* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
00011 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         */
00012 /* Lesser General Public License for more details.                           */
00013 /*                                                                           */
00014 /* You should have received a copy of the GNU Lesser General Public          */
00015 /* License along with this library; if not, write to the Free Software       */
00016 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
00017 /*                                                                           */
00018 /* For questions regarding this program contact                              */
00019 /* Daniel Holth <dholth@fastmail.fm> or visit                                */
00020 /* http://wiretap.stetson.edu/                                               */
00021 
00022 /*
00023  * This class was written by Martin Rumori (martin@rumori.de)
00024  */
00025 
00026 #ifndef LIBOSC_INETOSC_H
00027 #define LIBOSC_INETOSC_H
00028 
00029 #ifdef    __CYGWIN__
00030 #include <w32api/winsock2.h>
00031 #include <w32api/ws2tcpip.h>
00032 #include <poll.h>
00033 #include <unistd.h>
00034 
00035 #else  // __CYGWIN__
00036 #include <stdlib.h>
00037 #include <errno.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 #include <netdb.h>
00041 #include <sys/types.h>
00042 #include <sys/socket.h>
00043 #include <sys/poll.h>
00044 #include <netinet/in.h>
00045 #endif // __CYGWIN__
00046 
00047 #include <fcntl.h>
00048 
00049 #include <iostream>
00050 #include <string>
00051 
00052 // defines
00053 
00054 // The maximum datagram length for udp packets
00055 #define OSC_DGRAM_MAXLENGTH 65536
00056 
00057 // The maximum length for TCP OSC messages
00058 #define OSC_STREAM_MAXLENGTH 65536
00059 
00060 // The maximum number of pending TCP connections
00061 #define OSC_STREAM_MAXCONN 12
00062 
00063 
00071 class InetOSC
00072 {
00073 
00074 public:
00076   InetOSC ();
00077 
00079   virtual ~InetOSC ();
00080 
00082   typedef enum
00083     {
00085       OSC_IN_IPv4,
00087       OSC_IN_IPv6
00088     } InetOSCAddrType;
00089 
00091   typedef enum
00092     {
00094       OSC_IN_UDP,
00096       OSC_IN_TCP
00097     } InetOSCProto;
00098 
00117   typedef enum
00118     {
00120       OSC_IN_SUCCESS = 0x0000,
00122       OSC_IN_UNKNOWN = 0x0001,
00124       OSC_IN_NOT_IDLE = 0x0002,
00126       OSC_IN_WOULDBLOCK = 0x0003,
00128       OSC_IN_MAXCYCLES = 0x0004,
00130       OSC_IN_CLOSED_CONN = 0x0005,
00132       OSC_IN_PROTO_UNKNOWN = 0x0100,
00134       OSC_IN_ADDR_UNKNOWN = 0x0101,
00136       OSC_IN_NO_PROCESSOR = 0x0200,
00138       OSC_IN_NO_TRANSMGR = 0x0201,
00140       OSC_IN_NO_TCPMASTER = 0x0202,
00142       OSC_IN_ALREADY_SLAVE = 0x0203,
00144       OSC_IN_NOT_SLAVE =  0x0204,
00146       OSC_IN_GETHOST_ERRNO = 0xC000,
00148       OSC_IN_GETPROTO_ERRNO = 0xC001,
00150       OSC_IN_SOCKET_ERRNO = 0xC002,
00152       OSC_IN_SETSOCKOPT_ERRNO = 0xC003,
00154       OSC_IN_FCNTL_ERRNO = 0xC004,
00156       OSC_IN_BIND_ERRNO = 0xC005,
00158       OSC_IN_LISTEN_ERRNO = 0xC006,
00160       OSC_IN_CONNECT_ERRNO = 0xC007,
00162       OSC_IN_SHUTDOWN_ERRNO = 0xC008,
00164       OSC_IN_CLOSE_ERRNO = 0xC009,
00166       OSC_IN_RECV_ERRNO = 0xC00A,
00168       OSC_IN_SEND_ERRNO = 0xC00B,
00170       OSC_IN_POLL_ERRNO = 0xC00C,
00172       OSC_IN_ACCEPT_ERRNO = 0xC00D,
00174       OSC_IN_MALLOC_ERRNO = 0xC00E,
00175     } InetOSCError;
00176 
00181   const InetOSCError getError () const;
00182 
00187   const int getLastErrno () const;
00188 
00196   static const bool getErrorString (const InetOSCError err,
00197                                     std::string& errString);
00198 
00203   const InetOSCAddrType getAddrType (void) const;
00204 
00225   void setMaxHandleCycles (const int cycles = -1);
00226 
00233   const int getMaxHandleCycles (void) const;
00234 
00235 
00236 protected :
00237 
00248   bool createSocket (int *const sd,
00249                      const InetOSCProto proto,
00250                      const InetOSCAddrType addrType);
00251 
00260   bool bindSocketToAny (int *const sd,
00261                         const short port,
00262                         const InetOSCAddrType addrType);
00263 
00274   bool closeSocket (int *const sd, const bool setError = true);
00275 
00287   bool allocSockAddr (const InetOSCAddrType addrType,
00288                       struct sockaddr * *const addr,
00289                       socklen_t *const addrlen);
00290 
00301   bool setRemoteAddr (const std::string& host,
00302                       const short port,
00303                       const InetOSCAddrType addrType,
00304                       struct sockaddr *const addr);
00305 
00326   bool setRemotePort (const short port,
00327                       const InetOSCAddrType addrType,
00328                       struct sockaddr *const addr);
00329 
00338   std::string getHostAddress (const InetOSCAddrType addrType,
00339                          const struct sockaddr *const addr);
00340 
00350   std::string getHostName (const InetOSCAddrType addrType,
00351                       const struct sockaddr *const addr);
00352 
00361   short getPortNumber (const InetOSCAddrType addrType,
00362                        const struct sockaddr *const addr);
00363 
00370   bool setSocketNonBlock (int *const sd);
00371 
00379   bool setSocketReuseAddr (int *const sd, bool reuse = true);
00380 
00381 
00382   /* protected members */
00383 
00385   InetOSCAddrType _addrType;
00386 
00388   InetOSCError _error;
00389 
00391   int _lasterrno;
00392 
00394   int _socket;
00395 
00397   int _maxHandleCycles;
00398 
00399 };
00400 
00401 #endif // #ifndef LIBOSC_INETOSC_H

Generated on Thu Mar 2 23:51:35 2006 for libOSC++ by  doxygen 1.4.6