MythSocket::~MythSocket() { LOG(VB_SOCKET, LOG_INFO, LOC + QString("MythSocket dtor : cb 0x%2") .arg((intptr_t)(m_callback),0,16)); if (IsConnected()) DisconnectFromHost(); if (!m_useSharedThread) { m_thread->quit(); m_thread->wait(); delete m_thread; } else { QMutexLocker locker(&s_thread_lock); s_thread_cnt--; if (0 == s_thread_cnt) { s_thread->quit(); s_thread->wait(); delete s_thread; s_thread = NULL; } } m_thread = NULL; delete m_tcpSocket; m_tcpSocket = NULL; }
/* Function: rt_PktServerWork ================================================== * Abstract: * If not connected, establish communication of the packet line and the * data upload line. If connected, send/receive packets and parameters * on the packet line. */ PUBLIC void rt_PktServerWork( RTWExtModeInfo *ei, int_T numSampTimes, boolean_T *stopReq) { PktHeader pktHdr; boolean_T hdrAvail; boolean_T error = EXT_NO_ERROR; boolean_T disconnectOnError = FALSE; pktServerArgT *pktServerArg; /* * If not connected, attempt to make connection to host. */ if (!connected) { rtExtModeTestingKillIfOrphaned(FALSE); error = ExtOpenConnection(extUD,&connected); if (error != EXT_NO_ERROR) goto EXIT_POINT; } /* * If ExtOpenConnection is not blocking and there are no pending * requests to open a connection, we'll still be unconnected. */ if (!connected) goto EXIT_POINT; /* nothing to do */ /* * Process packets. */ /* Wait for a packet. */ error = GetPktHdr(&pktHdr, &hdrAvail); if (error != EXT_NO_ERROR) { fprintf(stderr, "\nError occurred getting packet header.\n"); disconnectOnError = TRUE; goto EXIT_POINT; } rtExtModeTestingKillIfOrphaned(hdrAvail); if (!hdrAvail) goto EXIT_POINT; /* nothing to do */ /* * This is the first packet. Should contain the string: * 'ext-mode'. Its contents are not important to us. * It is used as a flag to start the handshaking process. */ if (!commInitialized) { pktHdr.type = EXT_CONNECT; } /* * At this point we know that we have a packet: process it. */ #ifdef QNX_OS //taskSafe(); #endif switch(pktHdr.type) { case EXT_GET_TIME: { /* Skip verbosity print out - we get too many of these */ /*PRINT_VERBOSE(("got EXT_GET_TIME packet.\n"));*/ time_T t = rteiGetT(ei); error = SendPktToHost( EXT_GET_TIME_RESPONSE,sizeof(time_T), (char_T *)&t); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_ARM_TRIGGER: { error = ProcessArmTriggerPkt(pktHdr.size,numSampTimes); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_SELECT_SIGNALS: { error = ProcessSelectSignalsPkt(ei,numSampTimes,pktHdr.size); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_SELECT_TRIGGER: { error = ProcessSelectTriggerPkt(ei,pktHdr.size); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_CONNECT: { PRINT_VERBOSE(("got EXT_CONNECT packet.\n")); error = ProcessConnectPkt(ei); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_SETPARAM: { PRINT_VERBOSE(("got EXT_SETPARAM packet.\n")); error = ProcessSetParamPkt(ei, pktHdr.size); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_GETPARAMS: { PRINT_VERBOSE(("got EXT_GETPARAMS packet.\n")); error = ProcessGetParamsPkt(ei); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_DISCONNECT_REQUEST: { PRINT_VERBOSE(("got EXT_DISCONNECT_REQUEST packet.\n")); /* * Note that from the target's point of view this is * more a "notify" than a "request". The host needs to * have this acknowledged before it can begin closing * the connection. */ error = SendPktToHost(EXT_DISCONNECT_REQUEST_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; DisconnectFromHost(numSampTimes); break; } case EXT_DISCONNECT_REQUEST_NO_FINAL_UPLOAD: { PRINT_VERBOSE(("got EXT_DISCONNECT_REQUEST_NO_FINAL_UPLOAD packet.\n")); /* * The target receives this packet when the host is * immediately terminating the extmode communication due * to some error. The target should not send back a * response or a final upload of data because the host is * expecting neither. The target must be disconnected and * returned to a state where it is running and can be * re-connected to by the host. */ ForceDisconnectFromHost(numSampTimes); break; } case EXT_MODEL_START: PRINT_VERBOSE(("got EXT_MODEL_START packet.\n")); #ifdef QNX_OS { extern sem_t* startStopSem; sem_post(startStopSem); } #endif startModel = TRUE; error = SendPktToHost(EXT_MODEL_START_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_MODEL_STOP: PRINT_VERBOSE(("got EXT_MODEL_STOP packet.\n")); *stopReq = TRUE; break; case EXT_MODEL_PAUSE: PRINT_VERBOSE(("got EXT_MODEL_PAUSE packet.\n")); modelStatus = TARGET_STATUS_PAUSED; startModel = FALSE; error = SendPktToHost(EXT_MODEL_PAUSE_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_MODEL_STEP: PRINT_VERBOSE(("got EXT_MODEL_STEP packet.\n")); if ((modelStatus == TARGET_STATUS_PAUSED) && !startModel) { startModel = TRUE; } error = SendPktToHost(EXT_MODEL_STEP_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_MODEL_CONTINUE: PRINT_VERBOSE(("got EXT_MODEL_CONTINUE packet.\n")); if (modelStatus == TARGET_STATUS_PAUSED) { modelStatus = TARGET_STATUS_RUNNING; startModel = FALSE; } error = SendPktToHost(EXT_MODEL_CONTINUE_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_CANCEL_LOGGING: { error = ProcessCancelLoggingPkt(pktHdr.size); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } default: fprintf(stderr,"received invalid packet.\n"); break; } /* end switch */ EXIT_POINT: if (error != EXT_NO_ERROR) { if (disconnectOnError) { fprintf(stderr, "Error occurred in rt_PktServerWork.\n" "Disconnecting from host!\n"); /* An error in this function which causes disconnectOnError to be * set to true is caused by a physical failure in the external mode * connection. We assume this failure caused the host to disconnect. * The target must be disconnected and returned to a state * where it is running and can be re-connected to by the host. */ ForceDisconnectFromHost(numSampTimes); } } #ifdef VXWORKS //taskUnsafe(); #endif } /* end rt_PktServerWork */
/* Function: rt_MsgServerWork ================================================== * Abstract: * If not connected, establish communication of the message line and the * data upload line. If connected, send/receive messages and parameters * on the message line. */ PUBLIC boolean_T rt_MsgServerWork(SimStruct *S) { MsgHeader msgHdr; boolean_T hdrAvail; boolean_T error = EXT_NO_ERROR; boolean_T disconnectOnError = FALSE; /* * If not connected, attempt to make connection to host. */ if (!connected) { error = ExtOpenConnection(extUD,&connected); if (error != EXT_NO_ERROR) goto EXIT_POINT; } /* * If ExtOpenConnection is not blocking and there are no pending * requests to open a connection, we'll still be unconnected. */ if (!connected) goto EXIT_POINT; /* nothing do do */ /* * Process messages. */ /* Wait for a message. */ error = GetMsgHdr(&msgHdr, &hdrAvail); if (error != EXT_NO_ERROR) { printf("\nError occured getting message header.\n"); disconnectOnError = TRUE; goto EXIT_POINT; } if (!hdrAvail) goto EXIT_POINT; /* nothing to do */ /* * This is the first message. Should contain the string: * 'ext-mode'. Its contents are not important to us. * It is used as a flag to start the handshaking process. */ if (!commInitialized) { msgHdr.type = EXT_CONNECT; } /* * At this point we know that we have a message: process it. */ switch(msgHdr.type) { case EXT_GET_TIME: { time_T t = ssGetT(S); /* Skip verbosity print out - we get too many of these */ /*PRINT_VERBOSE(("got EXT_GET_TIME message.\n"));*/ error = SendMsgToHost( EXT_GET_TIME_RESPONSE,sizeof(time_T),(char_T *)&t); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_ARM_TRIGGER: { PRINT_VERBOSE(("got EXT_ARM_TRIGGER message.\n")); UploadArmTrigger(); break; } case EXT_SELECT_SIGNALS: { const char *msg; PRINT_VERBOSE(("got EXT_SELECT_SIGNALS message.\n")); msg = GetMsg(msgHdr.size); if (msg == NULL) { error = EXT_ERROR; goto EXIT_POINT; } error = UploadLogInfoInit(S, msg); if (error != NO_ERR) { printf( "\nError in UploadLogInfoInit(). Most likely a memory\n" "allocation error or an attempt to re-initialize the\n" "signal selection during the data logging process\n" "(i.e., multiple EXT_SELECT_SIGNAL messages were received\n" "before the logging session terminated or an\n" "EXT_CANCEL_LOGGING message was received)"); goto EXIT_POINT; } break; } case EXT_SELECT_TRIGGER: { const char *msg; PRINT_VERBOSE(("got EXT_SELECT_TRIGGER message.\n")); msg = GetMsg(msgHdr.size); if (msg == NULL) { error = EXT_ERROR; goto EXIT_POINT; } error = UploadInitTrigger(S, msg); if (error != EXT_NO_ERROR) { printf("\nError in UploadInitTrigger\n"); goto EXIT_POINT; } break; } case EXT_CONNECT: { PRINT_VERBOSE(("got EXT_CONNECT message.\n")); error = ProcessConnectMsg(S); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_SETPARAM: { PRINT_VERBOSE(("got EXT_SETPARAM message.\n")); error = ProcessSetParamMsg(S, msgHdr.size); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_GETPARAMS: { PRINT_VERBOSE(("got EXT_GETPARAMS message.\n")); error = ProcessGetParamsMsg(S); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; } case EXT_DISCONNECT_REQUEST: { PRINT_VERBOSE(("got EXT_DISCONNECT_REQUEST message.\n")); /* * Note that from the target's point of view this is * more a "notify" than a "request". The host needs to * have this acknowledged before it can begin closing * the connection. */ error = SendMsgToHost(EXT_DISCONNECT_REQUEST_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; DisconnectFromHost(S); break; } case EXT_MODEL_START: PRINT_VERBOSE(("got EXT_MODEL_START message.\n")); #ifdef VXWORKS { extern SEM_ID startStopSem; semGive(startStopSem); } #endif startModel = TRUE; error = SendMsgToHost(EXT_MODEL_START_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_MODEL_STOP: PRINT_VERBOSE(("got EXT_MODEL_STOP message.\n")); ssSetStopRequested(S, TRUE); break; case EXT_MODEL_PAUSE: PRINT_VERBOSE(("got EXT_MODEL_PAUSE message.\n")); modelStatus = TARGET_STATUS_PAUSED; startModel = FALSE; error = SendMsgToHost(EXT_MODEL_PAUSE_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_MODEL_STEP: PRINT_VERBOSE(("got EXT_MODEL_STEP message.\n")); if ((modelStatus == TARGET_STATUS_PAUSED) && !startModel) { startModel = TRUE; } error = SendMsgToHost(EXT_MODEL_STEP_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_MODEL_CONTINUE: PRINT_VERBOSE(("got EXT_MODEL_CONTINUE message.\n")); if (modelStatus == TARGET_STATUS_PAUSED) { modelStatus = TARGET_STATUS_RUNNING; startModel = FALSE; } error = SendMsgToHost(EXT_MODEL_CONTINUE_RESPONSE, 0, NULL); if (error != EXT_NO_ERROR) goto EXIT_POINT; break; case EXT_CANCEL_LOGGING: PRINT_VERBOSE(("got EXT_CANCEL_LOGGING message.\n")); UploadCancelLogging(); break; default: fprintf(stderr,"received invalid message.\n"); break; } /* end switch */ EXIT_POINT: if (error != EXT_NO_ERROR) { if (disconnectOnError) { fprintf(stderr, "Error occured in rt_MsgServerWork.\n" "Disconnecting from host!\n"); DisconnectFromHost(S); /* * Patch by Gopal Santhanam 5/25/2002 (for VXWORKS) * If there there was a problem and we have already disconnected * from the host, there is no point in returning that error * back to rt_MsgServer. That would cause the task servicing * external messages to quit. Once disconnected, we could * just as easily resume by waiting for a new connection. */ #ifdef VXWORKS error = EXT_NO_ERROR; #endif } } return(error); } /* end rt_MsgServerWork */