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 = %s", ServerDesc);
39 ServerDesc = gsAxWin3_int_ServerDesc;
41 _SysDebug("ServerDesc = %s", ServerDesc);
49 case '1': case '2': case '3': case '4': case '5':
50 case '6': case '7': case '8': case '9': case '0':
51 giConnectionType = CONNTYPE_SENDMESSAGE;
52 giConnectionNum = atoi(ServerDesc);
55 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
57 // TODO: Open socket and create UDP header
60 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
67 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
69 tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
70 gAxWin3_MessageCallback = Callback;
74 uint32_t AxWin3_int_GetWindowID(tHWND Window)
77 return Window->ServerID;
82 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
84 tAxWin_IPCMessage *ret;
86 ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
89 ret->Window = AxWin3_int_GetWindowID(Window);
90 ret->Size = ExtraBytes;
94 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
96 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
97 switch(giConnectionType)
99 case CONNTYPE_SENDMESSAGE:
100 SysSendMessage(giConnectionNum, size, Msg);
104 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
105 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
106 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
107 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
111 write(giConnectionNum, Msg, size);
118 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(int nFD, fd_set *fds)
121 tAxWin_IPCMessage *ret = NULL;
124 _SysSelect(nFD, fds, NULL, NULL, NULL, THREAD_EVENT_IPCMSG);
126 // Clear out IPC messages
127 while( (len = SysGetMessage(&tid, 0, NULL)) )
129 if( giConnectionType != CONNTYPE_SENDMESSAGE || tid != giConnectionNum )
131 _SysDebug("%i byte message from %i", len, tid);
132 // If not, pass the buck (or ignore)
133 if( gAxWin3_MessageCallback )
134 gAxWin3_MessageCallback(tid, len);
136 SysGetMessage(NULL, 0, GETMSG_IGNORE);
140 // Using CONNTYPE_SENDMESSAGE and server message has arrived
143 _SysDebug("malloc() failed, ignoring message");
144 SysGetMessage(NULL, 0, GETMSG_IGNORE);
147 SysGetMessage(NULL, len, ret);
151 if( giConnectionType == CONNTYPE_TCP || giConnectionType == CONNTYPE_UDP )
159 tAxWin_IPCMessage *AxWin3_int_WaitIPCMessage(int WantedID)
161 tAxWin_IPCMessage *msg;
164 msg = AxWin3_int_GetIPCMessage(0, NULL);
165 if(msg->ID == WantedID) return msg;
166 AxWin3_int_HandleMessage( msg );