Merge branch 'master' of git://cadel.mutabah.net/acess2
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / ipc_acess.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  * 
5  * ipc_acess.c
6  * - Interprocess communication (acess handlers)
7  */
8 #include <ipc_int.h>
9 #include <common.h>
10 #include <acess/sys.h>
11 #include <net.h>
12 #include <string.h>
13
14 // === CONSTANTS ===
15 #define STATICBUF_SIZE  64
16 #define AXWIN_PORT      4101
17
18 // === PROTOTYPES ===
19 void    IPC_Init(void);
20 void    IPC_FillSelect(int *nfds, fd_set *set);
21 void    IPC_HandleSelect(fd_set *set);
22  int    IPC_Type_Datagram_GetSize(const void *Ident);
23  int    IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
24 void    IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
25  int    IPC_Type_Sys_GetSize(const void *Ident);
26  int    IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
27 void    IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
28  int    IPC_Type_IPCPipe_GetSize(const void *Ident);
29  int    IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2);
30 void    IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data);
31
32 // === GLOBALS ===
33 const tIPC_Type gIPC_Type_Datagram = {
34         IPC_Type_Datagram_GetSize,
35         IPC_Type_Datagram_Compare, 
36         IPC_Type_Datagram_Send
37 };
38 const tIPC_Type gIPC_Type_SysMessage = {
39         IPC_Type_Sys_GetSize,
40         IPC_Type_Sys_Compare,
41         IPC_Type_Sys_Send
42 };
43 const tIPC_Type gIPC_Type_IPCPipe = {
44         IPC_Type_IPCPipe_GetSize,
45         IPC_Type_IPCPipe_Compare,
46         IPC_Type_IPCPipe_Send
47 };
48  int    giNetworkFileHandle = -1;
49  int    giIPCPipeHandle = -1;
50
51 // === CODE ===
52 void IPC_Init(void)
53 {
54          int    tmp;
55         // TODO: Check this
56         giNetworkFileHandle = _SysOpen("/Devices/ip/loop/udp", OPENFLAG_READ);
57         if( giNetworkFileHandle != -1 )
58         {
59                 tmp = AXWIN_PORT;
60                 _SysIOCtl(giNetworkFileHandle, 4, &tmp);        // TODO: Don't hard-code IOCtl number
61         }
62         
63         giIPCPipeHandle = _SysOpen("/Devices/ipcpipe/axwin"/*-$USER*/, OPENFLAG_CREATE);
64         _SysDebug("giIPCPipeHandle = %i", giIPCPipeHandle);
65         if( giIPCPipeHandle == -1 )
66                 _SysDebug("ERROR: Can't create IPCPipe handle");
67 }
68
69 void _setfd(int fd, int *nfds, fd_set *set)
70 {
71         if( fd >= 0 )
72         {
73                 if( fd >= *nfds )       *nfds = fd+1;
74                 FD_SET(fd, set);
75         }
76 }
77
78 void IPC_FillSelect(int *nfds, fd_set *set)
79 {
80         _setfd(giNetworkFileHandle, nfds, set);
81         _setfd(giIPCPipeHandle, nfds, set);
82         for( int i = 0; i < giIPC_ClientCount; i ++ )
83         {
84                 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
85                         _setfd( *(int*)(gIPC_Clients[i]->Ident), nfds, set );
86         }
87 }
88
89 void IPC_HandleSelect(fd_set *set)
90 {
91         if( giNetworkFileHandle != -1 && FD_ISSET(giNetworkFileHandle, set) )
92         {
93                 char    staticBuf[STATICBUF_SIZE];
94                  int    readlen, identlen;
95                 char    *msg;
96
97                 readlen = _SysRead(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
98                 
99                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
100                 msg = staticBuf + identlen;
101
102                 IPC_Handle( IPC_int_GetClient(&gIPC_Type_Datagram, staticBuf), readlen - identlen, (void*)msg);
103                 //_SysDebug("IPC_HandleSelect: UDP handled");
104         }
105         
106         if( giIPCPipeHandle != -1 && FD_ISSET(giIPCPipeHandle, set) )
107         {
108                 int newfd = _SysOpenChild(giIPCPipeHandle, "newclient", OPENFLAG_READ|OPENFLAG_WRITE);
109                 _SysDebug("newfd = %i", newfd);
110                 IPC_int_GetClient(&gIPC_Type_IPCPipe, &newfd);
111         }
112         
113         for( int i = 0; i < giIPC_ClientCount; i ++ )
114         {
115                 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
116                 {
117                          int fd = *(const int*)gIPC_Clients[i]->Ident;
118                         if( FD_ISSET(fd, set) )
119                         {
120                                 char    staticBuf[STATICBUF_SIZE];
121                                 size_t  len;
122                                 len = _SysRead(fd, staticBuf, sizeof(staticBuf));
123                                 if( len == (size_t)-1 ) {
124                                         // TODO: Check errno for EINTR
125                                         IPC_int_DropClient(gIPC_Clients[i]);
126                                         break;
127                                 }
128                                 IPC_Handle( gIPC_Clients[i], len, (void*)staticBuf );
129                         }
130                 }
131         }
132
133         size_t  len;
134         int     tid;
135         while( (len = _SysGetMessage(&tid, 0, NULL)) )
136         {
137                 char    data[len];
138                 _SysGetMessage(NULL, len, data);
139
140                 IPC_Handle( IPC_int_GetClient(&gIPC_Type_SysMessage, &tid), len, (void*)data );
141 //              _SysDebug("IPC_HandleSelect: Message handled");
142         }
143 }
144
145 int IPC_Type_Datagram_GetSize(const void *Ident)
146 {
147         return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
148 }
149
150 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
151 {
152         // Pass the buck :)
153         // - No need to worry about mis-matching sizes, as the size is computed
154         //   from the 3rd/4th bytes, hence it will differ before the size is hit.
155         return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
156 }
157
158 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
159 {
160          int    identlen = IPC_Type_Datagram_GetSize(Ident);
161         char    tmpbuf[ identlen + Length ];
162         memcpy(tmpbuf, Ident, identlen);        // Header
163         memcpy(tmpbuf + identlen, Data, Length);        // Data
164         // TODO: Handle fragmented packets
165         _SysWrite(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
166 }
167
168 int IPC_Type_Sys_GetSize(const void *Ident)
169 {
170         return sizeof(pid_t);
171 }
172
173 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
174 {
175         return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
176 }
177
178 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
179 {
180         _SysSendMessage( *(const tid_t*)Ident, Length, Data );
181 }
182
183 int IPC_Type_IPCPipe_GetSize(const void *Ident)
184 {
185         return sizeof(int);
186 }
187 int IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2)
188 {
189         return *(const int*)Ident1 - *(const int*)Ident2;
190 }
191 void IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data)
192 {
193         size_t rv = _SysWrite( *(const int*)Ident, Data, Length );
194         if(rv != Length) {
195                 _SysDebug("Sent message oversize %x", Length);
196         }
197 }

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