Usermode/AxWin3 - Working on interface library
[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 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
70 {
71         tAxWin_IPCMessage       *ret;
72
73         ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
74         ret->Flags = Flags;
75         ret->ID = Message;
76         ret->Window = Window;
77         ret->Size = ExtraBytes;
78         return ret;
79 }
80
81 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
82 {
83          int    size = sizeof(tAxWin_IPCMessage) + Msg->Size;
84         switch(giConnectionType)
85         {
86         case CONNTYPE_SENDMESSAGE:
87                 SysSendMessage(giConnectionNum, size, Msg);
88                 break;
89         case CONNTYPE_UDP: {
90                 // Create UDP header
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));
95                 }
96                 break;
97         case CONNTYPE_TCP:
98                 write(giConnectionNum, Msg, size);
99                 break;
100         default:
101                 break;
102         }
103 }
104
105 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
106 {
107          int    len;
108         tAxWin_IPCMessage       *ret = NULL;
109         switch(giConnectionType)
110         {
111         case CONNTYPE_SENDMESSAGE:
112                 while(SysGetMessage(NULL, NULL))
113                 {
114                         pid_t   tid;
115                         len = SysGetMessage(&tid, NULL);
116                         // Check if the message came from the server
117                         if(tid != giConnectionNum)
118                         {
119                                 // If not, pass the buck (or ignore)
120                                 if( gAxWin3_MessageCallback )
121                                         gAxWin3_MessageCallback(tid, len);
122                                 else
123                                         SysGetMessage(NULL, GETMSG_IGNORE);
124                                 continue ;
125                         }
126                         
127                         // If it's from the server, allocate a buffer and return it
128                         ret = malloc(len);
129                         if(ret == NULL) return NULL;
130                         SysGetMessage(NULL, ret);
131                         break;
132                 }
133                 break;
134         default:
135                 // TODO: Implement
136                 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
137                 break;
138         }
139
140         // No message?
141         if( ret == NULL )
142                 return NULL;
143
144         // TODO: Sanity checks, so a stupid server can't crash us
145
146         return ret;
147 }
148

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