1 /*----------------------------------------------------------------------------
  2 Name:      xmlBlasterSocket.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   SOCKET internal header (not included directly by clients)
  6 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7 -----------------------------------------------------------------------------*/
  8 #ifndef XMLBLASTER_SOCKET_H
  9 #define XMLBLASTER_SOCKET_H
 10 
 11 #include <stdlib.h>
 12 #include <util/basicDefs.h> /* Needed only to define _WINDOWS */
 13 
 14 #ifdef _WINDOWS
 15                              /* #if _MSC_VER > 1300 (which is VC7) but also for 1400 (which is VC8 2005) */
 16 #include <Winsock2.h>        /* WS2_32.DLL: The newer lib which extends winsock.h, new: Ws2tcpip.h */
 17 
 18 /*#  include <winsock.h> */  /* WSOCK32.DLL: The old lib, if you activate replace SD_BOTH with 2 in XmlBlasterConnectionUnparsed.c  */
 19 /* From a mailing list:
 20    #ifndef INCL_WINSOCK_API_TYPEDEFS
 21    #define INCL_WINSOCK_API_TYPEDEFS 1
 22    #endif
 23 
 24    // then include:
 25    #include <winsock2.h>
 26 
 27    also any header you include ( mainly a concern in your stdafx.h file ) that may suck in the winsock.h file you should do a:
 28 
 29    #define _WINSOCKAPI_
 30 */
 31 #  define ssize_t signed int
 32 #else
 33 #  if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__hpux__)
 34 #    include <sys/types.h>   /* Needed for __FreeBSD__ */
 35 #    include <netinet/in.h>
 36 #  endif
 37 #  include <sys/socket.h>
 38 #  include <netdb.h>
 39 #  include <arpa/inet.h>   /* inet_addr() */
 40 #endif
 41 #include <util/msgUtil.h>
 42 
 43 #ifdef __cplusplus
 44 #ifndef XMLBLASTER_C_COMPILE_AS_CPP /* 'g++ -DXMLBLASTER_C_COMPILE_AS_CPP ...' allows to compile the lib as C++ code */
 45 extern "C" {
 46 #endif
 47 #endif
 48 
 49 #define  MAX_MSG_LEN 1000000000
 50 
 51 
 52 
 53 /**
 54  * Settings for MSG_FLAG_POS_TYPE
 55  */
 56 typedef enum XMLBLASTER_MSG_TYPE_ENUM {
 57    MSG_TYPE_INVOKE = 73,
 58    MSG_TYPE_RESPONSE = 82,
 59    MSG_TYPE_EXCEPTION = 69
 60 } XMLBLASTER_MSG_TYPE;
 61 
 62 /**
 63  * Helper struct to hold all necessary informations
 64  */
 65 typedef struct SocketDataHolder {
 66    size_t msgLen;
 67    bool checksum;
 68    bool compressed;
 69    char type;  /**< XMLBLASTER_MSG_TYPE */
 70    char version;
 71    char requestId[MAX_REQUESTID_LEN];
 72    char methodName[MAX_METHODNAME_LEN];
 73    char secretSessionId[MAX_SESSIONID_LEN];
 74    size_t dataLenUncompressed;
 75    XmlBlasterBlob blob; /**< blob.data is allocated with malloc, you need to free() it yourself, is compressed if marked as such */
 76 } SocketDataHolder;
 77 
 78 #define MSG_LEN_FIELD_LEN 10
 79 #define MAX_PACKET_SIZE 10*1024
 80 #define MSG_FLAG_FIELD_LEN 6
 81 /* static const int MSG_FLAG_FIELD_LEN = 6; */
 82 enum MSG_FLAG_POS_ENUM {
 83    MSG_FLAG_POS_CHECKSUM = MSG_LEN_FIELD_LEN,
 84    MSG_FLAG_POS_COMPRESS,
 85    MSG_FLAG_POS_TYPE,
 86    MSG_FLAG_POS_RESERVED1,
 87    MSG_FLAG_POS_RESERVED2,
 88    MSG_FLAG_POS_VERSION,
 89    MSG_POS_REQESTID
 90 };
 91 
 92 #define XMLBLASTER_SOCKET_VERSION 49
 93 
 94 
 95 extern void closeSocket(int fd);
 96 extern ssize_t writen(const int fd, const char *ptr, const size_t nbytes);
 97 extern ssize_t readn(const int fd, char *ptr, const size_t nbytes, XmlBlasterNumReadFunc fpNumRead, void *userP);
 98 
 99 extern bool xbl_isOneway(XMLBLASTER_MSG_TYPE msgType, const char *const methodName);
100 
101 /**
102  * Creates a raw blob to push over a socket as described in the protocol.socket requirement. 
103  * @param msgType invoke, response or exception
104  * @param requestId The unique request ID for each invocation
105  * @param methodName The method name like "PUBLISH"
106  * @param secretSessionId The authentication string
107  * @param data is returned
108  * @param dataLen is returned
109  * @param debug Pass true for debugging output to stdout
110  * @param rawMsgLen
111  * @see http://www.xmlblaster.org/xmlBlaster/doc/requirements/protocol.socket.html
112  * @return The raw message, the caller needs to free() it.
113  */
114 extern char *encodeSocketMessage(
115               enum XMLBLASTER_MSG_TYPE_ENUM msgType,
116               const char * const requestId,    /**< The unique request ID for each invocation */
117               const char * const methodName,   
118               const char * const secretSessionId,
119               const char *data,
120               size_t dataLen,
121               bool debug,
122               size_t *rawMsgLen);
123 Dll_Export BlobHolder encodeMsgUnit(MsgUnit *msgUnit, bool debug);  /* export for C++ embedding */
124 Dll_Export BlobHolder encodeMsgUnitArr(MsgUnitArr *msgUnitArr, bool debug);
125 Dll_Export bool parseSocketData(int xmlBlasterSocket, const XmlBlasterReadFromSocketFuncHolder *fpHolder, SocketDataHolder *socketDataHolder, XmlBlasterException *exception, bool *stopP, bool udp, bool debug);
126 Dll_Export void convertToXmlBlasterException(const XmlBlasterBlob *blob, XmlBlasterException *exception, bool debug);
127 Dll_Export void encodeXmlBlasterException(XmlBlasterBlob *blob, const XmlBlasterException *exception, bool debug);
128 Dll_Export MsgUnitArr *parseMsgUnitArr(size_t dataLen, char *data);
129 Dll_Export QosArr *parseQosArr(size_t dataLen, char *data);
130 
131 #ifdef __cplusplus
132 #ifndef XMLBLASTER_C_COMPILE_AS_CPP
133 } /* extern "C" */
134 #endif
135 #endif
136 
137 #endif /* XMLBLASTER_SOCKET_H */


syntax highlighted by Code2HTML, v. 0.9.1