Homepage Demos Overview Downloads Tutorials Reference
Credits

Socket.h

Go to the documentation of this file.
00001 #ifndef Socket_h_DEFINED
00002 #define Socket_h_DEFINED
00003 
00004 #ifdef PLATFORM_APERIOS
00005 #  include <ant.h>
00006 #  include <Types.h>
00007 #endif
00008 #include <stdarg.h>
00009 #include <stdlib.h>
00010 
00011 //! holds socket enumerations and constants
00012 namespace SocketNS {
00013   
00014   //! Specifies transport type. TCP is usually a good idea
00015   enum TransportType_t {
00016     SOCK_STREAM=0, //!< TCP: guaranteed delivery, higher overhead
00017     SOCK_DGRAM     //!< UDP: no guarantees, low overhead
00018   };
00019 
00020   //! Internal TCP/UDP Connection State
00021   enum ConnectionState {
00022     CONNECTION_CLOSED,
00023     CONNECTION_CONNECTING,
00024     CONNECTION_CONNECTED,
00025     CONNECTION_LISTENING,
00026     CONNECTION_CLOSING,
00027     CONNECTION_ERROR
00028   };
00029 
00030   //! Chooses between blocking and non-blocking Wireless Input, Output. Blocking wireless output from the main process will affect the performance of the Aibo, and should only be used for debugging purposes
00031   enum FlushType_t {
00032     FLUSH_NONBLOCKING=0, //!< Writes and Reads return immediately, and are processed by another process, so Main can continue to run. Non-blocking reads require specifying a callback function to handle data received
00033     FLUSH_BLOCKING       //!< Blocking writes are a good idea for debugging - a blocking write will be transmitted before execution continues to the next statement. Blocking reads should be avoided, since they'll cause a significant slow down in the main process
00034   };
00035 
00036 };
00037 
00038 using namespace SocketNS;
00039 
00040 #ifndef PLATFORM_APERIOS
00041   typedef unsigned char byte;
00042 #endif
00043 
00044 //! Tekkotsu wireless Socket class
00045 /*! 
00046  * For more information on using wireless, please read the following tutorials:
00047  * - <a href="../AiboMon.html">TekkotsuMon</a>
00048  * - <a href="../Wireless.html">TCP/IP</a>
00049  * - <a href="../RemoteProcess.html">Remote Processing OPENR</a>
00050  * Tekkotsu Wireless and Remote Processing OPENR provide different
00051  * interfaces to comparable wireless functionality.
00052  *
00053  * The networking interface needs more documentation.  It also needs a
00054  * cleanup.  In the mean time, take a look at the TekkotsuMon objects
00055  * in <i>Tekkotsu</i><tt>/Behaviors/Mon</tt>.  They all listen for new
00056  * connections.  Unfortunately, at the momement there are no examples
00057  * of outgoing connections, but it should give you a pretty good idea
00058  * how to start moving.
00059  */
00060 
00061 class Socket {
00062   friend class Wireless;
00063 
00064 public:
00065   int sock; //!< unique non-negative integer representing socket. Serves as index into socket Objects array
00066 
00067 public:
00068   //! constructor
00069   explicit Socket(int sockn)
00070     : sock(sockn), trType(), flType(FLUSH_NONBLOCKING), verbosity(0), 
00071       endpoint(), state(CONNECTION_CLOSED), sendBufSize(), recvBufSize(),
00072       sendSize(0), recvSize(0), writeSize(0), readSize(0),
00073       tx(false), rx(false), sendBuffer(), recvBuffer(), sendData(NULL),
00074       recvData(NULL), readData(NULL), writeData(NULL), server_port(0),
00075       rcvcbckfn(NULL), textForward(false), textForwardBuf(NULL),
00076       forwardSock(NULL), daemon(false)
00077   { }
00078   virtual ~Socket() {} //!< destructor
00079 
00080   //! use getWriteBuffer to get a memory address to write bytes to, for
00081   //! subsequent writing to a connection.
00082   /*!
00083    * The getWriteBuffer-write(int) combo eliminates one buffer copy. You
00084    * don't need to use getWriteBuffer with write(byte*, int)
00085    * @return pointer to the current position in the current write buffer for this socket or NULL on error
00086    * @param bytesreq maximum number of bytes the caller intends to set before the write method is called */
00087   byte* getWriteBuffer(int bytesreq);
00088 
00089   //! writes the specified number of bytes starting at the pointer returned.
00090   /*!
00091    * in a (prior) call to getWriteBufer
00092    * @param size number of bytes to be sent from the current write buffer
00093    */
00094   void write(int size);
00095 
00096   //! Blocking read.
00097   /*!
00098    * Tries to read upto receive buffer size worth of data from this socket.
00099    *
00100    * Blocking read is currently broken - it will be fixed in the next release
00101    * @return number of bytes read or -1 on error
00102    */
00103   int read();
00104 
00105   //! getReadBuffer is used with blocking read's
00106   /*!
00107    * The read(void) and getReadBuffer combo eliminates one buffer copy. You
00108    * don't need to use getReadBuffer with read(byte*, int)
00109    *
00110    * Blocking read is currently broken - it will be fixed in the next release
00111    * @return pointer to the buffer the previous call to blocking read wrote into or NULL if no data was read
00112    */
00113   byte* getReadBuffer();
00114 
00115   //! initialize socket member variables. This is different from the constructor since sockets are reused
00116   void init(); 
00117 
00118   //! Chooses between blocking and non-blocking input, output.
00119   /*! This function
00120    * can only be called when a socket is disconnected, since it is a bad
00121    * idea to mix blocking and non-blocking input, output.
00122    * The default for a socket is non-blocking
00123    * @return 0 on success
00124    */
00125   int setFlushType(FlushType_t fType);
00126 
00127   //!causes this socket to forward output to stdout if it is not connected, call setForward(NULL) to unset
00128   void setTextForward() { textForward=true; forwardSock=NULL; }
00129 
00130   //!causes this socket to forward output to @a sock if it is not connected, pass NULL to unset
00131   void setForward(Socket * forsock) { forwardSock=forsock; textForward=false; }
00132 
00133   //! Picks a level of verbosity for filtering pprintf commands.
00134   /*! The higher the
00135    * verbosity, the more the number of messages printed. This is useful
00136    * for filtering out non-important messages with very little processor
00137    * cost. Default is 0.
00138    * @param verbose the higher the value of verbose, the more the output
00139    */
00140   void setVerbosity(int verbose) { verbosity=verbose; }
00141 
00142   //! Standard write - writes specified amount of data from a buffer to a
00143   //! connection
00144   /*! You might want to consider the getWriteBuffer-write(int) combo if you
00145    * call this often
00146    * @param buf buffer to write from
00147    * @param size number of bytes to write
00148    * @return the number of bytes actually written or -1 on error
00149    */
00150   int write(const byte *buf, int size);
00151 
00152   //! Blocking read.
00153   /*! You might want to consider the read(void) and getReadBuffer combo if you
00154    * call this often
00155    *
00156    * Blocking read is currently broken - it will be fixed in the next release
00157    * @param buf buffer to write from
00158    * @param size number of bytes to write
00159    * @return number of bytes actually read
00160    */
00161   
00162   int read(byte *buf, int size);
00163 
00164   //! It's standard stuff. man 3 printf on most systems should give you more
00165   //! information
00166   int printf(const char *fmt, ...);
00167 
00168   //! It's standard stuff. man 3 printf on most systems should give you more
00169   //! information
00170   int vprintf(const char *fmt, va_list al);
00171 
00172   //! Similar to printf, except it takes an extra first argument.
00173   /*! If vlevel is
00174    * than or equal to the current verbosity level, the string will be printed
00175    * else it will be ignored
00176    * @param vlevel if (vlevel<=verbosity) print, else ignore
00177    * @param fmt same as the standard printf's format string
00178    */
00179   int pprintf(int vlevel, const char *fmt, ...);
00180   
00181   //! Initiate blocking or nonblocking write transfer depending on the type
00182   //! of socket.
00183   /*! All write commands on the socket will implicity call this. You
00184    * don't need to call it, unless you're implementing your own write
00185    */
00186   void flush();
00187 
00188 protected:
00189   //@{
00190   //!private ALOKL_TODO
00191   TransportType_t trType;
00192   FlushType_t flType;
00193 
00194   int verbosity;
00195 
00196 #ifndef PLATFORM_APERIOS
00197   typedef char* antSharedBuffer;
00198   typedef unsigned int antModuleRef;
00199 #endif
00200 
00201   antModuleRef endpoint;
00202   ConnectionState state;
00203 
00204   int sendBufSize, recvBufSize, sendSize, recvSize, writeSize, readSize;
00205   bool tx, rx;
00206 
00207   antSharedBuffer sendBuffer, recvBuffer;
00208   byte *sendData, *recvData;
00209   byte *readData, *writeData;
00210   int server_port;
00211   int (*rcvcbckfn) (char*, int);
00212 
00213   bool textForward;
00214   char* textForwardBuf;
00215   Socket * forwardSock;
00216 
00217   bool daemon;
00218   //@}
00219  
00220 protected:
00221   Socket(const Socket&); //!< copy constructor, don't call
00222   Socket& operator= (const Socket&); //!< assignment operator, don't call
00223 };
00224 
00225 extern Socket* sout;  //!< the standard tekkotsu in/out console (default port 10001)
00226 extern Socket* serr;  //!< the standard tekkotsu error output (default port 10002)
00227 
00228 /*! @file
00229  * @brief Defines Tekkotsu wireless Socket class, also sout and serr
00230  * @author alokl (Creator)
00231  * 
00232  * $Author: ejt $
00233  * $Name: tekkotsu-2_2_2 $
00234  * $Revision: 1.19 $
00235  * $State: Exp $
00236  * $Date: 2004/12/23 01:47:07 $
00237  */
00238 
00239 #endif

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:15 2005 by Doxygen 1.4.0