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(void)
121 tAxWin_IPCMessage *ret = NULL;
122 switch(giConnectionType)
124 case CONNTYPE_SENDMESSAGE:
129 // Wait for a message to arrive
130 while( !(len = SysGetMessage(&tid, NULL)) )
132 _SysWaitEvent(THREAD_EVENT_IPCMSG);
135 // Check if the message came from the server
136 if(tid != giConnectionNum)
138 _SysDebug("%i byte message from %i", len, tid);
139 // If not, pass the buck (or ignore)
140 if( gAxWin3_MessageCallback )
141 gAxWin3_MessageCallback(tid, len);
143 SysGetMessage(NULL, GETMSG_IGNORE);
147 // If it's from the server, allocate a buffer and return it
150 _SysDebug("malloc() failed, ignoring message");
151 SysGetMessage(NULL, GETMSG_IGNORE);
154 SysGetMessage(NULL, ret);
160 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
168 // TODO: Sanity checks, so a stupid server can't crash us
173 tAxWin_IPCMessage *AxWin3_int_WaitIPCMessage(int WantedID)
175 tAxWin_IPCMessage *msg;
178 msg = AxWin3_int_GetIPCMessage();
179 if(msg->ID == WantedID) return msg;
180 AxWin3_int_HandleMessage( msg );