Usermode/AxWin3 - Clean up unneeded (and silly) log message
[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  256
16 #define AXWIN_PORT      4101
17
18 // === PROTOTYPES ===
19 void    IPC_Init(void);
20 void    IPC_FillSelect(int *nfds, fd_set *set, fd_set *err_set);
21 void    IPC_HandleSelect(fd_set *set, fd_set *err_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, fd_set *err_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                         _setfd( *(int*)(gIPC_Clients[i]->Ident), nfds, err_set );
87                 }
88         }
89 }
90
91 void IPC_HandleSelect(fd_set *set, fd_set *err_set)
92 {
93         if( giNetworkFileHandle != -1 && FD_ISSET(giNetworkFileHandle, set) )
94         {
95                 char    staticBuf[STATICBUF_SIZE];
96                  int    readlen, identlen;
97                 char    *msg;
98
99                 readlen = _SysRead(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
100                 
101                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
102                 msg = staticBuf + identlen;
103
104                 IPC_Handle( IPC_int_GetClient(&gIPC_Type_Datagram, staticBuf), readlen - identlen, (void*)msg);
105                 //_SysDebug("IPC_HandleSelect: UDP handled");
106         }
107         
108         if( giIPCPipeHandle != -1 && FD_ISSET(giIPCPipeHandle, set) )
109         {
110                 int newfd = _SysOpenChild(giIPCPipeHandle, "newclient", OPENFLAG_READ|OPENFLAG_WRITE);
111                 _SysDebug("newfd = %i", newfd);
112                 IPC_int_GetClient(&gIPC_Type_IPCPipe, &newfd);
113         }
114         
115         for( int i = 0; i < giIPC_ClientCount; i ++ )
116         {
117                 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
118                 {
119                          int fd = *(const int*)gIPC_Clients[i]->Ident;
120                         if( FD_ISSET(fd, set) )
121                         {
122                                 char    staticBuf[STATICBUF_SIZE];
123                                 size_t  len;
124                                 len = _SysRead(fd, staticBuf, sizeof(staticBuf));
125                                 if( len == (size_t)-1 ) {
126                                         // TODO: Check errno for EINTR
127                                         IPC_int_DropClient(gIPC_Clients[i]);
128                                         break;
129                                 }
130                                 IPC_Handle( gIPC_Clients[i], len, (void*)staticBuf );
131                         }
132                         if( FD_ISSET(fd, err_set) )
133                         {
134                                 // Client disconnected
135                                 IPC_int_DropClient(gIPC_Clients[i]);
136                                 _SysClose(fd);
137                         }
138                 }
139         }
140
141         size_t  len;
142         int     tid;
143         while( (len = _SysGetMessage(&tid, 0, NULL)) )
144         {
145                 char    data[len];
146                 _SysGetMessage(NULL, len, data);
147
148                 IPC_Handle( IPC_int_GetClient(&gIPC_Type_SysMessage, &tid), len, (void*)data );
149 //              _SysDebug("IPC_HandleSelect: Message handled");
150         }
151 }
152
153 int IPC_Type_Datagram_GetSize(const void *Ident)
154 {
155         return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
156 }
157
158 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
159 {
160         // Pass the buck :)
161         // - No need to worry about mis-matching sizes, as the size is computed
162         //   from the 3rd/4th bytes, hence it will differ before the size is hit.
163         return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
164 }
165
166 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
167 {
168          int    identlen = IPC_Type_Datagram_GetSize(Ident);
169         char    tmpbuf[ identlen + Length ];
170         memcpy(tmpbuf, Ident, identlen);        // Header
171         memcpy(tmpbuf + identlen, Data, Length);        // Data
172         // TODO: Handle fragmented packets
173         _SysWrite(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
174 }
175
176 int IPC_Type_Sys_GetSize(const void *Ident)
177 {
178         return sizeof(pid_t);
179 }
180
181 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
182 {
183         return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
184 }
185
186 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
187 {
188         _SysSendMessage( *(const tid_t*)Ident, Length, Data );
189 }
190
191 int IPC_Type_IPCPipe_GetSize(const void *Ident)
192 {
193         return sizeof(int);
194 }
195 int IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2)
196 {
197         return *(const int*)Ident1 - *(const int*)Ident2;
198 }
199 void IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data)
200 {
201          int    fd = *(const int*)Ident;
202         size_t rv = _SysWrite( fd, Data, Length );
203         if(rv != Length) {
204                 _SysDebug("Message send for IPCPipe FD %i %i req, %i sent",
205                         fd, Length, rv);
206         }
207 }

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