X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Faxwin3_src%2Flibaxwin3.so_src%2Fmsg.c;h=a9a79a30a72170d88a357820695a815aee4e44f3;hb=edb8fa42cff3758a25e985f4fdd7119b6206ce0a;hp=2b501672ac50a87cfbce426887d0b9c63475809d;hpb=11dbd684e9a3d907d43d71a3145205f1a86992fb;p=tpg%2Facess2.git diff --git a/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c b/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c index 2b501672..a9a79a30 100644 --- a/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c +++ b/Usermode/Applications/axwin3_src/libaxwin3.so_src/msg.c @@ -33,9 +33,8 @@ tAxWin3_MessageCallback gAxWin3_MessageCallback; // === CODE === void AxWin3_Connect(const char *ServerDesc) { - _SysDebug("ServerDesc = %s", ServerDesc); - if( !ServerDesc ) - { + _SysDebug("ServerDesc (argument) = %s", ServerDesc); + if( !ServerDesc ) { ServerDesc = gsAxWin3_int_ServerDesc; } _SysDebug("ServerDesc = %s", ServerDesc); @@ -50,6 +49,10 @@ void AxWin3_Connect(const char *ServerDesc) case '6': case '7': case '8': case '9': case '0': giConnectionType = CONNTYPE_SENDMESSAGE; giConnectionNum = atoi(ServerDesc); + if( giConnectionNum == 0 ) { + _SysDebug("Invalid server PID"); + exit(-1); + } break; case 'u': while(*ServerDesc && *ServerDesc != ':') ServerDesc ++; @@ -97,75 +100,61 @@ void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg) switch(giConnectionType) { case CONNTYPE_SENDMESSAGE: - SysSendMessage(giConnectionNum, size, Msg); + _SysSendMessage(giConnectionNum, size, Msg); break; case CONNTYPE_UDP: { // Create UDP header char tmpbuf[giAxWin3_int_UDPHeaderLen + size]; memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen); memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size); - write(giConnectionNum, tmpbuf, sizeof(tmpbuf)); + _SysWrite(giConnectionNum, tmpbuf, sizeof(tmpbuf)); } break; case CONNTYPE_TCP: - write(giConnectionNum, Msg, size); + _SysWrite(giConnectionNum, Msg, size); break; default: break; } } -tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void) +tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(int nFD, fd_set *fds) { int len; tAxWin_IPCMessage *ret = NULL; - switch(giConnectionType) + int tid; + + _SysSelect(nFD, fds, NULL, NULL, NULL, THREAD_EVENT_IPCMSG); + + // Clear out IPC messages + while( (len = _SysGetMessage(&tid, 0, NULL)) ) { - case CONNTYPE_SENDMESSAGE: - for( ;; ) + if( giConnectionType != CONNTYPE_SENDMESSAGE || tid != giConnectionNum ) { - pid_t tid; + _SysDebug("%i byte message from %i", len, tid); + // If not, pass the buck (or ignore) + if( gAxWin3_MessageCallback ) + gAxWin3_MessageCallback(tid, len); + else + _SysGetMessage(NULL, 0, GETMSG_IGNORE); + continue ; + } - // Wait for a message to arrive - while( !(len = SysGetMessage(&tid, NULL)) ) - { - _SysWaitEvent(THREAD_EVENT_IPCMSG); - } - - // Check if the message came from the server - if(tid != giConnectionNum) - { - _SysDebug("%i byte message from %i", len, tid); - // If not, pass the buck (or ignore) - if( gAxWin3_MessageCallback ) - gAxWin3_MessageCallback(tid, len); - else - SysGetMessage(NULL, GETMSG_IGNORE); - continue ; - } - - // If it's from the server, allocate a buffer and return it - ret = malloc(len); - if(ret == NULL) { - _SysDebug("malloc() failed, ignoring message"); - SysGetMessage(NULL, GETMSG_IGNORE); - return NULL; - } - SysGetMessage(NULL, ret); - break; + // Using CONNTYPE_SENDMESSAGE and server message has arrived + ret = malloc(len); + if(ret == NULL) { + _SysDebug("malloc() failed, ignoring message"); + _SysGetMessage(NULL, 0, GETMSG_IGNORE); + return NULL; } - break; - default: - // TODO: Implement - _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP"); + _SysGetMessage(NULL, len, ret); break; } - // No message? - if( ret == NULL ) - return NULL; - - // TODO: Sanity checks, so a stupid server can't crash us + if( giConnectionType == CONNTYPE_TCP || giConnectionType == CONNTYPE_UDP ) + { + // Check fds + } return ret; } @@ -175,10 +164,9 @@ tAxWin_IPCMessage *AxWin3_int_WaitIPCMessage(int WantedID) tAxWin_IPCMessage *msg; for(;;) { - msg = AxWin3_int_GetIPCMessage(); + msg = AxWin3_int_GetIPCMessage(0, NULL); if(msg->ID == WantedID) return msg; AxWin3_int_HandleMessage( msg ); - free(msg); } }