Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phSocket.h

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 /* -------------------------------------------------------------------------- *
00027  * phSocket.h: C++ Wrapper class for socket programming                       *
00028  *                                                                            *
00029  * -------------------------------------------------------------------------- *
00030  * This code is mostly from the Phission code base.                           *
00031  * This code was originally coded by John R. Watson in the Fall of 2003 and   *
00032  * added to Phission by myself. Since then I've added some necessary          *
00033  * functionality for use in a streaming JPEG/Image server/client for Phission.*
00034  * I'm using it here to further stretch/test it's functionality               *
00035  * -------------------------------------------------------------------------- */
00036 #ifndef _PHSOCKET_H
00037 #define _PHSOCKET_H
00038 
00039 /* ------------------------------------------------------------------------- */
00040 #include <phStandard.h>
00041 #include <phStdint.h>
00042 #include <phStdint.h>
00043 
00044 /* ---------------------------------------------------------------------- */
00049 /* ---------------------------------------------------------------------- */
00055 /* ---------------------------------------------------------------------- */
00056 #define phSocket_CHECK_RC(rc,perr,fmt,...) \
00057 { \
00058     if (rc == -2) goto error; /* other side disconnected, don't print */ \
00059     phCHECK_RC(rc,perr,fmt, ## __VA_ARGS__ ); \
00060 }
00061 
00062 #define phSocket_CONT_RC(rc,perr,fmt,...) \
00063 { \
00064     if (rc == -2) continue; /* other side disconnected, don't print */ \
00065     phPRINT_RC(rc,perr,fmt, ## __VA_ARGS__ ); \
00066 }
00067 
00068 /* ------------------------------------------------------------------------- */
00069 #define phNONBLOCK  0
00070 #define phBLOCK     1
00071 #define phWAIT      1
00072 #define phNOWAIT    0
00073 
00074 #define phLINGER_OFF    0
00075 #define phLINGER        1
00076 #define phNOLINGER      2
00077 
00078 #define phSOCKET_PORTMIN 1
00079 #define phSOCKET_PORTMAX 65534
00080 
00081 #include <phMutex.h>
00082 
00083 /* ---------------------------------------------------------------------- */
00084 typedef struct ph_socket_info_t *ph_socket_info;
00085 
00086 /* ------------------------------------------------------------------------- */
00093 PHAPI(class) phSocket : public phMutex
00094 {
00095 protected:
00096     int         m_dst_port;
00097     char       *m_dst_host;
00098     uint32_t    m_dst_host_size;
00099     
00100     int         m_src_port;
00101     char       *m_src_host;
00102     uint32_t    m_src_host_size;
00103 
00104     char       *m_temp_buf;
00105     uint32_t    m_temp_buf_size;
00106     
00107     phMutex     m_send_lock;
00108     phMutex     m_recv_lock;
00109     
00110     int         m_family;
00111     int         m_socket_type;
00112     int         m_protocol;
00113 
00114     int         m_call_shutdown;
00115    
00116     ph_socket_info m_info;
00117     
00118     /* m_connected: stores the logical state of the socket connection as
00119      * determined by the object. When an error condition or something
00120      * else happens to the lower socket levels, this may not be in sync 
00121      * until the object finds out something happened and can adjust it */
00122     bool                m_connected;
00123     /* m_warning_level: This allows an implementor to set whether the 
00124      * object should print out errors and warnings. Often, we don't want
00125      * this to happen so the default is to not print because it could
00126      * get quite noisy */
00127     int                 m_warning_level;
00128     /* m_exit_trigger: used in looping locations to end looping
00129      * if no other condition to end has been met and a thread
00130      * would like to exit */
00131     volatile int        m_exit_trigger;
00132 
00133     int                 m_blocking;
00134     int                 m_linger;
00135     int                 m_linger_time;
00136 
00137     /* Allocates a buffer (returned in buf) of size length bytes and
00138      * returns the size in len if successful; otherwise it returns 0 if 
00139      * it wasn't. if *buf and *len aren't NULL/0 then the buf is reallocd
00140      * */
00141     int allocBuffer         ( uint8_t **buf, uint32_t *len, uint32_t length );
00142     int initSocketVar       ();
00143     int applyBlocking       ();
00144     int applyLinger         ();
00145     int updateDstFromSock   ();
00146     int updateSrcFromSock   ();
00147 
00148     void   *getSrcSinPtr();
00149     void   *getDstSinPtr();
00150 
00151     void    setSocket( int32_t sock );
00152     int32_t getSocket();
00153 
00154     /* error method returns an integer of the last error which is specific
00155        to the socket implementation being used. */
00156     int lastSocketError();
00157     int lastSystemError();
00158 
00159 public:
00160     phSocket( const char *host = (const char *)NULL,
00161               const int  port = 0 );
00162     ~phSocket();
00163 
00164     int setSrcPort( int port );
00165     int getSrcPort();
00166     int setDstPort( int port );
00167     int getDstPort();
00168 
00169     int         setSrcHost( const char *host );
00170     const char *getSrcHost();
00171     int         setDstHost( const char *host );
00172     const char *getDstHost();
00173 
00174     /* connect: connect to a host on the specified port.  if polling is
00175      *          true, the client will poll the server until a connection
00176      *          is made
00177      */
00178     int connect( const char *host = (const char *)NULL, 
00179                  const int port = 0, 
00180                  const bool polling = true );
00181 
00182     /* disconnect: close the connection */
00183     void disconnect();
00184 
00185     /* send: send data over the socket */
00186     int send( const void *data, const int len );
00187 
00188     /* recv: receive data over the socket */
00189     int recv( void *data, const int len );
00190     int recv( void *data, const int len, int &recv_len, int wait );
00191 
00192     /* isReady: returns true if a socket is ready for reading */
00193     bool isReady();
00194 
00195     /* isConnected: returns if the current state of the object
00196      * says that we're in a connected state */
00197     bool isConnected();
00198 
00199     /* wakeup - Try to disturb the connection or something so it 
00200      * can release and allow thread to check the exit condition */
00201     void wakeup();
00202     
00203     /* setBlocking - set the block state of the socket connection to
00204      * phNONBLOCK or phBLOCK */
00205     int setBlocking( int set_blocking = phBLOCK );
00206     
00207     /* Sets the linger attribute and linger time on the socket */
00208     int setLinger( int set_linger = phLINGER, 
00209                    uint32_t linger_time = 0 );
00210 
00211     /* setSock: this allows an accepting server to set the 
00212      * socket value of a phSocket for a client's connection */
00213     int setSock( int newSock );
00214 
00215     /* Checks the destination addr:port against the
00216      * source destination:port to see if the socket is connected to 
00217      * itself. This is a workaround for the way the Linux kernel handles
00218      * connect. It doesn't check to prevent the srcport != dstport */
00219     int isSelfConnected();
00220     
00221     /* TODO: Remove warning level and add an error retrieving mechanism */
00222     int setWarningLevel( int level = 0 );
00223     int getWarningLevel();
00224 
00225     void setCallShutdown( int sd );
00226 
00227     /* Byte swapping functions */
00228     static uint32_t hton32( uint32_t var );
00229     static uint32_t ntoh32( uint32_t var );
00230     static uint16_t hton16( uint16_t var );
00231     static uint16_t ntoh16( uint16_t var );
00232 
00233     /* Transmission helper functions
00234      * These will convert to the proper network byte order */
00235     int recv_int32  ( int32_t  *i );
00236     int send_int32  ( int32_t   d );
00237     int recv_uint32 ( uint32_t *i );
00238     int send_uint32 ( uint32_t  d );
00239     int recv_int16  ( int16_t  *i );
00240     int send_int16  ( int16_t   d );
00241     int recv_uint16 ( uint16_t *i );
00242     int send_uint16 ( uint16_t  d );
00243 
00244     virtual int print_object( FILE *fd );
00245 };
00246  /* end \addtogroup Network */
00248 
00249 #endif /* PHSOCKET_H */




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:02 2007 for phission by  doxygen 1.4.4