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)
36 ServerDesc = gsAxWin3_int_ServerDesc;
45 case '1': case '2': case '3': case '4': case '5':
46 case '6': case '7': case '8': case '9': case '0':
47 giConnectionType = CONNTYPE_SENDMESSAGE;
48 giConnectionNum = atoi(ServerDesc);
51 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
53 // TODO: Open socket and create UDP header
56 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
63 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
65 tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
66 gAxWin3_MessageCallback = Callback;
70 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
72 tAxWin_IPCMessage *ret;
74 ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
78 ret->Window = Window->ServerID;
81 ret->Size = ExtraBytes;
85 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
87 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
88 switch(giConnectionType)
90 case CONNTYPE_SENDMESSAGE:
91 SysSendMessage(giConnectionNum, size, Msg);
95 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
96 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
97 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
98 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
102 write(giConnectionNum, Msg, size);
109 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
112 tAxWin_IPCMessage *ret = NULL;
113 switch(giConnectionType)
115 case CONNTYPE_SENDMESSAGE:
116 while(SysGetMessage(NULL, NULL))
119 len = SysGetMessage(&tid, NULL);
120 // Check if the message came from the server
121 if(tid != giConnectionNum)
123 // If not, pass the buck (or ignore)
124 if( gAxWin3_MessageCallback )
125 gAxWin3_MessageCallback(tid, len);
127 SysGetMessage(NULL, GETMSG_IGNORE);
131 // If it's from the server, allocate a buffer and return it
133 if(ret == NULL) return NULL;
134 SysGetMessage(NULL, ret);
140 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
148 // TODO: Sanity checks, so a stupid server can't crash us