2 * AxWin3 Interface Library
3 * - By John Hodge (thePowersGang)
6 * - Message handling / IPC
8 #include <axwin3/axwin.h>
12 #include "include/ipc.h"
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 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
74 tAxWin_IPCMessage *ret;
76 ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
80 ret->Window = Window->ServerID;
83 ret->Size = ExtraBytes;
87 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
89 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
90 switch(giConnectionType)
92 case CONNTYPE_SENDMESSAGE:
93 SysSendMessage(giConnectionNum, size, Msg);
97 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
98 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
99 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
100 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
104 write(giConnectionNum, Msg, size);
111 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
114 tAxWin_IPCMessage *ret = NULL;
115 switch(giConnectionType)
117 case CONNTYPE_SENDMESSAGE:
118 if(SysGetMessage(NULL, NULL) == 0)
120 while(SysGetMessage(NULL, NULL))
123 len = SysGetMessage(&tid, NULL);
124 // Check if the message came from the server
125 if(tid != giConnectionNum)
127 // If not, pass the buck (or ignore)
128 if( gAxWin3_MessageCallback )
129 gAxWin3_MessageCallback(tid, len);
131 SysGetMessage(NULL, GETMSG_IGNORE);
135 // If it's from the server, allocate a buffer and return it
137 if(ret == NULL) return NULL;
138 SysGetMessage(NULL, ret);
144 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
152 // TODO: Sanity checks, so a stupid server can't crash us