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"
25 enum eConnectionType giConnectionType;
26 int giConnectionNum; // FD or PID
27 char gaAxWin3_int_UDPHeader[] = {5,16,0,0}; // Port 4101
28 int giAxWin3_int_UDPHeaderLen = sizeof(gaAxWin3_int_UDPHeader);
29 const char *gsAxWin3_int_ServerDesc;
30 tAxWin3_MessageCallback gAxWin3_MessageCallback;
33 void AxWin3_Connect(const char *ServerDesc)
35 _SysDebug("ServerDesc = %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);
54 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
56 // TODO: Open socket and create UDP header
59 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
66 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
68 tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
69 gAxWin3_MessageCallback = Callback;
73 uint32_t AxWin3_int_GetWindowID(tHWND Window)
76 return Window->ServerID;
81 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
83 tAxWin_IPCMessage *ret;
85 ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
88 ret->Window = AxWin3_int_GetWindowID(Window);
89 ret->Size = ExtraBytes;
93 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
95 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
96 switch(giConnectionType)
98 case CONNTYPE_SENDMESSAGE:
99 SysSendMessage(giConnectionNum, size, Msg);
103 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
104 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
105 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
106 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
110 write(giConnectionNum, Msg, size);
117 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
120 tAxWin_IPCMessage *ret = NULL;
121 switch(giConnectionType)
123 case CONNTYPE_SENDMESSAGE:
124 _SysWaitEvent(THREAD_EVENT_IPCMSG);
125 while(SysGetMessage(NULL, NULL))
128 len = SysGetMessage(&tid, NULL);
129 // Check if the message came from the server
130 if(tid != giConnectionNum)
132 _SysDebug("%i byte message from %i", len, tid);
133 // If not, pass the buck (or ignore)
134 if( gAxWin3_MessageCallback )
135 gAxWin3_MessageCallback(tid, len);
137 SysGetMessage(NULL, GETMSG_IGNORE);
141 // If it's from the server, allocate a buffer and return it
144 _SysDebug("malloc() failed, ignoring message");
145 SysGetMessage(NULL, GETMSG_IGNORE);
148 SysGetMessage(NULL, ret);
154 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
162 // TODO: Sanity checks, so a stupid server can't crash us
167 tAxWin_IPCMessage *AxWin3_int_WaitIPCMessage(int WantedID)
169 tAxWin_IPCMessage *msg;
172 msg = AxWin3_int_GetIPCMessage();
173 if(msg->ID == WantedID) return msg;
174 AxWin3_int_HandleMessage( msg );