Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

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   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), forwardSock(NULL)
00062   { }
00063   ~Socket() {} //!< destructor
00064 
00065   //! use getWriteBuffer to get a memory address to write bytes to, for
00066   //! subsequent writing to a connection.
00067   /*!
00068    * The getWriteBuffer-write(int) combo eliminates one buffer copy. You
00069    * don't need to use getWriteBuffer with write(byte*, int)
00070    * @return pointer to the current position in the current write buffer for this socket or NULL on error
00071    * @param bytesreq maximum number of bytes the caller intends to set before the write method is called */
00072   byte* getWriteBuffer(int bytesreq);
00073 
00074   //! writes the specified number of bytes starting at the pointer returned.
00075   /*!
00076    * in a (prior) call to getWriteBufer
00077    * @param size number of bytes to be sent from the current write buffer
00078    */
00079   void write(int size);
00080 
00081   //! Blocking read.
00082   /*!
00083    * Tries to read upto receive buffer size worth of data from this socket.
00084    *
00085    * Blocking read is currently broken - it will be fixed in the next release
00086    * @return number of bytes read or -1 on error
00087    */
00088   int read();
00089 
00090   //! getReadBuffer is used with blocking read's
00091   /*!
00092    * The read(void) and getReadBuffer combo eliminates one buffer copy. You
00093    * don't need to use getReadBuffer with read(byte*, int)
00094    *
00095    * Blocking read is currently broken - it will be fixed in the next release
00096    * @return pointer to the buffer the previous call to blocking read wrote into or NULL if no data was read
00097    */
00098   byte* getReadBuffer();
00099 
00100   //! initialize socket member variables. This is different from the constructor since sockets are reused
00101   void init(); 
00102 
00103   //! Chooses between blocking and non-blocking input, output.
00104   /*! This function
00105    * can only be called when a socket is disconnected, since it is a bad
00106    * idea to mix blocking and non-blocking input, output.
00107    * The default for a socket is non-blocking
00108    * @return 0 on success
00109    */
00110   int setFlushType(FlushType_t fType);
00111 
00112   //!causes this socket to forward output to stdout if it is not connected, call setForward(NULL) to unset
00113   void setTextForward() { textForward=true; forwardSock=NULL; }
00114 
00115   //!causes this socket to forward output to @a sock if it is not connected, pass NULL to unset
00116   void setForward(Socket * forsock) { forwardSock=forsock; textForward=false; }
00117 
00118   //! Picks a level of verbosity for filtering pprintf commands.
00119   /*! The higher the
00120    * verbosity, the more the number of messages printed. This is useful
00121    * for filtering out non-important messages with very little processor
00122    * cost. Default is 0.
00123    * @param verbose the higher the value of verbose, the more the output
00124    */
00125   void setVerbosity(int verbose) { verbosity=verbose; }
00126 
00127   //! Standard write - writes specified amount of data from a buffer to a
00128   //! connection
00129   /*! You might want to consider the getWriteBuffer-write(int) combo if you
00130    * call this often
00131    * @param buf buffer to write from
00132    * @param size number of bytes to write
00133    * @return the number of bytes actually written or -1 on error
00134    */
00135   int write(const byte *buf, int size);
00136 
00137   //! Blocking read.
00138   /*! You might want to consider the read(void) and getReadBuffer combo if you
00139    * call this often
00140    *
00141    * Blocking read is currently broken - it will be fixed in the next release
00142    * @param buf buffer to write from
00143    * @param size number of bytes to write
00144    * @return number of bytes actually read
00145    */
00146   
00147   int read(byte *buf, int size);
00148 
00149   //! It's standard stuff. man 3 printf on most systems should give you more
00150   //! information
00151   int printf(const char *fmt, ...);
00152 
00153   //! It's standard stuff. man 3 printf on most systems should give you more
00154   //! information
00155   int vprintf(const char *fmt, va_list al);
00156 
00157   //! Similar to printf, except it takes an extra first argument.
00158   /*! If vlevel is
00159    * than or equal to the current verbosity level, the string will be printed
00160    * else it will be ignored
00161    * @param vlevel if (vlevel<=verbosity) print, else ignore
00162    * @param fmt same as the standard printf's format string
00163    */
00164   int pprintf(int vlevel, const char *fmt, ...);
00165   
00166   //! Initiate blocking or nonblocking write transfer depending on the type
00167   //! of socket.
00168   /*! All write commands on the socket will implicity call this. You
00169    * don't need to call it, unless you're implementing your own write
00170    */
00171   void flush();
00172 
00173 private:
00174   //@{
00175   //!private ALOKL_TODO
00176   TransportType_t trType;
00177   FlushType_t flType;
00178 
00179   int verbosity;
00180 
00181   antModuleRef endpoint;
00182   ConnectionState state;
00183 
00184   int sendBufSize, recvBufSize, sendSize, recvSize, writeSize, readSize;
00185   bool tx, rx;
00186 
00187   antSharedBuffer sendBuffer, recvBuffer;
00188   byte *sendData, *recvData;
00189   byte *readData, *writeData;
00190   int server_port;
00191   int (*rcvcbckfn) (char*, int);
00192 
00193   bool textForward;
00194   char* textForwardBuf;
00195   Socket * forwardSock;
00196   //@}
00197  
00198 private:
00199   Socket(const Socket&); //!< copy constructor, don't call
00200   Socket& operator= (const Socket&); //!< assignment operator, don't call
00201 };
00202 
00203 extern Socket* sout;  //!< the standard tekkotsu in/out console (default port 10001)
00204 extern Socket* serr;  //!< the standard tekkotsu error output (default port 10002)
00205 
00206 /*! @file
00207  * @brief Defines Tekkotsu wireless Socket class, also sout and serr
00208  * @author alokl (Creator)
00209  * 
00210  * $Author: ejt $
00211  * $Name: tekkotsu-1_4_1 $
00212  * $Revision: 1.12 $
00213  * $State: Exp $
00214  * $Date: 2003/06/13 03:23:05 $
00215  */
00216 
00217 #endif

Tekkotsu v1.4
Generated Sat Jul 19 00:06:31 2003 by Doxygen 1.3.2