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 64
16 #define AXWIN_PORT 4101
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);
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)
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 );
89 void IPC_HandleSelect(fd_set *set)
91 if( giNetworkFileHandle != -1 && FD_ISSET(giNetworkFileHandle, set) )
93 char staticBuf[STATICBUF_SIZE];
94 int readlen, identlen;
97 readlen = _SysRead(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
99 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
100 msg = staticBuf + identlen;
102 IPC_Handle( IPC_int_GetClient(&gIPC_Type_Datagram, staticBuf), readlen - identlen, (void*)msg);
103 //_SysDebug("IPC_HandleSelect: UDP handled");
106 if( giIPCPipeHandle != -1 && FD_ISSET(giIPCPipeHandle, set) )
108 int newfd = _SysOpenChild(giIPCPipeHandle, "newclient", OPENFLAG_READ|OPENFLAG_WRITE);
109 _SysDebug("newfd = %i", newfd);
110 IPC_int_GetClient(&gIPC_Type_IPCPipe, &newfd);
113 for( int i = 0; i < giIPC_ClientCount; i ++ )
115 if( gIPC_Clients[i] && gIPC_Clients[i]->IPCType == &gIPC_Type_IPCPipe )
117 int fd = *(const int*)gIPC_Clients[i]->Ident;
118 if( FD_ISSET(fd, set) )
120 char staticBuf[STATICBUF_SIZE];
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]);
128 IPC_Handle( gIPC_Clients[i], len, (void*)staticBuf );
135 while( (len = _SysGetMessage(&tid, 0, NULL)) )
138 _SysGetMessage(NULL, len, data);
140 IPC_Handle( IPC_int_GetClient(&gIPC_Type_SysMessage, &tid), len, (void*)data );
141 // _SysDebug("IPC_HandleSelect: Message handled");
145 int IPC_Type_Datagram_GetSize(const void *Ident)
147 return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
150 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
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));
158 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
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));
168 int IPC_Type_Sys_GetSize(const void *Ident)
170 return sizeof(pid_t);
173 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
175 return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
178 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
180 _SysSendMessage( *(const tid_t*)Ident, Length, Data );
183 int IPC_Type_IPCPipe_GetSize(const void *Ident)
187 int IPC_Type_IPCPipe_Compare(const void *Ident1, const void *Ident2)
189 return *(const int*)Ident1 - *(const int*)Ident2;
191 void IPC_Type_IPCPipe_Send(const void *Ident, size_t Length, const void *Data)
193 size_t rv = _SysWrite( *(const int*)Ident, Data, Length );
195 _SysDebug("Sent message oversize %x", Length);