76b38c2d7f8c772736999f7c8d0e81b0ca6c6a0c
[tpg/acess2.git] / Usermode / Applications / axwin3_src / libaxwin3.so_src / msg.c
1 /*
2  * AxWin3 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * msg.c
6  * - Message handling / IPC
7  */
8 #include <axwin3/axwin.h>
9 #include <acess/sys.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include "include/ipc.h"
13 #include "include/internal.h"
14
15 // === CONSTANTS ===
16 enum eConnectionType
17 {
18         CONNTYPE_SENDMESSAGE,
19         CONNTYPE_UDP,
20         CONNTYPE_TCP
21 };
22
23 // === GLOBALS ===
24 enum eConnectionType    giConnectionType;
25 int     giConnectionNum;        // FD or PID
26 char    gaAxWin3_int_UDPHeader[] = {5,16,0,0};  // Port 4101
27  int    giAxWin3_int_UDPHeaderLen = sizeof(gaAxWin3_int_UDPHeader);
28 const char      *gsAxWin3_int_ServerDesc;
29 tAxWin3_MessageCallback gAxWin3_MessageCallback;
30
31 // === CODE ===
32 void AxWin3_Connect(const char *ServerDesc)
33 {
34         _SysDebug("ServerDesc = %s", ServerDesc);
35         if( !ServerDesc )
36         {
37                 ServerDesc = gsAxWin3_int_ServerDesc;
38         }
39         _SysDebug("ServerDesc = %s", ServerDesc);
40         if( !ServerDesc )
41         {
42                 // TODO: Error out
43                 return ;
44         }
45         switch(ServerDesc[0])
46         {
47         case '1': case '2': case '3': case '4': case '5':
48         case '6': case '7': case '8': case '9': case '0':
49                 giConnectionType = CONNTYPE_SENDMESSAGE;
50                 giConnectionNum = atoi(ServerDesc);
51                 break;
52         case 'u':
53                 while(*ServerDesc && *ServerDesc != ':')        ServerDesc ++;
54                 ServerDesc ++;
55                 // TODO: Open socket and create UDP header
56                 break;
57         case 't':
58                 while(*ServerDesc && *ServerDesc != ':')        ServerDesc ++;
59                 ServerDesc ++;
60                 // TODO: Open socket
61                 break;
62         }
63 }
64
65 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
66 {
67         tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
68         gAxWin3_MessageCallback = Callback;
69         return old;
70 }
71
72 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
73 {
74         tAxWin_IPCMessage       *ret;
75
76         ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
77         ret->Flags = Flags;
78         ret->ID = Message;
79         if(Window)
80                 ret->Window = Window->ServerID;
81         else
82                 ret->Window = -1;
83         ret->Size = ExtraBytes;
84         return ret;
85 }
86
87 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
88 {
89          int    size = sizeof(tAxWin_IPCMessage) + Msg->Size;
90         switch(giConnectionType)
91         {
92         case CONNTYPE_SENDMESSAGE:
93                 SysSendMessage(giConnectionNum, size, Msg);
94                 break;
95         case CONNTYPE_UDP: {
96                 // Create UDP header
97                 char    tmpbuf[giAxWin3_int_UDPHeaderLen + size];
98                 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
99                 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
100                 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
101                 }
102                 break;
103         case CONNTYPE_TCP:
104                 write(giConnectionNum, Msg, size);
105                 break;
106         default:
107                 break;
108         }
109 }
110
111 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
112 {
113          int    len;
114         tAxWin_IPCMessage       *ret = NULL;
115         switch(giConnectionType)
116         {
117         case CONNTYPE_SENDMESSAGE:
118                 // TODO: Less hack, I need a version of select for GetMessage etc
119                 if(SysGetMessage(NULL, NULL) == 0)      sleep();
120                 while(SysGetMessage(NULL, NULL))
121                 {
122                         pid_t   tid;
123                         len = SysGetMessage(&tid, NULL);
124                         // Check if the message came from the server
125                         if(tid != giConnectionNum)
126                         {
127                                 // If not, pass the buck (or ignore)
128                                 if( gAxWin3_MessageCallback )
129                                         gAxWin3_MessageCallback(tid, len);
130                                 else
131                                         SysGetMessage(NULL, GETMSG_IGNORE);
132                                 continue ;
133                         }
134                         
135                         // If it's from the server, allocate a buffer and return it
136                         ret = malloc(len);
137                         if(ret == NULL) return NULL;
138                         SysGetMessage(NULL, ret);
139                         break;
140                 }
141                 break;
142         default:
143                 // TODO: Implement
144                 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
145                 break;
146         }
147
148         // No message?
149         if( ret == NULL )
150                 return NULL;
151
152         // TODO: Sanity checks, so a stupid server can't crash us
153
154         return ret;
155 }
156

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