Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3670

Show
Ignore:
Timestamp:
06/23/08 03:25:02 (3 months ago)
Author:
kris
Message:

added a convenient DataOutputPool?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/net/SocketPool.d

    r3665 r3670  
    2020private import  tango.net.SocketConduit, 
    2121                tango.net.InternetAddress; 
     22 
     23private import  tango.io.stream.DataStream; 
    2224 
    2325/******************************************************************************* 
     
    6062                this.factory = factory; 
    6163                this.address = address; 
    62         } 
    63  
    64         /*********************************************************************** 
    65  
    66                 Allocate a Connection from a list rather than creating a  
    67                 new one. Reap old entries as we go. 
    68  
    69         ***********************************************************************/ 
    70  
    71         final synchronized Connection borrow (Time time) 
    72         {   
    73                 if (freelist) 
    74                     do { 
    75                        auto c = freelist; 
    76  
    77                        freelist = c.next; 
    78                        if (freelist && (time - c.time > timeout)) 
    79                            c.close; 
    80                        else 
    81                           return c; 
    82                        } while (true); 
    83  
    84                 return new Connection (this); 
    8564        } 
    8665 
     
    140119                for (int attempts=3; attempts--;) 
    141120                     try { 
    142                          send (connection.bound);  
     121                         if (send) 
     122                             send (connection.bound);  
    143123 
    144124                         // load the reply. Don't retry on 
     
    177157        } 
    178158 
     159        /*********************************************************************** 
     160 
     161                Allocate a Connection from a list rather than creating a  
     162                new one. Reap old entries as we go. 
     163 
     164        ***********************************************************************/ 
     165 
     166        private final synchronized Connection borrow (Time time) 
     167        {   
     168                if (freelist) 
     169                    do { 
     170                       auto c = freelist; 
     171 
     172                       freelist = c.next; 
     173                       if (freelist && (time - c.time > timeout)) 
     174                           c.close; 
     175                       else 
     176                          return c; 
     177                       } while (true); 
     178 
     179                return new Connection (this); 
     180        } 
     181 
    179182 
    180183        /*********************************************************************** 
     
    185188        ***********************************************************************/ 
    186189 
    187         static class Connection 
     190        private static class Connection 
    188191        { 
    189192                Connection      next;    
     
    266269                                     } 
    267270                } 
     271        } 
     272} 
     273 
     274 
     275/******************************************************************************* 
     276         
     277        A pool of connected DataOuptut instances, each of which are thread- 
     278        safe (each connection has its own private output buffer). 
     279         
     280        Note that the connections will timeout after a period of inactivity,  
     281        and will subsequently cause a connected host to drop the supporting 
     282        session. 
     283 
     284        The connections are expected to be reasonably long-lived on the  
     285        hosting server, since we try to reuse existing sockets 
     286 
     287*******************************************************************************/ 
     288 
     289class DataOutputPool : SocketPool!(DataOutput) 
     290{  
     291        private bool flip; 
     292        private int  bufferSize; 
     293 
     294        /*********************************************************************** 
     295 
     296                Create a connection-pool for the specified address 
     297 
     298        ***********************************************************************/ 
     299 
     300        this (InternetAddress addr, int bufferSize=8192, bool flip=false, bool nagle=true) 
     301        { 
     302                this.flip = flip; 
     303                this.bufferSize = bufferSize; 
     304                super (addr, &factory, nagle); 
     305        } 
     306 
     307        /*********************************************************************** 
     308 
     309                Wrap the socket with a DataOutput instance 
     310 
     311        ***********************************************************************/ 
     312 
     313        private DataOutput factory (IConduit c) 
     314        {        
     315                return new DataOutput (c, bufferSize, flip); 
    268316        } 
    269317}