[Tinyos-2-commits] CVS: tinyos-2.x/support/sdk/cpp/sf tcpcomm.h, 1.2, 1.3 tcpcomm.cpp, 1.3, 1.4

akoepke andreaskoepke at users.sourceforge.net
Fri Sep 21 07:01:15 PDT 2007


Update of /cvsroot/tinyos/tinyos-2.x/support/sdk/cpp/sf
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv4929

Modified Files:
	tcpcomm.h tcpcomm.cpp 
Log Message:
return from select when a client is added or removed


Index: tcpcomm.h
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/cpp/sf/tcpcomm.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** tcpcomm.h	6 Jul 2007 20:29:12 -0000	1.2
--- tcpcomm.h	21 Sep 2007 14:01:13 -0000	1.3
***************
*** 107,110 ****
--- 107,114 ----
      int serverFD;
  
+     /* pipe fd pair to inform client reader thread of new clients */
+     int pipeWriteFD;
+     int pipeReadFD;
+     
      /* reference to read packet buffer */
      PacketBuffer &readBuffer;    
***************
*** 164,167 ****
--- 168,176 ----
      int reportError(const char *msg, int result);
  
+     /* write something into pipe to wake up client readerThread */
+     void stuffPipe();
+     
+     /* remove data written into pipe */
+     void clearPipe();
  
  public:

Index: tcpcomm.cpp
===================================================================
RCS file: /cvsroot/tinyos/tinyos-2.x/support/sdk/cpp/sf/tcpcomm.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** tcpcomm.cpp	18 Jul 2007 11:12:58 -0000	1.3
--- tcpcomm.cpp	21 Sep 2007 14:01:13 -0000	1.4
***************
*** 75,105 ****
      int opt;
      int rxBuf = 1024;
!     
!     serverFD = reportError("TCPComm::TCPComm : socket(AF_INET, SOCK_STREAM, 0)", socket(AF_INET, SOCK_STREAM, 0));
      memset(&me, 0, sizeof me);
      me.sin_family = AF_INET;
      me.sin_port = htons(port);
! 
      opt = 1;
!     if (!errorReported)
!         reportError("TCPComm::TCPComm : setsockopt(serverFD, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt))", setsockopt(serverFD, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)));
!     if (!errorReported)
!         reportError("TCPComm::TCPComm : setsockopt(serverFD, SOL_SOCKET, SO_RCVBUF, (char *)&rxBuf, sizeof(rxBuf))", setsockopt(serverFD, SOL_SOCKET, SO_RCVBUF, (char *)&rxBuf, sizeof(rxBuf)));
!     if (!errorReported)
!         reportError("TCPComm::TCPComm : bind(serverFD, (struct sockaddr *)&me, sizeof me)", bind(serverFD, (struct sockaddr *)&me, sizeof me));
!     if (!errorReported)
!         reportError("TCPComm::TCPComm : listen(serverFD, 5)", listen(serverFD, 5));
  
      // start thread for server socket (adding and removing clients)
      if (!errorReported)
      {
!         if (reportError("TCPComm::TCPComm : pthread_create( &serverThread, NULL, checkClientsThread, this)", pthread_create( &serverThread, NULL, checkClientsThread, this)) == 0) 
              serverThreadRunning = true;
          // start thread for reading from client connections
!         if (reportError("TCPComm::TCPComm : pthread_create( &readerThread, NULL, readClientsThread, this)", pthread_create( &readerThread, NULL, readClientsThread, this)) == 0)
              readerThreadRunning = true;
          // start thread for writing to client connections
!         if (reportError("TCPComm::TCPComm : pthread_create( &writerThread, NULL, writeClientsThread, this)", pthread_create( &writerThread, NULL, writeClientsThread, this)) == 0)
              writerThreadRunning = true;
      }
  }
--- 75,134 ----
      int opt;
      int rxBuf = 1024;
! 
!     /* create pipe to inform client reader of new clients */
!     if (!errorReported) {
!         int pipeFDPair[2];
!         reportError("TCPComm::TCPComm : pipe(pipeFDPair)", pipe(pipeFDPair));
!         pipeWriteFD = pipeFDPair[1];
!         pipeReadFD = pipeFDPair[0];
!     }
!     if (!errorReported) {
!         reportError("TCPComm::TCPComm : fcntl(pipeReadFD, F_SETFL, O_NONBLOCK);",
!                     fcntl(pipeReadFD, F_SETFL, O_NONBLOCK));
!     }
!     /* create server socket where clients connect */
!     if (!errorReported) {
!         serverFD = reportError("TCPComm::TCPComm : socket(AF_INET, SOCK_STREAM, 0)",
!                                socket(AF_INET, SOCK_STREAM, 0));
!     }
      memset(&me, 0, sizeof me);
      me.sin_family = AF_INET;
      me.sin_port = htons(port);
!     
      opt = 1;
!     if (!errorReported) {
!         reportError("TCPComm::TCPComm : setsockopt(serverFD, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt))",
!                     setsockopt(serverFD, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)));
!     }
!     if (!errorReported) {
!         reportError("TCPComm::TCPComm : setsockopt(serverFD, SOL_SOCKET, SO_RCVBUF, (char *)&rxBuf, sizeof(rxBuf))",
!                     setsockopt(serverFD, SOL_SOCKET, SO_RCVBUF, (char *)&rxBuf, sizeof(rxBuf)));
!     }
!     if (!errorReported) {
!         reportError("TCPComm::TCPComm : bind(serverFD, (struct sockaddr *)&me, sizeof me)",
!                     bind(serverFD, (struct sockaddr *)&me, sizeof me));
!     }
!     if (!errorReported) {
!         reportError("TCPComm::TCPComm : listen(serverFD, 5)",
!                     listen(serverFD, 5));
!     }
  
      // start thread for server socket (adding and removing clients)
      if (!errorReported)
      {
!         if (reportError("TCPComm::TCPComm : pthread_create( &serverThread, NULL, checkClientsThread, this)",
!                         pthread_create( &serverThread, NULL, checkClientsThread, this)) == 0) {
              serverThreadRunning = true;
+         }
          // start thread for reading from client connections
!         if (reportError("TCPComm::TCPComm : pthread_create( &readerThread, NULL, readClientsThread, this)",
!                         pthread_create( &readerThread, NULL, readClientsThread, this)) == 0) {
              readerThreadRunning = true;
+         }
          // start thread for writing to client connections
!         if (reportError("TCPComm::TCPComm : pthread_create( &writerThread, NULL, writeClientsThread, this)",
!                         pthread_create( &writerThread, NULL, writeClientsThread, this)) == 0) {
              writerThreadRunning = true;
+         }
      }
  }
***************
*** 247,250 ****
--- 276,280 ----
      }
      pthread_mutex_unlock( &clientInfo.countlock );
+     stuffPipe();
      DEBUG("TCPComm::addClient : unlock")
  }
***************
*** 273,276 ****
--- 303,307 ----
      }
      pthread_mutex_unlock( &clientInfo.countlock );
+     stuffPipe();
      DEBUG("TCPComm::removeClient : unlock")
  }
***************
*** 333,342 ****
          clientFDs = clientInfo.FDs;
          // removes the cleanup handler and executes it (unlock mutex)
!         pthread_cleanup_pop(1); 
! 
          // check all fds (work with temp set)...
          fd_set rfds;
          FD_ZERO(&rfds);
!         int maxFD = -1;
          set<int>::iterator it;
          for( it = clientFDs.begin(); it != clientFDs.end(); it++ )
--- 364,373 ----
          clientFDs = clientInfo.FDs;
          // removes the cleanup handler and executes it (unlock mutex)
!         pthread_cleanup_pop(1);
          // check all fds (work with temp set)...
          fd_set rfds;
          FD_ZERO(&rfds);
!         int maxFD = pipeReadFD;
!         FD_SET(pipeReadFD, &rfds);
          set<int>::iterator it;
          for( it = clientFDs.begin(); it != clientFDs.end(); it++ )
***************
*** 351,359 ****
          {
              //             run = false;
!             reportError("TCPComm::readClients : select(maxFD+1, &rfds, NULL, NULL NULL)", -1);
          }
          else
          {
!             for ( it = clientFDs.begin(); it != clientFDs.end(); it++)
              {
                  if (FD_ISSET(*it, &rfds))
--- 382,393 ----
          {
              //             run = false;
!             reportError("TCPComm::readClients : select(maxFD+1, &rfds, NULL, NULL, NULL)", -1);
          }
          else
          {
!             if(FD_ISSET(pipeReadFD, &rfds)) {
!                 clearPipe();
!             }
!             for (it = clientFDs.begin(); it != clientFDs.end(); it++)
              {
                  if (FD_ISSET(*it, &rfds))
***************
*** 549,550 ****
--- 583,597 ----
      << " , packets written = " << writtenPacketCount << endl;
  }
+ 
+ void TCPComm::stuffPipe() 
+ {
+     char info = 'n';
+     write(pipeWriteFD, &info, 1);
+ }
+ 
+ void TCPComm::clearPipe() {
+     char buf;
+     while(read(pipeReadFD, &buf, 1) > 0) {
+         ;
+     }
+ }



More information about the Tinyos-2-commits mailing list