2 * AxWin3 Interface Library
3 * - By John Hodge (thePowersGang)
6 * - Message handling / IPC
8 #include <axwin3/axwin.h>
12 #include "include/ipc.h"
23 enum eConnectionType giConnectionType;
24 int giConnectionNum; // FD or PID
25 char gaAxWin3_int_UDPHeader[] = {5,16,0,0}; // Port 4101
26 int giAxWin3_int_UDPHeaderLen = sizeof(gaAxWin3_int_UDPHeader);
27 const char *gsAxWin3_int_ServerDesc;
28 tAxWin3_MessageCallback gAxWin3_MessageCallback;
31 void AxWin3_Connect(const char *ServerDesc)
35 ServerDesc = gsAxWin3_int_ServerDesc;
44 case '1': case '2': case '3': case '4': case '5':
45 case '6': case '7': case '8': case '9': case '0':
46 giConnectionType = CONNTYPE_SENDMESSAGE;
47 giConnectionNum = atoi(ServerDesc);
50 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
52 // TODO: Open socket and create UDP header
55 while(*ServerDesc && *ServerDesc != ':') ServerDesc ++;
62 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
64 tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
65 gAxWin3_MessageCallback = Callback;
69 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
71 tAxWin_IPCMessage *ret;
73 ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
77 ret->Size = ExtraBytes;
81 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
83 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
84 switch(giConnectionType)
86 case CONNTYPE_SENDMESSAGE:
87 SysSendMessage(giConnectionNum, size, Msg);
91 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
92 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
93 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
94 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
98 write(giConnectionNum, Msg, size);
105 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
108 tAxWin_IPCMessage *ret = NULL;
109 switch(giConnectionType)
111 case CONNTYPE_SENDMESSAGE:
112 while(SysGetMessage(NULL, NULL))
115 len = SysGetMessage(&tid, NULL);
116 // Check if the message came from the server
117 if(tid != giConnectionNum)
119 // If not, pass the buck (or ignore)
120 if( gAxWin3_MessageCallback )
121 gAxWin3_MessageCallback(tid, len);
123 SysGetMessage(NULL, GETMSG_IGNORE);
127 // If it's from the server, allocate a buffer and return it
129 if(ret == NULL) return NULL;
130 SysGetMessage(NULL, ret);
136 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
144 // TODO: Sanity checks, so a stupid server can't crash us