abyss_socket.h

00001 #ifndef SOCKET_H_INCLUDED
00002 #define SOCKET_H_INCLUDED
00003 
00004 #include <netinet/in.h>
00005 
00006 #include <xmlrpc-c/abyss.h>
00007 
00008 #include <netinet/in.h>
00009 
00010 #define IPB1(x) (((unsigned char *)(&x))[0])
00011 #define IPB2(x) (((unsigned char *)(&x))[1])
00012 #define IPB3(x) (((unsigned char *)(&x))[2])
00013 #define IPB4(x) (((unsigned char *)(&x))[3])
00014 
00015 typedef struct in_addr TIPAddr;
00016 
00017 typedef void SocketCloseImpl(TSocket * const socketP);
00018 
00019 typedef void SocketDestroyImpl(TSocket * const socketP);
00020 
00021 typedef void SocketWriteImpl(TSocket *             const socketP,
00022                              const unsigned char * const buffer,
00023                              uint32_t              const len,
00024                              abyss_bool *          const failedP);
00025 
00026 typedef uint32_t SocketReadImpl(TSocket * const socketP,
00027                                 char *    const buffer,
00028                                 uint32_t  const len);
00029 
00030 typedef abyss_bool SocketConnectImpl(TSocket * const socketP,
00031                                      TIPAddr * const addrP,
00032                                      uint16_t  const portNumber);
00033 
00034 typedef abyss_bool SocketBindImpl(TSocket * const socketP,
00035                                   TIPAddr * const addrP,
00036                                   uint16_t  const portNumber);
00037 
00038 typedef abyss_bool SocketListenImpl(TSocket * const socketP,
00039                                     uint32_t  const backlog);
00040 
00041 typedef void SocketAcceptImpl(TSocket *    const listenSocketP,
00042                               abyss_bool * const connectedP,
00043                               abyss_bool * const failedP,
00044                               TSocket **   const acceptedSocketPP,
00045                               TIPAddr *    const ipAddrP);
00046 
00047 typedef uint32_t SocketErrorImpl(TSocket * const socketP);
00048 
00049 typedef uint32_t SocketWaitImpl(TSocket *  const socketP,
00050                                 abyss_bool const rd,
00051                                 abyss_bool const wr,
00052                                 uint32_t   const timems);
00053 
00054 typedef uint32_t SocketAvailableReadBytesImpl(TSocket * const socketP);
00055 
00056 typedef void SocketGetPeerNameImpl(TSocket *    const socketP,
00057                                    TIPAddr *    const ipAddrP,
00058                                    uint16_t *   const portNumberP,
00059                                    abyss_bool * const successP);
00060 
00061 struct TSocketVtbl {
00062     SocketCloseImpl              * close;
00063     SocketDestroyImpl            * destroy;
00064     SocketWriteImpl              * write;
00065     SocketReadImpl               * read;
00066     SocketConnectImpl            * connect;
00067     SocketBindImpl               * bind;
00068     SocketListenImpl             * listen;
00069     SocketAcceptImpl             * accept;
00070     SocketErrorImpl              * error;
00071     SocketWaitImpl               * wait;
00072     SocketAvailableReadBytesImpl * availableReadBytes;
00073     SocketGetPeerNameImpl        * getPeerName;
00074 };
00075 
00076 struct _TSocket {
00077     uint               signature;
00078         /* With both background and foreground use of sockets, and
00079            background being both fork and pthread, it is very easy to
00080            screw up socket lifetime and try to destroy twice.  We use
00081            this signature to help catch such bugs.
00082         */
00083     void *             implP;
00084     struct TSocketVtbl vtbl;
00085 };
00086 
00087 #define TIME_INFINITE   0xffffffff
00088 
00089 extern abyss_bool SocketTraceIsActive;
00090 
00091 abyss_bool
00092 SocketInit(void);
00093 
00094 void
00095 SocketTerm(void);
00096 
00097 void
00098 SocketClose(TSocket *       const socketP);
00099 
00100 void
00101 SocketCreate(const struct TSocketVtbl * const vtblP,
00102              void *                     const implP,
00103              TSocket **                 const socketPP);
00104 
00105 void
00106 SocketWrite(TSocket *             const socketP,
00107             const unsigned char * const buffer,
00108             uint32_t              const len,
00109             abyss_bool *          const failedP);
00110 
00111 uint32_t
00112 SocketRead(TSocket *       const socketP, 
00113            unsigned char * const buffer, 
00114            uint32_t        const len);
00115 
00116 abyss_bool
00117 SocketConnect(TSocket * const socketP,
00118               TIPAddr * const addrP,
00119               uint16_t  const portNumber);
00120 
00121 abyss_bool
00122 SocketBind(TSocket * const socketP,
00123            TIPAddr * const addrP,
00124            uint16_t  const portNumber);
00125 
00126 abyss_bool
00127 SocketListen(TSocket * const socketP,
00128              uint32_t  const backlog);
00129 
00130 void
00131 SocketAccept(TSocket *    const listenSocketP,
00132              abyss_bool * const connectedP,
00133              abyss_bool * const failedP,
00134              TSocket **   const acceptedSocketP,
00135              TIPAddr *    const ipAddrP);
00136 
00137 uint32_t
00138 SocketWait(TSocket *  const socketP,
00139            abyss_bool const rd,
00140            abyss_bool const wr,
00141            uint32_t   const timems);
00142 
00143 uint32_t
00144 SocketAvailableReadBytes(TSocket * const socketP);
00145 
00146 void
00147 SocketGetPeerName(TSocket *    const socketP,
00148                   TIPAddr *    const ipAddrP,
00149                   uint16_t *   const portNumberP,
00150                   abyss_bool * const successP);
00151 
00152 uint32_t
00153 SocketError(TSocket * const socketP);
00154 
00155 #endif