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"
24 enum eConnectionType giConnectionType;
25 int giConnectionNum; // FD or PID
26 char gaAxWin3_int_UDPHeader[] = {5,16,0,0}; // Port 4101
27 int giAxWin3_int_UDPHeaderLen = sizeof(gaAxWin3_int_UDPHeader);
28 const char *gsAxWin3_int_ServerDesc;
29 tAxWin3_MessageCallback gAxWin3_MessageCallback;
32 void AxWin3_Connect(const char *ServerDesc)
34 _SysDebug("ServerDesc = %s", ServerDesc);
37 ServerDesc = gsAxWin3_int_ServerDesc;
39 _SysDebug("ServerDesc = %s", ServerDesc);
47 case '1': case '2': case '3': case '4': case '5':
48 case '6': case '7': case '8': case '9': case '0':
49 giConnectionType = CONNTYPE_SENDMESSAGE;
50 giConnectionNum = atoi(ServerDesc);
53 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
55 // TODO: Open socket and create UDP header
58 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
65 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
67 tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
68 gAxWin3_MessageCallback = Callback;
72 uint32_t AxWin3_int_GetWindowID(tHWND Window)
75 return Window->ServerID;
80 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
82 tAxWin_IPCMessage *ret;
84 ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
87 ret->Window = AxWin3_int_GetWindowID(Window);
88 ret->Size = ExtraBytes;
92 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
94 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
95 switch(giConnectionType)
97 case CONNTYPE_SENDMESSAGE:
98 SysSendMessage(giConnectionNum, size, Msg);
102 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
103 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
104 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
105 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
109 write(giConnectionNum, Msg, size);
116 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
119 tAxWin_IPCMessage *ret = NULL;
120 switch(giConnectionType)
122 case CONNTYPE_SENDMESSAGE:
123 // TODO: Less hack, I need a version of select for GetMessage etc
124 if(SysGetMessage(NULL, NULL) == 0) sleep();
125 while(SysGetMessage(NULL, NULL))
128 len = SysGetMessage(&tid, NULL);
129 // Check if the message came from the server
130 if(tid != giConnectionNum)
132 // If not, pass the buck (or ignore)
133 if( gAxWin3_MessageCallback )
134 gAxWin3_MessageCallback(tid, len);
136 SysGetMessage(NULL, GETMSG_IGNORE);
140 // If it's from the server, allocate a buffer and return it
142 if(ret == NULL) return NULL;
143 SysGetMessage(NULL, ret);
149 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
157 // TODO: Sanity checks, so a stupid server can't crash us