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

Tekkotsu v2.1
Generated Tue Mar 16 23:19:15 2004 by Doxygen 1.3.5