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 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
71 int size = sizeof(tAxWin_IPCMessage) + Msg->Size;
72 switch(giConnectionType)
74 case CONNTYPE_SENDMESSAGE:
75 SysSendMessage(giConnectionNum, size, Msg);
79 char tmpbuf[giAxWin3_int_UDPHeaderLen + size];
80 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
81 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
82 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
86 write(giConnectionNum, Msg, size);
93 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
96 tAxWin_IPCMessage *ret = NULL;
97 switch(giConnectionType)
99 case CONNTYPE_SENDMESSAGE:
100 while(SysGetMessage(NULL, NULL))
103 len = SysGetMessage(&tid, NULL);
104 // Check if the message came from the server
105 if(tid != giConnectionNum)
107 // If not, pass the buck (or ignore)
108 if( gAxWin3_MessageCallback )
109 gAxWin3_MessageCallback(tid, len);
111 SysGetMessage(NULL, GETMSG_IGNORE);
115 // If it's from the server, allocate a buffer and return it
117 if(ret == NULL) return NULL;
118 SysGetMessage(NULL, ret);
124 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
132 // TODO: Sanity checks, so a stupid server can't crash us