Exemplo n.º 1
0
void *thread_cli(void *arg) {
    _xcc_status cc;
    int         count_read;
    int         ferr;
    short       filenum;
    short       filenum2;
    int         inx;

    arg = arg; // touch

    ferr = file_enable_open_cleanup();
    TEST_CHK_FEOK(ferr);
    ferr = BFILE_OPEN_((char *) sname, 4, &filenum,
                       0, 0, 0, 0, 0, 0, 0, NULL);
    TEST_CHK_FEOK(ferr);
    ferr = BFILE_OPEN_((char *) sname, 4, &filenum2,
                       0, 0, 0, 0, 0, 0, 0, NULL);
    TEST_CHK_FEOK(ferr);
    for (inx = 0; inx < loop; inx++) {
        sprintf(send_buffer, "hello, greetings from %s, inx=%d",
                my_name, inx);
        cc = BWRITEREADX(filenum,
                         send_buffer,
                         (int) (strlen(send_buffer) + 1), // cast
                         BUFSIZ,
                         &count_read,
                         0);
        TEST_CHK_CCEQ(cc);
        printf("%s\n", send_buffer);
    }
    ferr = BFILE_CLOSE_(filenum2);
    TEST_CHK_FEOK(ferr);
    return NULL;
}
void * CNSKListenerSrvr::tcpip_listener(void *arg)
{
   // Parameter is the CNSKListenerSrvr object
   CNSKListenerSrvr *listener = (CNSKListenerSrvr *) arg;

   int numReadyFds;
   int handledFds;
   ssize_t countRead;
   CTCPIPSystemSrvr* pnode=NULL;
   fd_set temp_read_fds, temp_error_fds;

   msg_enable_open_cleanup();
   file_enable_open_cleanup();

   //create a the dummy pipe
   int rc = pipe(listener->pipefd);
   if (rc < 0)
   {
	listener->TRACE_UNKNOWN_INPUT();
	SET_ERROR((long)0, NSK, TCPIP, UNKNOWN_API, E_SERVER,"tcpip_listener", O_PIPE, F_INIT_PIPE,SRVR_ERR_UNKNOWN_REQUEST,0);
        listener->TCP_TRACE_OUTPUT_R0();
   }
   FD_SET(listener->pipefd[0],&listener->read_fds_);
   FD_SET(listener->pipefd[0],&listener->error_fds_);
   if (listener->pipefd[0] > listener->max_read_fd_)
           listener->max_read_fd_ = listener->pipefd[0];

   // Persistently wait for input on sockets and then act on it.
   while(listener->m_bTCPThreadKeepRunning)
   {
      // Wait for ready-to-read on any of the tcpip ports
      memcpy(&temp_read_fds, &listener->read_fds_, sizeof(temp_read_fds));
      memcpy(&temp_error_fds, &listener->error_fds_, sizeof(temp_error_fds));

      numReadyFds = select(listener->max_read_fd_+1, &temp_read_fds, NULL,&temp_error_fds,NULL);

      srvrGlobal->mutex->lock();

      if (numReadyFds == -1)
      {
//LCOV_EXCL_START
         /*
		  *  Unexpected error from select - fdset cannot be relied on in this case
          */
		  srvrGlobal->mutex->unlock();
		  continue;
//LCOV_EXCL_STOP

      }
      else
      {
         if (numReadyFds > 0)
         {
            // Handle all ready-to-read file descriptors
            handledFds = 0;

	    if(FD_ISSET(listener->pipefd[0], &temp_read_fds))
	    {
		//dummy write, exit the loop
		listener->m_bTCPThreadKeepRunning = false;
		srvrGlobal->mutex->unlock();
		break;
	    }
	    else if (FD_ISSET(listener->m_nListenSocketFnum,&temp_read_fds))
	    {
                 // Initiate a new client session

               listener->OpenTCPIPSession();
               listener->TRACE_INPUT((short)listener->m_nListenSocketFnum, 0, 0, 0);
			   handledFds++;

            }
            else if ((pnode=GTransport.m_TCPIPSystemSrvr_list->m_current_node) != NULL && FD_ISSET(pnode->m_nSocketFnum,&temp_read_fds))
            {

		   short retries = 0;
		   do
		   {
	               countRead = recv(pnode->m_nSocketFnum,
	                                pnode->m_IObuffer,
                                MAX_TCP_BUFFER_LENGTH, 0);
		   } while ((countRead < 0) && (errno == EINTR) && (retries++ < 3));

               if (countRead <= 0)
	       {

                  GTransport.m_TCPIPSystemSrvr_list->del_node(pnode->m_nSocketFnum);
				  SRVR::BreakDialogue(NULL);
	        }
               else
		{
                    pnode->m_rlength = countRead;
                    if (listener->CheckTCPIPRequest(pnode) == NULL)
		    {
			SRVR::BreakDialogue(NULL);
		     }
	   	}

		handledFds++;
	   }
            else
            {
               listener->TRACE_UNKNOWN_INPUT();
               SET_ERROR((long)0, NSK, TCPIP, UNKNOWN_API, E_SERVER,"tcpip_listener", O_SELECT, F_FD_ISSET,SRVR_ERR_UNKNOWN_REQUEST, -2);
               listener->TCP_TRACE_OUTPUT_R0();
			   handledFds++;
            }

	   if(handledFds != numReadyFds)
	   {
               listener->TRACE_UNKNOWN_INPUT();
               SET_ERROR((long)0, NSK, TCPIP, UNKNOWN_API, E_SERVER,"tcpip_listener", O_SELECT, F_FD_ISSET,SRVR_ERR_UNKNOWN_REQUEST,0);
               listener->TCP_TRACE_OUTPUT_R0();
            }

	} // numReadyFds > 0
      } // else of if numReadFds == -1

      srvrGlobal->mutex->unlock();

   } //while(listener->m_bTCPThreadKeepRunning)

   return NULL;
}