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