2 * Acess GUI (AxWin) Version 2
3 * By John Hodge (thePowersGang)
8 #include <axwin2/messages.h>
11 #define AXWIN_PORT 4101
13 #define STATICBUF_SIZE 64
19 void IPC_FillSelect(int *nfds, fd_set *set);
20 void IPC_HandleSelect(fd_set *set);
21 void Messages_RespondDatagram(void *Ident, size_t Length, void *Data);
22 void Messages_RespondIPC(void *Ident, size_t Length, void *Data);
23 void Messages_Handle(size_t IdentLen, void *Ident, size_t MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond);
26 int giNetworkFileHandle = -1;
27 int giMessagesFileHandle = -1;
34 giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
35 tmp = AXWIN_PORT; ioctl(giNetworkFileHandle, 4, &tmp); // TODO: Don't hard-code IOCtl number
38 void IPC_FillSelect(int *nfds, fd_set *set)
40 if( giNetworkFileHandle > *nfds ) *nfds = giNetworkFileHandle;
41 FD_SET(giNetworkFileHandle, set);
44 void IPC_HandleSelect(fd_set *set)
46 if( FD_ISSET(giNetworkFileHandle, set) )
48 char staticBuf[STATICBUF_SIZE];
49 int readlen, identlen;
52 readlen = read(giNetworkFileHandle, sizeof(staticBuf), staticBuf);
54 // Assume that all connections are from localhost
55 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
56 msg = staticBuf + identlen;
58 Messages_Handle(identlen, staticBuf, readlen - identlen, (void*)msg, Messages_RespondDatagram);
61 while(SysGetMessage(NULL, NULL))
64 int len = SysGetMessage(&tid, NULL);
66 SysGetMessage(NULL, data);
68 Messages_Handle(sizeof(tid), &tid, len, (void*)data, Messages_RespondIPC);
72 void Messages_RespondDatagram(void *Ident, size_t Length, void *Data)
74 int addrSize = Net_GetAddressSize( ((uint16_t*)Ident)[1] );
75 char tmpbuf[ 4 + addrSize + Length ];
76 memcpy(tmpbuf, Ident, 4 + addrSize);
77 memcpy(tmpbuf + 4 + addrSize, Data, Length);
78 // TODO: Handle fragmented packets
79 write(giNetworkFileHandle, sizeof(tmpbuf), tmpbuf);
82 void Messages_RespondIPC(void *Ident, size_t Length, void *Data)
84 SysSendMessage( *(tid_t*)Ident, Length, Data );
87 void Messages_Handle(size_t IdentLen, void *Ident, size_t MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond)
89 if( MsgLen < sizeof(tAxWin_Message) )
91 if( MsgLen < sizeof(tAxWin_Message) + Msg->Size )
97 if( MsgLen < sizeof(tAxWin_Message) + 4 ) return;
98 Msg->ID = MSG_SRSP_VERSION;
102 *(uint16_t*)&Msg->Data[2] = -1;
103 Respond(Ident, sizeof(Msg->ID), Msg);
106 case MSG_SREQ_REGISTER:
107 if( Msg->Data[Msg->Size-1] != '\0' ) {
112 AxWin_RegisterClient(IdentLen, Ident, Respond, Msg->Data);
117 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);
118 _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);