Usermode/AxWin3 - Added ipcpipe support
[tpg/acess2.git] / Usermode / Applications / axwin3_src / libaxwin3.so_src / msg.c
index 6c134a3..955da44 100644 (file)
 #include "include/internal.h"
 #include "include/ipc.h"
 
+#define assert(cnd)    do{if(!(cnd)){_SysDebug("Assertion failed: %s", #cnd);}}while(0)
+
+#define STATICBUF_SIZE 0x1000
+
 // === CONSTANTS ===
 enum eConnectionType
 {
        CONNTYPE_NONE,
        CONNTYPE_SENDMESSAGE,
+       CONNTYPE_IPCPIPE,
        CONNTYPE_UDP,
        CONNTYPE_TCP
 };
@@ -33,7 +38,6 @@ tAxWin3_MessageCallback       gAxWin3_MessageCallback;
 // === CODE ===
 void AxWin3_Connect(const char *ServerDesc)
 {
-       _SysDebug("ServerDesc (argument) = %s", ServerDesc);
        if( !ServerDesc ) {
                ServerDesc = gsAxWin3_int_ServerDesc;
        }
@@ -64,6 +68,20 @@ void AxWin3_Connect(const char *ServerDesc)
                ServerDesc ++;
                // TODO: Open socket
                break;
+       case 'p':
+               assert( strncmp(ServerDesc, "pipe:", 5) == 0 );
+               ServerDesc += 5;
+               giConnectionType = CONNTYPE_IPCPIPE;
+               giConnectionNum = _SysOpen(ServerDesc, OPENFLAG_READ|OPENFLAG_WRITE);
+               if( giConnectionNum == -1 ) {
+                       _SysDebug("Cannot open IPC Pipe '%s'", ServerDesc);
+                       exit(-1);
+               }
+               break;
+       default:
+               _SysDebug("Unknown server desc format '%s'", ServerDesc);
+               exit(-1);
+               break;
        }
 }
 
@@ -100,18 +118,19 @@ void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
        switch(giConnectionType)
        {
        case CONNTYPE_SENDMESSAGE:
-               SysSendMessage(giConnectionNum, size, Msg);
+               _SysSendMessage(giConnectionNum, size, Msg);
                break;
        case CONNTYPE_UDP: {
                // Create UDP header
                char    tmpbuf[giAxWin3_int_UDPHeaderLen + size];
                memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
                memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
-               write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
+               _SysWrite(giConnectionNum, tmpbuf, sizeof(tmpbuf));
                }
                break;
+       case CONNTYPE_IPCPIPE:
        case CONNTYPE_TCP:
-               write(giConnectionNum, Msg, size);
+               _SysWrite(giConnectionNum, Msg, size);
                break;
        default:
                break;
@@ -122,12 +141,23 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(int nFD, fd_set *fds)
 {
         int    len;
        tAxWin_IPCMessage       *ret = NULL;
-       pid_t   tid;
+        int    tid;
+       fd_set  local_set;
+       
+       if(!fds)
+               fds = &local_set;
+
+       if( giConnectionType != CONNTYPE_SENDMESSAGE )
+       {
+               if(nFD <= giConnectionNum)
+                       nFD = giConnectionNum+1;
+               FD_SET(giConnectionNum, fds);
+       }
 
        _SysSelect(nFD, fds, NULL, NULL, NULL, THREAD_EVENT_IPCMSG);
        
        // Clear out IPC messages
-       while( (len = SysGetMessage(&tid, 0, NULL)) )
+       while( (len = _SysGetMessage(&tid, 0, NULL)) )
        {
                if( giConnectionType != CONNTYPE_SENDMESSAGE || tid != giConnectionNum )
                {
@@ -136,7 +166,7 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(int nFD, fd_set *fds)
                        if( gAxWin3_MessageCallback )
                                gAxWin3_MessageCallback(tid, len);
                        else
-                               SysGetMessage(NULL, 0, GETMSG_IGNORE);
+                               _SysGetMessage(NULL, 0, GETMSG_IGNORE);
                        continue ;
                }
                
@@ -144,16 +174,31 @@ tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(int nFD, fd_set *fds)
                ret = malloc(len);
                if(ret == NULL) {
                        _SysDebug("malloc() failed, ignoring message");
-                       SysGetMessage(NULL, 0, GETMSG_IGNORE);
+                       _SysGetMessage(NULL, 0, GETMSG_IGNORE);
                        return NULL;
                }
-               SysGetMessage(NULL, len, ret);
+               _SysGetMessage(NULL, len, ret);
                break;
        }
 
-       if( giConnectionType == CONNTYPE_TCP || giConnectionType == CONNTYPE_UDP )
+       if( giConnectionType != CONNTYPE_SENDMESSAGE )
        {
-               // Check fds
+               if( FD_ISSET(giConnectionNum, fds) )
+               {
+                       char    tmpbuf[STATICBUF_SIZE];
+                       char    *data = tmpbuf;
+                       size_t len = _SysRead(giConnectionNum, tmpbuf, sizeof(tmpbuf));
+                       
+                       if( giConnectionType == CONNTYPE_UDP )
+                       {
+                               assert(len > giAxWin3_int_UDPHeaderLen);
+                               len -= giAxWin3_int_UDPHeaderLen;
+                               data += giAxWin3_int_UDPHeaderLen;
+                       }
+                       assert(len >= sizeof(tAxWin_IPCMessage));
+                       ret = malloc(len);
+                       memcpy(ret, data, len);
+               }
        }
 
        return ret;

UCC git Repository :: git.ucc.asn.au