35057a82a7d7904fd9fa366bdb4181de4561193a
[tpg/acess2.git] / Usermode / Libraries / libaxwin3.so_src / msg.c
1 /*
2  * AxWin3 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * msg.c
6  * - Message handling / IPC
7  */
8 #include <axwin3/axwin.h>
9 #include <acess/sys.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include "include/ipc.h"
13
14 // === CONSTANTS ===
15 enum eConnectionType
16 {
17         CONNTYPE_SENDMESSAGE,
18         CONNTYPE_UDP,
19         CONNTYPE_TCP
20 };
21
22 // === GLOBALS ===
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;
29
30 // === CODE ===
31 void AxWin3_Connect(const char *ServerDesc)
32 {
33         if( !ServerDesc )
34         {
35                 ServerDesc = gsAxWin3_int_ServerDesc;
36         }
37         if( !ServerDesc )
38         {
39                 // TODO: Error out
40                 return ;
41         }
42         switch(ServerDesc[0])
43         {
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);
48                 break;
49         case 'u':
50                 while(*ServerDesc && *ServerDesc != ':')        ServerDesc ++;
51                 ServerDesc ++;
52                 // TODO: Open socket and create UDP header
53                 break;
54         case 't':
55                 while(*ServerDesc && *ServerDesc != ':')        ServerDesc ++;
56                 ServerDesc ++;
57                 // TODO: Open socket
58                 break;
59         }
60 }
61
62 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
63 {
64         tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
65         gAxWin3_MessageCallback = Callback;
66         return old;
67 }
68
69 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
70 {
71          int    size = sizeof(tAxWin_IPCMessage) + Msg->Size;
72         switch(giConnectionType)
73         {
74         case CONNTYPE_SENDMESSAGE:
75                 SysSendMessage(giConnectionNum, size, Msg);
76                 break;
77         case CONNTYPE_UDP: {
78                 // Create UDP header
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));
83                 }
84                 break;
85         case CONNTYPE_TCP:
86                 write(giConnectionNum, Msg, size);
87                 break;
88         default:
89                 break;
90         }
91 }
92
93 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
94 {
95          int    len;
96         tAxWin_IPCMessage       *ret = NULL;
97         switch(giConnectionType)
98         {
99         case CONNTYPE_SENDMESSAGE:
100                 while(SysGetMessage(NULL, NULL))
101                 {
102                         pid_t   tid;
103                         len = SysGetMessage(&tid, NULL);
104                         // Check if the message came from the server
105                         if(tid != giConnectionNum)
106                         {
107                                 // If not, pass the buck (or ignore)
108                                 if( gAxWin3_MessageCallback )
109                                         gAxWin3_MessageCallback(tid, len);
110                                 else
111                                         SysGetMessage(NULL, GETMSG_IGNORE);
112                                 continue ;
113                         }
114                         
115                         // If it's from the server, allocate a buffer and return it
116                         ret = malloc(len);
117                         if(ret == NULL) return NULL;
118                         SysGetMessage(NULL, ret);
119                         break;
120                 }
121                 break;
122         default:
123                 // TODO: Implement
124                 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
125                 break;
126         }
127
128         // No message?
129         if( ret == NULL )
130                 return NULL;
131
132         // TODO: Sanity checks, so a stupid server can't crash us
133
134         return ret;
135 }
136

UCC git Repository :: git.ucc.asn.au