00001 /******************************************************************************* 00002 00003 @file SocketConduit.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, March 2004 00034 Jan 1st 2005 - Added RedShodan patch for timeout query 00035 00036 @author Kris 00037 00038 *******************************************************************************/ 00039 00040 module mango.io.SocketConduit; 00041 00042 public import mango.io.Socket; 00043 00044 private import mango.io.Buffer; 00045 00046 /******************************************************************************* 00047 00048 A wrapper around the bare Socket to implement the IConduit abstraction 00049 and add socket-specific functionality. SocketConduit data-transfer is 00050 typically performed in conjunction with an IBuffer instance, but can 00051 be handled directly using raw arrays if preferred. See FileConduit for 00052 examples of both approaches. 00053 00054 *******************************************************************************/ 00055 00056 class SocketConduit : Socket, ISocketReader 00057 { 00058 // expose Conduit read() 00059 alias Socket.read read; 00060 00061 private bool textual; 00062 00063 // freelist support 00064 private SocketConduit next; 00065 private bool fromList; 00066 private static SocketConduit freelist; 00067 00068 /*********************************************************************** 00069 00070 Create a streaming Internet Socket. Use the next ctor instead. 00071 00072 ***********************************************************************/ 00073 00074 deprecated this () 00075 { 00076 this (true); 00077 } 00078 00079 /*********************************************************************** 00080 00081 Create a streaming Internet Socket 00082 00083 ***********************************************************************/ 00084 00085 this (bool textual) 00086 { 00087 this.textual = textual; 00088 super (AddressFamily.INET, Type.STREAM, Protocol.IP); 00089 } 00090 00091 /*********************************************************************** 00092 00093 Override closure() to deallocate this SocketConduit 00094 when it has been closed. Note that one should *not* 00095 delete a SocketConduit when FreeList is enabled ... 00096 00097 ***********************************************************************/ 00098 00099 override void close () 00100 { 00101 // be a nice client, and tell the server? 00102 //super.shutdown(); 00103 00104 // do this first cos' we're gonna' reset the 00105 // socket handle during deallocate() 00106 super.close (); 00107 00108 00109 // deallocate if this came from the free-list, 00110 // otherwise just wait for the GC to handle it 00111 if (fromList) 00112 deallocate (this); 00113 } 00114 00115 /*********************************************************************** 00116 00117 Read from conduit into a target buffer. Note that this 00118 uses SocketSet to handle timeouts, such that the socket 00119 does not stall forever. 00120 00121 (for the ISocketReader interface) 00122 00123 ***********************************************************************/ 00124 00125 uint read (IBuffer target) 00126 { 00127 return target.fill (this); 00128 } 00129 00130 /*********************************************************************** 00131 00132 Returns true if this conduit is text-based 00133 00134 ***********************************************************************/ 00135 00136 bool isTextual () 00137 { 00138 return textual; 00139 } 00140 00141 /*********************************************************************** 00142 00143 Construct this SocketConduit with the given socket handle; 00144 this is for FreeList and ServerSocket support. 00145 00146 ***********************************************************************/ 00147 00148 static SocketConduit create (socket_t handle) 00149 { 00150 // allocate one from the free-list 00151 return allocate (handle); 00152 } 00153 00154 /*********************************************************************** 00155 00156 Construct this SocketConduit with the given socket handle; 00157 this is for FreeList and ServerSocket support. 00158 00159 ***********************************************************************/ 00160 00161 private this (socket_t handle) 00162 { 00163 super (handle); 00164 } 00165 00166 /*********************************************************************** 00167 00168 Allocate a SocketConduit from a list rather than 00169 creating a new one 00170 00171 ***********************************************************************/ 00172 00173 private static synchronized SocketConduit allocate (socket_t sock) 00174 { 00175 SocketConduit s; 00176 00177 if (freelist) 00178 { 00179 s = freelist; 00180 freelist = s.next; 00181 s.set (sock); 00182 } 00183 else 00184 { 00185 s = new SocketConduit (sock); 00186 s.fromList = true; 00187 } 00188 return s; 00189 } 00190 00191 /*********************************************************************** 00192 00193 Return this SocketConduit to the free-list 00194 00195 ***********************************************************************/ 00196 00197 private static synchronized void deallocate (SocketConduit s) 00198 { 00199 // socket handle is no longer valid 00200 s.reset (); 00201 s.next = freelist; 00202 freelist = s; 00203 } 00204 } 00205 00206 00207