2 * AxWin3 Interface Library
3 * - By John Hodge (thePowersGang)
6 * - Message handling / IPC
8 #include <axwin3/axwin.h>
12 #include <ipcmessages.h> // AxWin3 common
13 #include "include/internal.h"
14 #include "include/ipc.h"
26 enum eConnectionType giConnectionType;
27 int giConnectionNum; // FD or PID
28 char gaAxWin3_int_UDPHeader[] = {5,16,0,0}; // Port 4101
29 int giAxWin3_int_UDPHeaderLen = sizeof(gaAxWin3_int_UDPHeader);
30 const char *gsAxWin3_int_ServerDesc;
31 tAxWin3_MessageCallback gAxWin3_MessageCallback;
34 void AxWin3_Connect(const char *ServerDesc)
36 _SysDebug("ServerDesc (argument) = %s", ServerDesc);
38 ServerDesc = gsAxWin3_int_ServerDesc;
40 _SysDebug("ServerDesc = %s", ServerDesc);
48 case '1': case '2': case '3': case '4': case '5':
49 case '6': case '7': case '8': case '9': case '0':
50 giConnectionType = CONNTYPE_SENDMESSAGE;
51 giConnectionNum = atoi(ServerDesc);
52 if( giConnectionNum == 0 ) {
53 _SysDebug("Invalid server PID");
58 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
60 // TODO: Open socket and create UDP header
63 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
70 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
72 tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
73 gAxWin3_MessageCallback = Callback;
77 uint32_t AxWin3_int_GetWindowID(tHWND Window)
80 return Window->ServerID;
85 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
87 tAxWin_IPCMessage *ret;
89 ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
92 ret->Window = AxWin3_int_GetWindowID(Window);
93 ret->Size = ExtraBytes;
97 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
99 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
100 switch(giConnectionType)
102 case CONNTYPE_SENDMESSAGE:
103 _SysSendMessage(giConnectionNum, size, Msg);
107 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
108 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
109 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
110 _SysWrite(giConnectionNum, tmpbuf, sizeof(tmpbuf));
114 _SysWrite(giConnectionNum, Msg, size);
121 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(int nFD, fd_set *fds)
124 tAxWin_IPCMessage *ret = NULL;
127 _SysSelect(nFD, fds, NULL, NULL, NULL, THREAD_EVENT_IPCMSG);
129 // Clear out IPC messages
130 while( (len = _SysGetMessage(&tid, 0, NULL)) )
132 if( giConnectionType != CONNTYPE_SENDMESSAGE || tid != giConnectionNum )
134 _SysDebug("%i byte message from %i", len, tid);
135 // If not, pass the buck (or ignore)
136 if( gAxWin3_MessageCallback )
137 gAxWin3_MessageCallback(tid, len);
139 _SysGetMessage(NULL, 0, GETMSG_IGNORE);
143 // Using CONNTYPE_SENDMESSAGE and server message has arrived
146 _SysDebug("malloc() failed, ignoring message");
147 _SysGetMessage(NULL, 0, GETMSG_IGNORE);
150 _SysGetMessage(NULL, len, ret);
154 if( giConnectionType == CONNTYPE_TCP || giConnectionType == CONNTYPE_UDP )
162 tAxWin_IPCMessage *AxWin3_int_WaitIPCMessage(int WantedID)
164 tAxWin_IPCMessage *msg;
167 msg = AxWin3_int_GetIPCMessage(0, NULL);
168 if(msg->ID == WantedID) return msg;
169 AxWin3_int_HandleMessage( msg );