home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / feselect.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.9 KB  |  190 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "stdafx.h"
  20.  
  21. #include "feselect.h"
  22. #include "timer.h"
  23. #include "ssl.h"
  24. #include "hiddenfr.h"
  25.  
  26. //  We have one and only one select tracker.
  27. CSelectTracker selecttracker;
  28.  
  29.  
  30. #define MAX_SIMULTANIOUS_SOCKETS 100
  31. /* should never have more than MAX sockets */
  32. PRPollDesc poll_desc_array[MAX_SIMULTANIOUS_SOCKETS];
  33. unsigned int fd_set_size = 0;               
  34.  
  35. //  Have any entries in select list?
  36. BOOL CSelectTracker::HasSelect(SelectType stType)
  37. {
  38.     return fd_set_size;
  39. }
  40.  
  41. //  Number of select entries in list.
  42. int CSelectTracker::CountSelect(SelectType stType)
  43. {
  44.     return(fd_set_size);
  45. }
  46.  
  47. //  Add a select entry, no duplicates.
  48. void CSelectTracker::AddSelect(SelectType stType, PRFileDesc *prFD)
  49. {
  50.     unsigned int index = fd_set_size;
  51.     unsigned int count;
  52.  
  53. #define CONNECT_FLAGS PR_POLL_READ | PR_POLL_EXCEPT | PR_POLL_WRITE
  54. #define READ_FLAGS    PR_POLL_READ | PR_POLL_EXCEPT
  55.  
  56.     //  Go through the list, make sure that there is not already an entry for fd.
  57.     for(count=0; count < fd_set_size; count++)
  58.     {
  59.         if(poll_desc_array[count].fd == prFD)
  60.         {
  61.             // found it
  62.             // verify that it has the same flags that we wan't, 
  63.             // otherwise add it again with different flags
  64.             if((stType == ConnectSelect && poll_desc_array[count].in_flags == CONNECT_FLAGS)
  65.                || (stType == SocketSelect && poll_desc_array[count].in_flags == READ_FLAGS))
  66.             {
  67.                 index = count;
  68.                 break;
  69.             }
  70.         }
  71.     }
  72.  
  73.     // need to add a new one if we didnt' find the index
  74.     if(index == fd_set_size)
  75.     {
  76.         poll_desc_array[fd_set_size].fd = prFD;
  77.         fd_set_size++;
  78.     }
  79.     
  80.     if(stType == ConnectSelect)
  81.         poll_desc_array[index].in_flags = CONNECT_FLAGS;
  82.     else if(stType == SocketSelect)
  83.         poll_desc_array[index].in_flags = READ_FLAGS;
  84.     else
  85.         XP_ASSERT(0);
  86. }
  87.  
  88. //  Remove a select if it exists.
  89. void CSelectTracker::RemoveSelect(SelectType stType, PRFileDesc *prFD)
  90. {
  91.     unsigned int count;
  92.  
  93.     //  Go through the list
  94.     for(count=0; count < fd_set_size; count++)
  95.     {
  96.         if(poll_desc_array[count].fd == prFD)
  97.         {
  98.             
  99.             if((stType == ConnectSelect && poll_desc_array[count].in_flags == CONNECT_FLAGS)
  100.                || (stType == SocketSelect && poll_desc_array[count].in_flags == READ_FLAGS))
  101.             {
  102.                 // found it collapse the list
  103.  
  104.                 fd_set_size--;
  105.                 if(count < fd_set_size)
  106.                     XP_MEMCPY(&poll_desc_array[count], &poll_desc_array[count+1], (fd_set_size - count) * sizeof(PRPollDesc));
  107.  
  108.                 return;
  109.             }
  110.         }
  111.     }
  112.  
  113.     // didn't find it.  opps
  114. }
  115.  
  116. BOOL CSelectTracker::HasSelect(SelectType stType, PRFileDesc *prFD)
  117. {
  118.     unsigned int count;
  119.  
  120.     //  Go through the list
  121.     for(count=0; count < fd_set_size; count++)
  122.     {
  123.         if(poll_desc_array[count].fd == prFD)
  124.         {
  125.             
  126.             if((stType == ConnectSelect && poll_desc_array[count].in_flags == CONNECT_FLAGS)
  127.                || (stType == SocketSelect && poll_desc_array[count].in_flags == READ_FLAGS))
  128.                return TRUE;
  129.         }
  130.     }
  131.     return FALSE;
  132. }
  133.  
  134. extern "C"
  135. void FE_AbortDNSLookup(PRFileDesc *fd)
  136. {
  137.  
  138. }
  139.  
  140. extern "C"
  141. void FE_SetReadPoll(PRFileDesc *fd) 
  142. {
  143.     selecttracker.AddSelect(SocketSelect, fd);
  144. }
  145.  
  146. extern "C"
  147. void FE_ClearReadPoll(PRFileDesc *fd) 
  148. {
  149.     selecttracker.RemoveSelect(SocketSelect, fd);
  150. }
  151.  
  152. extern "C"
  153. void FE_SetConnectPoll(PRFileDesc *fd) 
  154. {
  155.     selecttracker.AddSelect(ConnectSelect, fd);
  156. }
  157.  
  158. extern "C"
  159. void FE_ClearConnectPoll(PRFileDesc *fd) 
  160. {
  161.     selecttracker.RemoveSelect(ConnectSelect, fd);
  162. }
  163.  
  164. /* call PR_Poll and call Netlib if necessary 
  165.  *
  166.  * return FALSE if nothing to do.
  167.  */
  168. Bool CSelectTracker::PollNetlib(void)
  169. {
  170.     static PRIntervalTime interval = PR_MillisecondsToInterval(1); 
  171.  
  172.     if(fd_set_size < 1)
  173.         return FALSE;
  174.  
  175.     register int status = PR_Poll(poll_desc_array, fd_set_size, interval);
  176.  
  177.     if(status < 1)
  178.         return TRUE; // potential for doing stuff in the future.
  179.  
  180.     // for now call on all active sockets.
  181.     // if this is too much call only one, but reorder the list each time.
  182.     for(register unsigned int count=0; count < fd_set_size; count++)
  183.     {
  184.         if(poll_desc_array[count].out_flags & (PR_POLL_READ | PR_POLL_WRITE | PR_POLL_EXCEPT))
  185.             NET_ProcessNet(poll_desc_array[count].fd, NET_SOCKET_FD);
  186.     }
  187.  
  188.     return TRUE;
  189. }
  190.