2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
6 * - Interprocess communication (acess handlers)
10 #include <acess/sys.h>
15 #define STATICBUF_SIZE 256
16 #define AXWIN_PORT 4101
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);
33 const tIPC_Type gIPC_Type_Datagram = {
34 IPC_Type_Datagram_GetSize,
35 IPC_Type_Datagram_Compare,
36 IPC_Type_Datagram_Send
38 const tIPC_Type gIPC_Type_SysMessage = {
43 const tIPC_Type gIPC_Type_IPCPipe = {
44 IPC_Type_IPCPipe_GetSize,
45 IPC_Type_IPCPipe_Compare,
48 int giNetworkFileHandle = -1;
49 int giIPCPipeHandle = -1;
56 giNetworkFileHandle = _SysOpen("/Devices/ip/loop/udp", OPENFLAG_READ);
57 if( giNetworkFileHandle != -1 )
60 _SysIOCtl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
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");
69 void _setfd(int fd, int *nfds, fd_set *set)
73 if( fd >= *nfds ) *nfds = fd+1;
78 void IPC_FillSelect(int *nfds, fd_set *set, fd_set *err_set)
80 _setfd(giNetworkFileHandle, nfds, set);
81 _setfd(giIPCPipeHandle, nfds, set);
82 for( int i = 0; i < giIPC_ClientCount; i ++ )
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 );
91 void IPC_HandleSelect(fd_set *set, fd_set *err_set)
93 if( giNetworkFileHandle != -1 && FD_ISSET(giNetworkFileHandle, set) )
95 char staticBuf[STATICBUF_SIZE];
96 int readlen, identlen;
99 readlen = _SysRead(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
101 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
102 msg = staticBuf + identlen;
104 IPC_Handle( IPC_int_GetClient(&gIPC_Type_Datagram, staticBuf), readlen - identlen, (void*)msg);
105 //_SysDebug("IPC_HandleSelect: UDP handled");
108 if( giIPCPipeHandle != -1 && FD_ISSET(giIPCPipeHandle, set) )
110 int newfd = _SysOpenChild(giIPCPipeHandle, "newclient", OPENFLAG_READ|OPENFLAG_WRITE);
111 _SysDebug("newfd = %i", newfd);
112 IPC_int_GetClient(&gIPC_Type_IPCPipe, &newfd);
115 for( int i = 0; i < giIPC_ClientCount; i ++ )
117 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
119 int fd = *(const int*)gIPC_Clients[i]->Ident;
120 if( FD_ISSET(fd, set) )
122 char staticBuf[STATICBUF_SIZE];
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]);
130 IPC_Handle( gIPC_Clients[i], len, (void*)staticBuf );
132 if( FD_ISSET(fd, err_set) )
134 // Client disconnected
135 IPC_int_DropClient(gIPC_Clients[i]);
143 while( (len = _SysGetMessage(&tid, 0, NULL)) )
146 _SysGetMessage(NULL, len, data);
148 IPC_Handle( IPC_int_GetClient(&gIPC_Type_SysMessage, &tid), len, (void*)data );
149 // _SysDebug("IPC_HandleSelect: Message handled");
153 int IPC_Type_Datagram_GetSize(const void *Ident)
155 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
158 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
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));
166 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
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));
176 int IPC_Type_Sys_GetSize(const void *Ident)
178 return sizeof(pid_t);
181 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
183 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
186 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
188 _SysSendMessage( *(const tid_t*)Ident, Length, Data );
191 int IPC_Type_IPCPipe_GetSize(const void *Ident)
195 int IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2)
197 return *(const int*)Ident1 - *(const int*)Ident2;
199 void IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data)
201 int fd = *(const int*)Ident;
202 size_t rv = _SysWrite( fd, Data, Length );
204 _SysDebug("Message send for IPCPipe FD %i %i req, %i sent",