HTNet.h
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 10k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /*                                                              Asyncronous Socket Management
  2.                               ASYNCRONOUS SOCKET MANAGEMENT
  3.                                              
  4.  */
  5. /*
  6. **      (c) COPYRIGHT MIT 1995.
  7. **      Please first read the full copyright statement in the file COPYRIGH.
  8. */
  9. /*
  10.    This module contains the routines for handling the set of active sockets currently in
  11.    use by the multithreaded clients. It is an internal module to the Library, the
  12.    application interface is implemented in the Event Module. Look for more information in
  13.    the Multithread Specifications.
  14.    
  15.    This module is implemented by HTNet.c, and it is a part of the W3C Reference Library.
  16.    
  17.  */
  18. #ifndef HTNET_H
  19. #define HTNET_H
  20. #include "HTEvntrg.h"
  21. #include "HTReq.h"
  22. /*
  23. The HTNet Object
  24.    The HTNet object is the core of the request queue management. This object contains
  25.    information about the socket descriptor, the input read buffer etc. required to
  26.    identify and service a request.
  27.    
  28.  */
  29. typedef struct _HTNet HTNet;
  30. /*
  31. Request Call Back Functions
  32.    Callback functions can be registered to be called before and after a request has either
  33.    been started or has terminated. The following functions are the generic registration
  34.    mechanisms where we use lists as the basic data container. Then there is two methods
  35.    for binding a list of callback functions to the set which is called before and to the
  36.    set set which is called after
  37.    
  38.    In both cases there can be more than one callback function which are called on turn and
  39.    each callback function can be associated with a status code of the request. For example
  40.    one callback function can be registered for HT_LOADED, another for HT_ERROR etc.
  41.    
  42.   REGISTER A REQUEST CALLBACK
  43.   
  44.    Register a call back function that is to be called on every termination of a request.
  45.    Several call back functions can be registered in which case all of them are called in
  46.    the reverse order of which they were registered (last one first). We name the calling
  47.    mechansm of calling the functions for the before loop and the after loop.
  48.    
  49.    In case the callback function is registered as being called after the request has
  50.    terminated the result of the request is passed to the fucntion. The status signifies
  51.    which call back function to call depending of the result of the request. This can be
  52.    
  53.   HT_ERROR               An error occured
  54.                          
  55.   HT_LOADED              The document was loaded
  56.                          
  57.   HT_NO_DATA             OK, but no data
  58.                          
  59.   HT_RETRY               Retry request after at a later time
  60.                          
  61.   HT_REDIRECT            The request has been redirected and we send back the new URL
  62.                          
  63.   HT_ALL                 All of above
  64.                          
  65.    Any callback function any code it likes, but IF NOT the code is HT_OK, then the
  66.    callback loop is stopped. If we are in the before loop and a function returns anything
  67.    else than HT_OK then we immediately jump to the after loop passing the last return code
  68.    from the before loop.
  69.    
  70.  */
  71. typedef int HTNetCallback (HTRequest * request, int status);
  72. extern BOOL HTNetCall_add (HTList * list, HTNetCallback *cbf, int status);
  73. /*
  74.   DELETE A SINGLE CALLBAK
  75.   
  76.    Removes a callback function from a list
  77.    
  78.  */
  79. extern BOOL HTNetCall_delete (HTList * list, HTNetCallback *cbf);
  80. /*
  81.   DELETE A LIST OF CALLBACKS
  82.   
  83.    Unregisters all call back functions in the list
  84.    
  85.  */
  86. extern BOOL HTNetCall_deleteAll (HTList * list);
  87. /*
  88.   CALL LIST OF REGISTERED CALLBACK FUNCTIONS
  89.   
  90.    Call all the call back functions registered in the list IF not the status is HT_IGNORE.
  91.     The callback functions are called in the order of which they were registered. At the
  92.    moment an application callback function is called, it can free the request object - it
  93.    is no longer used by the Library. Returns what the last callback function returns
  94.    
  95.  */
  96. extern int HTNetCall_execute (HTList * list, HTRequest * request, int status);
  97. /*
  98.   BEFORE CALLBACKS
  99.   
  100.    Global set of callback functions BEFORE the request is issued. The list can be NULL.
  101.    
  102.  */
  103. extern BOOL HTNetCall_addBefore (HTNetCallback *cbf, int status);
  104. extern BOOL HTNet_setBefore     (HTList * list);
  105. extern HTList * HTNet_before    (void);
  106. extern int HTNet_callBefore     (HTRequest *request, int status);
  107. /*
  108.   AFTER CALLBACKS
  109.   
  110.    Global set of callback functions AFTER the request is issued. The list can be NULL
  111.    
  112.  */
  113. extern BOOL HTNetCall_addAfter  (HTNetCallback *cbf, int status);
  114. extern BOOL HTNet_setAfter      (HTList * list);
  115. extern HTList * HTNet_after     (void);
  116. extern int HTNet_callAfter      (HTRequest *request, int status);
  117. /*
  118. Request Queue
  119.    The request queue ensures that no more than a fixed number of TCP connections are open
  120.    at the same time. If more requests are handed to the Library, they are put into the
  121.    pending queue and initiated when sockets become free.
  122.    
  123.   NUMBER OF SIMULTANOUS OPEN TCP CONNECTIONS
  124.   
  125.    Set the max number of simultanous sockets. The default value is HT_MAX_SOCKETS which is
  126.    6. The number of persistent connections depend on this value as a deadlock can occur if
  127.    all available sockets a persistent (see the DNS Manager for more information on setting
  128.    the number of persistent connections). The number of persistent connections can never
  129.    be more than the max number of sockets-2, so letting newmax=2 prevents persistent
  130.    sockets.
  131.    
  132.  */
  133. extern BOOL HTNet_setMaxSocket (int newmax);
  134. extern int  HTNet_maxSocket (void);
  135. /*
  136.   LIST ACTIVE QUEUE
  137.   
  138.    Returns the list of active requests that are currently having an open connection.
  139.    Returns list of HTNet objects or NULL if error.
  140.    
  141.  */
  142. extern HTList *HTNet_activeQueue (void);
  143. extern BOOL HTNet_idle (void);
  144. /*
  145.   ARE WE ACTIVE?
  146.   
  147.    We have some small functions that tell whether there are registered requests in the Net
  148.    manager. There are tree queues: The active, the pending, and the persistent. The active
  149.    queue is the set of requests that are actively sending or receiving data. The pending
  150.    is the requests that we have registered but which are waiting for a free socket. The
  151.    Persistent queue are requets that are waiting to use the same socket in order to save
  152.    network resoures (if the server understands persistent connections).
  153.    
  154.     Active Reqeusts?
  155.     
  156.    Returns whether there are requests in the active queue or not
  157.    
  158.  */
  159. extern BOOL HTNet_idle (void);
  160. /*
  161.     Registered Requests?
  162.     
  163.    Returns whether there are requests registered in any of the lists or not
  164.    
  165.  */
  166. extern BOOL HTNet_isEmpty (void);
  167. /*
  168.   LIST PENDING QUEUE
  169.   
  170.    Returns the list of pending requests that are waiting to become active. Returns list of
  171.    HTNet objects or NULL if error
  172.    
  173.  */
  174. extern HTList *HTNet_pendingQueue (void);
  175. /*
  176. Create an Object
  177.    You can create a new HTNet object as a new request to be handled. If we have more than
  178.    HTMaxActive connections already then put this into the pending queue, else start the
  179.    request by calling the call back function registered with this access method.  Returns
  180.    YES if OK, else NO
  181.    
  182.  */
  183. extern BOOL HTNet_newClient (HTRequest * request);
  184. /*
  185.    You can create a new HTNet object as a new request to be handled. If we have more than
  186.    HTMaxActive connections already then return NO. Returns YES if OK, else NO
  187.    
  188.  */
  189. extern BOOL HTNet_newServer (HTRequest * request, SOCKET sockfd, char *access);
  190. /*
  191.    And you can create a plain new HTNet object using the following method:
  192.    
  193.  */
  194. extern HTNet * HTNet_new (HTRequest * request, SOCKET sockfd);
  195. /*
  196.   DUPLICATE AN EXISTING OBJECT
  197.   
  198.    Creates a new HTNet object as a duplicate of the same request. Returns YES if OK, else
  199.    NO.
  200.    
  201.  */
  202. extern HTNet * HTNet_dup (HTNet * src);
  203. /*
  204. HTNet Object Methods
  205.   MAKE AN OBJECT WAIT
  206.   
  207.    Let a net object wait for a persistent socket. It will be launched from the
  208.    HTNet_delete() function when the socket gets free.
  209.    
  210.  */
  211. extern BOOL HTNet_wait (HTNet *net);
  212. /*
  213.   PRIORITY MANAGEMENT
  214.   
  215.    Each HTNet object is created with a priority which it inherits from the Request
  216.    manager. However, in some stuations it is useful to be to change the current priority
  217.    after the request has been started. These two functions allow you to do this. The
  218.    effect will show up the first time (which might be imidiately) the socket blocks and
  219.    control returns to the event loop. Also have a look at how you can do this before the
  220.    request is issued in the request manager.
  221.    
  222.  */
  223. extern HTPriority HTNet_priority (HTNet * net);
  224. extern BOOL HTNet_setPriority (HTNet * net, HTPriority priority);
  225. /*
  226.   DELETE AN OBJECT
  227.   
  228.    Deletes the HTNet object from the list of active requests and calls any registered call
  229.    back functions IF not the status is HT_IGNORE. This is used if we have internal
  230.    requests that the app doesn't know about. We also see if we have pending requests that
  231.    can be started up now when we have a socket free. The callback functions are called in
  232.    the reverse order of which they were registered (last one first);
  233.    
  234.  */
  235. extern BOOL HTNet_delete (HTNet * me, int status);
  236. /*
  237.   DELETE ALL HTNET OBJECTS
  238.   
  239.    Deletes all HTNet object that might either be active or pending We DO NOT call the call
  240.    back functions - A crude way of saying goodbye!
  241.    
  242.  */
  243. extern BOOL HTNet_deleteAll (void);
  244. /*
  245.   KILL A REQUEST
  246.   
  247.    Kill the request by calling the call back function with a request for closing the
  248.    connection. Does not remove the object. This is done by HTNet_delete() function which
  249.    is called by the load routine.  Returns OK if success, NO on error
  250.    
  251.  */
  252. extern BOOL HTNet_kill (HTNet * me);
  253. /*
  254.   KILL ALL REQUESTS
  255.   
  256.    Kills all registered (active+pending) requests by calling the call back function with a
  257.    request for closing the connection. We do not remove the HTNet object as it is done by
  258.    HTNet_delete().  Returns OK if success, NO on error
  259.    
  260.  */
  261. extern BOOL HTNet_killAll (void);
  262. /*
  263. Data Access Methods
  264.   SOCKET DESCRIPTOR
  265.   
  266.  */
  267. extern BOOL HTNet_setSocket (HTNet * net, SOCKET sockfd);
  268. extern SOCKET HTNet_socket (HTNet * net);
  269. /*
  270.  */
  271. #endif /* HTNET_H */
  272. /*
  273.    End of declaration module */