00001 /******************************************************************************* 00002 00003 @file ServerSocket.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 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.io.ServerSocket; 00040 00041 public import mango.io.Socket; 00042 00043 private import mango.io.Exception, 00044 mango.io.SocketConduit; 00045 00046 private import mango.io.model.IBuffer, 00047 mango.io.model.IConduit; 00048 00049 /******************************************************************************* 00050 00051 ServerSocket is a wrapper upon the basic socket functionality to 00052 simplify the API somewhat. You use a ServerSocket to listen for 00053 inbound connection requests, and get back a SocketConduit when a 00054 connection is made. 00055 00056 *******************************************************************************/ 00057 00058 class ServerSocket : Socket 00059 { 00060 private int linger = -1; 00061 00062 /*********************************************************************** 00063 00064 Construct a ServerSocket on the given address, with the 00065 specified number of backlog connections supported. The 00066 socket is bound to the given address, and set to listen 00067 for incoming connections. Note that the socket address 00068 can be setup for reuse, so that a halted server may be 00069 restarted immediately. 00070 00071 ***********************************************************************/ 00072 00073 this (InternetAddress addr, int backlog, bool socketReuse = false) 00074 { 00075 super (AddressFamily.INET, Type.STREAM, Protocol.IP); 00076 setAddressReuse (socketReuse); 00077 bind (addr); 00078 listen (backlog); 00079 } 00080 00081 /*********************************************************************** 00082 00083 Set the period in which dead sockets are left lying around 00084 by the O/S 00085 00086 ***********************************************************************/ 00087 00088 override void setLingerPeriod (int period) 00089 { 00090 linger = period; 00091 } 00092 00093 /*********************************************************************** 00094 00095 Wait for a client to connect to us, and return a connected 00096 SocketConduit. 00097 00098 ***********************************************************************/ 00099 00100 override SocketConduit accept () 00101 { 00102 return cast(SocketConduit) super.accept (); 00103 } 00104 00105 /*********************************************************************** 00106 00107 Overrides the default socket behaviour to create a socket 00108 for an incoming connection. Here we provide a SocketConduit 00109 instead. 00110 00111 ***********************************************************************/ 00112 00113 protected override Socket createSocket (socket_t handle) 00114 { 00115 Socket socket = SocketConduit.create (handle); 00116 00117 // force abortive closure to avoid prolonged OS scavenging? 00118 if (linger >= 0) 00119 socket.setLingerPeriod (linger); 00120 00121 return socket; 00122 } 00123 } 00124 00125