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

Tekkotsu v2.2
Generated Tue Oct 19 14:19:16 2004 by Doxygen 1.3.9.1